Skip to content

Commit

Permalink
Merge pull request #28 from synapseweb3/feat/add_send_transfer_packet…
Browse files Browse the repository at this point in the history
…_script

feat: add send_erc20_packet script
  • Loading branch information
ashuralyk authored Oct 26, 2023
2 parents ad73e57 + 7cff90b commit 8213b64
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 88 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ function bindPort(string calldata portId, address moduleAddress)

To comply with IBC protocol regulations, a channel cannot function until it is paired with a business module through a port.

## Send ICS20 Packet (WIP)
## Send ICS20 Packet (using ERC20)

In the previous migration step, a standard ICS20 transfer module has already been bound at port `port-0` by default. Run script `scripts/send_packet.js` to send a standard custom ICS20 packet:
In the previous migration step, a standard ICS20 transfer module has already been bound at port `transfer` by default. Run script `scripts/send_erc20_packet.js` to send a standard custom ICS20 packet:

```bash
$ yarn send
```

This is because an IBC packet cannot be sent by directly calling the method in `OwnableIBCHandler` contract. Instead, it should be sent by calling a method in the business module, and the handler contract will be invoked subsequently.
This is because an IBC packet cannot be sent by directly calling the method in `OwnableIBCHandler` contract. Instead, it should be sent by calling a method in the business module, and the handler contract will be invoked subsequently.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"scripts": {
"compile": "truffle compile",
"migrate": "truffle migrate --network axon",
"send": "truffle exec './scripts/send_packet.js' --network axon",
"query": "truffle exec './scripts/query_packet.js' --network axon"
"send": "truffle exec './scripts/send_erc20_packet_back.js' --network axon"
},
"dependencies": {
"@openzeppelin/contracts": "^4.9.3",
Expand All @@ -17,4 +16,4 @@
"ganache": "^7.9.0",
"truffle": "^5.11.1"
}
}
}
28 changes: 0 additions & 28 deletions scripts/common.js

This file was deleted.

19 changes: 0 additions & 19 deletions scripts/query_packet.js

This file was deleted.

59 changes: 59 additions & 0 deletions scripts/send_erc20_packet_back.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// This is for emitting SendPacket event directly from Axon to initial the IBC communication

async function main() {
// First provider account.
const [sender] = await web3.eth.getAccounts();
// Receiver should be CKB address, which is first 20 bytes of hash of sender's lock_script
const receiver = process.env.RECEIVER;
console.log("Sender and receiver:", sender, receiver);

const ICS20TransferERC20 = await artifacts.require("ICS20TransferERC20");
const IERC20 = await artifacts.require("IERC20");

const transfer = await ICS20TransferERC20.at(
process.env.TRANSFER_CONTRACT_ADDRESS
);

const port = "transfer";
const channel = process.env.CHANNEL;

const denom = `${port}/${channel}/${process.env.DENOM}`;

// Check token associated with the denom that is created before
let tokenAddr = await transfer.denomTokenContract(denom);
if (tokenAddr == "0x0000000000000000000000000000000000000000") {
// const ERC20PresetMinterPauser = await artifacts.require("ERC20PresetMinterPauser");
// const token_name = process.env.TOKEN_NAME;
// const token_symbol = process.env.TOKEN_SIMBOL;
// const token = await ERC20PresetMinterPauser.new(token_name, token_symbol);
// await token.grantRole(await token.MINTER_ROLE(), transfer.address);
// await token.mint(sender, 999);
// await transfer.setDenomTokenContract(denom, token.address);
// tokenAddr = token.address;
console.error("Axon cannot be source zone now, so the transferred token should already exist on CKB");
return;
}

// Check balance.
const amount = process.env.AMOUNT;
const token = await IERC20.at(tokenAddr);
if ((await token.balanceOf(receiver)) >= amount) {
throw "balance should at least be " + amount;
}

// Send packet: ERC20 approve and ICS20 sendTransfer.
await token.approve(transfer.address, amount, {
from: sender,
});
let result = await transfer.sendTransfer(denom, amount, receiver, port, channel, 0, {
from: sender,
});
console.log(`Successfully send ${amount} token to ${receiver} with denom ${denom}: ${result}`);
}

module.exports = (callback) => {
main().then(callback).catch(e => {
console.log("Error:", e.message, e);
callback();
});
};
35 changes: 0 additions & 35 deletions scripts/send_packet.js

This file was deleted.

0 comments on commit 8213b64

Please sign in to comment.