最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error: data out-of-bounds (length=3, offset=32, code=BUFFER_OVERRUN, version=abi5.0.7) next-js, ethereum - Stack Ov

programmeradmin5浏览0评论

I am developing ethereum and nextjs project. When project first initlaized I am getting this error:

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
}

I was getting inside getServerSideProps. Initially I had this

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);
}

I checked the code, could not find any issue. I thought maybe the issue would be returning an array of addresses. (As far as I know, we cannot return an array of structs from solidity, but array of addresses would be fine). Nevertheless, I decided to get each campaign one by one. So I changed the contract code:

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;
      }
  }

Then I modified the server side code properly:

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 || [] },
  };
}

After modification, I still get the same error. But if I refresh the page, I don't see any more errors and I get the data on UI. That makes me think that there is no issue with the contract. it must be something with interaction with javascript.

I am developing ethereum and nextjs project. When project first initlaized I am getting this error:

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
}

I was getting inside getServerSideProps. Initially I had this

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);
}

I checked the code, could not find any issue. I thought maybe the issue would be returning an array of addresses. (As far as I know, we cannot return an array of structs from solidity, but array of addresses would be fine). Nevertheless, I decided to get each campaign one by one. So I changed the contract code:

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;
      }
  }

Then I modified the server side code properly:

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 || [] },
  };
}

After modification, I still get the same error. But if I refresh the page, I don't see any more errors and I get the data on UI. That makes me think that there is no issue with the contract. it must be something with interaction with javascript.

Share Improve this question edited Oct 5, 2021 at 13:16 Yilmaz asked Oct 5, 2021 at 2:27 YilmazYilmaz 49.8k18 gold badges216 silver badges270 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

I had this same problem while working with Polygon. I had to change my rpc url from the wss to the https one. Don't know if this helps but thought I'd suggest.

Tip: Check your ABI.

In my case, my function returns a tuple with 1 uint256 and 3 strings, but in my ABI it was returning 4 strings in this tuple.

Example:

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) {
     ...
     }

In the ABI, it was:

  {"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": [{"ponents": [{"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"},

Check your ABI and remove some invalid values.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论