|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + "github.com/go-openapi/runtime/middleware" |
| 8 | + "github.com/massalabs/station/api/swagger/server/models" |
| 9 | + "github.com/massalabs/station/api/swagger/server/restapi/operations" |
| 10 | + "github.com/massalabs/station/int/config" |
| 11 | + "github.com/massalabs/station/pkg/node/sendoperation" |
| 12 | + "github.com/massalabs/station/pkg/node/sendoperation/signer" |
| 13 | + "github.com/massalabs/station/pkg/onchain" |
| 14 | +) |
| 15 | + |
| 16 | +func NewExecuteSCHandler(config *config.NetworkInfos) operations.CmdExecuteSCHandler { |
| 17 | + return &executeSC{networkInfos: config} |
| 18 | +} |
| 19 | + |
| 20 | +type executeSC struct { |
| 21 | + networkInfos *config.NetworkInfos |
| 22 | +} |
| 23 | + |
| 24 | +//nolint:funlen |
| 25 | +func (d *executeSC) Handle(params operations.CmdExecuteSCParams) middleware.Responder { |
| 26 | + if params.Body.Bytecode == "" { |
| 27 | + return operations.NewCmdExecuteSCUnprocessableEntity(). |
| 28 | + WithPayload( |
| 29 | + &models.Error{ |
| 30 | + Message: "Smart contract bytecode is required", |
| 31 | + }) |
| 32 | + } |
| 33 | + |
| 34 | + smartContractByteCode, err := base64.StdEncoding.DecodeString(params.Body.Bytecode) |
| 35 | + if err != nil { |
| 36 | + return operations.NewCmdExecuteSCBadRequest(). |
| 37 | + WithPayload( |
| 38 | + &models.Error{ |
| 39 | + Code: err.Error(), |
| 40 | + Message: err.Error(), |
| 41 | + }) |
| 42 | + } |
| 43 | + |
| 44 | + maxCoins, err := strconv.ParseUint(string(params.Body.MaxCoins), 10, 64) |
| 45 | + if err != nil { |
| 46 | + return operations.NewCmdExecuteSCBadRequest(). |
| 47 | + WithPayload( |
| 48 | + &models.Error{ |
| 49 | + Code: errorInvalidMaxCoins, |
| 50 | + Message: err.Error(), |
| 51 | + }) |
| 52 | + } |
| 53 | + |
| 54 | + fee, err := strconv.ParseUint(string(params.Body.Fee), 10, 64) |
| 55 | + if err != nil { |
| 56 | + return operations.NewCmdExecuteSCBadRequest(). |
| 57 | + WithPayload( |
| 58 | + &models.Error{ |
| 59 | + Code: errorInvalidFee, |
| 60 | + Message: err.Error(), |
| 61 | + }) |
| 62 | + } |
| 63 | + |
| 64 | + maxGas := uint64(sendoperation.MaxGasAllowedExecuteSC) |
| 65 | + |
| 66 | + if string(params.Body.MaxGas) != "" { |
| 67 | + parsedMaxGas, err := strconv.ParseUint(string(params.Body.MaxGas), 10, 64) |
| 68 | + if err != nil { |
| 69 | + return operations.NewCmdExecuteSCBadRequest().WithPayload( |
| 70 | + &models.Error{ |
| 71 | + Code: errorInvalidMaxGas, |
| 72 | + Message: "Error during max gas conversion: " + err.Error(), |
| 73 | + }) |
| 74 | + } |
| 75 | + |
| 76 | + maxGas = parsedMaxGas |
| 77 | + } |
| 78 | + |
| 79 | + var datastore []onchain.DatastoreEntry = []onchain.DatastoreEntry{} |
| 80 | + |
| 81 | + if len(params.Body.Datastore) > 0 { |
| 82 | + datastore = make([]onchain.DatastoreEntry, len(params.Body.Datastore)) |
| 83 | + for i, entry := range params.Body.Datastore { |
| 84 | + key, err := base64.StdEncoding.DecodeString(string(entry[0])) |
| 85 | + if err != nil { |
| 86 | + return operations.NewCmdExecuteSCBadRequest(). |
| 87 | + WithPayload( |
| 88 | + &models.Error{ |
| 89 | + Code: errorInvalidDatastore, |
| 90 | + Message: err.Error(), |
| 91 | + }) |
| 92 | + } |
| 93 | + value, err := base64.StdEncoding.DecodeString(string(entry[1])) |
| 94 | + if err != nil { |
| 95 | + return operations.NewCmdExecuteSCBadRequest(). |
| 96 | + WithPayload( |
| 97 | + &models.Error{ |
| 98 | + Code: errorInvalidDatastore, |
| 99 | + Message: err.Error(), |
| 100 | + }) |
| 101 | + } |
| 102 | + datastore[i] = onchain.DatastoreEntry{ |
| 103 | + Key: key, |
| 104 | + Value: value, |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + operationResponse, err := onchain.ExecuteSC( |
| 110 | + d.networkInfos, |
| 111 | + params.Body.Nickname, |
| 112 | + maxGas, |
| 113 | + maxCoins, |
| 114 | + fee, |
| 115 | + sendoperation.DefaultExpiryInSlot, |
| 116 | + smartContractByteCode, |
| 117 | + datastore, |
| 118 | + &signer.WalletPlugin{}, |
| 119 | + "Executing contract bytecode: "+params.Body.Description, |
| 120 | + ) |
| 121 | + if err != nil { |
| 122 | + return operations.NewCmdExecuteSCInternalServerError(). |
| 123 | + WithPayload( |
| 124 | + &models.Error{ |
| 125 | + Code: err.Error(), |
| 126 | + Message: err.Error(), |
| 127 | + }) |
| 128 | + } |
| 129 | + |
| 130 | + return operations.NewCmdExecuteSCOK(). |
| 131 | + WithPayload(&operations.CmdExecuteSCOKBody{ |
| 132 | + OperationID: operationResponse.OperationID, |
| 133 | + }) |
| 134 | +} |
0 commit comments