Skip to content

Commit

Permalink
Merge pull request #65 from make-software/feature/CMW-608/parsing_tra…
Browse files Browse the repository at this point in the history
…nsform

Feature/cmw 608/parsing transform
  • Loading branch information
koltsov-iv authored Nov 1, 2023
2 parents 5752ac6 + 258f9fa commit 0c83e68
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
6 changes: 6 additions & 0 deletions tests/data/transform/AddUInt512.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"key": "balance-cac81394a4906c62a999c4b3c24d886c452b0a9ed39419e6e8f861d72ee59303",
"transform": {
"AddUInt512": "100000000"
}
}
14 changes: 14 additions & 0 deletions tests/data/transform/WriteDeployInfo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"key": "deploy-e379d531188e90892ca07dba8469def6f9a45e627717b2a3ae4a8ec440c9f001",
"transform": {
"WriteDeployInfo": {
"deploy_hash": "e379d531188e90892ca07dba8469def6f9a45e627717b2a3ae4a8ec440c9f001",
"transfers": [
"transfer-78c4663dd4b83fa35d031e8173dccb53431520adf559a3853892530946fcb777"
],
"from": "account-hash-e15156d73d79f92b13aa147552a01ef2a3d677134f13961b51033ffd9fba481c",
"source": "uref-012e4506440c6bf5427d3d76ad20a1bdefc6bf5d79cee3d28bfaae36603f1133-007",
"gas": "100000000"
}
}
}
42 changes: 42 additions & 0 deletions tests/types/transform_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package types

import (
"encoding/json"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/make-software/casper-go-sdk/types"
)

func Test_Transform_AddUInt512(t *testing.T) {
fixture, err := os.ReadFile("../data/transform/AddUInt512.json")
require.NoError(t, err)
var transformKey types.TransformKey

err = json.Unmarshal(fixture, &transformKey)
require.NoError(t, err)

val, err := transformKey.Transform.ParseAsUInt512()
require.NoError(t, err)

assert.True(t, transformKey.Transform.IsAddUint512())
assert.EqualValues(t, 100000000, val.Value().Int64())
}

func Test_Transform_WriteDeployInfo(t *testing.T) {
fixute, err := os.ReadFile("../data/transform/WriteDeployInfo.json")
require.NoError(t, err)
var transformKey types.TransformKey

err = json.Unmarshal(fixute, &transformKey)
require.NoError(t, err)

val, err := transformKey.Transform.ParseAsWriteDeployInfo()
require.NoError(t, err)

assert.True(t, transformKey.Transform.IsWriteDeployInfo())
assert.EqualValues(t, 1, len(val.Transfers))
}
35 changes: 35 additions & 0 deletions types/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"strings"

"github.com/make-software/casper-go-sdk/types/clvalue"
"github.com/make-software/casper-go-sdk/types/key"
)

Expand Down Expand Up @@ -82,6 +83,14 @@ func (t *Transform) IsWriteBid() bool {
return strings.Contains(string(*t), "WriteBid")
}

func (t *Transform) IsAddUint512() bool {
return strings.Contains(string(*t), "AddUInt512")
}

func (t *Transform) IsWriteDeployInfo() bool {
return strings.Contains(string(*t), "WriteDeployInfo")
}

func (t *Transform) ParseAsWriteWithdraws() ([]UnbondingPurse, error) {
type RawWriteWithdrawals struct {
UnbondingPurses []UnbondingPurse `json:"WriteWithdraw"`
Expand Down Expand Up @@ -120,3 +129,29 @@ func (t *Transform) ParseAsWriteCLValue() (*Argument, error) {

return &jsonRes.WriteCLValue, nil
}

func (t *Transform) ParseAsUInt512() (*clvalue.UInt512, error) {
type RawUInt512 struct {
UInt512 clvalue.UInt512 `json:"AddUInt512"`
}

jsonRes := RawUInt512{}
if err := json.Unmarshal(*t, &jsonRes); err != nil {
return nil, err
}

return &jsonRes.UInt512, nil
}

func (t *Transform) ParseAsWriteDeployInfo() (*DeployInfo, error) {
type RawWriteDeployInfo struct {
WriteDeployInfo DeployInfo `json:"WriteDeployInfo"`
}

jsonRes := RawWriteDeployInfo{}
if err := json.Unmarshal(*t, &jsonRes); err != nil {
return nil, err
}

return &jsonRes.WriteDeployInfo, nil
}

0 comments on commit 0c83e68

Please sign in to comment.