MAINNET:
Loading...
TESTNET:
Loading...
/
onflow.org
Flow Playground

Contracts Management

How to manage contracts


⚠️ Required: Your project must follow the required structure and it must be initialized to use the following functions.

deployContractByName(props)

Deploys contract code located inside a Cadence file. Returns the transaction result.

Arguments

Props object accepts the following fields:

NameTypeOptionalDescription
namestringname of the file in contracts folder (sans .cdc extension) and name of the contract (please note those should be the same)
toAddress(optional) account address, where contract will be deployed. If this is not specified, framework will create new account with randomized alias.
addressMapAddressMap(optional) object to use for address mapping of existing deployed contracts
args[Any](optional) arguments, which will be passed to contract initializer. (optional) if template does not expect any arguments.
updateboolean(optional) whether to update deployed contract. Default: false

Returns

TypeDescription
ResponseObjectResult of the deploying transaction.

Usage:

import path from "path"
import {init, emulator, deployContractByName} from "@onflow/flow-js-testing"

const main = async () => {
  const basePath = path.resolve(__dirname, "../cadence")

  init(basePath)
  await emulator.start()

  // We will deploy our contract to the address that corresponds to "Alice" alias
  const to = await getAccountAddress("Alice")

  // We assume there is a file on "../cadence/contracts/Wallet.cdc" path
  const name = "Wallet"

  // Arguments will be processed and type matched in the same order as they are specified
  // inside of a contract template
  const args = [1337, "Hello", {name: "Alice"}]

  const [deploymentResult, error] = await deployContractByName({to, name})
  console.log(deploymentResult, error)

  await emulator.stop()
}

main()

In the rare case you would want to deploy contract code not from an existing template file, but rather from a string representation of it, the deployContract method will help you achieve this.

deployContract(props)

Deploys contract code specified as string. Returns transaction result.

Arguments

Props object accepts the following fields:

NameTypeOptionalDescription
contractCodestringstring representation of contract
namestringname of the contract to be deployed. Should be the same as the name of the contract provided in contractCode
toAddressaccount address, where contract will be deployed. If this is not specified, framework will create new account with randomized alias.
addressMapAddressMapobject to use for import resolver. Default: {}
args[Any]arguments, which will be passed to contract initializer. Default: []
updatebooleanwhether to update deployed contract. Default: false

Usage:

import path from "path"
import {init, emulator, deployContract} from "@onflow/flow-js-testing"

const main = async () => {
  const basePath = path.resolve(__dirname, "../cadence")

  await init(basePath)
  await emulator.start()

  const to = await getAccountAddress("Alice")
  const name = "Wallet"
  const contractCode = `
        pub contract Wallet{
            init(amount: Int){
                log(amount)
                log("Thank you for the food!")
            }
        }
    `
  const args = [1337]

  const [deploymentResult, error] = await deployContractByName({
    to,
    name,
    contractCode,
    args,
  })

  console.log(deploymentResult, error)

  await emulator.stop()
}

main()

While the framework has an automatic import resolver for Cadence code, you might want to know where (what address) your Contract is currently deployed to. We provide a method getContractAddress for this.

getContractAddress(name)

Returns the address of the account where the contract is currently deployed.

Arguments

NameTypeDescription
namestringname of the contract
import {getContractAddress} from "@onflow/flow-js-testing"

const main = async () => {
  const basePath = path.resolve(__dirname, "../cadence")

  await init(basePath)
  await emulator.start()

  // if we ommit "to" it will be deployed to a newly generated address with "unknown" alias
  await deployContractByName({name: "HelloWorld"})

  const contract = await getContractAddress("HelloWorld")
  console.log({contract})
}

main()

📣 Framework does not support contracts with identical names deployed to different accounts. While you can deploy contract to a new address, the internal system, which tracks where contracts are deployed, will only store last address.