错误:在调用Multicall合约的聚合函数时,发送交易需要一个签名者(signer)。

9

1
你需要提供代码和具体问题。 - Ashikul
嘿,你有解决它吗? - Spandan Singh
3个回答

7

在执行solidity方法时,您必须提供一个签名者。

您可以从您的web3提供程序中获取签名者。

您可以像这样将签名者绑定到合同中

import Contract from './artifacts/contracts/Contract.sol/Contract.json'
const contractDeployedAddress = "0xblah";

const provider = new ethers.providers.Web3Provider(window.ethereum)
const signer = provider.getSigner();
const contract = new ethers.Contract(contractDeployedAddress, Contract.abi, signer)

await contract.someMethodThatRequiresSigning();

3

我有一个类似的情况,使用@web3-react参考uniswap-interface代码。

@web3-react基于ethers.js,我们必须使用signer执行状态更改方法。我发布了一个解决方案示例。

const { library, account } = useActiveWeb3React();

const contract = getContract(
      CONTRACT_ADDRESS,
      abi,
      library
    );
const signer = contract.connect(library.getSigner());
signer.someStateChangingMethods();

这可能会对你有所帮助。 https://docs.ethers.io/v5/getting-started/#getting-started--writing


0

要进行多次调用,您需要对函数进行编码。

// assuming you created swapRouterContract properly and set params1, params2
// interface.encodeFunctionData available in ether
const encData1 = swapRouterContract.interface.encodeFunctionData(
    "exactInputSingle",
    [params1]
  );

  const encData2 = swapRouterContract.interface.encodeFunctionData(
    "exactInputSingle",
    [params2]
  );

多功能调用意味着我们将通过单个请求调用多个函数。

 const calls = [encData1, encData2];
// To send data to the contract, we need to send it in a way that the contract can read it. That is, they need to be encoded.
  // data is transformed into byte
  const encMultiCall = swapRouterContract.interface.encodeFunctionData(
    "multicall",
    [calls]
  );

发送交易,我们需要“发送方”,“接收方”和“数据”。
  const txArgs = {
    to: V3SwapRouter,
    from: WALLET_ADDRESS,
    data: encMultiCall,
  };

这个交易必须由签名者发送。如果您通过 Metamask 发送交易,Metamask 会自动使用账户的私钥进行签名。但是,如果您通过代码发送交易,则必须创建一个签名者。

const provider = new ethers.providers.JsonRpcProvider(TEST_URL);
// get the secret of the account 
const wallet = new ethers.Wallet(WALLET_SECRET);
// connect the wallet to the provider
const signer = wallet.connect(provider);

现在你可以发送交易:

const tx = await signer.sendTransaction(txArgs);
console.log("tx", tx);
// wait for the tx processed and added to the blockchain
const receipt = await tx.wait();
console.log(receipt);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接