我有一个部署到本地网络的智能合约。我正在使用
Hardhat
控制台与其交互。但是,我无法从控制台切换到不同的钱包/帐户。
contract PaymentChannel {
address public sender;
address public recipient;
uint public expiration;
// the total amount in this payment channel
uint public totalAmount;
uint public paidAmount;
constructor(address _recipient, uint duration) payable {
console.log("constructor", msg.sender, _recipient, duration);
sender = msg.sender;
recipient = _recipient;
expiration = block.timestamp + duration;
totalAmount = msg.value;
}
}
部署到本地 Ganache 后,我通过
Hardhat
从 npx hardhat console --network ganache
连接到该网络。它成功记录到 Hardhat
控制台。
contract = await hre.ethers.getContractAt('PaymentChannel', '0x2b48dcF3AD875416e33D858E34555C13dc555cFc');
然后我就可以使用
contract
实例来调用此合约中的任何方法:
> await contract.sender()
'0x763BB3c3C568681383eb04a5272e535c6d268128'
> await contract.totalAmount();
10000000000000000000n
但是,当我尝试使用
connect
切换到其他帐户时,它不允许我调用任何合约的方法:
> recipient = await contract.connect('0x47D20A3Fb51C6Cf3aB847136bC6CedBf86c8BD58')
> await recipient.sender()
Uncaught:
Error: contract runner does not support calling (operation="call", code=UNSUPPORTED_OPERATION, version=6.10.0)
at REPL89:1:49 {
code: 'UNSUPPORTED_OPERATION',
operation: 'call',
shortMessage: 'contract runner does not support calling'
我可以确认单元测试中使用的相同代码
connect
工作正常。控制台中出现此错误的原因可能是什么?
我认为您在连接函数中提供了错误的参数类型,它不接受字符串。您必须提供
Signer
对象而不是字符串(地址)。
要解决这个问题,您应该首先使用帐户的私钥创建一个 Signer 实例(
0x47D20A3Fb51C6Cf3aB847136bC6CedBf86c8BD58
)。
您可以使用私钥从 ethers.js 库实例化 Wallet 对象。
const privateKey = '0x71bE63f3384f5fb98995898A86B02Fb2426c5788'(实际私钥)
const account = new ethers.Wallet(privateKey);
const 接收者=等待contract.connect(account); 等待收件人.发件人()