-
Notifications
You must be signed in to change notification settings - Fork 0
/
newInterchainToken.js
191 lines (162 loc) · 5.13 KB
/
newInterchainToken.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const hre = require("hardhat");
const crypto = require("crypto");
const ethers = hre.ethers;
const {
AxelarQueryAPI,
Environment,
EvmChain,
GasToken,
} = require("@axelar-network/axelarjs-sdk");
const interchainTokenServiceContractABI = require("./utils/interchainTokenServiceABI");
const interchainTokenFactoryContractABI = require("./utils/interchainTokenFactoryABI");
const interchainTokenContractABI = require("./utils/interchainTokenABI");
const interchainTokenServiceContractAddress =
"0xB5FB4BE02232B1bBA4dC8f81dc24C26980dE9e3C";
const interchainTokenFactoryContractAddress =
"0x83a93500d23Fbc3e82B410aD07A6a9F7A0670D66";
const SOURCE_CHAIN = EvmChain.FANTOM;
const DESTINATION_CHAIN = EvmChain.CELO;
// // Set Salt value from registerAndDeploy()
// const SALT =
// "0x256f05b979b62392a70aaa1a1a76ca56a58dea338ab6122f694a327a17783c44";
async function getSigner() {
const [signer] = await ethers.getSigners();
return signer;
}
async function getContractInstance(contractAddress, contractABI, signer) {
return new ethers.Contract(contractAddress, contractABI, signer);
}
// Register and deploy a new interchain token to the Fantom testnet
async function registerAndDeploy() {
// Generate random salt
const salt = "0x" + crypto.randomBytes(32).toString("hex");
// Create a new token
const name = "Axelar BB";
const symbol = "ABB";
const decimals = 18;
// Intial token supply
const initialSupply = ethers.utils.parseEther("1000000000");
// Get a signer to sign the transaction
const signer = await getSigner();
// Create contract instances
const interchainTokenFactoryContract = await getContractInstance(
interchainTokenFactoryContractAddress,
interchainTokenFactoryContractABI,
signer
);
const interchainTokenServiceContract = await getContractInstance(
interchainTokenServiceContractAddress,
interchainTokenServiceContractABI,
signer
);
// Generate a unique token ID using the signer's address and salt
const tokenId = await interchainTokenFactoryContract.interchainTokenId(
signer.address,
salt
);
// Retrieve new token address
const tokenAddress =
await interchainTokenServiceContract.interchainTokenAddress(tokenId);
// Retrieve token manager address
const expectedTokenManagerAddress =
await interchainTokenServiceContract.tokenManagerAddress(tokenId);
// Deploy new Interchain Token
const deployTxData =
await interchainTokenFactoryContract.deployInterchainToken(
salt,
name,
symbol,
decimals,
initialSupply,
signer.address
);
console.log(
`
Deployed Token ID: ${tokenId},
Token Address: ${tokenAddress},
Transaction Hash: ${deployTxData.hash},
salt: ${salt},
Expected Token Manager Address: ${expectedTokenManagerAddress},
`
);
}
const api = new AxelarQueryAPI({ environment: Environment.TESTNET });
// Estimate gas costs.
async function gasEstimator() {
const gas = await api.estimateGasFee(
SOURCE_CHAIN,
DESTINATION_CHAIN,
GasToken.FTM,
700000,
1.1
);
return gas;
}
// Deploy to remote chain: Polygon
async function deployToRemoteChain() {
// Get a signer for authorizing transactions
const signer = await getSigner();
// Get contract for remote deployment
const interchainTokenFactoryContract = await getContractInstance(
interchainTokenFactoryContractAddress,
interchainTokenFactoryContractABI,
signer
);
// Estimate gas fees
const gasAmount = await gasEstimator();
// Set Salt value from registerAndDeploy()
const salt =
"0x1ed1167cfe3ef124a89d5ab4161051abb3210e467dd6cdbc77f2ab4d96d098c4";
// Initiate transaction
const txn = await interchainTokenFactoryContract.deployRemoteInterchainToken(
"Fantom",
salt,
signer.address,
"celo",
gasAmount,
{ value: gasAmount }
);
console.log(`Transaction Hash: ${txn.hash}`);
}
async function transferTokens() {
// Get signer
const signer = await getSigner();
const interchainToken = await getContractInstance(
"0xc028ebdF906a890be73C594b6a14858D0552544A", // Update with new token address
interchainTokenContractABI, // Interchain Token contract ABI
signer
);
// Calculate gas amount
const gasAmount = await gasEstimator();
// Initate transfer via token
const transfer = await interchainToken.interchainTransfer(
"Polygon", // Destination chain
"0x510e5EA32386B7C48C4DEEAC80e86859b5e2416C", // Update with your own wallet address
ethers.utils.parseEther("25"), // Transfer 25 tokens
"0x", // Empty data payload
{ value: gasAmount } // Transaction options
);
console.log("Transfer Transaction Hash:", transfer.hash);
}
async function main() {
const functionName = process.env.FUNCTION_NAME;
switch (functionName) {
case "registerAndDeploy":
await registerAndDeploy();
break;
case "deployToRemoteChain":
await deployToRemoteChain();
break;
case "transferTokens":
await transferTokens();
break;
default:
console.error(`Unknown function: ${functionName}`);
process.exitCode = 1;
return;
}
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});