From d0b54b9b9d4d59ce3c3e048fc89198e6c244cb27 Mon Sep 17 00:00:00 2001 From: Himitsuko Date: Tue, 29 Nov 2022 17:04:25 +0700 Subject: [PATCH] chore: optimize action decoder --- types/action_view.go | 56 +++++++++++++++++++++++++++++- types/data_receiver_view.go | 6 ---- types/execution_outcome_view.go | 49 ++++++++++++++++++++++---- types/receipt_view.go | 61 ++++++++++++++++++++++++++++++--- 4 files changed, 154 insertions(+), 18 deletions(-) delete mode 100644 types/data_receiver_view.go diff --git a/types/action_view.go b/types/action_view.go index f67ca9a..5ce2000 100644 --- a/types/action_view.go +++ b/types/action_view.go @@ -1,4 +1,58 @@ package types -type ActionView struct { +import "encoding/json" + +type ActionView map[string]interface{} + +type DeployContract struct { + Code string `json:"code"` +} + +type FunctionCall struct { + MethodName string `json:"method_name"` + Args string `json:"args"` + Gas *BigInt `json:"gas"` + Deposit *BigInt `json:"deposit"` +} + +type Transfer struct { + Deposit *BigInt `json:"deposit"` +} + +type Stake struct { + Stake *BigInt `json:"stake"` + PublicKey string `json:"public_key"` +} + +func (actionView *ActionView) IsDeployContract() bool { + _, ok := (*actionView)["DeployContract"] + return ok +} + +func (actionView *ActionView) IsFunctionCall() bool { + _, ok := (*actionView)["FunctionCall"] + return ok +} + +func (actionView *ActionView) IsTransfer() bool { + _, ok := (*actionView)["Transfer"] + return ok +} + +func (actionView *ActionView) IsStake() bool { + _, ok := (*actionView)["Stake"] + return ok +} + +func (actionView *ActionView) GetFunctionCall() *FunctionCall { + if actionView.IsFunctionCall() { + data, err := json.Marshal((*actionView)["FunctionCall"]) + if err != nil { + return nil + } + fc := FunctionCall{} + err = json.Unmarshal(data, &fc) + return &fc + } + return nil } diff --git a/types/data_receiver_view.go b/types/data_receiver_view.go deleted file mode 100644 index 5d54be1..0000000 --- a/types/data_receiver_view.go +++ /dev/null @@ -1,6 +0,0 @@ -package types - -type DataReceiverView struct { - DataId string `json:"data_id"` - ReceiverId string `json:"receiver_id"` -} diff --git a/types/execution_outcome_view.go b/types/execution_outcome_view.go index 7373505..983fb28 100644 --- a/types/execution_outcome_view.go +++ b/types/execution_outcome_view.go @@ -1,11 +1,46 @@ package types type ExecutionOutcomeView struct { - Logs []string `json:"logs"` - ReceiptIds []string `json:"receipt_ids"` - GasBurnt uint64 `json:"gas_burnt"` - TokensBurnt *BigInt `json:"tokens_burnt"` - ExecutorId string `json:"executor_id"` - Status map[string]interface{} `json:"status"` - Metadata ExecutionMetadataView `json:"metadata"` + Logs []string `json:"logs"` + ReceiptIds []string `json:"receipt_ids"` + GasBurnt uint64 `json:"gas_burnt"` + TokensBurnt *BigInt `json:"tokens_burnt"` + ExecutorId string `json:"executor_id"` + Status Status `json:"status"` + Metadata ExecutionMetadataView `json:"metadata"` +} + +type Status map[string]interface{} + +func (status Status) IsUnknown() bool { + _, ok := status["Unknown"] + return ok +} + +func (status Status) IsFailure() bool { + _, ok := status["Failure"] + return ok +} + +func (status Status) IsSuccess() bool { + _, ok1 := status["SuccessValue"] + _, ok2 := status["SuccessReceiptId"] + + return ok1 || ok2 +} + +func (status Status) SuccessValue() *string { + _, ok := status["SuccessValue"] + if status.IsSuccess() && ok { + return status["SuccessValue"].(*string) + } + return nil +} + +func (status Status) SuccessReceiptId() *string { + _, ok := status["SuccessReceiptId"] + if status.IsSuccess() && ok { + return status["SuccessReceiptId"].(*string) + } + return nil } diff --git a/types/receipt_view.go b/types/receipt_view.go index f997ccb..7f8d9ed 100644 --- a/types/receipt_view.go +++ b/types/receipt_view.go @@ -1,8 +1,61 @@ package types +import "encoding/json" + type ReceiptView struct { - PredecessorId string `json:"predecessor_id"` - ReceiverId string `json:"receiver_id"` - ReceiptId string `json:"receipt_id"` - Receipt map[string]interface{} `json:"receipt"` + PredecessorId string `json:"predecessor_id"` + ReceiverId string `json:"receiver_id"` + ReceiptId string `json:"receipt_id"` + Receipt Receipt `json:"receipt"` +} + +type DataReceiverView struct { + DataId string `json:"data_id"` + ReceiverId string `json:"receiver_id"` +} + +type Data struct { + DataId string + Data []uint8 +} + +type Action struct { + SignerId string `json:"signer_id"` + SignerPublicKey string `json:"signer_public_key"` + GasPrice *BigInt `json:"gas_price"` + OutputDataReceivers []DataReceiverView `json:"output_data_receivers"` + InputDataIds []string `json:"input_data_ids"` + Actions []ActionView `json:"actions"` +} + +type Receipt map[string]interface{} + +func (receipt Receipt) IsAction() bool { + _, ok := receipt["Action"] + return ok +} + +func (receipt Receipt) IsData() bool { + _, ok := receipt["Data"] + return ok +} + +func (receipt Receipt) GetAction() *Action { + if receipt.IsAction() { + data, err := json.Marshal(receipt["Action"]) + if err != nil { + return nil + } + action := Action{} + err = json.Unmarshal(data, &action) + return &action + } + return nil +} + +func (receipt Receipt) GetData() *Data { + if receipt.IsData() { + return receipt["Data"].(*Data) + } + return nil }