Skip to main content
Version: Current

Cadence Generation

Using the NFT Catalog, you can generate common scripts and transactions to be run against the Flow Blockchain to support your application.

How-to generate scripts and transactions

From JavaScript

Installation


_10
npm install @onflow/fcl
_10
npm install flow-catalog

or


_10
yarn add @onflow/fcl
_10
yarn add flow-catalog

Usage

1. Retrieve a list of transactions available for code generation: NOTE: In order to properly bootstrap the method, you will need to run and await on the getAddressMaps() method, passing it into all of the methods as shown below.


_10
import { getAddressMaps, scripts } from "flow-catalog";
_10
import * as fcl from "@onflow/fcl"
_10
_10
const main = async () => {
_10
const addressMap = await getAddressMaps();
_10
console.log(await scripts.getSupportedGeneratedTransactions(addressMap));
_10
};
_10
_10
main();

2. Provide a Catalog collection identifier to generate code


_15
const getTemplatedTransactionCode = async function() {
_15
const catalogAddressMap = await getAddressMaps()
_15
const result = await cadence.scripts.genTx({
_15
_15
/*
_15
'CollectionInitialization' is one of the available transactions from step 1.
_15
'Flunks' is the collection identifier in this case
_15
'Flow' is a fungible token identifier (if applicable to the transaction being used)
_15
*/
_15
_15
args: ['CollectionInitialization', 'Flunks', 'flow'],
_15
addressMap: catalogAddressMap
_15
})
_15
return result
_15
}

3. Use the generated code in a transaction


_10
const txId = await fcl.mutate({
_10
cadence: await getTemplatedTransactionCode()[0],
_10
limit: 9999,
_10
args: (arg: any, t: any) => []
_10
});
_10
const transaction = await fcl.tx(txId).onceSealed()
_10
return transaction

From non-javascript environments

Cadence scripts and transactions can be generated directly on-chain via scripts. You will need to be able to run cadence scripts to continue.

1. Retrieve a list of transactions available for code generation

Run the following script to retrieve available code generation methods: https://github.com/dapperlabs/nft-catalog/blob/main/cadence/scripts/get_supported_generated_transactions.cdc

2. Provide a catalog collection identifier to generate code

You may use the following script to generate code: https://github.com/dapperlabs/nft-catalog/blob/main/cadence/scripts/gen_tx.cdc

For example, from the CLI this may be run like the following: flow -n mainnet scripts execute ./get_tx.cdc CollectionInitialization Flunks flow

In the above example, CollectionInitialization is one of the supported transactions returned from step 1, Flunks is the name of an entry on the catalog (https://www.flow-nft-catalog.com/catalog/mainnet/Flunks), and flow is a fungible token identifier.