Skip to main content

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:

NameTypeOptionalDescription
codestringstring representation of Cadence transaction
namestringname 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
addressMapAddressMapname/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 or name field shall be specified. Method will throw an error if both of them are empty. If name field provided, framework will source code from file and override value passed via code 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 via addressMap field

Usage


_36
import path from "path"
_36
import {
_36
init,
_36
emulator,
_36
sendTransaction,
_36
getAccountAddress,
_36
} from "@onflow/flow-js-testing"
_36
_36
const 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
_36
main()

sendTransaction(name, signers, args)

This signature provides simplified way to send a transaction, since most of the time you will utilize existing Cadence files.

NameTypeOptionalDescription
namestringname 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


_29
import path from "path"
_29
import {
_29
init,
_29
emulator,
_29
sendTransaction,
_29
shallPass,
_29
} from "@onflow/flow-js-testing"
_29
_29
const 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
_29
main()