status | flip | authors | sponsor | updated |
---|---|---|---|---|
implemented |
1071 |
Deniz Mert Edincik ([email protected]) |
Austin Kline ([email protected]) |
2022-10-11 |
This proposal will allow Cadence programs to import contracts dynamically from an address.
There is a popular request, especially from people coming from EVM space, to have a way to import contracts from an address by name ( dynamic import functionality )
Cadence lacks dynamic imports, which leads to mass imports in some contracts ( e.g., alchemy, find ) which also causes many contracts to fail if one of the crucial contracts ceases to exist.
- It makes it possible to use patterns common to Solidity developers in Flow. ( Such as creating an empty
Vault
or emptyCollection
for the FT/NFT type without prior knowledge of the contract except if it implements the relevant standard. - It allows importing and using contracts with a name collision. See onflow/cadence#1171
- It will prevent a single contract from breaking the ecosystem
We suggest adding a new method borrow
to both AuthAccount.Contracts
and PublicAccount.Contracts
.
Borrowing a Contract will return a reference to the contract, avoiding move/copy problems.
Implemented a proof of concept in onflow/cadence#1934
fun borrow<T: &Any>(name: String): &T?
if let contract = getAccount(0x1).contracts.borrow<&FungibleToken>("ExampleFT") {
let emptyVault <- contract.createEmptyVault()
}
Adding a method named borrowContract
to both PublicAccount
and AuthAccount
.
fun borrowContract<T: &Any>(name: String): &T?
Example usage will be:
if let contract = getAccount(0x1).borrowContract<&FungibleToken>("ExampleFT") {
let emptyVault <- contract.createEmptyVault()
}
This change should be backward compatible.
The proposed change can affect how users write their smart contracts, which can cause some minor issues with reading contract code due to the nature of dynamic imports. But eventually, we don't see any negative security impact.
- Dynamically resolving import addresses onflow/cadence#550
- Solidity has a way to import and call contracts from an address.