-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add read only call sc endpoint (#1309)
* add read only call sc endpoint * fix convert struct and add robot test * refactor * rename * fix a response * improve a doc comment * improve a doc comment
- Loading branch information
Showing
7 changed files
with
427 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
package cmd | ||
|
||
const ( | ||
errorSendOperation = "Execute-0001" | ||
errorInvalidArgs = "Execute-0002" | ||
errorInvalidFee = "Execute-0003" | ||
errorInvalidMaxGas = "Execute-0004" | ||
errorInvalidCoin = "Execute-0005" | ||
errorSendOperation = "Execute-0001" | ||
errorInvalidArgs = "Execute-0002" | ||
errorInvalidFee = "Execute-0003" | ||
errorInvalidMaxGas = "Execute-0004" | ||
errorInvalidCoin = "Execute-0005" | ||
errorInvalidNickname = "Execute-0006" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/base64" | ||
|
||
"github.com/go-openapi/runtime/middleware" | ||
"github.com/massalabs/station/api/swagger/server/models" | ||
"github.com/massalabs/station/api/swagger/server/restapi/operations" | ||
"github.com/massalabs/station/int/config" | ||
"github.com/massalabs/station/pkg/node" | ||
sendOperation "github.com/massalabs/station/pkg/node/sendoperation" | ||
"github.com/massalabs/station/pkg/wallet" | ||
) | ||
|
||
func NewReadOnlyCallSCHandler(config *config.NetworkInfos) operations.CmdReadOnlyCallSCHandler { | ||
return &ReadOnlyCallSC{networkInfos: config} | ||
} | ||
|
||
type ReadOnlyCallSC struct { | ||
networkInfos *config.NetworkInfos | ||
} | ||
|
||
func (e *ReadOnlyCallSC) Handle(params operations.CmdReadOnlyCallSCParams) middleware.Responder { | ||
args, err := base64.StdEncoding.DecodeString(params.Body.Args) | ||
if err != nil { | ||
return operations.NewCmdReadOnlyCallSCUnprocessableEntity(). | ||
WithPayload( | ||
&models.Error{ | ||
Code: errorInvalidArgs, | ||
Message: err.Error(), | ||
}) | ||
} | ||
|
||
acc, err := wallet.Fetch(params.Body.Nickname) | ||
if err != nil { | ||
return operations.NewCmdReadOnlyCallSCBadRequest().WithPayload( | ||
&models.Error{ | ||
Code: errorInvalidNickname, | ||
Message: "Error during wallet fetch: " + err.Error(), | ||
}) | ||
} | ||
|
||
if params.Body.Fee == "" { | ||
params.Body.Fee = "0" | ||
} | ||
|
||
coins, errResponse := amountToString(params.Body.Coins, uint64(0)) | ||
if errResponse != nil { | ||
return errResponse | ||
} | ||
|
||
result, err := sendOperation.ReadOnlyCallSC( | ||
params.Body.At, | ||
params.Body.Name, | ||
args, | ||
coins, | ||
string(params.Body.Fee), | ||
acc.Address, | ||
node.NewClient(e.networkInfos.NodeURL), | ||
) | ||
if err != nil { | ||
return operations.NewCmdReadOnlyCallSCInternalServerError().WithPayload( | ||
&models.Error{Code: errorSendOperation, Message: "Error: read only callSC failed: " + err.Error()}) | ||
} | ||
|
||
modelResult := CreateReadOnlyResult(*result) | ||
|
||
return operations.NewCmdReadOnlyCallSCOK().WithPayload(&modelResult) | ||
} |
Oops, something went wrong.