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 05d0bfb
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 50 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
52 changes: 5 additions & 47 deletions precompiles/bank/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,70 +10,28 @@ import (
"github.com/zeta-chain/node/precompiles/logs"
)

const (
DepositEventName = "Deposit"
WithdrawEventName = "Withdraw"
)

func (c *Contract) AddDepositLog(
ctx sdk.Context,
stateDB vm.StateDB,
zrc20Depositor common.Address,
zrc20Token common.Address,
cosmosAddr string,
cosmosCoin string,
amount *big.Int,
) error {
event := c.Abi().Events[DepositEventName]

// ZRC20, cosmos coin and depositor.
topics, err := logs.MakeTopics(
event,
[]interface{}{common.BytesToAddress(zrc20Depositor.Bytes())},
[]interface{}{common.BytesToAddress(zrc20Token.Bytes())},
[]interface{}{cosmosCoin},
)
if err != nil {
return err
}

// 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
}

logs.AddLog(ctx, c.Address(), stateDB, topics, data)

return nil
}

func (c *Contract) AddWithdrawLog(
func (c *Contract) addEventLog(
ctx sdk.Context,
stateDB vm.StateDB,
zrc20Withdrawer common.Address,
eventName string,
zrc20Addr common.Address,
zrc20Token common.Address,
cosmosAddr string,
cosmosCoin string,
amount *big.Int,
) error {
event := c.Abi().Events[WithdrawEventName]
event := c.Abi().Events[eventName]

Check warning on line 23 in precompiles/bank/logs.go

View check run for this annotation

Codecov / codecov/patch

precompiles/bank/logs.go#L22-L23

Added lines #L22 - L23 were not covered by tests

// ZRC20, cosmos coin and witgdrawer are indexed.
topics, err := logs.MakeTopics(
event,
[]interface{}{common.BytesToAddress(zrc20Withdrawer.Bytes())},
[]interface{}{common.BytesToAddress(zrc20Addr.Bytes())},
[]interface{}{common.BytesToAddress(zrc20Token.Bytes())},
[]interface{}{cosmosCoin},
)
if err != nil {
return err

Check warning on line 32 in precompiles/bank/logs.go

View check run for this annotation

Codecov / codecov/patch

precompiles/bank/logs.go#L25-L32

Added lines #L25 - L32 were not covered by tests
}

// Pack cosmos address and amount as data.
data, err := logs.PackArguments([]logs.Argument{
{Type: "string", Value: cosmosAddr},
{Type: "uint256", Value: amount},
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(),

Check warning on line 154 in precompiles/bank/method_deposit.go

View check run for this annotation

Codecov / codecov/patch

precompiles/bank/method_deposit.go#L151-L154

Added lines #L151 - L154 were not covered by tests
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(),

Check warning on line 133 in precompiles/bank/method_withdraw.go

View check run for this annotation

Codecov / codecov/patch

precompiles/bank/method_withdraw.go#L130-L133

Added lines #L130 - L133 were not covered by tests
Expand Down
2 changes: 1 addition & 1 deletion precompiles/types/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@ func Test_ErrInsufficientBalance(t *testing.T) {
if got != expect {
t.Errorf("Expected %v, got %v", expect, got)
}
}
}

0 comments on commit 05d0bfb

Please sign in to comment.