encodeCall

Creates a transaction from a function call.

Type

;<T extends new (...args: any) => any, K extends keyof InstanceType<T>>(params: {
  target: InstanceType<T>
  property: string
  args: Parameters<InstanceType<T>[K]>
  mod?: string
}) =>
  Promise<{
    tx: NakamotoJS.Transaction
    effect: { res: Json; env: Json }
  }>

Parameters

params

An object with the configuration parameters to encode the expression in a transaction.

Key Type Description
target InstanceType<T> The smart object on which to call the function
property string The name of the function being called
args Parameters<InstanceType<T>[K]> The arguments to the function call
mod string A module specifier

Module specifiers are encoded as strings of the form <transaction id>:<output number>

Return Value

See encode.

Description

See encode.

Example

import { Computer, Contract } from '@bitcoin-computer/lib'
import { chain, expect, network, url } from '../../utils/index.js'

describe('encodeCall', async () => {
  it('Should encode a function call', async () => {
    // A smart contract
    class Counter extends Contract {
      n: number

      constructor() {
        super({ n: 0 })
      }

      inc() {
        this.n += 1
      }
    }

    // Create and fund wallet
    const computer = new Computer({ chain, network, url })
    await computer.faucet(1e8)

    // Create an on-chain object from the smart contract
    const counter = await computer.new(Counter, [])

    // Encode a function call
    const { tx } = await computer.encodeCall({
      target: counter,
      property: 'inc',
      args: [],
    })

    // Decode the meta data
    expect(await computer.decode(tx!)).to.deep.eq({
      exp: `__bc__.inc()`,
      env: { __bc__: counter._rev },
      mod: undefined,
    })

    // Broadcast the tx to commit the change
    await computer.broadcast(tx!)
  })
})

Source