Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBC transfer precompile #1459

Closed
wants to merge 16 commits into from
Prev Previous commit
Next Next commit
address review comments - part one
  • Loading branch information
dssei committed Mar 22, 2024
commit 5db906cfd2d8afe4dea2e158feb2e30f6ab90e29
6 changes: 4 additions & 2 deletions precompiles/ibc/IBC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ IBC constant IBC_CONTRACT = IBC(
interface IBC {
// Transactions
function transfer(
address fromAddress,
address toAddress,
string memory port,
string memory channel,
string memory denom,
uint256 amount
uint256 amount,
uint64 revisionNumber,
uint64 revisionHeight,
uint64 timeoutTimestamp
) external returns (bool success);
}
20 changes: 15 additions & 5 deletions precompiles/ibc/abi.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
[
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keeping formatted for readability for now (will flatten when code is post-draft)

{
"inputs": [
{
"internalType": "address",
"name": "fromAddress",
"type": "address"
},
{
"internalType": "address",
"name": "toAddress",
Expand All @@ -30,6 +25,21 @@
"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",
Expand Down
44 changes: 27 additions & 17 deletions precompiles/ibc/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,39 +92,35 @@ func (p Precompile) Run(evm *vm.EVM, caller common.Address, input []byte, value

switch method.Name {
case TransferMethod:
return p.transfer(ctx, method, args)
return p.transfer(ctx, method, args, caller)
}
return
}
func (p Precompile) transfer(ctx sdk.Context, method *abi.Method, args []interface{}) ([]byte, error) {
pcommon.AssertArgsLength(args, 6)
func (p Precompile) transfer(ctx sdk.Context, method *abi.Method, args []interface{}, caller common.Address) ([]byte, error) {
pcommon.AssertArgsLength(args, 8)
senderSeiAddr, ok := p.evmKeeper.GetSeiAddress(ctx, caller)

senderAddress, err := p.accAddressFromArg(ctx, args[0])
receiverAddress, err := p.accAddressFromArg(ctx, args[0])
if err != nil {
return nil, err
}

receiverAddress, err := p.accAddressFromArg(ctx, args[1])
if err != nil {
return nil, err
}

port, ok := args[2].(string)
port, ok := args[1].(string)
if !ok {
return nil, errors.New("port is not a string")
}

channelID, ok := args[3].(string)
channelID, ok := args[2].(string)
if !ok {
return nil, errors.New("channelID is not a string")
}

denom := args[4].(string)
denom := args[3].(string)
if denom == "" {
return nil, errors.New("invalid denom")
}

amount, ok := args[5].(*big.Int)
amount, ok := args[4].(*big.Int)
if !ok {
return nil, errors.New("amount is not a big.Int")
}
Expand All @@ -139,13 +135,27 @@ func (p Precompile) transfer(ctx sdk.Context, method *abi.Method, args []interfa
Amount: sdk.NewIntFromBigInt(amount),
}

ctx.BlockHeight()
revisionNumber, ok := args[5].(uint64)
if !ok {
return nil, errors.New("revisionNumber is not a uint64")
}

revisionHeight, ok := args[6].(uint64)
if !ok {
return nil, errors.New("revisionHeight is not a uint64")
}

height := clienttypes.Height{
RevisionNumber: 1,
RevisionHeight: uint64(ctx.BlockHeight() + 1),
RevisionNumber: revisionNumber,
RevisionHeight: revisionHeight,
}

timeoutTimestamp, ok := args[7].(uint64)
if !ok {
return nil, errors.New("timeoutTimestamp is not a uint64")
}

err = p.transferKeeper.SendTransfer(ctx, port, channelID, coin, senderAddress, receiverAddress.String(), height, 0)
err = p.transferKeeper.SendTransfer(ctx, port, channelID, coin, senderSeiAddr, receiverAddress.String(), height, timeoutTimestamp)

if err != nil {
return nil, err
Expand Down
35 changes: 21 additions & 14 deletions precompiles/ibc/ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ func TestPrecompile_Run(t *testing.T) {
}

type input struct {
senderEvmAddr common.Address
receiverEvmAddr common.Address
sourcePort string
sourceChannel string
denom string
amount *big.Int
senderEvmAddr common.Address
receiverEvmAddr common.Address
sourcePort string
sourceChannel string
denom string
amount *big.Int
revisionNumber uint64
revisionHeight uint64
timeoutTimestamp uint64
}
type args struct {
caller common.Address
Expand All @@ -62,12 +65,15 @@ func TestPrecompile_Run(t *testing.T) {
args: args{
caller: senderEvmAddress,
input: &input{
senderEvmAddr: senderEvmAddress,
receiverEvmAddr: receiverEvmAddress,
sourcePort: "sourcePort",
sourceChannel: "sourceChannel",
denom: "denom",
amount: big.NewInt(100),
senderEvmAddr: senderEvmAddress,
receiverEvmAddr: receiverEvmAddress,
sourcePort: "sourcePort",
sourceChannel: "sourceChannel",
denom: "denom",
amount: big.NewInt(100),
revisionNumber: 1,
revisionHeight: 1,
timeoutTimestamp: 1,
},
value: nil,
},
Expand All @@ -88,8 +94,9 @@ func TestPrecompile_Run(t *testing.T) {
p, _ := ibc.NewPrecompile(tt.fields.transferKeeper, k)
transfer, err := p.ABI.MethodById(p.TransferID)
require.Nil(t, err)
inputs, err := transfer.Inputs.Pack(tt.args.input.senderEvmAddr, tt.args.input.receiverEvmAddr,
tt.args.input.sourcePort, tt.args.input.sourceChannel, tt.args.input.denom, tt.args.input.amount)
inputs, err := transfer.Inputs.Pack(tt.args.input.receiverEvmAddr,
tt.args.input.sourcePort, tt.args.input.sourceChannel, tt.args.input.denom, tt.args.input.amount,
tt.args.input.revisionNumber, tt.args.input.revisionHeight, tt.args.input.timeoutTimestamp)
require.Nil(t, err)
gotBz, err := p.Run(&evm, tt.args.caller, append(p.TransferID, inputs...), tt.args.value)
if (err != nil) != tt.wantErr {
Expand Down