-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
263 lines (226 loc) · 8.59 KB
/
index.mjs
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import {loadStdlib} from '@reach-sh/stdlib';
import * as lockBackend from './lock.main.mjs';
import * as mintBackend from './mint.main.mjs';
import readline from 'readline';
import { ask } from '@reach-sh/stdlib/ask.mjs';
import dotenv from 'dotenv';
//let stdlib = loadStdlib('ALGO');
dotenv.config();
// Setup readline interface for user input
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const getAlgoStdlib = () => {
let stdlib = loadStdlib('ALGO');
stdlib.setProviderByName("TestNet");
return stdlib;
}
const getVoiStdlib = () => {
let stdlib = loadStdlib('ALGO');
stdlib.setProviderByEnv({
ALGO_SERVER: 'https://testnet-api.voi.nodly.io',
ALGO_PORT: '',
ALGO_TOKEN_HEADER: 'X-Algo-API-Token',
ALGO_TOKEN: '',
ALGO_INDEXER_SERVER: 'https://testnet-idx.voi.nodly.io',
ALGO_INDEXER_PORT: '',
ALGO_INDEXER_TOKEN_HEADER: 'X-Indexer-API-Token',
ALGO_INDEXER_TOKEN: '',
REACH_ISOLATED_NETWORK: 'no',
ALGO_NODE_WRITE_ONLY: 'yes',
});
return stdlib;
}
// deploy mint contract on Voi
const deployMintContract = async (network) => {
console.log(`Deploying Mint Contract...`);
let stdlib = (network == 'testnet') ? getVoiStdlib() : loadStdlib('ALGO');
const acc = await stdlib.newAccountFromMnemonic(process.env.TESTNET_DEPLOYER_MNEMONIC);
if (stdlib.balanceOf(acc) < stdlib.parseCurrency(100) && stdlib.canFundFromFaucet(acc)) {
await stdlib.fundFromFaucet(acc, stdlib.parseCurrency(100));
}
const ctcMint = await acc.contract(mintBackend);
await new Promise((resolve, reject) => {
ctcMint.p.Deployer({
parameters: {
sourceTokenId: 10458941,
sourceChainId: 1, // 1 = Algorand
w_name: 'wUSDC'.padEnd(32, "\0"),
w_symbol: 'wUSDC'.padEnd(8,"\0"),
w_url: 'https://centre.io'.padEnd(96, "\0"),
w_metadata: ''.padEnd(32, "\0"),
w_supply: stdlib.bigNumberify("18446744073709551615"),
w_decimals: 6,
},
ready: async () => {
console.log(`Contract has been deployed.`);
const ctcMintInfo = await ctcMint.getInfo();
console.log(`The Mint contract is deployed as = ${JSON.stringify(ctcMintInfo)}`);
const [ targetTokenIdMaybe, targetTokenId ] = await ctcMint.v.wrappedToken();
stdlib.assert(targetTokenIdMaybe == 'Some', 'no target token minted');
console.log(`The target token ID is = ${targetTokenId}`);
resolve();
}
});
});
return ctcMint;
}
// deploy lock contract on Algorand
const deployLockContract = async (network) => {
let stdlib = (network == 'testnet') ? getAlgoStdlib() : loadStdlib('ALGO');
const targetTokenId = await ask("Enter the target token ID: ");
if (targetTokenId == '') {
console.log("Invalid target token ID");
return;
}
const acc = await stdlib.newAccountFromMnemonic(process.env.TESTNET_DEPLOYER_MNEMONIC);
if (stdlib.balanceOf(acc) < stdlib.parseCurrency(100) && stdlib.canFundFromFaucet(acc)) {
await stdlib.fundFromFaucet(acc, stdlib.parseCurrency(100));
}
let tok, tokenId;
if (network == 'testnet') {
//tok = await acc.tokenMetadata(10458941);
tokenId = 10458941
}
else {
// for devnet we need to mint the token to use for the lock contract
tok = await stdlib.launchToken(acc, "DevNet USDC", "USDC", {
decimals: 6,
supply: stdlib.bigNumberify("18446744073709551615"),
url: "https://centre.io",
metadataHash: "",
clawback: acc.networkAccount,
freeze: acc.networkAccount,
reserve: acc.networkAccount,
manager: acc.networkAccount,
});
tokenId = tok.id;
}
await new Promise(async (resolve, reject) => {
ctcLock.p.Deployer({
parameters: {
sourceToken: tokenId,
sourceChainId: 1, // 1 = Algorand
targetChainId: 2, // 2 = Voi
targetTokenId: targetTokenId, // Token ID on Voi
},
ready: async () => {
console.log(`Contract has been deployed.`);
const ctcLockInfo = await ctcLock.getInfo();
console.log(`The Lock contract is deployed as = ${JSON.stringify(ctcLockInfo)}`);
const [ sourceTokenIdMaybe, sourceTokenId ] = await ctcLock.v.sourceToken();
const [ targetChainIdMaybe, targetChainId ] = await ctcLock.v.targetChainId();
const [ targetTokenIdMaybe, targetTokenId ] = await ctcLock.v.targetTokenId();
const [ bridgedTokensMaybe, bridgedTokens ] = await ctcLock.v.bridgedTokens();
console.log(`Lock contract details:`);
console.log(` sourceTokenId = ${sourceTokenId}`);
console.log(` targetChainId = ${targetChainId}`);
console.log(` targetTokenId = ${targetTokenId}`);
console.log(` bridgedTokens = ${bridgedTokens}`);
resolve();
}
});
});
}
const testContracts = async () => {
console.log('testLockContract');
let stdlibA = getAlgoStdlib();
// step 1; lock USDC on Algorand
// connect using TESTNET_USER
const accAlgo = await stdlibA.newAccountFromMnemonic(process.env.TESTNET_USER_MNEMONIC);
if (stdlibA.balanceOf(accAlgo) < stdlibA.parseCurrency(100) && stdlibA.canFundFromFaucet(accAlgo)) {
await stdlibA.fundFromFaucet(accAlgo, stdlibA.parseCurrency(100));
}
// get the lock contract
const ctcLock = accAlgo.contract(lockBackend, JSON.parse(process.env.LOCK_CONTRACT));
// show details
const [ sourceTokenIdMaybe, sourceTokenId ] = await ctcLock.v.sourceToken();
const [ targetChainIdMaybe, targetChainId ] = await ctcLock.v.targetChainId();
const [ targetTokenIdMaybe, targetTokenId ] = await ctcLock.v.targetTokenId();
const [ bridgedTokensMaybe, bridgedTokens ] = await ctcLock.v.bridgedTokens();
console.log(`Lock contract details:`);
console.log(` sourceTokenId = ${sourceTokenId}`);
console.log(` targetChainId = ${targetChainId}`);
console.log(` targetTokenId = ${targetTokenId}`);
console.log(` bridgedTokens = ${bridgedTokens}`);
// step 2; mint wrapped token on Voi
// connect using TESTNET_USER
let stdlibV = getVoiStdlib();
const accVoi = await stdlibV.newAccountFromMnemonic(process.env.TESTNET_USER_MNEMONIC);
if (stdlibV.balanceOf(accVoi) < stdlibV.parseCurrency(100) && stdlibV.canFundFromFaucet(accVoi)) {
await stdlibV.fundFromFaucet(accVoi, stdlibV.parseCurrency(100));
}
console.log(stdlibV.formatCurrency(await accVoi.balanceOf()));
const ctcMint = accVoi.contract(mintBackend, JSON.parse(process.env.MINT_CONTRACT));
// show details
const [ sourceTokenIdMaybe2, sourceTokenId2 ] = await ctcMint.v.sourceTokenId();
const [ sourceChainIdMaybe, sourceChainId ] = await ctcMint.v.sourceChainId();
const [ wrappedTokenSupplyMaybe, wrappedTokenSupply ] = await ctcMint.v.wrappedTokenSupply();
console.log(`Mint contract details:`);
console.log(` sourceTokenId = ${sourceTokenId2}`);
console.log(` sourceChainId = ${sourceChainId}`);
console.log(` wrappedTokenSupply = ${wrappedTokenSupply}`);
// get the wrapped token
const [ targetTokenIdMaybe2, targetTokenId2 ] = await ctcMint.v.wrappedToken();
console.log(`Wrapped token ID = ${targetTokenId2}`);
}
const changeNetwork = async (network) => {
await new Promise((resolve, reject) => {
rl.question(`Select network (1) TestNet (2) DevNet: `, async (answer) => {
switch(answer.toUpperCase()) {
case '1':
network = 'testnet';
console.log('Network changed to TestNet');
break;
case '2':
network = 'devnet';
console.log('Network changed to DevNet');
break;
default:
console.log('Invalid choice. Network unchanged.');
break;
}
resolve();
});
});
return network;
}
const main = async () => {
let network = process.env.NETWORK || 'devnet';
console.log(`Tower Bridge`);
console.log(`Using network: ${network.toUpperCase()}`);
console.log(``);
console.log(`1. Deploy Mint contract`);
console.log(`2. Deploy Lock contract`);
console.log(`3. Test contracts`);
console.log(`4. Change network`);
console.log(`Q. Quit`);
const askQuestion = () => {
rl.question('What would you like to do? ', async (answer) => {
switch(answer.toUpperCase()) {
case '1':
await deployMintContract(network);
break;
case '2':
await deployLockContract(network);
break;
case '3':
await testContracts(network);
break;
case '4':
network = await changeNetwork();
break;
case 'Q':
process.exit();
break;
default:
console.log('Invalid choice');
break;
}
askQuestion();
});
};
askQuestion();
}
main();