Skip to content

contract.tx.*

Submit a contract transaction, it share the compatbile API design with @polkadot/api-contract.

Same as contract.query.*, the function name will convert to camelCase when calling with SDK.

In Phat Contract, contract transaction required gas fee & storage deposit fee for a transaction.

Usage

example.ts
import * as fs from 'node:fs'
import { signAndSend } from '@phala/sdk'
import { client, pair, cert } from './setup'
 
const abi = fs.readFileSync('path/to/your/Certificate.abi.json', 'utf-8')
const contract = await getContract({
  client,
  contractId: '0x...',
  abi,
})
 
const remark = 'Hi Remark'
// estimate the gas fee & storage deposit fee
const { gasRequired, storageDeposit } = await contract.query.setRemark(pair.address, { cert }, remark)
const options = {
  gasLimit: gasRequired.refTime,
  storageDepositLimit: storageDeposit.isCharge ? storageDeposit.asCharge : null,
}
const result = await signAndSend(contract.tx.setRemark(options, remark), pair) 
// If you works with `WsProvider`.
await result.waitFinalized()

Parameters

options

PinkContractOptions

The options for the transaction. It's a object with the following properties:

  • gasLimit: number. Optional. The maximum amount of gas to use for the transaction. The transaction will failed if the gas used exceed the gasLimit. If not specified, the transaction will always failed. It will use token in the cluster balance.
  • storageDepositLimit: number. Optional. The maximum amount of storage deposit to use for the transaction. The transaction will failed it the storage for the contract has been increased. It will use token in the cluster balance.
  • value: number. Optional. The amount of token to transfer to the contract. If not specified, it will use 0. It will use token in the user balance.
  • deposit: number. Optional. The amount of token to deposit to the cluster before execute the transaction. If not specified, it will use 0.
  • plain: boolean. optional. By default, the contract transaction is visible on chain and encrypted. If plain is true, the transaction will be visible on chain and not encrypted.
  • nonce: string. Optional. It's not the nonce for the on-chain transaction (since it will be auto-incremented), it's the nonce for the off-chain transaction and you can use it as the identify of the transaction to filtering the logs.

...rest

any[]

The arguments for the transaction.

Returns

SubmittableExtrinct<PinkSubmittableResult>

The return value is the SubmittableExtrinct object. You can call the .signAndSend method of it to submit the transaction to the chain, or use the helper function signAndSend function to handle it.

PinkSubmittableResult

Once you submit the extrinct to the chain, it will return the PinkSubmittableResult instance.

The PinkSubmittableResult is the sub-class of SubmittableResult from @polkadot/api.

On-top of the original SubmittableResult, it has an additional method waitFinalized. It can use for wait for one of the specified conditions to be met.

waitFinalized

  • predicate: () => Promise<boolean>. Optional. The function to check customized condition to be met or not. It will runs every one second, once it returns true, the waitFinalized will resolve.
  • options: object. Optional.
    • timeout: number. Optional. The timeout in ms for the waitFinalized. If the predicate not return true before the timeout, or predicate is nullish value and the timeout has be met, the waitFinalized will reject.
    • blocks: number. Optional. Wait for the specified number of blocks to be finalized, it the predicate is nullish value, waitFinalized will be resolved once the specified number of blocks has been finalized. Otherwise, it will throws Timeout error.