From f1e5b3aa06f1bd74dbb28149ab1cdcd935ff36fb Mon Sep 17 00:00:00 2001 From: Francisco de Borja Aranda Castillejo Date: Tue, 17 Sep 2024 13:20:59 +0200 Subject: [PATCH] pack event arguments generically --- changelog.md | 1 + precompiles/bank/logs.go | 53 ++++++-------------------------- precompiles/logs/logs.go | 38 +++++++++++++++-------- precompiles/staking/logs.go | 12 ++++++-- precompiles/types/errors_test.go | 14 ++++++++- 5 files changed, 59 insertions(+), 59 deletions(-) diff --git a/changelog.md b/changelog.md index accd802d9f..b58231fb03 100644 --- a/changelog.md +++ b/changelog.md @@ -9,6 +9,7 @@ * [2784](https://github.com/zeta-chain/node/pull/2784) - staking precompiled contract * [2795](https://github.com/zeta-chain/node/pull/2795) - support restricted address in Solana * [2861](https://github.com/zeta-chain/node/pull/2861) - emit events from staking precompile +* [2860](https://github.com/zeta-chain/node/pull/2860) - bank precompiled contract ### Refactor diff --git a/precompiles/bank/logs.go b/precompiles/bank/logs.go index 9e1e22473c..f2118ae07f 100644 --- a/precompiles/bank/logs.go +++ b/precompiles/bank/logs.go @@ -4,7 +4,6 @@ import ( "math/big" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/vm" @@ -38,27 +37,11 @@ func (c *Contract) AddDepositLog( return err } - // Amount and cosmos address are part of event data. - uint256Type, err := abi.NewType("uint256", "", nil) - if err != nil { - return err - } - - stringType, err := abi.NewType("string", "", nil) - if err != nil { - return err - } - - arguments := abi.Arguments{ - { - Type: stringType, - }, - { - Type: uint256Type, - }, - } - - data, err := arguments.Pack(cosmosAddr, amount) + // Pack cosmos address and amount as data. + data, err := logs.PackArguments([]logs.Argument{ + {Type: "string", Value: cosmosAddr}, + {Type: "uint256", Value: amount}, + }) if err != nil { return err } @@ -90,27 +73,11 @@ func (c *Contract) AddWithdrawLog( return err } - // Amount and cosmos address are part of event data. - uint256Type, err := abi.NewType("uint256", "", nil) - if err != nil { - return err - } - - stringType, err := abi.NewType("string", "", nil) - if err != nil { - return err - } - - arguments := abi.Arguments{ - { - Type: stringType, - }, - { - Type: uint256Type, - }, - } - - data, err := arguments.Pack(cosmosAddr, amount) + // Pack cosmos address and amount as data. + data, err := logs.PackArguments([]logs.Argument{ + {Type: "string", Value: cosmosAddr}, + {Type: "uint256", Value: amount}, + }) if err != nil { return err } diff --git a/precompiles/logs/logs.go b/precompiles/logs/logs.go index f168e33da4..07960a442e 100644 --- a/precompiles/logs/logs.go +++ b/precompiles/logs/logs.go @@ -1,8 +1,6 @@ package logs import ( - "math/big" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" @@ -10,6 +8,11 @@ import ( "github.com/ethereum/go-ethereum/core/vm" ) +type Argument struct { + Type string + Value interface{} +} + // AddLog adds log to stateDB func AddLog(ctx sdk.Context, precompileAddr common.Address, stateDB vm.StateDB, topics []common.Hash, data []byte) { stateDB.AddLog(&types.Log{ @@ -38,18 +41,29 @@ func MakeTopics(event abi.Event, query ...[]interface{}) ([]common.Hash, error) return topics, nil } -// PackBigInt is a helper function to pack a uint256 amount -func PackBigInt(amount *big.Int) ([]byte, error) { - uint256Type, err := abi.NewType("uint256", "", nil) - if err != nil { - return nil, err +// PackArguments packs an arbitrary number of logs.Arguments as non-indexed data. +// When packing data, make sure the Argument are passed in the same order as the event definition. +func PackArguments(args []Argument) ([]byte, error) { + types := abi.Arguments{} + toPack := []interface{}{} + + for _, arg := range args { + abiType, err := abi.NewType(arg.Type, "", nil) + if err != nil { + return nil, err + } + + types = append(types, abi.Argument{ + Type: abiType, + }) + + toPack = append(toPack, arg.Value) } - arguments := abi.Arguments{ - { - Type: uint256Type, - }, + data, err := types.Pack(toPack...) + if err != nil { + return nil, err } - return arguments.Pack(amount) + return data, nil } diff --git a/precompiles/staking/logs.go b/precompiles/staking/logs.go index 165dd91aa3..ea8e51274c 100644 --- a/precompiles/staking/logs.go +++ b/precompiles/staking/logs.go @@ -37,7 +37,9 @@ func (c *Contract) AddStakeLog( } // amount is part of event data - data, err := logs.PackBigInt(amount) + data, err := logs.PackArguments([]logs.Argument{ + {Type: "uint256", Value: amount}, + }) if err != nil { return err } @@ -67,7 +69,9 @@ func (c *Contract) AddUnstakeLog( } // amount is part of event data - data, err := logs.PackBigInt(amount) + data, err := logs.PackArguments([]logs.Argument{ + {Type: "uint256", Value: amount}, + }) if err != nil { return err } @@ -108,7 +112,9 @@ func (c *Contract) AddMoveStakeLog( } // amount is part of event data - data, err := logs.PackBigInt(amount) + data, err := logs.PackArguments([]logs.Argument{ + {Type: "uint256", Value: amount}, + }) if err != nil { return err } diff --git a/precompiles/types/errors_test.go b/precompiles/types/errors_test.go index 4fa3bc72e2..288501a38d 100644 --- a/precompiles/types/errors_test.go +++ b/precompiles/types/errors_test.go @@ -78,8 +78,20 @@ func Test_ErrUnexpected(t *testing.T) { Got: "bar", } got := e.Error() - expect := "unexpected foo, got: bar" + expect := "unexpected error in foo: bar" if got != expect { t.Errorf("Expected %v, got %v", expect, got) } } + +func Test_ErrInsufficientBalance(t *testing.T) { + e := ErrInsufficientBalance{ + Requested: "foo", + Got: "bar", + } + got := e.Error() + expect := "insufficient balance: requested foo, current bar" + if got != expect { + t.Errorf("Expected %v, got %v", expect, got) + } +} \ No newline at end of file