66 "fmt"
77 "strconv"
88
9- "github.com/tidwall/gjson"
10-
119 "github.com/iotaledger/wasp/v2/clients/iota-go/iotago"
1210 "github.com/iotaledger/wasp/v2/clients/iota-go/iotago/iotatest"
1311 "github.com/iotaledger/wasp/v2/clients/iota-go/iotajsonrpc"
@@ -22,7 +20,7 @@ type EstimationRequest struct {
2220 GasBudget json.Number
2321}
2422
25- func DecodeCreateAndSendRequest (msg * EstimationRequest , cmd iotago.ProgrammableMoveCall , inputs []iotajsonrpc.ProgrammableTransactionBlockPureInput ) error {
23+ func DecodeCreateAndSendRequest (msg * EstimationRequest , cmd * iotago.ProgrammableMoveCall , inputs []iotajsonrpc.ProgrammableTransactionBlockPureInput ) error {
2624 if len (cmd .Arguments ) != 7 {
2725 return errors .New ("create_and_send_request has invalid parameters" )
2826 }
@@ -60,7 +58,7 @@ func DecodeCreateAndSendRequest(msg *EstimationRequest, cmd iotago.ProgrammableM
6058 return nil
6159}
6260
63- func DecodeCoin (assets * Assets , cmd iotago.ProgrammableMoveCall , inputs []iotajsonrpc.ProgrammableTransactionBlockPureInput ) error {
61+ func DecodeCoin (assets * Assets , cmd * iotago.ProgrammableMoveCall , inputs []iotajsonrpc.ProgrammableTransactionBlockPureInput ) error {
6462 var err error
6563 if len (cmd .Arguments ) != 2 {
6664 return fmt .Errorf ("malformed PTB" )
@@ -82,7 +80,7 @@ func DecodeCoin(assets *Assets, cmd iotago.ProgrammableMoveCall, inputs []iotajs
8280 return nil
8381}
8482
85- func DecodeAsset (assets * Assets , cmd iotago.ProgrammableMoveCall , inputs []iotajsonrpc.ProgrammableTransactionBlockPureInput ) error {
83+ func DecodeAsset (assets * Assets , cmd * iotago.ProgrammableMoveCall , inputs []iotajsonrpc.ProgrammableTransactionBlockPureInput ) error {
8684 var err error
8785 if len (cmd .Arguments ) != 2 {
8886 return fmt .Errorf ("malformed PTB" )
@@ -114,74 +112,56 @@ func DecodeAsset(assets *Assets, cmd iotago.ProgrammableMoveCall, inputs []iotaj
114112// I don't expect it to change often if ever, so that seems to be a straight forward way.
115113func DecodeDryRunTransaction (dryRunRes * iotajsonrpc.DryRunTransactionBlockResponse ) (* Assets , * EstimationRequest , * cryptolib.Address , error ) {
116114 tx := dryRunRes .Input .Data .V1 .Transaction .Data .ProgrammableTransaction
117- cmds := gjson .ParseBytes (tx .Commands )
118- var err error
115+
116+ var cmds []struct {
117+ MoveCall * iotago.ProgrammableMoveCall `json:"MoveCall,omitempty"`
118+ }
119+ if err := json .Unmarshal (tx .Commands , & cmds ); err != nil {
120+ return nil , nil , cryptolib .NewEmptyAddress (), fmt .Errorf ("can't decode dry run response: %w" , err )
121+ }
119122
120123 assets := NewAssets (0 )
121124 request := & EstimationRequest {
122125 Message : iscmove.Message {},
123126 }
124127
125- cmds .ForEach (func (key , value gjson.Result ) bool {
126- if moveCall := value .Get ("MoveCall" ); moveCall .Exists () {
127- var cmd iotago.ProgrammableMoveCall
128- err = json .Unmarshal ([]byte (moveCall .String ()), & cmd )
129- if err != nil {
130- err = fmt .Errorf ("can't decode dry run response: %w" , err )
131- return false
132- }
133-
128+ for _ , moveCall := range cmds {
129+ if cmd := moveCall .MoveCall ; cmd != nil {
134130 // take all placed coins into assets
135131 if cmd .Function == "place_coin" {
136132 var inputs []iotajsonrpc.ProgrammableTransactionBlockPureInput
137- err = json .Unmarshal (tx .Inputs , & inputs )
138- if err != nil {
139- err = fmt .Errorf ("can't decode place_coin command: %w" , err )
140- return false
133+ if err := json .Unmarshal (tx .Inputs , & inputs ); err != nil {
134+ return nil , nil , cryptolib .NewEmptyAddress (), fmt .Errorf ("can't decode place_coin command: %w" , err )
141135 }
142136
143- err = DecodeCoin (assets , cmd , inputs )
144- if err != nil {
145- err = fmt .Errorf ("can't decode place_coin command: %w" , err )
146- return false
137+ if err := DecodeCoin (assets , cmd , inputs ); err != nil {
138+ return nil , nil , cryptolib .NewEmptyAddress (), fmt .Errorf ("can't decode place_coin command: %w" , err )
147139 }
148140 }
149141
150142 if cmd .Function == "place_asset" {
151143 var inputs []iotajsonrpc.ProgrammableTransactionBlockPureInput
152- err = json .Unmarshal (tx .Inputs , & inputs )
153- if err != nil {
154- err = fmt .Errorf ("can't decode place_asset command: %w" , err )
155- return false
144+ if err := json .Unmarshal (tx .Inputs , & inputs ); err != nil {
145+ return nil , nil , cryptolib .NewEmptyAddress (), fmt .Errorf ("can't decode place_asset command: %w" , err )
156146 }
157147
158- err = DecodeAsset (assets , cmd , inputs )
159- if err != nil {
160- err = fmt .Errorf ("can't decode place_asset command: %w" , err )
161- return false
148+ if err := DecodeAsset (assets , cmd , inputs ); err != nil {
149+ return nil , nil , cryptolib .NewEmptyAddress (), fmt .Errorf ("can't decode place_asset command: %w" , err )
162150 }
163151 }
164152
165153 if cmd .Function == "create_and_send_request" {
166154 var inputs []iotajsonrpc.ProgrammableTransactionBlockPureInput
167- err = json .Unmarshal (tx .Inputs , & inputs )
168- if err != nil {
169- err = fmt .Errorf ("can't decode create_and_send_request command: %w" , err )
170- return false
155+ if err := json .Unmarshal (tx .Inputs , & inputs ); err != nil {
156+ return nil , nil , cryptolib .NewEmptyAddress (), fmt .Errorf ("can't decode create_and_send_request command: %w" , err )
171157 }
172158
173- err = DecodeCreateAndSendRequest (request , cmd , inputs )
174- if err != nil {
175- err = fmt .Errorf ("can't decode create_and_send_request command: %w" , err )
176- return false
159+ if err := DecodeCreateAndSendRequest (request , cmd , inputs ); err != nil {
160+ return nil , nil , cryptolib .NewEmptyAddress (), fmt .Errorf ("can't decode create_and_send_request command: %w" , err )
177161 }
178162 }
179163 }
180- return true // Continue iteration
181- })
182- if err != nil {
183- return nil , nil , cryptolib .NewEmptyAddress (), err
184164 }
185165
186- return assets , request , cryptolib .NewAddressFromIota (& dryRunRes .Input .Data .V1 .Sender ), err
166+ return assets , request , cryptolib .NewAddressFromIota (& dryRunRes .Input .Data .V1 .Sender ), nil
187167}
0 commit comments