Error: data out-of-bounds (length=3, offset=32, code=BUFFER_



我正在开发以太坊和nextjs项目。当项目第一次初始化时,我得到这个错误:

Error: data out-of-bounds (length=3, offset=32, code=BUFFER_OVERRUN, version=abi/5.0.7)
at Logger.makeError (/home/Documents/projects/ethereum/kickstarter/node_modules/@ethersproject/logger/lib/index.js:199:21)
at Logger.throwError (/home/Documents/projects/ethereum/kickstarter/node_modules/@ethersproject/logger/lib/index.js:208:20)
at Reader._peekBytes (/home/Documents/projects/ethereum/kickstarter/node_modules/@ethersproject/abi/lib/coders/abstract-coder.js:149:24)
at Reader.readBytes (/home/Documents/projects/ethereum/kickstarter/node_modules/@ethersproject/abi/lib/coders/abstract-coder.js:161:26)
at Reader.readValue (/home/Documents/projects/ethereum/kickstarter/node_modules/@ethersproject/abi/lib/coders/abstract-coder.js:167:48)
at NumberCoder.decode (/home/Documents/projects/ethereum/kickstarter/node_modules/@ethersproject/abi/lib/coders/number.js:49:28)
at /home/Documents/projects/ethereum/kickstarter/node_modules/@ethersproject/abi/lib/coders/array.js:106:31
at Array.forEach (<anonymous>)
at Object.unpack (/home/Documents/projects/ethereum/kickstarter/node_modules/@ethersproject/abi/lib/coders/array.js:85:12)
at TupleCoder.decode (/home/Documents/projects/ethereum/kickstarter/node_modules/@ethersproject/abi/lib/coders/tuple.js:39:49)
at AbiCoder.decode (/home/Documents/projects/ethereum/kickstarter/node_modules/@ethersproject/abi/lib/abi-coder.js:93:22)
at ABICoder.decodeParametersWith (/home/Documents/projects/ethereum/kickstarter/node_modules/web3-eth-abi/lib/index.js:303:30)
at ABICoder.decodeParameters (/home/Documents/projects/ethereum/kickstarter/node_modules/web3-eth-abi/lib/index.js:284:17)
at Contract._decodeMethodReturn (/home/Documents/projects/ethereum/kickstarter/node_modules/web3-eth-contract/lib/index.js:469:22)
at Method.outputFormatter (/home/Documents/projects/ethereum/kickstarter/node_modules/web3-eth-contract/lib/index.js:759:42)
at Method.formatOutput (/home/Documents/projects/ethereum/kickstarter/node_modules/web3-core-method/lib/index.js:146:54) {
reason: 'data out-of-bounds',
code: 'BUFFER_OVERRUN',
length: 3,
offset: 32
}

我正在进入getServerSideProps。一开始我有这个

let campaigns;
try {
// in solidity contract, this returns an array of contracts
campaigns = await factory.methods.getDeployedCampaign().call();

} catch (e) {
console.log("error in index server", e);
}

我检查了代码,没有发现任何问题。我想问题可能是返回一个地址数组。(据我所知,我们不能返回的数组结构坚固,但是数组的地址就可以)。尽管如此,我还是决定一个接一个地获得每个广告。所以我改变了合同代码:

contract CampaignFactory{
address[] public deployedCampaigns;
uint public campaignsCount;

function createCampaign(uint minimum)public{
Campaign newCampaign=new Campaign(minimum,msg.sender);
deployedCampaigns.push(address(newCampaign));
campaignsCount++;

}
// return individual campaign
function getDeployedCampaign(uint index) public view returns(address ){
return deployedCampaigns[index];
}

function getCampaignCounts() public view returns(uint){
return campaignsCount;
}
}

然后我修改了服务器端代码:

export async function getServerSideProps(context) {
let campaigns;
let campaignsCount;
try {
// get the number of campaigns
campaignsCount = await factory.methods.getCampaignCounts().call();
campaigns = await Promise.all(
Array(parseInt(campaignsCount))
.fill()
.map((element, index) => {
return factory.methods.getDeployedCampaign(index).call();
})
);
console.log("camapigns in index", campaigns);
} catch (e) {
console.log("error in index server", e);
}
return {
props: { campaigns: campaigns || [] },
};
}

修改后,我仍然得到相同的错误。但如果我刷新页面,我不会看到任何错误,我得到UI上的数据。这使我认为合同没有问题。它一定是与javascript交互的东西。

我在使用Polygon时遇到了同样的问题。我不得不将我的rpc url从wss更改为https。不知道这是否有帮助,但我想我建议。

提示:检查您的ABI。

在我的情况下,我的函数返回1 uint256和3个字符串的元组,但在我的ABI中,它在这个元组中返回4个字符串。

的例子:

struct Contact{
uint256 id;
string symbol;
string address_;
string nickname;
}

function getContacts(address from, string calldata symbol, uint256 fromId, uint256 length) external view returns (Contact[] memory) {
...
}

在ABI中是:

{"inputs": [{"internalType": "address","name": "from","type": "address"},
{"internalType": "string","name": "symbol","type": "string"},
{"internalType": "uint256","name": "fromId","type": "uint256"},
{"internalType": "uint256","name": "length","type": "uint256"}],
"name": "getContacts",
"outputs": [{"components": [{"internalType": "uint256","name": "id","type": "uint256"},
{"internalType": "string","name": "symbol","type": "string"},
{"internalType": "string","name": "address_","type": "string"},
{"internalType": "string","name": "nickname","type": "string"},
{"internalType": "string","name": "OTHERTHING","type": "string"}],//<-- ERROR
"internalType": "struct Contacts.Contact[]","name": "","type": "tuple[]"}],
"stateMutability": "view", "type": "function"},

检查您的ABI并删除一些无效的值。