Algorand’s AVM 1.1: Contract-to-Contract Calls
The first two examples above demonstrate clear functional value to the contract-calling capability. The third invites the endless possibilities of modular contracts being used in a myriad of applications.
That said, there are some technical considerations developers should be aware of as they design their dapps to use this new functionality.
A contract calling another contract is a type of “inner transaction”, since it is a transaction initiated from inside a smart contract. As such, contracts called by Contract A will be part of the same transaction group as contract A. This means they all execute, or none of them do.It is possible for Contract A to call Contract B that calls Contract C, etc. up to depth 8.a valid call sequenceSince the introduction of inner transactions in AVM 1.0, there has been a limit on the number of inner transactions per top-level contract. This limit was previously 16 inner transactions within each top-level transaction, but has been changed to 256 total inner transactions within a single transaction group.two valid transaction groupsa transaction group disallowed by the limit on the number of inner transactions. This group has 1 contract call and 256 pay transactions, for a total of 257 inner transactions.Smart contracts on Algorand are given an opcode budget of 700 to guarantee performance. When calling a contract from a contract, an additional 700 opcode budget is allocated to the entire transaction group. In other words, the opcode budget is pooled across the group, including inner contract calls.
example: James is writing a particularly complex smart contract involving some hashing, and he knows it’s going to take more than 700 opcode budget to execute. He calls an empty contract from his top-level contract, effectively giving himself an additional 700 opcode budget — he can now use nearly 1400 opcode budget for his program. If that’s still not enough, he can make 256 inner contract calls, totaling 256*700 nearly 180,000 opcode budget for this program.
Those familiar with developing on Algorand will recall that opcode budget is already pooled across transaction groups, which can have up to 16 transactions. The 256 inner transactions are for the entire transaction group. In other words, if a smart contract makes 256 inner pay calls, then there can be no other transactions in the transaction group calling that contract.Remember that every transaction on Algorand costs a minimum fee of 0.001 ALGO. A contract that calls two other contracts will cost 0.003 ALGO to execute.
example: if James’s contract makes 256 inner contract calls, it will cost 0.256 ALGO (at time of writing, ~0.18$) to execute it all.Smart contracts on Algorand are given access to specific external resources when called: the developer has to define which addresses, applications, and assets that the contract will be interacting with (these are referred to as “foreign” apps/assets/addresses). Contracts called by a contract can be given access to a subset of the foreign objects that the top-level contract has access to. Separately, any assets or apps created in a transaction group are implicitly accessible by all contracts in the group, regardless of depth.Contract A cannot call any contract that calls Contract A. This behavior is known as “re-entrancy” and is known to increase the potential for security vulnerabilities, hence the restriction.two transactions groups disallowed by the re-entrancy restriction
For code-level details and information about other features being released alongside contract-to-contract calls, see the article on our dev portal: Contract Calls and an ABI Come to Algorand.
For information on our previous update, AVM 1.0, see here: Discover AVM 1.0
The contract-to-contract functionality is currently on Algorand’s Betanet, and will be on Mainnet and Testnet by mid-March 2022. Please come try it out on Betanet, and we will welcome all your feedback through Discord, github, or the forum.