Skip to content

Commit

Permalink
chore: optimize action decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
Himitsuko committed Nov 29, 2022
1 parent 033fe57 commit d0b54b9
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 18 deletions.
56 changes: 55 additions & 1 deletion types/action_view.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 0 additions & 6 deletions types/data_receiver_view.go

This file was deleted.

49 changes: 42 additions & 7 deletions types/execution_outcome_view.go
Original file line number Diff line number Diff line change
@@ -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
}
61 changes: 57 additions & 4 deletions types/receipt_view.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit d0b54b9

Please sign in to comment.