Send Transactions
Another common case is interactions that mutate network state - sending tokens from one account to another, minting new NFT, etc. Framework provides sendTransaction
method to achieve this. This method have 2 different signatures.
⚠️ Required: Your project must follow the required structure it must be initialized to use the following functions.
sendTransaction(props)
Send transaction to network. Provides explicit control over how you pass values.
Arguments
props
object accepts following fields:
Name | Type | Optional | Description |
---|---|---|---|
code | string | ✅ | string representation of Cadence transaction |
name | string | ✅ | name of the file in transaction folder to use (sans .cdc extension) |
args | [any] | ✅ | an array of arguments to pass to transaction. Optional if transaction does not expect any arguments. |
signers | [Address or SignerInfo] | ✅ | an array of Address or SignerInfo objects representing transaction autorizers |
addressMap | AddressMap | ✅ | name/address map to use as lookup table for addresses in import statements |
transformers | [CadenceTransformer] | ✅ | an array of operators to modify the code, before submitting it to network |
⚠️ Required: Either
code
orname
field shall be specified. Method will throw an error if both of them are empty. Ifname
field provided, framework will source code from file and override value passed viacode
field.
📣 if
signers
field not provided, service account will be used to authorize the transaction.
📣 Pass
addressMap
only in cases, when you would want to override deployed contract. Otherwide imports can be resolved automatically without explicitly passing them viaaddressMap
field
Usage
_36import path from "path"_36import {_36 init,_36 emulator,_36 sendTransaction,_36 getAccountAddress,_36} from "@onflow/flow-js-testing"_36_36const main = async () => {_36 const basePath = path.resolve(__dirname, "../cadence")_36_36 // Init framework_36 await init(basePath)_36 // Start emulator_36 await emulator.start()_36_36 // Define code and arguments we want to pass_36 const code = `_36 transaction(message: String){_36 prepare(signer: &Account){_36 log(message)_36 }_36 }_36 `_36 const args = ["Hello, from Cadence"]_36 const Alice = await getAccountAddress("Alice")_36 const signers = [Alice]_36_36 const [result, error] = await sendTransaction({code, args, signers})_36 console.log({result}, {error})_36_36 // Stop emulator instance_36 await emulator.stop()_36}_36_36main()
sendTransaction(name, signers, args)
This signature provides simplified way to send a transaction, since most of the time you will utilize existing Cadence files.
Name | Type | Optional | Description |
---|---|---|---|
name | string | ✅ | name of the file in transaction folder to use (sans .cdc extension) |
args | [any] | ✅ | an array of arguments to pass to transaction. Optional if transaction does not expect any arguments. |
signers | [Address or SignerInfoObject] | ✅ | an array of Address or array of SignerInfoObject representing transaction autorizers |
Usage
_29import path from "path"_29import {_29 init,_29 emulator,_29 sendTransaction,_29 shallPass,_29} from "@onflow/flow-js-testing"_29_29const main = async () => {_29 const basePath = path.resolve(__dirname, "../cadence")_29_29 // Init framework_29 await init(basePath)_29 // Start emulator_29 await emulator.start()_29_29 // Define arguments we want to pass_29 const args = ["Hello, Cadence"]_29_29 const [result, error, logs] = await shallPass(_29 sendTransaction("log-message", [], args)_29 )_29 console.log({result}, {error}, {logs})_29_29 // Stop the emulator instance_29 await emulator.stop()_29}_29_29main()