Skip to content

Commit

Permalink
include review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
fbac committed Sep 23, 2024
1 parent dbec3ca commit af858ab
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 42 deletions.
10 changes: 9 additions & 1 deletion precompiles/bank/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,15 @@ func (c *Contract) Run(evm *vm.EVM, contract *vm.Contract, readOnly bool) ([]byt
return err
})
if execErr != nil {
return method.Outputs.Pack(false)
res, errPack := method.Outputs.Pack(false)
if errPack != nil {
return nil, errPack

Check warning on line 156 in precompiles/bank/bank.go

View check run for this annotation

Codecov / codecov/patch

precompiles/bank/bank.go#L156

Added line #L156 was not covered by tests
}

// Return the proper result (true/false) and the error message.
// This way we make bank compliant with smart contracts which would expect a true/false.
// And also with Go bindings which would expect an error.
return res, err
}
return res, nil

Expand Down
30 changes: 24 additions & 6 deletions precompiles/bank/method_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,17 @@ func (c *Contract) deposit(

// Safety check: token has to be a valid whitelisted ZRC20 and not be paused.
t, found := c.fungibleKeeper.GetForeignCoins(ctx, zrc20Addr.String())
if !found || t.Paused {
if !found {
return nil, &ptypes.ErrInvalidToken{
Got: zrc20Addr.String(),
Reason: "token is not a whitelisted ZRC20 or it's paused",
Reason: "token is not a whitelisted ZRC20",

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

View check run for this annotation

Codecov / codecov/patch

precompiles/bank/method_deposit.go#L65-L67

Added lines #L65 - L67 were not covered by tests
}
}

if t.Paused {
return nil, &ptypes.ErrInvalidToken{
Got: zrc20Addr.String(),
Reason: "token is paused",

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

View check run for this annotation

Codecov / codecov/patch

precompiles/bank/method_deposit.go#L72-L74

Added lines #L72 - L74 were not covered by tests
}
}

Expand All @@ -86,7 +93,13 @@ func (c *Contract) deposit(
}

balance, ok := resBalanceOf[0].(*big.Int)
if !ok || balance.Cmp(amount) < 0 || balance.Cmp(big.NewInt(0)) <= 0 {
if !ok {
return nil, &ptypes.ErrUnexpected{
Got: "ZRC20 balanceOf returned an unexpected type",

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

View check run for this annotation

Codecov / codecov/patch

precompiles/bank/method_deposit.go#L97-L98

Added lines #L97 - L98 were not covered by tests
}
}

if balance.Cmp(amount) < 0 || balance.Cmp(big.NewInt(0)) <= 0 {
return nil, &ptypes.ErrInvalidAmount{
Got: balance.String(),
}
Expand All @@ -110,7 +123,13 @@ func (c *Contract) deposit(
}

allowance, ok := resAllowance[0].(*big.Int)
if !ok || allowance.Cmp(amount) < 0 || allowance.Cmp(big.NewInt(0)) <= 0 {
if !ok {
return nil, &ptypes.ErrUnexpected{
Got: "ZRC20 allowance returned an unexpected type",

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

View check run for this annotation

Codecov / codecov/patch

precompiles/bank/method_deposit.go#L127-L128

Added lines #L127 - L128 were not covered by tests
}
}

if allowance.Cmp(amount) < 0 || allowance.Cmp(big.NewInt(0)) <= 0 {
return nil, &ptypes.ErrInvalidAmount{
Got: allowance.String(),
}
Expand Down Expand Up @@ -152,8 +171,7 @@ func (c *Contract) deposit(
}

// 3. Interactions: create cosmos coin and send.
err = c.bankKeeper.MintCoins(ctx, types.ModuleName, coinSet)
if err != nil {
if err := c.bankKeeper.MintCoins(ctx, types.ModuleName, coinSet); err != nil {
return nil, &ptypes.ErrUnexpected{
When: "MintCoins",
Got: err.Error(),

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

View check run for this annotation

Codecov / codecov/patch

precompiles/bank/method_deposit.go#L175-L177

Added lines #L175 - L177 were not covered by tests
Expand Down
46 changes: 41 additions & 5 deletions precompiles/bank/method_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,14 @@ func Test_Methods(t *testing.T) {
)

success, err := ts.bankContract.Run(ts.mockEVM, ts.mockVMContract, false)
require.NoError(t, err)
require.Error(t, err)
require.ErrorAs(
t,
ptypes.ErrInvalidAmount{
Got: "0",
},
err,
)

res, err := ts.bankABI.Methods[DepositMethodName].Outputs.Unpack(success)
require.NoError(t, err)
Expand Down Expand Up @@ -127,7 +134,14 @@ func Test_Methods(t *testing.T) {
)

success, err := ts.bankContract.Run(ts.mockEVM, ts.mockVMContract, false)
require.NoError(t, err)
require.Error(t, err)
require.ErrorAs(
t,
ptypes.ErrInvalidAmount{
Got: "0",
},
err,
)

res, err := ts.bankABI.Methods[DepositMethodName].Outputs.Unpack(success)
require.NoError(t, err)
Expand Down Expand Up @@ -158,7 +172,14 @@ func Test_Methods(t *testing.T) {
)

success, err := ts.bankContract.Run(ts.mockEVM, ts.mockVMContract, false)
require.NoError(t, err)
require.Error(t, err)
require.ErrorAs(
t,
ptypes.ErrInvalidAmount{
Got: "500",
},
err,
)

res, err := ts.bankABI.Methods[DepositMethodName].Outputs.Unpack(success)
require.NoError(t, err)
Expand Down Expand Up @@ -205,7 +226,14 @@ func Test_Methods(t *testing.T) {
)

success, err := ts.bankContract.Run(ts.mockEVM, ts.mockVMContract, false)
require.NoError(t, err)
require.Error(t, err)
require.ErrorAs(
t,
ptypes.ErrInvalidAmount{
Got: "1000",
},
err,
)

res, err := ts.bankABI.Methods[DepositMethodName].Outputs.Unpack(success)
require.NoError(t, err)
Expand Down Expand Up @@ -409,7 +437,15 @@ func Test_Methods(t *testing.T) {
)

success, err = ts.bankContract.Run(ts.mockEVM, ts.mockVMContract, false)
require.NoError(t, err)
require.Error(t, err)
require.ErrorAs(
t,
ptypes.ErrInsufficientBalance{
Requested: "501",
Got: "500",
},
err,
)

res, err = ts.bankABI.Methods[WithdrawMethodName].Outputs.Unpack(success)
require.NoError(t, err)
Expand Down
79 changes: 49 additions & 30 deletions precompiles/bank/method_withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,26 @@ func (c *Contract) withdraw(

// Safety check: token has to be a valid whitelisted ZRC20 and not be paused.
t, found := c.fungibleKeeper.GetForeignCoins(ctx, zrc20Addr.String())
if !found || t.Paused {
if !found {
return nil, &ptypes.ErrInvalidToken{
Got: zrc20Addr.String(),
Reason: "token is not a whitelisted ZRC20 or it's paused",
Reason: "token is not a whitelisted ZRC20",

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

View check run for this annotation

Codecov / codecov/patch

precompiles/bank/method_withdraw.go#L61-L63

Added lines #L61 - L63 were not covered by tests
}
}

if t.Paused {
return nil, &ptypes.ErrInvalidToken{
Got: zrc20Addr.String(),
Reason: "token is paused",

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

View check run for this annotation

Codecov / codecov/patch

precompiles/bank/method_withdraw.go#L68-L70

Added lines #L68 - L70 were not covered by tests
}
}

// Caller has to have enough cosmos coin balance to withdraw the requested amount.
coin := c.bankKeeper.GetBalance(ctx, fromAddr, ZRC20ToCosmosDenom(zrc20Addr))
if coin.Amount.IsNil() {
if !coin.IsValid() {
return nil, &ptypes.ErrInsufficientBalance{
Requested: amount.String(),
Got: "nil",
Got: "invalid coin",

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

View check run for this annotation

Codecov / codecov/patch

precompiles/bank/method_withdraw.go#L77-L79

Added lines #L77 - L79 were not covered by tests
}
}

Expand Down Expand Up @@ -103,39 +110,19 @@ func (c *Contract) withdraw(
}

balance, ok := resBalanceOf[0].(*big.Int)
if !ok || balance.Cmp(amount) == -1 {
return nil, &ptypes.ErrInvalidAmount{
Got: "not enough bank balance",
}
}

// 2. Effect: transfer balance.

// function transfer(address recipient, uint256 amount) public virtual override returns (bool)
resTransferFrom, err := c.CallContract(
ctx,
&c.fungibleKeeper,
c.zrc20ABI,
zrc20Addr,
"transfer",
[]interface{}{caller /* sender */, amount},
)
if err != nil {
if !ok {
return nil, &ptypes.ErrUnexpected{
When: "transferFrom",
Got: err.Error(),
Got: "ZRC20 balanceOf returned an unexpected type",

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

View check run for this annotation

Codecov / codecov/patch

precompiles/bank/method_withdraw.go#L114-L115

Added lines #L114 - L115 were not covered by tests
}
}

transferred, ok := resTransferFrom[0].(bool)
if !ok || !transferred {
return nil, &ptypes.ErrUnexpected{
When: "transferFrom",
Got: "transaction not successful",
if balance.Cmp(amount) == -1 {
return nil, &ptypes.ErrInvalidAmount{
Got: balance.String(),

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

View check run for this annotation

Codecov / codecov/patch

precompiles/bank/method_withdraw.go#L120-L121

Added lines #L120 - L121 were not covered by tests
}
}

// 3. Interactions: send to module and burn.
// 2. Effect: burn cosmos coin balance.
if err := c.bankKeeper.SendCoinsFromAccountToModule(ctx, fromAddr, types.ModuleName, coinSet); err != nil {
return nil, &ptypes.ErrUnexpected{
When: "SendCoinsFromAccountToModule",
Expand All @@ -157,6 +144,38 @@ func (c *Contract) withdraw(
}
}

// 3. Interactions: send to module and burn.

// function transfer(address recipient, uint256 amount) public virtual override returns (bool)
resTransfer, err := c.CallContract(
ctx,
&c.fungibleKeeper,
c.zrc20ABI,
zrc20Addr,
"transfer",
[]interface{}{caller /* sender */, amount},
)
if err != nil {
return nil, &ptypes.ErrUnexpected{
When: "transfer",
Got: err.Error(),

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

View check run for this annotation

Codecov / codecov/patch

precompiles/bank/method_withdraw.go#L159-L161

Added lines #L159 - L161 were not covered by tests
}
}

transferred, ok := resTransfer[0].(bool)
if !ok {
return nil, &ptypes.ErrUnexpected{
Got: "ZRC20 transfer returned an unexpected type",

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

View check run for this annotation

Codecov / codecov/patch

precompiles/bank/method_withdraw.go#L167-L168

Added lines #L167 - L168 were not covered by tests
}
}

if !transferred {
return nil, &ptypes.ErrUnexpected{
When: "transfer",
Got: "transaction not successful",

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

View check run for this annotation

Codecov / codecov/patch

precompiles/bank/method_withdraw.go#L173-L175

Added lines #L173 - L175 were not covered by tests
}
}

return method.Outputs.Pack(true)
}

Expand Down

0 comments on commit af858ab

Please sign in to comment.