|
| 1 | +require('dotenv').config() |
| 2 | + |
| 3 | +const fs = require('fs') |
| 4 | +const { signAndSend, signCertificate, OnChainRegistry, options, PinkCodePromise, KeyringPairProvider } = require('@phala/sdk') |
| 5 | +const { ApiPromise, Keyring, WsProvider } = require('@polkadot/api') |
| 6 | +const BN = require('bn.js') |
| 7 | + |
| 8 | +async function main() { |
| 9 | + const endpoint = process.env.ENDPOINT |
| 10 | + const account = process.env.POLKADOT_ACCOUNT |
| 11 | + if (!endpoint || !account) { |
| 12 | + console.log('Please create your own .env file with `ENDPOINT` and `POLKADOT_ACCOUNT`.') |
| 13 | + return process.exit(1) |
| 14 | + } |
| 15 | + const targetFile = 'abis/fat_badges.contract' |
| 16 | + // const targetFile = process.argv[2] |
| 17 | + // if (!endpoint || !targetFile) { |
| 18 | + // console.log('Usage: node upload.js [path/to/yours.contract]') |
| 19 | + // return process.exit(1) |
| 20 | + // } |
| 21 | + if (!fs.existsSync(targetFile)) { |
| 22 | + console.log(`${targetFile} not exists.`) |
| 23 | + return process.exit(1) |
| 24 | + } |
| 25 | + const contractFile = JSON.parse(fs.readFileSync(targetFile)) |
| 26 | + |
| 27 | + // Initialization |
| 28 | + console.log('Connecting to', endpoint, '...') |
| 29 | + const provider = new WsProvider(endpoint) |
| 30 | + const api = await ApiPromise.create(options({ provider, noInitWarn: true })) |
| 31 | + console.log('Connected.') |
| 32 | + |
| 33 | + const keyring = new Keyring({ type: 'sr25519' }) |
| 34 | + const user = pair = keyring.addFromUri(process.env.POLKADOT_ACCOUNT) |
| 35 | + const accountInfo = await api.query.system.account(user.address) |
| 36 | + const free = accountInfo.data.free.div(new BN(1e10)) / 100 |
| 37 | + if (free < 20) { |
| 38 | + console.log('Not enough balance. Please transfer some tokens not less then 20 PHA to', user.address) |
| 39 | + return process.exit(1) |
| 40 | + } |
| 41 | + console.log(`Account ${user.address} has ${free} PHA.`) |
| 42 | + |
| 43 | + const phatRegistry = await OnChainRegistry.create(api) |
| 44 | + |
| 45 | + const clusterId = phatRegistry.clusterId |
| 46 | + // const clusterInfo = phatRegistry.clusterInfo |
| 47 | + const pruntimeURL = phatRegistry.pruntimeURL |
| 48 | + console.log('Cluster ID:', clusterId) |
| 49 | + console.log('Pruntime Endpoint URL:', pruntimeURL) |
| 50 | + |
| 51 | + const balance = await phatRegistry.getClusterBalance(user.address) |
| 52 | + console.log('Cluster Balance:', balance.total.toPrimitive() / 1e12, balance.free.toPrimitive() / 1e12) |
| 53 | + |
| 54 | + if ((balance.free.toPrimitive() / 1e12) < 500) { |
| 55 | + console.log('Transfer to cluster...') |
| 56 | + try { |
| 57 | + await signAndSend(phatRegistry.transferToCluster(user.address, 1e12 * 500), user) |
| 58 | + } catch (err) { |
| 59 | + console.log(`Transfer to cluster failed: ${err}`) |
| 60 | + // console.error(err) |
| 61 | + return process.exit(1) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + const cert = await signCertificate({ pair }) |
| 66 | + /// |
| 67 | + /// All prepare conditions ready. |
| 68 | + /// |
| 69 | + |
| 70 | + // |
| 71 | + // Step 1: Upload with PinkCodePromise |
| 72 | + // |
| 73 | + console.log('Upload codes...') |
| 74 | + const codePromise = new PinkCodePromise(api, phatRegistry, contractFile, contractFile.source.wasm) |
| 75 | + const uploadResult = await signAndSend(codePromise.tx.new(), user) |
| 76 | + // await uploadResult.waitFinalized(user, cert) |
| 77 | + await uploadResult.waitFinalized() |
| 78 | + console.log('Code ready in cluster.') |
| 79 | + |
| 80 | + // |
| 81 | + // Step 2: instantiate with PinkBlueprintPromise |
| 82 | + // |
| 83 | + console.log('Instantiating...') |
| 84 | + const instantiateResult = await uploadResult.blueprint.send.new({ pair, cert, address: cert.address }) |
| 85 | + await instantiateResult.waitFinalized() |
| 86 | + |
| 87 | + const { contractId, contract } = instantiateResult |
| 88 | + console.log('Contract uploaded & instantiated: ', contractId) |
| 89 | + |
| 90 | + // |
| 91 | + // Step 3: adjust staking for the contract (optional) |
| 92 | + // |
| 93 | + console.info(`Auto staking to the contract...`); |
| 94 | + await signAndSend(api.tx.phalaPhatTokenomic.adjustStake(contractId, 1e10), user) |
| 95 | + |
| 96 | + // |
| 97 | + // New in v0.6.x: bind provider & proxy based flavor. |
| 98 | + // |
| 99 | + contract.provider = new KeyringPairProvider(api, pair) |
| 100 | + |
| 101 | + // |
| 102 | + // query test. |
| 103 | + // |
| 104 | + const old_call = await contract.query.getTotalBadges(user.address, { cert }) |
| 105 | + const new_call = await contract.q.getTotalBadges() |
| 106 | + console.log('total:', old_call.output.toJSON(), new_call.output.toJSON(), old_call.output.toJSON() === new_call.output.toJSON()) |
| 107 | + |
| 108 | + // |
| 109 | + // trx with auto-deposit test. |
| 110 | + // |
| 111 | + const name = `Badge${new Date().getTime()}` |
| 112 | + // const result = await contract.send.newBadge({ pair, cert, address: cert.address }, name) |
| 113 | + const result = await contract.exec.newBadge({ args: [name] }) |
| 114 | + await result.waitFinalized() |
| 115 | + console.log('trx submited with nonce:', result.nonce) |
| 116 | + |
| 117 | + // |
| 118 | + // query data after trx. |
| 119 | + // |
| 120 | + // const { output: after } = await contract.query.getTotalBadges(pair.address, { cert }) |
| 121 | + const { output: after } = await contract.q.getTotalBadges() |
| 122 | + console.log('total:', after.toJSON()) |
| 123 | +} |
| 124 | + |
| 125 | +main().then(() => process.exit(0)).catch(err => { |
| 126 | + console.error(err) |
| 127 | + process.exit(1) |
| 128 | +}) |
0 commit comments