Skip to content

Commit

Permalink
IBC transfer precompile (#1485)
Browse files Browse the repository at this point in the history
* plug in keepers

* add ibc precompile

* remove redundant keepers
  • Loading branch information
dssei authored Mar 28, 2024
1 parent 3e4db93 commit 01f00b3
Show file tree
Hide file tree
Showing 11 changed files with 624 additions and 28 deletions.
5 changes: 4 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,9 @@ func New(
wasmOpts...,
)

app.EvmKeeper = *evmkeeper.NewKeeper(keys[evmtypes.StoreKey], memKeys[evmtypes.MemStoreKey], app.GetSubspace(evmtypes.ModuleName), app.BankKeeper, &app.AccountKeeper, &app.StakingKeeper)
app.EvmKeeper = *evmkeeper.NewKeeper(keys[evmtypes.StoreKey], memKeys[evmtypes.MemStoreKey],
app.GetSubspace(evmtypes.ModuleName), app.BankKeeper, &app.AccountKeeper, &app.StakingKeeper,
app.TransferKeeper)
app.evmRPCConfig, err = evmrpc.ReadConfig(appOpts)
if err != nil {
panic(fmt.Sprintf("error reading EVM config due to %s", err))
Expand Down Expand Up @@ -664,6 +666,7 @@ func New(
app.GovKeeper,
app.DistrKeeper,
app.OracleKeeper,
app.TransferKeeper,
); err != nil {
panic(err)
}
Expand Down
14 changes: 14 additions & 0 deletions precompiles/common/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types"
"github.com/ethereum/go-ethereum/common"
oracletypes "github.com/sei-protocol/sei-chain/x/oracle/types"
)
Expand Down Expand Up @@ -61,3 +62,16 @@ type DistributionKeeper interface {
SetWithdrawAddr(ctx sdk.Context, delegatorAddr sdk.AccAddress, withdrawAddr sdk.AccAddress) error
WithdrawDelegationRewards(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) (sdk.Coins, error)
}

type TransferKeeper interface {
SendTransfer(
ctx sdk.Context,
sourcePort,
sourceChannel string,
token sdk.Coin,
sender sdk.AccAddress,
receiver string,
timeoutHeight clienttypes.Height,
timeoutTimestamp uint64,
) error
}
19 changes: 19 additions & 0 deletions precompiles/common/precompiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
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"
"github.com/sei-protocol/sei-chain/x/evm/state"
)
Expand Down Expand Up @@ -83,3 +84,21 @@ func HandlePaymentUseiWei(ctx sdk.Context, precompileAddr sdk.AccAddress, payer
}
return usei, wei, nil
}

func GetRemainingGas(ctx sdk.Context, evmKeeper EVMKeeper) uint64 {
gasMultipler := evmKeeper.GetPriorityNormalizer(ctx)
seiGasRemaining := ctx.GasMeter().Limit() - ctx.GasMeter().GasConsumedToLimit()
return new(big.Int).Mul(new(big.Int).SetUint64(seiGasRemaining), gasMultipler.RoundInt().BigInt()).Uint64()
}

func ValidateCaller(ctx sdk.Context, evmKeeper EVMKeeper, caller common.Address, callingContract common.Address) error {
if caller == callingContract {
// not a delegate call
return nil
}
codeHash := evmKeeper.GetCodeHash(ctx, callingContract)
if evmKeeper.IsCodeHashWhitelistedForDelegateCall(ctx, codeHash) {
return nil
}
return fmt.Errorf("calling contract %s with code hash %s is not whitelisted for delegate calls", callingContract.Hex(), codeHash.Hex())
}
22 changes: 22 additions & 0 deletions precompiles/ibc/IBC.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

address constant IBC_PRECOMPILE_ADDRESS = 0x0000000000000000000000000000000000001009;

IBC constant IBC_CONTRACT = IBC(
IBC_PRECOMPILE_ADDRESS
);

interface IBC {
// Transactions
function transfer(
string toAddress,
string memory port,
string memory channel,
string memory denom,
uint256 amount,
uint64 revisionNumber,
uint64 revisionHeight,
uint64 timeoutTimestamp
) external returns (bool success);
}
56 changes: 56 additions & 0 deletions precompiles/ibc/abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
[
{
"inputs": [
{
"internalType": "string",
"name": "toAddress",
"type": "string"
},
{
"internalType": "string",
"name": "port",
"type": "string"
},
{
"internalType": "string",
"name": "channel",
"type": "string"
},
{
"internalType": "string",
"name": "denom",
"type": "string"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "uint64",
"name": "revisionNumber",
"type": "uint64"
},
{
"internalType": "uint64",
"name": "revisionHeight",
"type": "uint64"
},
{
"internalType": "uint64",
"name": "timeoutTimestamp",
"type": "uint64"
}
],
"name": "transfer",
"outputs": [
{
"internalType": "bool",
"name": "success",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
}
]
Loading

0 comments on commit 01f00b3

Please sign in to comment.