-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-erc20.ts
203 lines (162 loc) · 7.62 KB
/
create-erc20.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
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
import { ApiPromise, WsProvider, Keyring } from "@polkadot/api";
import { KeyringPair } from '@polkadot/keyring/types';
import { U8aFixed } from '@polkadot/types/codec';
import * as web3Utils from 'web3-utils';
import * as crypto from '@polkadot/util-crypto';
// Provider is set to localhost for development
const wsProvider = new WsProvider("ws://localhost:9944");
// Keyring needed to sign using Alice account
const keyring = new Keyring({ type: 'sr25519' });
// ByteCode of our ERC20 exemple: copied from ./truffle/contracts/MyToken.json
const ERC20_BYTECODES = require("./truffle/contracts/MyToken.json").bytecode;
// Setup the API and Alice Account
async function init() {
console.log(`Initiating the API (ignore message "Unable to resolve type B..." and "Unknown types found...")`);
// Initiate the polkadot API.
const api = await ApiPromise.create({
provider: wsProvider,
types: {
// mapping the actual specified address format
Address: "AccountId",
// mapping the lookup
LookupSource: "AccountId",
Account: {
nonce: "U256",
balance: "U256"
},
Transaction: {
nonce: "U256",
action: "String",
gas_price: "u64",
gas_limit: "u64",
value: "U256",
input: "Vec<u8>",
signature: "Signature"
},
Signature: {
v: "u64",
r: "H256",
s: "H256"
}
}
});
console.log(`Initialiation done`);
console.log(`Genesis at block: ${api.genesisHash.toHex()}`);
const alice = keyring.addFromUri('//Alice', { name: 'Alice default' });
const bob = keyring.addFromUri('//Bob', { name: 'Bob default' });
const { nonce, data: balance } = await api.query.system.account(alice.address);
console.log(`Alice Substrate Account: ${alice.address}`);
console.log(`Alice Substrate Account (nonce: ${nonce}) balance, free: ${balance.free.toHex()}`);
const aliceEvmAccount = `0x${crypto.blake2AsHex(crypto.decodeAddress(alice.address), 256).substring(26)}`;
console.log(`Alice EVM Account: ${aliceEvmAccount}`);
const evmData = (await api.query.evm.accounts(aliceEvmAccount)) as any;
console.log(`Alice EVM Account (nonce: ${evmData.nonce}) balance: ${evmData.balance.toHex()}`);
return { api, alice, bob };
}
// Create the ERC20 contract from ALICE
async function step1(api: ApiPromise, alice: KeyringPair) {
console.log(`\nStep 1: Creating Smart Contract`);
// params: [bytecode, initialBalance, gasLimit, gasPrice],
// tx: api.tx.evm.create
const transaction = await api.tx.evm.create(ERC20_BYTECODES, 0, 4294967295, 1, null);
const contract = new Promise<{ block: string, address: string }>(async (resolve, reject) => {
const unsub = await transaction.signAndSend(alice, (result) => {
console.log(`Contract creation is ${result.status}`);
if (result.status.isInBlock) {
console.log(`Contract included at blockHash ${result.status.asInBlock}`);
console.log(`Waiting for finalization... (can take a minute)`);
} else if (result.status.isFinalized) {
const contractAddress = (
result.events?.find(
event => event?.event?.index.toHex() == "0x0500"
)?.event.data[0] as any
).address as string;
console.log(`Contract finalized at blockHash ${result.status.asFinalized}`);
console.log(`Contract address: ${contractAddress}`);
unsub();
resolve({
block: result.status.asFinalized.toString(),
address: contractAddress
});
}
});
});
return contract;
}
// Retrieve Alice & Contract Storage
async function step2(api: ApiPromise, alice: KeyringPair, contractAddress: string) {
console.log(`\nStep 2: Retrieving Contract from evm address: ${contractAddress}`);
// Retrieve Alice account with new nonce value
const { nonce, data: balance } = await api.query.system.account(alice.address);
console.log(`Alice Substrate Account (nonce: ${nonce}) balance, free: ${balance.free}`);
const accountCode = (await api.query.evm.accountCodes(contractAddress)).toString();
console.log(`Contract account code: ${accountCode.substring(0, 16)}...${accountCode.substring(accountCode.length - 16)}`);
// Computing Contract Storage Slot, using slot 0 and alice EVM account
const aliceEvmAccount = `0x${crypto.blake2AsHex(crypto.decodeAddress(alice.address), 256).substring(26)}`;
const slot = "0";
const mapStorageSlot = slot.padStart(64, '0');
const mapKey = aliceEvmAccount.toString().substring(2).padStart(64, '0');
const storageKey = web3Utils.sha3('0x'.concat(mapKey.concat(mapStorageSlot)));
console.log(`Alice Contract storage key: ${storageKey}`);
const accountStorage = (await api.query.evm.accountStorages(contractAddress, storageKey)).toString();
console.log(`Alice Contract account storage: ${accountStorage}`);
return;
}
// Transfer tokens to Bob
async function step3(api: ApiPromise, alice: KeyringPair, bob: KeyringPair, contractAddress: string) {
const bobEvmAccount = `0x${crypto.blake2AsHex(crypto.decodeAddress(bob.address), 256).substring(26)}`;
console.log(`\nStep 3: Transfering Tokens to Bob EVM Account: ${bobEvmAccount}`);
console.log(`Preparing transfer of 0xdd`);
// params: [contractAddress, inputCode, value,m gasLimit, gasPrice],
// tx: api.tx.evm.create
const transferFnCode = `a9059cbb000000000000000000000000`;
const tokensToTransfer = `00000000000000000000000000000000000000000000000000000000000000dd`;
const inputCode = `0x${transferFnCode}${bobEvmAccount.substring(2)}${tokensToTransfer}`;
console.log(`Sending call input: ${inputCode}`);
const transaction = await api.tx.evm.call(contractAddress, inputCode, 0, 4294967295, 1, null);
const data = new Promise<{ block: string, address: string }>(async (resolve, reject) => {
const unsub = await transaction.signAndSend(alice, (result) => {
console.log(`Transfer is ${result.status}`);
if (result.status.isInBlock) {
console.log(`Transfer included at blockHash ${result.status.asInBlock}`);
console.log(`Waiting for finalization... (can take a minute)`);
} else if (result.status.isFinalized) {
console.log(`Transfer finalized at blockHash ${result.status.asFinalized}`);
unsub();
resolve();
}
});
});
return data;
}
// Retrieve Bob
async function step4(api: ApiPromise, bob: KeyringPair, contractAddress: string) {
console.log(`\nStep 4: Retrieving Bob tokens`);
// Retrieve Bob account with new nonce value
const { nonce, data: balance } = await api.query.system.account(bob.address);
console.log(`Bob Substrate Account (nonce: ${nonce}) balance, free: ${balance.free}`);
const bobEvmAccount = `0x${crypto.blake2AsHex(crypto.decodeAddress(bob.address), 256).substring(26)}`;
console.log(`Bob EVM Account: ${bobEvmAccount}`);
const evmData = (await api.query.evm.accounts(bobEvmAccount)) as any;
console.log(`Bob EVM Account (nonce: ${evmData.nonce}) balance: ${evmData.balance.toHex()}`);
const slot = "0";
const mapStorageSlot = slot.padStart(64, '0');
const mapKey = bobEvmAccount.toString().substring(2).padStart(64, '0');
const storageKey = web3Utils.sha3('0x'.concat(mapKey.concat(mapStorageSlot)));
console.log(`Bob Contract storage key: ${storageKey}`);
const accountStorage = (await api.query.evm.accountStorages(contractAddress, storageKey)).toString();
console.log(`Bob Contract account storage: ${accountStorage}`);
return;
}
async function main() {
const { api, alice, bob } = await init();
// step 1: Creating the contract from ALICE
const contractAccount = await step1(api, alice)
// step 2: Retrieving Alice and Contract information
await step2(api, alice, contractAccount.address);
// step 3: Transfering Smart Contract tokens from Alice to Bob
await step3(api, alice, bob, contractAccount.address);
// step 3: Retrieving Bob information
await step4(api, bob, contractAccount.address);
}
main().catch(console.error).then(() => process.exit(0));