encodeNew

Creates a transaction from a constructor call.

Type

;<T extends new (...args: any) => any>(params: {
  constructor: T
  args?: ConstructorParameters<T>
  mod?: string
}) =>
  Promise<{
    tx: NakamotoJS.Transaction
    effect: { res: Json; env: Json }
  }>

Parameters

params

Key Description
constructor A JavaScript class that extends from Contract.
args Arguments to the constructor of the class.
mod A string of the form <id>:<num> specifying the location of a module.

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('encodeNew', async () => {
  it('Should encode a constructor call', async () => {
    // A smart contract
    class C extends Contract {}

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

    // Encode a constructor call
    const { tx, effect } = await computer.encodeNew({
      constructor: C,
      args: [],
    })

    // Decode transaction
    expect(await computer.decode(tx!)).to.deep.eq({
      exp: `${C} new C()`,
      env: {},
      mod: undefined,
    })

    // Broadcast the tx to create the on-chain object
    const txId = await computer.broadcast(tx!)

    // Synchronizing to the transaction id always returns the effect
    expect(await computer.sync(txId)).deep.eq(effect)
  })
})

Source