Smart Contracts: Flow

Flow aggregates all contracts in a single Account. Unlike other SDKs, it is not possible to simply invoke methods on smart contracts without passing a small bit of Cadence code to the chain. Elements will, just in time, insert the appropriate import statements to the Cadence code which you supply using the scripting engine.

In order to separate the contract address from the code, Elements provides two options when defining a flow contract address. In both cases, we use the flow Wallet Account ID to find the contract.

Assuming you have two Flow Smart Contracts:

pub contract MyFirstContract { /*...*/ }
pub contract MySecondContract { /*...*/ }

Assuming they were deployed to the account with the ID:

0xf8d6e0586b0a20c7

Specify all Contracts

When specifying a Flow account, you may simply specify the address of the account. This will result in an import statement which will import all contracts within the account. Elemetns will generate the following header just in time.

import 0xf8d6e0586b0a20c7

Therefore, both MyFirstContract and MySecondContract will be visible in the scope of the script. The following script will execute, assuming that both contracts have the appropriate methods.

MyFirstContract.foo()
MySecondContract.foo()

The final script sent to Flow will look like this:

import 0xf8d6e0586b0a20c7

MyFirstContract.foo()
MySecondContract.foo()

Specifying a Specific Contract

In some cases, you may wish to avoid colliding with names in the existing scope. In Cadence, thi sis typically done with the import from statement. To specify the import format, you can append the Account address with the name of the specific contract.

For example, to specify MyFirstContract you simply append :MyFirstContract to the Flow Address owning the contract to make the Elemetns addres. The final address in this example will look as follows:

0xf8d6e0586b0a20c7:MyFirstContract

This will result in Elemetns automatically generating the following header just in time.

import MyFirstContract from 0xf8d6e0586b0a20c7

Therefore, only MyFirstContract will not collide with any other imports. The following script must only reference that contract.

MyFirstContract.foo()

The final script sent to Flow will look like this:

import MyFirstContract from 0xf8d6e0586b0a20c7

MyFirstContract.foo()