-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusdc.act.ts
150 lines (110 loc) · 5.4 KB
/
usdc.act.ts
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
import { File, Directory } from 'atma-io';
import { UAction } from 'atma-utest'
import { ChainAccountService } from '@dequanto/ChainAccountService';
import { HardhatProvider } from '@dequanto/hardhat/HardhatProvider';
import { Config } from '@dequanto/config/Config';
import { USDC } from '@0xc/eth/USDC/USDC';
import { TAccount } from '@dequanto/models/TAccount';
import { $bigint } from '@dequanto/utils/$bigint';
import { l } from '@dequanto/utils/$logger';
import { $require } from '@dequanto/utils/$require';
import { $promise } from '@dequanto/utils/$promise';
let config = await Config.fetch({
// overrides default path
"config-accounts": "./config/accounts.json",
// @Demo The value should be fetched securely
"pin": "hello"
});
let accounts = new ChainAccountService({ config });
let provider = new HardhatProvider();
let client = provider.client('localhost');
UAction.create({
/**
* Generate two sample accounts. In this demo we will transfer USDC from "foo" to "bar"
*/
async 'create-accounts' () {
let [ foo, bar ] = await accounts.createMany(['foo', 'bar'], 'hardhat');
l`Created 2 accounts in ./config/accounts.json`
l`foo bold<${foo.address}>`
l`bar bold<${bar.address}>`
},
async 'rpc-info' () {
console.log(await client.getNodeInfos());
},
async 'set-balance' () {
let foo = await accounts.get('foo');
console.log(`foo`, foo.address);
$require.notNull(foo, `"foo" account not found`);
// First argument is the address - use same address as on mainnet, override only the RPC client
let usdc = new USDC(void 0, client);
let balanceBefore = await usdc.storage.balanceAndBlacklistStates(foo.address);
l`Balance (${foo.address}) yellow<before>: bold<${balanceBefore}> wei`
// Set balance of
let decimals = await usdc.decimals();
console.log(`DECIMALS: ${decimals}`);
console.log(`DECIMALS: ${decimals}`);
let amount = $bigint.toWei(50_000, decimals);
await usdc.storage.$set(`balanceAndBlacklistStates["${foo.address}"]`, amount);
await $promise.wait(200);
let balanceAfter = await usdc.balanceOf(foo.address);
l`Balance green<after>: bold<${balanceAfter}> wei`;
},
async 'transfer-balance' () {
let foo = await accounts.get('foo');
let bar = await accounts.get('bar');
$require.notNull(foo, "foo");
$require.notNull(bar, "bar");
console.log(`foo`, foo.address);
console.log(`bar`, bar.address);
// Fund foo account
await client.debug.setBalance(foo.address, 10n**18n);
// First argument is the address - use same address as on mainnet, override only the RPC client
let usdc = new USDC(void 0, client);
let barBalance = await usdc.balanceOf(bar.address);
if (barBalance > 0n) {
l`Reset bar USDC balance from ${barBalance} to 0`;
await usdc.storage.$set(`balanceAndBlacklistStates["${bar.address}"]`, 0n);
}
let fooBalance = await usdc.storage.$get(`balanceAndBlacklistStates["${foo.address}"]`);
l`Send ${fooBalance} usdc`;
let tx = await usdc.transfer(foo as TAccount, bar.address, fooBalance);
let receipt = await tx.wait();
},
async 'redeploy-with-new-compilation' () {
let sourceDir = '0xc/eth/USDC/USDC/';
let outputDir = `./test/bin/contracts/`;
await Directory.copyTo(sourceDir, outputDir, { verbose: true })
let modifyFile = `Users/aloysius.chan/Repositories/circlefin/stablecoin-evm-private-eurc-mainnet-eth/contracts/v1/FiatTokenV1.sol`;
await File.replaceAsync(`${outputDir}${modifyFile}`
, `_transfer(msg.sender, to, value);`
, `_transfer(msg.sender, to, value + 5);`
);
let compileFile = `Users/aloysius.chan/Repositories/circlefin/stablecoin-evm-private-eurc-mainnet-eth/contracts/v2/FiatTokenV2_2.sol`;
let { bytecode, linkReferences } = await provider.compileSol(`${outputDir}${compileFile}`, {
paths: {
root: outputDir
}
});
bytecode = await provider.linkReferences(bytecode, linkReferences, {
"SignatureChecker": "0x800C32EaA2a6c93cF4CB51794450ED77fBfbB172"
});
let usdc = new USDC(void 0, client);
await client.debug.setCode(usdc.address, bytecode);
let foo = await accounts.get('foo');
let bar = await accounts.get('bar');
await client.debug.setBalance(foo.address, 10n ** 18n);
await usdc.storage.$set(`balanceAndBlacklistStates["${foo.address}"]`, 100n);
await usdc.storage.$set(`balanceAndBlacklistStates["${bar.address}"]`, 0n);
let barBalanceBefore = await usdc.balanceOf(bar.address);
$require.eq(barBalanceBefore, 0n, `Bar balance should be 0 before transfer`);
l`Bar Balance: ${barBalanceBefore}`;
let fooBalanceBefore = await usdc.balanceOf(foo.address);
$require.eq(fooBalanceBefore, 100n, `Foo balance should be 0 before transfer`);
l`Foo Balance: ${fooBalanceBefore}`;
let tx = await usdc.transfer(foo as TAccount, bar.address, 50n);
await tx.wait();
let barBalanceAfter = await usdc.balanceOf(bar.address);
$require.eq(barBalanceAfter, 55n, `Bar should have 50 transferred and +5 by modification`);
l`Bar Balance: ${barBalanceAfter}`;
}
});