Skip to content

Commit

Permalink
chore: cleaned up solidity contracts calling bankSend precompile
Browse files Browse the repository at this point in the history
  • Loading branch information
onikonychev committed Nov 1, 2024
1 parent 6ff8739 commit c43219e
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 33 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

22 changes: 14 additions & 8 deletions x/evm/embeds/contracts/TestERC20TransferThenPrecompileSend.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,21 @@ contract TestERC20TransferThenPrecompileSend {
"ERC-20 transfer failed"
);

(bool success, ) = FUNTOKEN_PRECOMPILE_ADDRESS.call(
abi.encodeWithSignature(
"bankSend(address,uint256,string)",
erc20,
uint256(precompileAmount),
precompileRecipient
)
uint256 sentAmount = FUNTOKEN_PRECOMPILE.bankSend(
erc20,
precompileAmount,
precompileRecipient
);

require(success, string.concat("Failed to call bankSend"));
require(
sentAmount == precompileAmount,
string.concat(
"IFunToken.bankSend succeeded but transferred the wrong amount",
"sentAmount ",
Strings.toString(sentAmount),
"expected ",
Strings.toString(precompileAmount)
)
);
}
}
43 changes: 28 additions & 15 deletions x/evm/embeds/contracts/TestFunTokenPrecompileLocalGas.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./FunToken.sol";
import "./IFunToken.sol";
import "@openzeppelin/contracts/utils/Strings.sol";

contract TestFunTokenPrecompileLocalGas {
address erc20;
Expand All @@ -16,15 +17,21 @@ contract TestFunTokenPrecompileLocalGas {
uint256 amount,
string memory bech32Recipient
) public {
(bool success,) = FUNTOKEN_PRECOMPILE_ADDRESS.call(
abi.encodeWithSignature(
"bankSend(address,uint256,string)",
erc20,
uint256 sentAmount = FUNTOKEN_PRECOMPILE.bankSend(
erc20,
amount,
bech32Recipient
bech32Recipient
);
require(
sentAmount == amount,
string.concat(
"IFunToken.bankSend succeeded but transferred the wrong amount",
"sentAmount ",
Strings.toString(sentAmount),
"expected ",
Strings.toString(amount)
)
);
require(success, "Failed to call bankSend");
}

// Calls bankSend of the FunToken Precompile with the gas amount set in parameter.
Expand All @@ -34,14 +41,20 @@ contract TestFunTokenPrecompileLocalGas {
string memory bech32Recipient,
uint256 customGas
) public {
(bool success,) = FUNTOKEN_PRECOMPILE_ADDRESS.call{gas: customGas}(
abi.encodeWithSignature(
"bankSend(address,uint256,string)",
erc20,
amount,
bech32Recipient
uint256 sentAmount = FUNTOKEN_PRECOMPILE.bankSend{gas: customGas}(
erc20,
amount,
bech32Recipient
);
require(
sentAmount == amount,
string.concat(
"IFunToken.bankSend succeeded but transferred the wrong amount",
"sentAmount ",
Strings.toString(sentAmount),
"expected ",
Strings.toString(amount)
)
);
require(success, "Failed to call bankSend with custom gas");
}
}
}
4 changes: 2 additions & 2 deletions x/evm/evmmodule/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ func (s *Suite) TestExportInitGenesis() {
s.Require().NoError(err)

// Transfer ERC-20 tokens to user A
_, err = deps.EvmKeeper.ERC20().Transfer(erc20Addr, fromUser, toUserA, amountToSendA, deps.Ctx)
_, _, err = deps.EvmKeeper.ERC20().Transfer(erc20Addr, fromUser, toUserA, amountToSendA, deps.Ctx)
s.Require().NoError(err)

// Transfer ERC-20 tokens to user B
_, err = deps.EvmKeeper.ERC20().Transfer(erc20Addr, fromUser, toUserB, amountToSendB, deps.Ctx)
_, _, err = deps.EvmKeeper.ERC20().Transfer(erc20Addr, fromUser, toUserB, amountToSendB, deps.Ctx)
s.Require().NoError(err)

// Create fungible token from bank coin
Expand Down
4 changes: 2 additions & 2 deletions x/evm/keeper/erc20_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (s *Suite) TestERC20Calls() {
s.T().Log("Transfer - Not enough funds")
{
amt := big.NewInt(9_420)
_, err := deps.EvmKeeper.ERC20().Transfer(contract, deps.Sender.EthAddr, evm.EVM_MODULE_ADDRESS, amt, deps.Ctx)
_, _, err := deps.EvmKeeper.ERC20().Transfer(contract, deps.Sender.EthAddr, evm.EVM_MODULE_ADDRESS, amt, deps.Ctx)
s.ErrorContains(err, "ERC20: transfer amount exceeds balance")
// balances unchanged
evmtest.AssertERC20BalanceEqual(s.T(), deps, contract, deps.Sender.EthAddr, big.NewInt(0))
Expand All @@ -45,7 +45,7 @@ func (s *Suite) TestERC20Calls() {
s.T().Log("Transfer - Success (sanity check)")
{
amt := big.NewInt(9_420)
sentAmt, err := deps.EvmKeeper.ERC20().Transfer(
sentAmt, _, err := deps.EvmKeeper.ERC20().Transfer(
contract, evm.EVM_MODULE_ADDRESS, deps.Sender.EthAddr, amt, deps.Ctx,
)
s.Require().NoError(err)
Expand Down
1 change: 0 additions & 1 deletion x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ func (k Keeper) EthChainID(ctx sdk.Context) *big.Int {
func (k *Keeper) AddToBlockGasUsed(
ctx sdk.Context, gasUsed uint64,
) (blockGasUsed uint64, err error) {

// Either k.EvmState.BlockGasUsed.GetOr() or k.EvmState.BlockGasUsed.Set()
// also consume gas and could panic.
defer HandleOutOfGasPanic(&err, "")
Expand Down
2 changes: 1 addition & 1 deletion x/evm/precompile/funtoken_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,5 +254,5 @@ func (s *FuntokenSuite) TestPrecompileLocalGas() {
randomAcc.String(), // to
big.NewInt(50_000), // customGas - too small
)
s.Require().ErrorContains(err, "Failed to call bankSend")
s.Require().ErrorContains(err, "execution reverted")
}

0 comments on commit c43219e

Please sign in to comment.