The Foreign Call Protocol (FCP) facilitates the ability for SmartWeave contracts to behave inter-operably by invoking functions from other contracts that support FCP.
The lack of interoperability between Arweave-based smart contracts has widely been considered a limiting factor to what developers can build with SmartWeave. It forced smart contract developers to design mechanisms that could not utilize or interface with external, existing mechanisms. The FCP removes this restriction.
User sends a nested contract interaction transaction for contract A to contract B by calling its invoke
function.
This contract interaction to contract B adds another contract interaction for contract A to contract B's foreignCalls
array in the state (outbox)
User sends a contract interaction transaction to contract A invoking its readOutbox
function
If contract A trusts contract B, this interaction triggers contract A to read the state of contract B to find the nested interaction inside of contract B's foreignCalls
array
With the new interaction, contract A recursively invokes the handle
function to effectively process the nested interaction and update its own state
The original nested interaction transaction is then added to the invocations
state inside of contract A to prevent it from being evaluated twice.
The FCP allows for two-way communication between SmartWeave contracts on Arweave. In order to facilitate this, contracts need two new arrays in their states and two new functions in their sources:
State modifications
invocations: string[]
Contracts must keep a log of the FCP transactions they have already invoked
foreignCalls: { txID: string, contract: string, input:*contract interaction object* }[]
Contracts must keep a log of proposed invocations to other contracts (effectively like an "outbox")
Contract source modifications
invoke(foreignContract: string, invocation:*contract interaction object*)
Function allows for adding interactions with other contracts into its foreignCalls
state
readOutbox(contract: string, id: integer)
Function allows for reading the foreignCalls
state of other contracts and evaluating them accordingly
See two example contracts that successfully utilized the Foreign Call Protocol: ABC and DEF.