# 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 } from '@bitcoin-computer/lib'
import { chain, expect, network, url } from '../../utils'

// A smart contract
class C extends Contract {}

// Create wallet
const computer = new Computer({ chain, network, url })

describe('encodeNew', async () => {
  // Fund wallet
  before('Fund wallet', async () => {
    await computer.faucet(1e8)
  })

  it('Should encode a constructor call', async () => {
    // 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: '',
    })

    // 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)
  })
})

Sources