-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate_ae_erc-20_to_aeternity_mainnet.js
106 lines (98 loc) · 3.47 KB
/
migrate_ae_erc-20_to_aeternity_mainnet.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
const { AeSdk, MemoryAccount, Node, getAddressFromPriv } = require('@aeternity/aepp-sdk')
const CONTRACT_ACI = require('./aci/TokenMigrationACI.json')
// the keypair to use for transactions
let keypair
// the entrypoint to call
let entrypoint
let params = []
const shutdown = (varName) => {
console.error(`Missing ENV variable: ${varName}`)
process.exit(1)
}
const setParams = (expectedVariables) => {
for(let i=0; i<expectedVariables.length; i++) {
const param = process.env[expectedVariables[i]]
if(!param) {
shutdown(expectedVariables[i])
}
if(expectedVariables[i] === 'SIBLINGS') {
// load array
params.push(JSON.parse(param))
} else if(expectedVariables[i] === 'SIGNATURE') {
// manipulate signature if required (for whatever reason?!)
// https://github.com/aeternity/aepp-token-migration-backend/blob/master/rest_api/base/base_api.go#L260
let signature = param.substring(2)
let vValue = signature.substring(signature.length - 2)
switch(vValue) {
case '00':
case '27':
vValue = '1b'
break
case '01':
case '28':
vValue = '1c'
break
default:
break
}
params.push(vValue + signature.substring(0, signature.length - 2))
} else {
params.push(param)
}
}
}
const processEnvironmentInput = () => {
entrypoint = process.env.ENTRYPOINT
if(!entrypoint) {
shutdown('ENTRYPOINT')
}
switch(entrypoint) {
case 'balance':
case 'root_hash':
case 'migrations_count':
break
case 'is_migrated':
setParams(['ETH_ADDRESS'])
break
case 'contained_in_merkle_tree':
setParams(['ETH_ADDRESS', 'TOKEN_AMOUNT', 'LEAF_INDEX', 'SIBLINGS'])
break
case 'migrate':
setParams(['TOKEN_AMOUNT', 'AE_ADDRESS','LEAF_INDEX', 'SIBLINGS', 'SIGNATURE'])
secretKey = process.env.SECRET_KEY
if(!secretKey) {
shutdown('SECRET_KEY')
}
keypair = {
secretKey,
publicKey: getAddressFromPriv(secretKey)
}
break
default:
console.error(`entrypoint not supported`)
process.exit(1)
}
}
const main = async () => {
const node = new Node('https://mainnet.aeternity.io')
const aeSdk = new AeSdk({
nodes: [
{ name: 'ae_mainnet', instance: node },
]
})
const contractAddress = 'ct_eJhrbPPS4V97VLKEVbSCJFpdA4uyXiZujQyLqMFoYV88TzDe6'
const contractInstance = await aeSdk.getContractInstance({ aci: CONTRACT_ACI, contractAddress })
if(entrypoint === 'migrate') {
aeSdk.addAccount(new MemoryAccount({ keypair }), { select: true })
console.log('performing migration...')
const migrationTx = await contractInstance.call(entrypoint, params)
console.log(`Migration tx-hash: ${migrationTx.hash}`)
console.log(`New total migration count: ${migrationTx.decodedResult}`)
} else {
console.log('performing a dry-run...')
const dryRunCall = await contractInstance.call(entrypoint, params, {callStatic: true})
console.log(`${entrypoint} => ${dryRunCall.decodedResult}`)
}
}
processEnvironmentInput()
main()