Skip to content

Submit a Transaction

Transactions change blockchain state. The examples on this page target only the current Koinos Foundation public testnet, use test tokens with no monetary value, and do not broadcast unless you explicitly opt in.

Configure a signer

Keep a dedicated testnet WIF outside source control:

export function signerFromEnvironment(env = process.env) {
  if (!env.TESTNET_WIF) {
    throw new Error("Set TESTNET_WIF to a dedicated, funded testnet key");
  }
  const provider = new Provider(TESTNET.rpc);
  const signer = Signer.fromWif(env.TESTNET_WIF);
  signer.provider = provider;
  return { provider, signer };
}

View complete file · Run example

Encode a transfer

This function creates a real testnet KOIN contract operation but does not sign or send it:

export async function createTransferOperation({ from, to, amount }) {
  const contract = new Contract({
    id: TESTNET.koinContract,
    provider: new Provider(TESTNET.rpc),
    abi: transferAbi,
  });
  const { operation } = await contract.functions.transfer(
    {
      from: addressBytes(from),
      to: addressBytes(to),
      value: utils.parseUnits(amount, 8),
    },
    { onlyOperation: true }
  );
  return operation;
}

View complete file · Run example

Resource and broadcast options

export function safeTransactionOptions(env = process.env) {
  return {
    rcLimit: env.RC_LIMIT ?? "100000000",
    broadcast: env.BROADCAST === "true",
  };
}

View complete file · Run example

BROADCAST is false unless its value is exactly true.

Dry-run preview

export async function previewTransfer({ from, to, amount }) {
  const operation = await createTransferOperation({ from, to, amount });
  return { network: TESTNET.name, broadcast: false, operations: [operation] };
}

View complete file · Run example

npm start executes this safe path and prints the encoded operation.

Receipt handling

export function assertSuccessfulReceipt(receipt) {
  if (receipt.reverted) {
    throw new Error(`Transaction reverted: ${(receipt.logs ?? []).join("; ")}`);
  }
  return receipt;
}

View complete file · Run example

The complete source exports broadcastTransfer. It refuses to run without both BROADCAST=true and TESTNET_WIF. Verify the destination and amount before enabling it.

Best practices

  • Use separate testnet and mainnet wallets.
  • Never paste a WIF into code, documentation, or an online runner.
  • Preview and test the operation before broadcasting.
  • Check receipt.reverted and wait for confirmation where needed.

Continue with multiple operations.