-
Notifications
You must be signed in to change notification settings - Fork 2
/
tx-plain.js
95 lines (83 loc) · 2.85 KB
/
tx-plain.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
require('dotenv').config()
const fs = require('fs')
const { signAndSend, signCertificate, OnChainRegistry, options, PinkCodePromise } = require('@phala/sdk')
const { ApiPromise, Keyring, WsProvider } = require('@polkadot/api')
const argv = require('arg')({
'--ws': String,
'--suri': String,
'--clusterId': String,
'--worker': String,
'--pruntimeURL': String
})
async function main() {
const targetFile = 'abis/fat_badges.contract'
if (!fs.existsSync(targetFile)) {
console.log(`${targetFile} not exists.`)
return process.exit(1)
}
const contractFile = JSON.parse(fs.readFileSync(targetFile))
const ws = argv['--ws'] || process.env.ENDPOINT
const suri = argv['--suri'] || process.env.POLKADOT_ACCOUNT
if (!ws) {
throw new Error('No ws endpoint specified')
}
if (!suri) {
throw new Error('No suri specified')
}
const clusterId = argv['--clusterId']
const workerId = argv['--worker']
const pruntimeURL = argv['--pruntimeURL']
const connectionOptions = {}
if (clusterId) {
connectionOptions.clusterId = clusterId
}
if (workerId) {
connectionOptions.workerId = workerId
} else if (pruntimeURL) {
connectionOptions.pruntimeURL = pruntimeURL
}
const apiPromise = await ApiPromise.create(options({
provider: new WsProvider(ws),
noInitWarn: true
}))
const registry = await OnChainRegistry.create(apiPromise, connectionOptions)
const keyring = new Keyring({ type: 'sr25519' })
const pair = keyring.addFromUri(suri)
const cert = await signCertificate({ pair })
console.log('Connected via', ws)
console.log('Cluster ID:', registry.clusterId)
console.log('Worker ID:', registry.remotePubkey)
console.log('Worker Endpoint:', registry.pruntimeURL)
///
/// All prepare conditions ready.
///
//
// setUp: Upload & instantiate the contract
//
const codePromise = new PinkCodePromise(apiPromise, registry, contractFile, contractFile.source.wasm)
const uploadResult = await signAndSend(codePromise.upload(), pair)
await uploadResult.waitFinalized()
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)
//
// END: setUp
//
//
// NOTE: with the `{ plain: true }`, you can debugging the payload with subscan; it also means, your payload
// will visible to everyone. So use it carefully, and ensure when you debugging with it, no sensitive
// data in the payload.
//
//
const result = await contract.send.newBadge(
{ pair, cert, address: cert.address, plain: true },
`Badge${new Date().getTime()}`
)
await result.waitFinalized()
console.log('trx submited')
}
main().then(() => process.exit(0)).catch(err => {
console.error(err)
process.exit(1)
})