Building a simple transaction from the contract
Take a look at https://github.com/kadena-community/kadena.js/blob/main/packages/libs/client-examples/src/example-contract/simple-transfer.ts for a complete example.
Now that everything is bootstrapped, we can start building transactions.
Create a new file and name it transfer.ts
(or .js
):
import { Pact } from '@kadena/client'; const unsignedTransaction = Pact.builder .execution( Pact.modules.coin.transfer('k:your-pubkey', 'k:receiver-pubkey', { decimal: '231', }), ) .addSigner('your-pubkey', (withCapability) => [ // add necessary coin.GAS capability (this defines who pays the gas) withCapability('coin.GAS'), // add necessary coin.TRANSFER capability withCapability('coin.TRANSFER', 'k:your-pubkey', 'k:receiver-pubkey', { decimal: '231', }), ]) .setMeta({ chainId: '1', sender: 'your-pubkey' }) .setNetworkId('mainnet01') .createTransaction();
import { Pact } from '@kadena/client'; const unsignedTransaction = Pact.builder .execution( Pact.modules.coin.transfer('k:your-pubkey', 'k:receiver-pubkey', { decimal: '231', }), ) .addSigner('your-pubkey', (withCapability) => [ // add necessary coin.GAS capability (this defines who pays the gas) withCapability('coin.GAS'), // add necessary coin.TRANSFER capability withCapability('coin.TRANSFER', 'k:your-pubkey', 'k:receiver-pubkey', { decimal: '231', }), ]) .setMeta({ chainId: '1', sender: 'your-pubkey' }) .setNetworkId('mainnet01') .createTransaction();
Notes
- Namespaced arguments (
k:
,w:
etc) are account names, where non-namespaced arguments are assumed to be public keys. - The contract doesn't specify whether you need to pass an account name or public key. This is knowledge that can be obtained by inspecting the contract downloaded earlier or consulting the documentation for the contract.
- The
addSigner
function accepts thepublic-key
of the signer and let signer add the capabilities they want to sign for. Note thatcoin.GAS
doesn't have any arguments, butcoin.TRANSFER
does. - The
setMeta
argument object has asender
property. This is apublic-key
and could be gas station address in some scenarios. - To add an Unrestricted Signer (Unscoped Signature ), call
addSigner
without extra arguments