From 6141582aea18e32a0f7413c9b4c6012197ee6181 Mon Sep 17 00:00:00 2001 From: Himitsuko Date: Thu, 1 Dec 2022 00:12:36 +0700 Subject: [PATCH] chore: add utilities function to near lake framework --- types/action_view.go | 35 +++++++++++++++++++++++++++----- types/signed_transaction_view.go | 27 +++++++++++++++++------- 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/types/action_view.go b/types/action_view.go index 5ce2000..04cc679 100644 --- a/types/action_view.go +++ b/types/action_view.go @@ -1,18 +1,22 @@ package types -import "encoding/json" +import ( + "encoding/base64" + "encoding/json" +) type ActionView map[string]interface{} +type ActionArgs string 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"` + MethodName string `json:"method_name"` + Args ActionArgs `json:"args"` + Gas *BigInt `json:"gas"` + Deposit *BigInt `json:"deposit"` } type Transfer struct { @@ -56,3 +60,24 @@ func (actionView *ActionView) GetFunctionCall() *FunctionCall { } return nil } + +func (args *ActionArgs) Decode() (string, error) { + decodedData, err := base64.StdEncoding.DecodeString(string(*args)) + if err != nil { + return "{}", err + } + return string(decodedData), nil +} + +func ConvertActionView(i interface{}) *ActionView { + data, err := json.Marshal(i) + if err != nil { + return nil + } + action := ActionView{} + err = json.Unmarshal(data, &action) + if err != nil { + return nil + } + return &action +} diff --git a/types/signed_transaction_view.go b/types/signed_transaction_view.go index 276b878..bbde5a0 100644 --- a/types/signed_transaction_view.go +++ b/types/signed_transaction_view.go @@ -1,11 +1,24 @@ package types +type TransactionAction string + +const ( + FunctionCallAction = "FunctionCall" + TransferAction = "Transfer" +) + type SignedTransactionView struct { - SignerId string `json:"signer_id"` - PublicKey string `json:"public_key"` - Nonce uint64 `json:"nonce"` - ReceiverId string `json:"receiver_id"` - Actions []map[string]interface{} `json:"actions"` - Signature string `json:"signature"` - Hash string `json:"hash"` + SignerId string `json:"signer_id"` + PublicKey string `json:"public_key"` + Nonce uint64 `json:"nonce"` + ReceiverId string `json:"receiver_id"` + Actions []interface{} `json:"actions"` + Signature string `json:"signature"` + Hash string `json:"hash"` +} + +func (s SignedTransactionView) LoopActions(f func(interface{})) { + for _, action := range s.Actions { + f(action) + } }