Send Transactions

How to 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

1
import path from "path"
2
import {
3
init,
4
emulator,
5
sendTransaction,
6
getAccountAddress,
7
} from "@onflow/flow-js-testing"
8
9
const main = async () => {
10
const basePath = path.resolve(__dirname, "../cadence")
11
12
// Init framework
13
await init(basePath)
14
// Start emulator
15
await emulator.start()
16
17
// Define code and arguments we want to pass
18
const code = `
19
transaction(message: String){
20
prepare(signer: AuthAccount){
21
log(message)
22
}
23
}
24
`
25
const args = ["Hello, from Cadence"]
26
const Alice = await getAccountAddress("Alice")
27
const signers = [Alice]
28
29
const [result, error] = await sendTransaction({code, args, signers})
30
console.log({result}, {error})
31
32
// Stop emulator instance
33
await emulator.stop()
34
}
35
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

1
import path from "path"
2
import {
3
init,
4
emulator,
5
sendTransaction,
6
shallPass,
7
} from "@onflow/flow-js-testing"
8
9
const main = async () => {
10
const basePath = path.resolve(__dirname, "../cadence")
11
12
// Init framework
13
await init(basePath)
14
// Start emulator
15
await emulator.start()
16
17
// Define arguments we want to pass
18
const args = ["Hello, Cadence"]
19
20
const [result, error, logs] = await shallPass(
21
sendTransaction("log-message", [], args)
22
)
23
console.log({result}, {error}, {logs})
24
25
// Stop the emulator instance
26
await emulator.stop()
27
}
28
29
main()