Skip to content

Commit

Permalink
fix: transfer erc20 token to module address before processing (#937)
Browse files Browse the repository at this point in the history
Co-authored-by: nulnut <[email protected]>
  • Loading branch information
zakir-code and nulnut authored Jan 22, 2025
1 parent 4bb4b7f commit 69b553f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
8 changes: 5 additions & 3 deletions precompiles/crosschain/bridge_call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,15 @@ func (suite *CrosschainPrecompileTestSuite) TestContract_BridgeCall() {

erc20TokenKeeper := contract.NewERC20TokenKeeper(suite.App.EvmKeeper)
minter := common.BytesToAddress(authtypes.NewModuleAddress(erc20types.ModuleName).Bytes())
_, err := erc20TokenKeeper.Mint(suite.Ctx, tokenAddr, minter, suite.GetSender(), big.NewInt(100))
_, err := erc20TokenKeeper.Mint(suite.Ctx, tokenAddr, minter, suite.signer.Address(), big.NewInt(100))
suite.Require().NoError(err)
suite.MintTokenToModule(erc20types.ModuleName, sdk.NewCoin("usdt", sdkmath.NewInt(100)))

suite.App.CrosschainKeepers.GetKeeper(suite.chainName).
SetLastObservedBlockHeight(suite.Ctx, 100, 100)

_, err = erc20TokenKeeper.Approve(suite.Ctx, tokenAddr, suite.signer.Address(), suite.crosschainAddr, big.NewInt(1))
suite.Require().NoError(err)
txResponse := suite.BridgeCall(suite.Ctx, suite.signer.Address(), contract.BridgeCallArgs{
DstChain: suite.chainName,
Refund: suite.signer.Address(),
Expand All @@ -200,9 +202,9 @@ func (suite *CrosschainPrecompileTestSuite) TestContract_BridgeCall() {
Memo: nil,
})
suite.Require().NotNil(txResponse)
suite.Require().Len(txResponse.Logs, 2)
suite.Require().GreaterOrEqual(len(txResponse.Logs), 2)

balance, err := erc20TokenKeeper.BalanceOf(suite.Ctx, tokenAddr, suite.GetSender())
balance, err := erc20TokenKeeper.BalanceOf(suite.Ctx, tokenAddr, suite.signer.Address())
suite.Require().NoError(err)
suite.Require().Equal(big.NewInt(99), balance)
}
9 changes: 4 additions & 5 deletions precompiles/crosschain/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,19 @@ func (c *Keeper) EvmTokenToBaseCoin(ctx sdk.Context, caller contract.Caller, cro
}
baseCoin := sdk.NewCoin(erc20Token.Denom, sdkmath.NewIntFromBigInt(amount))
erc20ModuleAddress := common.BytesToAddress(authtypes.NewModuleAddress(erc20types.ModuleName))

erc20TokenKeeper := contract.NewERC20TokenKeeper(caller)
if _, err = erc20TokenKeeper.TransferFrom(ctx, tokenAddr, crosschainAddress, holder, erc20ModuleAddress, amount); err != nil {
return sdk.Coin{}, err
}
if erc20Token.IsNativeCoin() {
if _, err = erc20TokenKeeper.Burn(ctx, tokenAddr, erc20ModuleAddress, holder, amount); err != nil {
if _, err = erc20TokenKeeper.Burn(ctx, tokenAddr, erc20ModuleAddress, erc20ModuleAddress, amount); err != nil {
return sdk.Coin{}, err
}
if erc20Token.Denom == fxtypes.DefaultDenom {
err = c.bankKeeper.SendCoins(ctx, tokenAddr.Bytes(), holder.Bytes(), sdk.NewCoins(baseCoin))
return baseCoin, err
}
} else if erc20Token.IsNativeERC20() {
if _, err = erc20TokenKeeper.TransferFrom(ctx, tokenAddr, erc20ModuleAddress, holder, erc20ModuleAddress, amount); err != nil {
return sdk.Coin{}, err
}
if err = c.bankKeeper.MintCoins(ctx, erc20types.ModuleName, sdk.NewCoins(baseCoin)); err != nil {
return sdk.Coin{}, err
}
Expand Down
23 changes: 12 additions & 11 deletions solidity/contracts/test/CrosschainTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,6 @@ contract CrosschainTest {
_amount + _fee
);
IERC20(_token).approve(CROSS_CHAIN_ADDRESS, _amount + _fee);
}

if (_token != address(0)) {
uint256 allowance = IERC20(_token).allowance(
address(this),
CROSS_CHAIN_ADDRESS
);
require(
allowance == _amount + _fee,
"allowance not equal amount + fee"
);
} else {
require(
msg.value == _amount + _fee,
Expand Down Expand Up @@ -76,6 +65,18 @@ contract CrosschainTest {
uint256 _gasLimit,
bytes memory _memo
) external returns (uint256) {
require(
_tokens.length == _amounts.length,
"token and amount length not equal"
);
for (uint256 i = 0; i < _tokens.length; i++) {
IERC20(_tokens[i]).transferFrom(
msg.sender,
address(this),
_amounts[i]
);
IERC20(_tokens[i]).approve(CROSS_CHAIN_ADDRESS, _amounts[i]);
}
return
ICrosschain(CROSS_CHAIN_ADDRESS).bridgeCall(
_dstChain,
Expand Down
Loading

0 comments on commit 69b553f

Please sign in to comment.