Contracts Management
⚠️ 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:
Name | Type | Optional | Description |
---|---|---|---|
name | string | name of the file in contracts folder (with .cdc extension) and name of the contract (please note those should be the same) | |
to | Address | ✅ | (optional) account address, where contract will be deployed. If this is not specified, framework will create new account with randomized alias. |
addressMap | AddressMap | ✅ | (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. |
update | boolean | ✅ | (optional) whether to update deployed contract. Default: false |
transformers | [CadenceTransformer] | ✅ | an array of operators to modify the code, before submitting it to network |
Returns
Type | Description |
---|---|
ResponseObject | Result of the deploying transaction. |
Usage
_27import path from "path";_27import { init, emulator, deployContractByName } from "@onflow/flow-js-testing";_27_27const main = async () => {_27 const basePath = path.resolve(__dirname, "../cadence");_27_27 await init(basePath);_27 await emulator.start();_27_27 // We will deploy our contract to the address that corresponds to "Alice" alias_27 const to = await getAccountAddress("Alice");_27_27 // We assume there is a file on "../cadence/contracts/Wallet.cdc" path_27 const name = "Wallet";_27_27 // Arguments will be processed and type matched in the same order as they are specified_27 // inside of a contract template_27 const args = [1337, "Hello", { name: "Alice" }];_27_27 const [deploymentResult, err] = await deployContractByName({ to, name });_27 console.log({ deploymentResult }, { err });_27 }_27_27 await emulator.stop();_27};_27_27main();
In a bit more rare case you would want to deploy contract code not from existing template file, but rather
from string representation of it. deployContract
method will help you achieve this.
deployContract(props)
Deploys contract code specified as string. Returns the transaction result.
Arguments
Props object accepts the following fields:
Name | Type | Optional | Description |
---|---|---|---|
contractCode | string | string representation of contract | |
name | string | name of the contract to be deployed. Should be the same as the name of the contract provided in contractCode | |
to | Address | ✅ | account address, where contract will be deployed. If this is not specified, framework will create new account with randomized alias. |
addressMap | AddressMap | ✅ | object to use for import resolver. Default: {} |
args | [Any] | ✅ | arguments, which will be passed to contract initializer. Default: [] |
update | boolean | ✅ | whether to update deployed contract. Default: false |
transformers | [CadenceTransformer] | ✅ | an array of operators to modify the code, before submitting it to network |
Returns
Type | Description |
---|---|
ResponseObject | Result of the deploying transaction. |
Usage
_42import path from "path"_42import {_42 init,_42 emulator,_42 getAccountAddress,_42 deployContract,_42 executeScript,_42} from "@onflow/flow-js-testing"_42;(async () => {_42 const basePath = path.resolve(__dirname, "../cadence")_42_42 await init(basePath)_42 await emulator.start()_42_42 // We can specify, which account will hold the contract_42 const to = await getAccountAddress("Alice")_42_42 const name = "Wallet"_42 const code = `_42 pub contract Wallet{_42 pub let balance: UInt_42 init(balance: UInt){_42 self.balance = balance_42 }_42 }_42 `_42 const args = [1337]_42_42 await deployContract({to, name, code, args})_42_42 const [balance, err] = await executeScript({_42 code: `_42 import Wallet from 0x01_42 pub fun main(): UInt{_42 return Wallet.balance_42 }_42 `,_42 })_42 console.log({balance}, {err})_42_42 await emulator.stop()_42})()
While framework have automatic import resolver for Contracts you might want to know where it's currently deployed.
We provide a method getContractAddress
for this.
getContractAddress(name)
Returns address of the account where the contract is currently deployed.
Arguments
Name | Type | Description |
---|---|---|
name | string | name of the contract |
Returns
Type | Description |
---|---|
Address | 0x prefixed address |
Usage
_17import path from "path"_17import {init, emulator, deployContractByName, getContractAddress} from "../src"_17;(async () => {_17 const basePath = path.resolve(__dirname, "./cadence")_17_17 await init(basePath)_17 await emulator.start()_17_17 // if we omit "to" it will be deployed to Service Account_17 // but let's pretend we don't know where it will be deployed :)_17 await deployContractByName({name: "Hello"})_17_17 const contractAddress = await getContractAddress("Hello")_17 console.log({contractAddress})_17_17 await emulator.stop()_17})()
📣 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.