我已经编写了一个简单的可靠代理合同,但委托合同中的变量存在问题。当我委托调用时,我所有的变量都等于 0,除非有常量。有什么原因还是我错过了什么?
我的代理合同:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.3;
contract Proxy {
mapping(string => address) public strategies;
function addStrategy(string memory id, address implementation) external {
strategies[id] = implementation;
}
function removeStrategy(string memory id) external {
delete strategies[id];
}
function displayVar(string memory strategyId) external {
address strategy = strategies[strategyId];
require(strategy != address(0x0), "Strategy not found..");
(bool success, bytes memory data) = strategy.delegatecall(
abi.encodeWithSignature("displayVar()")
);
}
}
代表合同:
pragma solidity ^0.8.3;
import "hardhat/console.sol";
contract Delegate {
mapping(string => address) public strategies;
address public constant CRV = 0xD533a949740bb3306d119CC777fa900bA034cd52;
address public curve = 0x90E00ACe148ca3b23Ac1bC8C240C2a7Dd9c2d7f5;
address public constant cvx = 0xF403C135812408BFbE8713b5A23a04b3D48AAE31;
address public constant CVX = 0x4e3FBD56CD56c3e72c1403e103b45Db9da5B9D2B;
function displayVar() external returns (bool) {
console.log(CRV);
console.log(curve);
console.log(cvx);
console.log(CVX);
}
}
使用 HardHat 进行的测试:
import { Contract, ContractFactory } from "ethers";
import { ethers } from "hardhat";
describe("test via proxy", function () {
let Proxy: ContractFactory, proxy: Contract;
let Delegate: ContractFactory, delegate: Contract;
const stratName = "test";
before(async function () {
Proxy = await ethers.getContractFactory("Proxy");
proxy = await Proxy.deploy();
await proxy.deployed();
Delegate = await ethers.getContractFactory("Delegate");
delegate = await Delegate.deploy();
await delegate.deployed();
await proxy.addStrategy(stratName, delegate.address);
});
it("should display", async function () {
const [owner] = await ethers.getSigners();
await proxy.connect(owner).displayVar(stratName);
});
});
最后的输出是:
0xd533a949740bb3306d119cc777fa900ba034cd52
0x0000000000000000000000000000000000000000
0xf403c135812408bfbe8713b5a23a04b3d48aae31
0x4e3fbd56cd56c3e72c1403e103b45db9da5b9d2b