Skip to content

Commit

Permalink
pack event arguments generically
Browse files Browse the repository at this point in the history
  • Loading branch information
fbac committed Sep 17, 2024
1 parent e7ddcb0 commit f1e5b3a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 59 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
53 changes: 10 additions & 43 deletions precompiles/bank/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
38 changes: 26 additions & 12 deletions precompiles/logs/logs.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
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"
"github.com/ethereum/go-ethereum/core/types"
"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{
Expand Down Expand Up @@ -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
}
12 changes: 9 additions & 3 deletions precompiles/staking/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 13 additions & 1 deletion precompiles/types/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit f1e5b3a

Please sign in to comment.