Using the run_external
callframe fails with the following error:
Proxy
:
// SPDX-License-Identifier: Apache-2.0
contract;
use std::{
call_frames::first_param,
context::*,
asset::*,
execution::run_external,
hash::Hash,
hash::sha256,
};
abi Proxy {
#[storage(read, write)]
fn set_proxy_target(new_target: ContractId);
}
// Inspired by EIP-2535: Diamonds, Multifacet proxy
#[namespace(diamonds)]
storage {
SRC14 {
/// The [ContractId] of the target contract.
///
/// # Additional Information
///
/// `target` is stored at sha256("storage_SRC14_0")
target in 0x7bb458adc1d118713319a5baa00a2d049dd64d2916477d2688d76970c898cd55: ContractId = ContractId::zero(),
},
}
impl Proxy for Contract {
#[storage(read, write)]
fn set_proxy_target(new_target: ContractId) {
storage::SRC14.target.write(new_target);
}
}
#[fallback]
#[storage(read)]
fn fallback() {
run_external(storage::SRC14.target.read())
}
ProxyImplementation
// SPDX-License-Identifier: Apache-2.0
contract;
use std::{context::*, asset::*};
abi ProxyImpl {
#[storage(read,write)]
fn set_bits(value: b256);
#[storage(read)]
fn get_bits() -> b256;
}
storage {
implementation1 {
bits: b256 = 0x0000000000000000000000000000000000000000000000000000000000000000,
},
}
impl ProxyImpl for Contract {
#[storage(read,write)]
fn set_bits(value: b256) {
storage::implementation1.bits.write(value);
}
#[storage(read)]
fn get_bits() -> b256 {
storage::implementation1.bits.read()
}
}
Test:
const proxy = await deploy("Proxy", deployer)
const proxyImpl = await deploy("ProxyImpl", deployer)
const bits = (await diamondProxyImpl.functions.get_bits().get()).value
console.log("bits", bits) // 0x0000000000000000000000000000000000000000000000000000000000000000
// setting target
let target = (await diamondProxy.functions.proxy_target().get()).value
console.log("proxy target before", target)
;(await diamondProxy.functions.set_proxy_target(toContract(diamondProxyImpl)).call()).waitForResult()
target = (await diamondProxy.functions.proxy_target().get()).value
console.log("proxy target after", target)
const newProxy = new ProxyImpl(diamondProxy.id, deployer)
// fails here
const newBits = (await newProxy.functions.get_bits().get()).value
console.log("new bits", newBits)