Skip to content

Commit

Permalink
add high level event function
Browse files Browse the repository at this point in the history
  • Loading branch information
fbac committed Sep 17, 2024
1 parent f1e5b3a commit 4232a98
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
2 changes: 2 additions & 0 deletions precompiles/bank/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
38 changes: 34 additions & 4 deletions precompiles/bank/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion precompiles/bank/method_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion precompiles/bank/method_withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 4232a98

Please sign in to comment.