Phala-Network / phala-blockchain

The Phala Network Blockchain, pRuntime and the bridge.
https://phala.network
Apache License 2.0
332 stars 149 forks source link

jssdk: contract actions #1536

Closed Leechael closed 7 months ago

Leechael commented 7 months ago

This PR propose for introduce three new actions like viem

It propose to work with HttpProvider from polkadot-js.

The usage summary:

await cryptoWaitReady()

const metadata = await fetchMetadata('http://10.0.0.120:19944')
const api = new ApiPromise(options({
  provider: new HttpProvider('http://10.0.0.120:19944'),
  metadata,
  noInitWarn: true,
}))
await api.isReady
const client = new OnChainRegistry(api)
await client.connect({
  clusterId: '0x0000000000000000000000000000000000000000000000000000000000000001',
  pubkey: '0x3a3d45dc55b57bf542f4c6ff41af080ec675317f4ed50ae1d2713bf9f892692d',
  pruntimeURL: 'http://10.0.0.120:18200',
})

const contractId = '0x72590ce8ec70a1c13379be5cce4be8fc5a6173073fd169f9fbf93a33d485a9e0'
const contractKey = '0xa6c952655e61d39de165452be68a51719b23b7cbc60c56366590414d518dbf63'
const abi = fs.readFileSync('./abis/fat_badges.contract', 'utf8')

const provider = await KeyringPairProvider.createFromSURI(api, process.env.POLKADOT_ACCOUNT)
console.log('address: ', provider.address)

const name = `Badge${new Date().getTime()}`
const { request } = await estimateContract(client, {
  address: contractId,
  contractKey,
  abi,
  provider,
  functionName: 'newBadge',
  args: [name],
})
await sendPinkCommand(client, request)

// unlike PinkContractPromise, we don't have `waitFinalized`, so we need do that by hand.
await new Promise(resolve => setTimeout(resolve, 6_000))

const totalBadgesBefore = await sendPinkQuery(client, {
  address: contractId,
  abi,
  provider,
  functionName: 'getTotalBadges',
})
console.log('getTotalBadges:', totalBadgesBefore.toHuman())