Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Oct 21, 2024
1 parent d53a13e commit aec06b8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
22 changes: 9 additions & 13 deletions app/ibc-hooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ type MsgCall struct {
// Hex encoded execution input bytes.
Input string `protobuf:"bytes,3,opt,name=input,proto3" json:"input,omitempty"`
// Value is the amount of fee denom token to transfer to the contract.
Value string `protobuf:"bytes,4,opt,name=value,proto3" json:"value,omitempty"`

Value string `protobuf:"bytes,4,opt,name=value,proto3" json:"value,omitempty"`
}
```

Expand All @@ -74,7 +73,7 @@ msg := MsgCall{
ContractAddr: packet.data.memo["evm"]["message"]["contract_addr"],
// Hex encoded execution input bytes.
Input: packet.data.memo["evm"]["message"]["input"],

// Value is the amount of fee denom token to transfer to the contract.
Value: packet.data.memo["evm"]["message"]["value"]
}
```
Expand Down Expand Up @@ -170,30 +169,27 @@ Also when a contract make IBC transfer request, it should provide async callback

### IBC Transfer using ERC20Wrapper

`src -> dst`: use the ERC20Wrapper contract to wrap and do ibc-transfer
`src -> dst`: Execute the ERC20Wrapper contract to wrap and do ibc-transfer

`dst -> src`: ibc-trasfer and execute the ERC20Wrapper contract via ibc-hook

`dst -> src`: unwrapped the wrapped token by execute hook
- data example

```json
{
//... other ibc fields that we don't care about
"data": {
"denom": "wrapped token denom", // will be transformed to the local denom (ibc/...)
"amount": "1000",
"sender": "addr on counterparty chain", // will be transformed
"receiver": "ModuleAddr::ModuleName::FunctionName",
"receiver": "0xcontractaddr",
"memo": {
"evm": {
// execute message on receive packet
"message": {
"contract_addr": "...", // should query erc20 wrapper contract addr
"input": "...", // function selector(fc078758) + abiCoder.encode([string,address,address],denom,amount) ref)https://docs.ethers.org/v6/api/abi/abi-coder/#AbiCoder-encode
"contract_addr": "0xerc20_wrapper_contract", // should query erc20 wrapper contract addr
"input": "pack(unwrap, denom, recipient, amount)", // function selector(fc078758) + abiCoder.encode([string,address,address],denom,amount) ref) https://docs.ethers.org/v6/api/abi/abi-coder/#AbiCoder-encode
"value": "0"
},
// optional field to get async callback (ack and timeout)
"async_callback": {
"id": 1,
"contract_addr": "0x1"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion x/evm/contracts/erc20_wrapper/ERC20Wrapper.go

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion x/evm/contracts/erc20_wrapper/ERC20Wrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ contract ERC20Wrapper is

/**
* @notice This function wraps the tokens and transfer the tokens by ibc transfer
* @dev A sender requires sender approve to this contract to transfer the tokens.
*/
function wrap(
string memory channel,
Expand All @@ -42,6 +43,7 @@ contract ERC20Wrapper is
uint timeout
) public {
_ensureWrappedTokenExists(token);

// lock origin token
IERC20(token).transferFrom(msg.sender, address(this), amount);
uint wrappedAmt = _convertDecimal(
Expand All @@ -53,12 +55,14 @@ contract ERC20Wrapper is
ERC20(wrappedTokens[token]).mint(address(this), wrappedAmt);

callBackId += 1;

// store the callback data
ibcCallBack[callBackId] = IbcCallBack({
sender: msg.sender,
wrappedTokenDenom: COSMOS_CONTRACT.to_denom(wrappedTokens[token]),
wrappedAmt: wrappedAmt
});

// do ibc transfer wrapped token
COSMOS_CONTRACT.execute_cosmos(
_ibc_transfer(
Expand All @@ -85,8 +89,10 @@ contract ERC20Wrapper is
originTokens[wrappedToken] != address(0),
"origin token doesn't exist"
);

// burn wrapped token
ERC20(wrappedToken).burnFrom(msg.sender, wrappedAmt);

// unlock origin token and transfer to receiver
uint amount = _convertDecimal(
wrappedAmt,
Expand All @@ -108,7 +114,8 @@ contract ERC20Wrapper is
_handleFailedIbcTransfer(callback_id);
}

// internal function //
// internal functions //

function _handleFailedIbcTransfer(uint64 callback_id) internal {
IbcCallBack memory callback = ibcCallBack[callback_id];
unwrap(
Expand Down
2 changes: 1 addition & 1 deletion x/evm/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (k Keeper) InitializeWithDecimals(ctx context.Context, decimals uint8) erro
}

// 3. Deploy and store the wrapper ERC20 factory contract for IBC transfers to the destination chain (not compatible due to 18 decimals).
_, wrapperAddr, _, err := k.EVMCreate2(ctx, types.StdAddress, code, nil, types.ERC20WrapperSalt)
_, wrapperAddr, _, err := k.EVMCreate2(ctx, types.StdAddress, code, nil, types.ERC20WrapperSalt, nil)
if err != nil {
return err
}

Check warning on line 56 in x/evm/keeper/genesis.go

View check run for this annotation

Codecov / codecov/patch

x/evm/keeper/genesis.go#L55-L56

Added lines #L55 - L56 were not covered by tests
Expand Down

0 comments on commit aec06b8

Please sign in to comment.