From 4232a98bee92175e9b5e676f5bd471a19f5a6e45 Mon Sep 17 00:00:00 2001 From: Francisco de Borja Aranda Castillejo Date: Tue, 17 Sep 2024 13:38:10 +0200 Subject: [PATCH] add high level event function --- precompiles/bank/const.go | 2 ++ precompiles/bank/logs.go | 38 ++++++++++++++++++++++++++--- precompiles/bank/method_deposit.go | 2 +- precompiles/bank/method_withdraw.go | 2 +- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/precompiles/bank/const.go b/precompiles/bank/const.go index e3c5084652..144c374aa4 100644 --- a/precompiles/bank/const.go +++ b/precompiles/bank/const.go @@ -7,9 +7,11 @@ const ( // Write methods. DepositMethodName = "deposit" DepositMethodGas = 200_000 + DepositEventName = "Deposit" WithdrawMethodName = "withdraw" WithdrawMethodGas = 200_000 + WithdrawEventName = "Withdraw" // Read methods. BalanceOfMethodName = "balanceOf" diff --git a/precompiles/bank/logs.go b/precompiles/bank/logs.go index f2118ae07f..146d758108 100644 --- a/precompiles/bank/logs.go +++ b/precompiles/bank/logs.go @@ -10,10 +10,40 @@ import ( "github.com/zeta-chain/node/precompiles/logs" ) -const ( - DepositEventName = "Deposit" - WithdrawEventName = "Withdraw" -) +func (c *Contract) addEventLog( + ctx sdk.Context, + stateDB vm.StateDB, + eventName string, + zrc20Addr common.Address, + zrc20Token common.Address, + cosmosAddr string, + cosmosCoin string, + amount *big.Int, +) error { + event := c.Abi().Events[eventName] + + topics, err := logs.MakeTopics( + event, + []interface{}{common.BytesToAddress(zrc20Addr.Bytes())}, + []interface{}{common.BytesToAddress(zrc20Token.Bytes())}, + []interface{}{cosmosCoin}, + ) + if err != nil { + return err + } + + data, err := logs.PackArguments([]logs.Argument{ + {Type: "string", Value: cosmosAddr}, + {Type: "uint256", Value: amount}, + }) + if err != nil { + return err + } + + logs.AddLog(ctx, c.Address(), stateDB, topics, data) + + return nil +} func (c *Contract) AddDepositLog( ctx sdk.Context, diff --git a/precompiles/bank/method_deposit.go b/precompiles/bank/method_deposit.go index d6e5d118ba..88778efdd1 100644 --- a/precompiles/bank/method_deposit.go +++ b/precompiles/bank/method_deposit.go @@ -148,7 +148,7 @@ func (c *Contract) deposit( } } - if err := c.AddDepositLog(ctx, evm.StateDB, caller, zrc20Addr, toAddr.String(), coinSet.Denoms()[0], amount); err != nil { + if err := c.addEventLog(ctx, evm.StateDB, DepositEventName, caller, zrc20Addr, toAddr.String(), coinSet.Denoms()[0], amount); err != nil { return nil, &ptypes.ErrUnexpected{ When: "AddDepositLog", Got: err.Error(), diff --git a/precompiles/bank/method_withdraw.go b/precompiles/bank/method_withdraw.go index daf19fc1f0..3e3a79063f 100644 --- a/precompiles/bank/method_withdraw.go +++ b/precompiles/bank/method_withdraw.go @@ -127,7 +127,7 @@ func (c *Contract) withdraw( } } - if err := c.AddWithdrawLog(ctx, evm.StateDB, caller, zrc20Addr, fromAddr.String(), coinSet.Denoms()[0], amount); err != nil { + if err := c.addEventLog(ctx, evm.StateDB, WithdrawEventName, caller, zrc20Addr, fromAddr.String(), coinSet.Denoms()[0], amount); err != nil { return nil, &ptypes.ErrUnexpected{ When: "AddWithdrawLog", Got: err.Error(),