Skip to content

Commit

Permalink
Merge branch 'main' into zakir/test-bridge-call
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code authored Jan 10, 2025
2 parents d522d32 + 558e8f1 commit 0f96655
Show file tree
Hide file tree
Showing 30 changed files with 175 additions and 274 deletions.
18 changes: 0 additions & 18 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ import (
feemarkettypes "github.com/evmos/ethermint/x/feemarket/types"
"github.com/spf13/cast"

"github.com/pundiai/fx-core/v8/contract"
"github.com/pundiai/fx-core/v8/precompiles/bank"
"github.com/pundiai/fx-core/v8/precompiles/crosschain"
stakingprecompile "github.com/pundiai/fx-core/v8/precompiles/staking"
Expand Down Expand Up @@ -392,7 +391,6 @@ func NewAppKeeper(
appKeepers.AccountKeeper,
appKeepers.BankKeeper,
appKeepers.EvmKeeper,
contract.NewERC20TokenKeeper(appKeepers.EvmKeeper),
authAddr,
)

Expand All @@ -409,8 +407,6 @@ func NewAppKeeper(
appKeepers.Erc20Keeper,
appKeepers.AccountKeeper,
appKeepers.EvmKeeper,
contract.NewBridgeFeeQuoteKeeper(appKeepers.EvmKeeper, contract.BridgeFeeAddress),
contract.NewERC20TokenKeeper(appKeepers.EvmKeeper),
authAddr,
)
appKeepers.PolygonKeeper = crosschainkeeper.NewKeeper(
Expand All @@ -425,8 +421,6 @@ func NewAppKeeper(
appKeepers.Erc20Keeper,
appKeepers.AccountKeeper,
appKeepers.EvmKeeper,
contract.NewBridgeFeeQuoteKeeper(appKeepers.EvmKeeper, contract.BridgeFeeAddress),
contract.NewERC20TokenKeeper(appKeepers.EvmKeeper),
authAddr,
)
appKeepers.AvalancheKeeper = crosschainkeeper.NewKeeper(
Expand All @@ -441,8 +435,6 @@ func NewAppKeeper(
appKeepers.Erc20Keeper,
appKeepers.AccountKeeper,
appKeepers.EvmKeeper,
contract.NewBridgeFeeQuoteKeeper(appKeepers.EvmKeeper, contract.BridgeFeeAddress),
contract.NewERC20TokenKeeper(appKeepers.EvmKeeper),
authAddr,
)
appKeepers.EthKeeper = crosschainkeeper.NewKeeper(
Expand All @@ -457,8 +449,6 @@ func NewAppKeeper(
appKeepers.Erc20Keeper,
appKeepers.AccountKeeper,
appKeepers.EvmKeeper,
contract.NewBridgeFeeQuoteKeeper(appKeepers.EvmKeeper, contract.BridgeFeeAddress),
contract.NewERC20TokenKeeper(appKeepers.EvmKeeper),
authAddr,
)
appKeepers.ArbitrumKeeper = crosschainkeeper.NewKeeper(
Expand All @@ -473,8 +463,6 @@ func NewAppKeeper(
appKeepers.Erc20Keeper,
appKeepers.AccountKeeper,
appKeepers.EvmKeeper,
contract.NewBridgeFeeQuoteKeeper(appKeepers.EvmKeeper, contract.BridgeFeeAddress),
contract.NewERC20TokenKeeper(appKeepers.EvmKeeper),
authAddr,
)
appKeepers.OptimismKeeper = crosschainkeeper.NewKeeper(
Expand All @@ -489,8 +477,6 @@ func NewAppKeeper(
appKeepers.Erc20Keeper,
appKeepers.AccountKeeper,
appKeepers.EvmKeeper,
contract.NewBridgeFeeQuoteKeeper(appKeepers.EvmKeeper, contract.BridgeFeeAddress),
contract.NewERC20TokenKeeper(appKeepers.EvmKeeper),
authAddr,
)
appKeepers.Layer2Keeper = crosschainkeeper.NewKeeper(
Expand All @@ -505,8 +491,6 @@ func NewAppKeeper(
appKeepers.Erc20Keeper,
appKeepers.AccountKeeper,
appKeepers.EvmKeeper,
contract.NewBridgeFeeQuoteKeeper(appKeepers.EvmKeeper, contract.BridgeFeeAddress),
contract.NewERC20TokenKeeper(appKeepers.EvmKeeper),
authAddr,
)
appKeepers.TronKeeper = crosschainkeeper.NewKeeper(
Expand All @@ -521,8 +505,6 @@ func NewAppKeeper(
appKeepers.Erc20Keeper,
appKeepers.AccountKeeper,
appKeepers.EvmKeeper,
contract.NewBridgeFeeQuoteKeeper(appKeepers.EvmKeeper, contract.BridgeFeeAddress),
contract.NewERC20TokenKeeper(appKeepers.EvmKeeper),
authAddr,
)

Expand Down
4 changes: 3 additions & 1 deletion contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math/big"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -86,7 +87,8 @@ var (

type Caller interface {
QueryContract(ctx context.Context, from, contract common.Address, abi abi.ABI, method string, res interface{}, args ...interface{}) error
ApplyContract(ctx context.Context, from, contract common.Address, value *big.Int, abi abi.ABI, method string, constructorData ...interface{}) (*evmtypes.MsgEthereumTxResponse, error)
ApplyContract(ctx context.Context, from, contract common.Address, value *big.Int, abi abi.ABI, method string, args ...interface{}) (*evmtypes.MsgEthereumTxResponse, error)
ExecuteEVM(ctx sdk.Context, from common.Address, contract *common.Address, value *big.Int, gasLimit uint64, data []byte) (*evmtypes.MsgEthereumTxResponse, error)
}

type Contract struct {
Expand Down
1 change: 1 addition & 0 deletions contract/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type EvmKeeper interface {
CreateContractWithCode(ctx sdk.Context, address common.Address, code []byte) error
ApplyContract(ctx context.Context, from, contract common.Address, value *big.Int, abi abi.ABI, method string, constructorData ...interface{}) (*evmtypes.MsgEthereumTxResponse, error)
QueryContract(ctx context.Context, from, contract common.Address, abi abi.ABI, method string, res interface{}, args ...interface{}) error
ExecuteEVM(ctx sdk.Context, from common.Address, contract *common.Address, value *big.Int, gasLimit uint64, data []byte) (*evmtypes.MsgEthereumTxResponse, error)
}

type AccountKeeper interface {
Expand Down
2 changes: 1 addition & 1 deletion precompiles/crosschain/bridge_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (m *BridgeCallMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, erro
}
baseCoins = append(baseCoins, baseCoin)
}
nonce, err := crosschainKeeper.BridgeCallBaseCoin(ctx, sender, args.Refund, args.To, baseCoins, args.Data, args.Memo, args.QuoteId, args.GasLimit, fxTarget, originTokenAmount)
nonce, err := crosschainKeeper.BridgeCallBaseCoin(ctx, vmCaller, sender, args.Refund, args.To, baseCoins, args.Data, args.Memo, args.QuoteId, args.GasLimit, fxTarget, originTokenAmount)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions precompiles/crosschain/crosschain.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (m *LegacyCrosschainMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte
if err = fxTarget.ValidateExternalAddr(args.Receipt); err != nil {
return err
}
vmCaller := precompiles.NewVMCall(evm)

baseCoin := sdk.Coin{}
totalAmount := big.NewInt(0).Add(args.Amount, args.Fee)
Expand All @@ -83,7 +84,6 @@ func (m *LegacyCrosschainMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte
return err
}
} else {
vmCaller := precompiles.NewVMCall(evm)
baseCoin, err = m.EvmTokenToBaseCoin(ctx, vmCaller, crosschainKeeper, sender, args.Token, totalAmount)
if err != nil {
return err
Expand All @@ -92,7 +92,7 @@ func (m *LegacyCrosschainMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte

amountCoin := sdk.NewCoin(baseCoin.Denom, sdkmath.NewIntFromBigInt(args.Amount))
feeCoin := sdk.NewCoin(baseCoin.Denom, sdkmath.NewIntFromBigInt(args.Fee))
if err = crosschainKeeper.CrosschainBaseCoin(ctx, sender.Bytes(), args.Receipt,
if err = crosschainKeeper.CrosschainBaseCoin(ctx, vmCaller, sender.Bytes(), args.Receipt,
amountCoin, feeCoin, fxTarget, args.Memo, isOriginToken); err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion precompiles/crosschain/execute_claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ethereum/go-ethereum/core/vm"

fxcontract "github.com/pundiai/fx-core/v8/contract"
"github.com/pundiai/fx-core/v8/precompiles"
evmtypes "github.com/pundiai/fx-core/v8/x/evm/types"
)

Expand Down Expand Up @@ -55,7 +56,7 @@ func (m *ExecuteClaimMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, er
}
stateDB := evm.StateDB.(evmtypes.ExtStateDB)
if err = stateDB.ExecuteNativeAction(contract.Address(), nil, func(ctx sdk.Context) error {
preExecuteErr, executeErr := crosschainKeeper.ExecuteClaim(ctx, args.EventNonce.Uint64())
preExecuteErr, executeErr := crosschainKeeper.ExecuteClaim(ctx, precompiles.NewVMCall(evm), args.EventNonce.Uint64())
if preExecuteErr != nil {
return preExecuteErr
}
Expand Down
7 changes: 4 additions & 3 deletions precompiles/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ethereum/go-ethereum/common"
evmtypes "github.com/evmos/ethermint/x/evm/types"

"github.com/pundiai/fx-core/v8/contract"
crosschaintypes "github.com/pundiai/fx-core/v8/x/crosschain/types"
erc20types "github.com/pundiai/fx-core/v8/x/erc20/types"
)
Expand All @@ -27,10 +28,10 @@ type BankKeeper interface {
}

type CrosschainKeeper interface {
ExecuteClaim(ctx sdk.Context, eventNonce uint64) (error, error)
ExecuteClaim(ctx sdk.Context, caller contract.Caller, eventNonce uint64) (error, error)
BridgeCoinSupply(ctx context.Context, token, target string) (sdk.Coin, error)
CrosschainBaseCoin(ctx sdk.Context, from sdk.AccAddress, receipt string, amount, fee sdk.Coin, fxTarget *crosschaintypes.FxTarget, memo string, originToken bool) error
BridgeCallBaseCoin(ctx sdk.Context, from, refund, to common.Address, coins sdk.Coins, data, memo []byte, quoteId, gasLimit *big.Int, fxTarget *crosschaintypes.FxTarget, originTokenAmount sdkmath.Int) (uint64, error)
CrosschainBaseCoin(ctx sdk.Context, caller contract.Caller, from sdk.AccAddress, receipt string, amount, fee sdk.Coin, fxTarget *crosschaintypes.FxTarget, memo string, originToken bool) error
BridgeCallBaseCoin(ctx sdk.Context, caller contract.Caller, from, refund, to common.Address, coins sdk.Coins, data, memo []byte, quoteId, gasLimit *big.Int, fxTarget *crosschaintypes.FxTarget, originTokenAmount sdkmath.Int) (uint64, error)
GetERC20TokenByAddr(ctx sdk.Context, erc20Addr common.Address) (erc20types.ERC20Token, error)

HasOracleAddrByExternalAddr(ctx sdk.Context, externalAddress string) bool
Expand Down
19 changes: 15 additions & 4 deletions precompiles/vm_caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
Expand Down Expand Up @@ -37,18 +38,28 @@ func (v *VMCall) QueryContract(_ context.Context, from, contract common.Address,
return nil
}

func (v *VMCall) ApplyContract(_ context.Context, from, contract common.Address, value *big.Int, abi abi.ABI, method string, constructorData ...interface{}) (*evmtypes.MsgEthereumTxResponse, error) {
data, err := abi.Pack(method, constructorData...)
func (v *VMCall) ApplyContract(_ context.Context, from, contract common.Address, value *big.Int, abi abi.ABI, method string, args ...interface{}) (*evmtypes.MsgEthereumTxResponse, error) {
data, err := abi.Pack(method, args...)
if err != nil {
return nil, types.ErrABIPack.Wrap(err.Error())
}
ret, leftoverGas, vmErr := v.evm.Call(vm.AccountRef(from), contract, data, v.maxGas, value)
return v.ExecuteEVM(sdk.Context{}, from, &contract, value, v.maxGas, data)
}

func (v *VMCall) ExecuteEVM(_ sdk.Context, from common.Address, contract *common.Address, value *big.Int, gasLimit uint64, data []byte) (*evmtypes.MsgEthereumTxResponse, error) {
if value == nil {
value = big.NewInt(0)
}
if contract == nil {
contract = &common.Address{}
}
ret, leftoverGas, vmErr := v.evm.Call(vm.AccountRef(from), *contract, data, gasLimit, value)
var vmError string
if vmErr != nil {
vmError = vmErr.Error()
}
return &evmtypes.MsgEthereumTxResponse{
GasUsed: v.maxGas - leftoverGas,
GasUsed: gasLimit - leftoverGas,
VmError: vmError,
Ret: ret,
}, nil
Expand Down
2 changes: 1 addition & 1 deletion x/crosschain/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (k Keeper) cleanupTimeOutBridgeCall(ctx sdk.Context) (err error) {
quoteInfo, found := k.GetOutgoingBridgeCallQuoteInfo(ctx, data.Nonce)
if !found {
// 1. handler bridge call refund
if err = k.RefundOutgoingBridgeCall(ctx, data); err != nil {
if err = k.RefundOutgoingBridgeCall(ctx, k.evmKeeper, data); err != nil {
return true
}
} else {
Expand Down
9 changes: 5 additions & 4 deletions x/crosschain/keeper/attestation_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/pundiai/fx-core/v8/contract"
"github.com/pundiai/fx-core/v8/x/crosschain/types"
)

Expand All @@ -27,7 +28,7 @@ func (k Keeper) AttestationHandler(ctx sdk.Context, externalClaim types.External
}
}

func (k Keeper) ExecuteClaim(ctx sdk.Context, eventNonce uint64) (preExecuteErr, executeErr error) {
func (k Keeper) ExecuteClaim(ctx sdk.Context, caller contract.Caller, eventNonce uint64) (preExecuteErr, executeErr error) {
externalClaim, preExecuteErr := k.GetPendingExecuteClaim(ctx, eventNonce)
if preExecuteErr != nil {
return preExecuteErr, nil
Expand All @@ -37,11 +38,11 @@ func (k Keeper) ExecuteClaim(ctx sdk.Context, eventNonce uint64) (preExecuteErr,
cacheCtx, commit := ctx.CacheContext()
switch claim := externalClaim.(type) {
case *types.MsgSendToFxClaim:
executeErr = k.SendToFxExecuted(cacheCtx, claim)
executeErr = k.SendToFxExecuted(cacheCtx, caller, claim)
case *types.MsgBridgeCallClaim:
executeErr = k.BridgeCallExecuted(cacheCtx, claim)
executeErr = k.BridgeCallExecuted(cacheCtx, caller, claim)
case *types.MsgBridgeCallResultClaim:
executeErr = k.BridgeCallResultExecuted(cacheCtx, claim)
executeErr = k.BridgeCallResultExecuted(cacheCtx, caller, claim)
default:
executeErr = sdkerrors.ErrInvalidRequest.Wrapf("invalid claim type: %s", claim.GetType())
}
Expand Down
14 changes: 7 additions & 7 deletions x/crosschain/keeper/bridge_call_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/pundiai/fx-core/v8/x/crosschain/types"
)

func (k Keeper) BridgeCallExecuted(ctx sdk.Context, msg *types.MsgBridgeCallClaim) error {
func (k Keeper) BridgeCallExecuted(ctx sdk.Context, caller contract.Caller, msg *types.MsgBridgeCallClaim) error {
k.CreateBridgeAccount(ctx, msg.TxOrigin)
if senderAccount := k.ak.GetAccount(ctx, msg.GetSenderAddr().Bytes()); senderAccount != nil {
if _, ok := senderAccount.(sdk.ModuleAccountI); ok {
Expand All @@ -41,12 +41,12 @@ func (k Keeper) BridgeCallExecuted(ctx sdk.Context, msg *types.MsgBridgeCallClai
baseCoins = baseCoins.Add(baseCoin)
}

if err := k.HandlerBridgeCallInFee(ctx, msg.GetSenderAddr(), msg.QuoteId.BigInt(), msg.GasLimit.BigInt()); err != nil {
if err := k.HandlerBridgeCallInFee(ctx, caller, msg.GetSenderAddr(), msg.QuoteId.BigInt(), msg.GasLimit.BigInt()); err != nil {
return err
}

cacheCtx, commit := sdk.UnwrapSDKContext(ctx).CacheContext()
err := k.BridgeCallEvm(cacheCtx, msg.GetSenderAddr(), msg.GetRefundAddr(), msg.GetToAddr(),
err := k.BridgeCallEvm(cacheCtx, caller, msg.GetSenderAddr(), msg.GetRefundAddr(), msg.GetToAddr(),
receiverAddr, baseCoins, msg.MustData(), msg.MustMemo(), isMemoSendCallTo, msg.GetGasLimit())
if !ctx.IsCheckTx() {
telemetry.IncrCounterWithLabels(
Expand Down Expand Up @@ -76,7 +76,7 @@ func (k Keeper) BridgeCallExecuted(ctx sdk.Context, msg *types.MsgBridgeCallClai
if fxtypes.IsOriginDenom(coin.Denom) {
continue
}
if _, err = k.erc20Keeper.BaseCoinToEvm(ctx, msg.GetRefundAddr(), coin); err != nil {
if _, err = k.erc20Keeper.BaseCoinToEvm(ctx, caller, msg.GetRefundAddr(), coin); err != nil {
return err
}
}
Expand All @@ -94,11 +94,11 @@ func (k Keeper) BridgeCallExecuted(ctx sdk.Context, msg *types.MsgBridgeCallClai
return err
}

func (k Keeper) BridgeCallEvm(ctx sdk.Context, sender, refundAddr, to, receiverAddr common.Address, baseCoins sdk.Coins, data, memo []byte, isMemoSendCallTo bool, gasLimit uint64) error {
func (k Keeper) BridgeCallEvm(ctx sdk.Context, caller contract.Caller, sender, refundAddr, to, receiverAddr common.Address, baseCoins sdk.Coins, data, memo []byte, isMemoSendCallTo bool, gasLimit uint64) error {
tokens := make([]common.Address, 0, baseCoins.Len())
amounts := make([]*big.Int, 0, baseCoins.Len())
for _, coin := range baseCoins {
tokenContract, err := k.erc20Keeper.BaseCoinToEvm(ctx, receiverAddr, coin)
tokenContract, err := k.erc20Keeper.BaseCoinToEvm(ctx, caller, receiverAddr, coin)
if err != nil {
return err
}
Expand Down Expand Up @@ -127,7 +127,7 @@ func (k Keeper) BridgeCallEvm(ctx sdk.Context, sender, refundAddr, to, receiverA
if gasLimit == 0 {
gasLimit = k.GetBridgeCallMaxGasLimit(ctx)
}
txResp, err := k.evmKeeper.ExecuteEVM(ctx, callEvmSender, &to, nil, gasLimit, args)
txResp, err := caller.ExecuteEVM(ctx, callEvmSender, &to, nil, gasLimit, args)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion x/crosschain/keeper/bridge_call_in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (suite *KeeperTestSuite) TestBridgeCallHandler() {
erc20Tokens = append(erc20Tokens, erc20Token)
}

err := suite.Keeper().BridgeCallExecuted(suite.Ctx, &tc.Msg)
err := suite.Keeper().BridgeCallExecuted(suite.Ctx, suite.App.EvmKeeper, &tc.Msg)
if tc.Success {
suite.Require().NoError(err)
if !tc.CallContract {
Expand Down
Loading

0 comments on commit 0f96655

Please sign in to comment.