-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathseed-phat-bricks.ts
129 lines (117 loc) · 5.91 KB
/
seed-phat-bricks.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
require('dotenv').config()
import fs from 'node:fs'
import type { u64, Bool } from '@polkadot/types-codec'
import { signAndSend, signCertificate, OnChainRegistry, options, PinkCodePromise, PinkBlueprintPromise, KeyringPairProvider, type FrameSystemAccountInfo, PinkContractPromise, PinkContractQuery, PinkContractTx, getClient, unsafeGetAbiFromGitHubRepoByCodeHash, unsafeGetWasmFromGithubRepoByCodeHash } from '@phala/sdk'
import { ApiPromise, Keyring, WsProvider } from '@polkadot/api'
const LEGO_CODE_HASH = '0xe5b86ea5a92b207cffa9f814911bb96cc900bef19817f3e07b75a23f60ca963b'
const ACTION_OFFCHAIN_ROLLUP_CODE_HASH = '0x96ca5480eb52b8087b1e64cae52c75e6db037e1920320653584ef920db5d29d5'
const BRICK_PROFILE_CODE_HASH = '0x27332bc5304f12851f8d48e50df26d6be4b2a2764dcd5b6fdbc9ba4158bc9c84'
const BRICK_PROFILE_FACTORY_CODE_HASH = '0xeb65d30766360fb51fe01f638142ae4a5ef1ffbe66c016336b17977f4d5f7c29'
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 client = await getClient({ transport: endpoint })
const keyring = new Keyring({ type: 'sr25519', ss58Format: 30 })
const pair = keyring.addFromUri(account)
// const provider = await KeyringPairProvider.createFromSURI(client.api, account, { ss58Format: 30 })
const provider = await KeyringPairProvider.create(client.api, pair)
console.log('endpoint: ', endpoint)
console.log('deployer:', provider.address)
await provider.send(client.transferToCluster(provider.address, 2000 * 1e12))
// prepare the lego_rs instance.
{
const { output: existsCheck } = await client.systemContract!.q.system.codeExists<Bool>({ args: [LEGO_CODE_HASH, 'Ink'] })
if (existsCheck.isErr) {
throw new Error('Failed to check if the contract exists: is it PRuntime running healthy')
}
let blueprint: PinkBlueprintPromise
if (existsCheck.asOk.isFalse) {
const [wasmCode, abi] = await Promise.all([
unsafeGetWasmFromGithubRepoByCodeHash(LEGO_CODE_HASH),
unsafeGetAbiFromGitHubRepoByCodeHash(LEGO_CODE_HASH),
])
const codePromise = new PinkCodePromise(client.api, client, abi, wasmCode)
const upload = await codePromise.send({ provider })
await upload.waitFinalized()
blueprint = upload.blueprint
} else {
const abi = await unsafeGetAbiFromGitHubRepoByCodeHash(LEGO_CODE_HASH)
blueprint = new PinkBlueprintPromise(client.api, client, abi, LEGO_CODE_HASH)
}
const instantiation = await blueprint.send.default({ provider })
await instantiation.waitFinalized()
console.log(`lego_rs instantiated: ${instantiation.contractId}`)
}
// action_offchain_rollup & brick_profile both are upload only
{
const { output: existsCheck } = await client.systemContract!.q.system.codeExists<Bool>({ args: [ACTION_OFFCHAIN_ROLLUP_CODE_HASH, 'Ink'] })
if (existsCheck.isErr) {
throw new Error('Failed to check if the contract exists: is it PRuntime running healthy')
}
if (existsCheck.asOk.isFalse) {
console.log('Fetching wasm and abi for action_offchain_rollup...')
const [wasmCode, abi] = await Promise.all([
unsafeGetWasmFromGithubRepoByCodeHash(ACTION_OFFCHAIN_ROLLUP_CODE_HASH),
unsafeGetAbiFromGitHubRepoByCodeHash(ACTION_OFFCHAIN_ROLLUP_CODE_HASH),
])
console.log('Downloaded.')
const codePromise = new PinkCodePromise(client.api, client, abi, wasmCode)
const upload = await codePromise.send({ provider })
await upload.waitFinalized()
console.log('`action_offchain_rollup` uploaded.')
} else {
console.log('`action_offchain_rollup` already exists, skip.')
}
}
{
const { output: existsCheck } = await client.systemContract!.q.system.codeExists<Bool>({ args: [BRICK_PROFILE_CODE_HASH, 'Ink'] })
if (existsCheck.isErr) {
throw new Error('Failed to check if the contract exists: is it PRuntime running healthy')
}
if (existsCheck.asOk.isFalse) {
console.log('Fetching wasm and abi for brick_profile...')
const [wasmCode, abi] = await Promise.all([
unsafeGetWasmFromGithubRepoByCodeHash(BRICK_PROFILE_CODE_HASH),
unsafeGetAbiFromGitHubRepoByCodeHash(BRICK_PROFILE_CODE_HASH),
])
console.log('Downloaded.')
const codePromise = new PinkCodePromise(client.api, client, abi, wasmCode)
const upload = await codePromise.send({ provider })
await upload.waitFinalized()
console.log('`brick_profile` uploaded.')
} else {
console.log('`brick_profile` already exists, skip.')
}
}
{
const { output: existsCheck } = await client.systemContract!.q.system.codeExists<Bool>({ args: [BRICK_PROFILE_FACTORY_CODE_HASH, 'Ink'] })
if (existsCheck.isErr) {
throw new Error('Failed to check if the contract exists: is it PRuntime running healthy')
}
let blueprint: PinkBlueprintPromise
if (existsCheck.asOk.isFalse) {
const [wasmCode, abi] = await Promise.all([
unsafeGetWasmFromGithubRepoByCodeHash(BRICK_PROFILE_FACTORY_CODE_HASH),
unsafeGetAbiFromGitHubRepoByCodeHash(BRICK_PROFILE_FACTORY_CODE_HASH),
])
const codePromise = new PinkCodePromise(client.api, client, abi, wasmCode)
const upload = await codePromise.send({ provider })
await upload.waitFinalized()
blueprint = upload.blueprint
} else {
const abi = await unsafeGetAbiFromGitHubRepoByCodeHash(BRICK_PROFILE_FACTORY_CODE_HASH)
blueprint = new PinkBlueprintPromise(client.api, client, abi, BRICK_PROFILE_FACTORY_CODE_HASH)
}
const instantiation = await blueprint.send.new({ provider }, BRICK_PROFILE_CODE_HASH)
await instantiation.waitFinalized()
console.log(`brick_profile_factory instantiated: ${instantiation.contractId}`)
}
}
main().then(() => process.exit(0)).catch(err => {
console.error(err)
process.exit(1)
})