-
Notifications
You must be signed in to change notification settings - Fork 2
/
upload-asm-only.js
124 lines (106 loc) · 4.17 KB
/
upload-asm-only.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
// Simulate the environment without WebAssembly supports, also shim with asm.js
globalThis.WebAssembly = undefined;
require('@polkadot/wasm-crypto/initOnlyAsm');
require('dotenv').config()
const fs = require('fs')
const { signAndSend, signCertificate, OnChainRegistry, options, PinkCodePromise } = require('@phala/sdk')
const { ApiPromise, Keyring, WsProvider } = require('@polkadot/api')
const BN = require('bn.js')
async function main() {
const endpoint = process.env.ENDPOINT
const account = process.env.POLKADOT_ACCOUNT
if (!endpoint || !account) {
console.log('Please create your own .env file with `ENDPOINT` and `POLKADOT_ACCOUNT`.')
return process.exit(1)
}
const targetFile = 'abis/fat_badges.contract'
// const targetFile = process.argv[2]
// if (!endpoint || !targetFile) {
// console.log('Usage: node upload.js [path/to/yours.contract]')
// return process.exit(1)
// }
if (!fs.existsSync(targetFile)) {
console.log(`${targetFile} not exists.`)
return process.exit(1)
}
const contractFile = JSON.parse(fs.readFileSync(targetFile))
// Initialization
console.log('Connecting to', endpoint, '...')
const provider = new WsProvider(endpoint)
const api = await ApiPromise.create(options({ provider, noInitWarn: true }))
console.log('Connected.')
const keyring = new Keyring({ type: 'sr25519' })
const user = pair = keyring.addFromUri(process.env.POLKADOT_ACCOUNT)
const accountInfo = await api.query.system.account(user.address)
const free = accountInfo.data.free.div(new BN(1e10)) / 100
if (free < 20) {
console.log('Not enough balance. Please transfer some tokens not less then 20 PHA to', user.address)
return process.exit(1)
}
console.log(`Account ${user.address} has ${free} PHA.`)
const phatRegistry = await OnChainRegistry.create(api)
const clusterId = phatRegistry.clusterId
// const clusterInfo = phatRegistry.clusterInfo
const pruntimeURL = phatRegistry.pruntimeURL
console.log('Cluster ID:', clusterId)
console.log('Pruntime Endpoint URL:', pruntimeURL)
const balance = await phatRegistry.getClusterBalance(user.address)
console.log('Cluster Balance:', balance.total.toPrimitive() / 1e12, balance.free.toPrimitive() / 1e12)
if ((balance.free.toPrimitive() / 1e12) < 10) {
console.log('Transfer to cluster...')
try {
await signAndSend(phatRegistry.transferToCluster(user.address, 1e12 * 10), user)
} catch (err) {
console.log(`Transfer to cluster failed: ${err}`)
console.error(err)
return process.exit(1)
}
}
const cert = await signCertificate({ pair })
///
/// All prepare conditions ready.
///
//
// Step 1: Upload with PinkCodePromise
//
console.log('Upload codes...')
const codePromise = new PinkCodePromise(api, phatRegistry, contractFile, contractFile.source.wasm)
const uploadResult = await signAndSend(codePromise.tx.new(), user)
await uploadResult.waitFinalized()
console.log('Code ready in cluster.')
//
// Step 2: instantiate with PinkBlueprintPromise
//
console.log('Instantiating...')
const instantiateResult = await uploadResult.blueprint.send.new({ pair, cert, address: cert.address })
await instantiateResult.waitFinalized()
const { contractId, contract } = instantiateResult
console.log('Contract uploaded & instantiated: ', contractId)
//
// Step 3: adjust staking for the contract (optional)
//
console.info(`Auto staking to the contract...`);
await signAndSend(api.tx.phalaPhatTokenomic.adjustStake(contractId, 1e10), user)
//
// query test.
//
const totalQueryResponse = await contract.query.getTotalBadges(user.address, { cert })
const total = totalQueryResponse.output.toJSON()
console.log('total:', total)
//
// trx with auto-deposit test.
//
const name = `Badge${new Date().getTime()}`
const result = await contract.send.newBadge({ pair, cert, address: cert.address }, name)
await result.waitFinalized()
console.log('trx submited')
//
// query data after trx.
//
const { output: after } = await contract.query.getTotalBadges(pair.address, { cert })
console.log('total:', after.toJSON())
}
main().then(() => process.exit(0)).catch(err => {
console.error(err)
process.exit(1)
})