You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to request for Institution details which returns two or more data but getting these error.
Error: Transaction reverted: function returned an unexpected amount of data
at Institution.getInstituteData (contracts/Institution.sol:112)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at HardhatNode.runCall (node_modules/hardhat/src/internal/hardhat-network/provider/node.ts:713:20)
at EthModule._callAction (node_modules/hardhat/src/internal/hardhat-network/provider/modules/eth.ts:365:9)
at HardhatNetworkProvider.request (node_modules/hardhat/src/internal/hardhat-network/provider/provider.ts:124:18)
at staticCallResult (node_modules/ethers/src.ts/contract/contract.ts:337:22)
at staticCall (node_modules/ethers/src.ts/contract/contract.ts:303:24)
at Proxy.getInstituteData (node_modules/ethers/src.ts/contract/contract.ts:351:41)
Here is my Institution.sol
// SPDX-License-Identifier: MITpragma solidity^0.8.14;
import"./Certification.sol";
error NotOwner();
contractInstitution {
// State Variablesaddresspublic owner;
// Mappingsmapping(address=> Institute) private institutes; // Institutes Mappingmapping(address=> Course[]) private instituteCourses; // Courses Mapping// Eventsevent instituteAdded(string_instituteName);
// Modifiermodifier onlyOwner() {
if (msg.sender!= owner) revertNotOwner();
_;
}
constructor() {
owner =msg.sender;
}
struct Course {
string course_name;
string course_code;
// Other attributes can be added
}
struct Institute {
string institute_name;
string institute_acronym;
string institute_link;
}
function stringToBytes32(
stringmemorysource
) privatepurereturns (bytes32result) {
bytesmemory tempEmptyStringTest =bytes(source);
if (tempEmptyStringTest.length==0) {
return0x0;
}
assembly {
result :=mload(add(source, 32))
}
}
function addInstitute(
address_address,
stringmemory_institute_name,
stringmemory_institute_acronym,
stringmemory_institute_link,
Course[] memory_institute_courses
) public onlyOwner {
bytesmemory tempEmptyStringNameTest =bytes(
institutes[_address].institute_name
);
require(
tempEmptyStringNameTest.length==0,
"Institute with token already exists"
);
require(
_institute_courses.length>0,
"Atleast one course must be added"
);
institutes[_address] =Institute(
_institute_name,
_institute_acronym,
_institute_link
);
for (uint256 i =0; i < _institute_courses.length; i++) {
instituteCourses[_address].push(_institute_courses[i]);
}
emitinstituteAdded(_institute_name);
}
// Called by Institutions// function getInstituteData()// public// view// returns (string memory, string memory, string memory, Course[] memory)// {// Institute memory temp = institutes[msg.sender];// bytes memory tempEmptyStringNameTest = bytes(temp.institute_name);// require(// tempEmptyStringNameTest.length > 0,// "Institute account does not exist!"// );// return (// temp.institute_name,// temp.institute_acronym,// temp.institute_link,// instituteCourses[msg.sender]// );// }// Called by Smart Contractsfunction getInstituteData(
address_address
)
publicviewreturns (stringmemory, stringmemory, stringmemory, Course[] memory)
{
require(
Certification(msg.sender).owner() == owner,
"Incorrect smart contract & authorizations!"
);
Institute memory temp = institutes[_address];
bytesmemory tempEmptyStringNameTest =bytes(temp.institute_name);
require(
tempEmptyStringNameTest.length>0,
"Institute does not exist!"
);
return (
temp.institute_name,
temp.institute_acronym,
temp.institute_link,
instituteCourses[_address]
);
}
function checkInstitutePermission(
address_address
) publicviewreturns (bool) {
Institute memory temp = institutes[_address];
bytesmemory tempEmptyStringNameTest =bytes(temp.institute_name);
if (tempEmptyStringNameTest.length>0) {
returntrue;
} else {
returnfalse;
}
}
}
Here is my test file
const { assert, expect } =require("chai");
const { ethers } =require("hardhat");
describe("Institution Contract", function () {
let mockOwner_acc,
mockInstitute_acc,
mockInvalid_acc,
mockInstitute,
mockInstitute_course,
institution,
certification;
beforeEach(async function () {
const accounts = await ethers.getSigners();
mockOwner_acc = accounts[0];
mockInstitute_acc = accounts[1];
mockInvalid_acc = accounts[2];
mockInstitute = {
institute_name: "Heritage Institute of Technology",
institute_acronym: "HITK",
institute_link: "www.heritageit.edu",
};
mockInstitute_course = [
{
course_name: "Computer Science and Engineering",
course_code: "CSE",
},
{
course_name: "Electronics and Communication Engineering",
course_code: "ECE",
},
{
course_name: "Computer Science and Business Studies",
course_code: "CSBS",
},
];
const Institution = await ethers.getContractFactory("Institution");
institution = await Institution.deploy({
from: mockOwner_acc,
});
institution_address = await institution.getAddress();
const Certification = await ethers.getContractFactory("Certification");
certification = await Certification.deploy(await institution.getAddress(), {
from: mockOwner_acc,
});
});
describe("Deployment of Institution Contract", () => {
it("It has correct owner", async function () {
const instituteContract_owner = await institution.owner();
assert.equal(instituteContract_owner, mockOwner_acc.address);
});
});
describe("Adding institute", () => {
it("Only owner can add Institute, others cannot add", async function () {
const attackerConnectedAccounts = await institution.connect(
mockInvalid_acc
);
await expect(
attackerConnectedAccounts.addInstitute(
mockInstitute_acc.address,
mockInstitute.institute_name,
mockInstitute.institute_acronym,
mockInstitute.institute_link,
mockInstitute_course
)
).to.be.revertedWithCustomError(attackerConnectedAccounts, "NotOwner");
});
it("Adds an institute with valid details", async function () {
const receipt = await institution.addInstitute(
mockInstitute_acc.address,
mockInstitute.institute_name,
mockInstitute.institute_acronym,
mockInstitute.institute_link,
mockInstitute_course,
{ from: mockOwner_acc }
);
const { events, logs } = await receipt.wait();
// console.log(logs[0].fragment.name);// console.log(logs[0].args._instituteName);assert.equal(logs.length, 1);
assert.equal(logs[0].fragment.name, "instituteAdded");
assert.equal(logs[0].args._instituteName, mockInstitute.institute_name);
});
it("Fails if Institute already existed", async function () {
// const institution2 = await Institution.new({ from: mockOwner_acc });
const receipt = await institution.addInstitute(
mockInstitute_acc.address,
mockInstitute.institute_name,
mockInstitute.institute_acronym,
mockInstitute.institute_link,
mockInstitute_course,
{ from: mockOwner_acc }
);
await expect(
institution.addInstitute(
mockInstitute_acc.address,
mockInstitute.institute_name,
mockInstitute.institute_acronym,
mockInstitute.institute_link,
mockInstitute_course,
{ from: mockOwner_acc }
)
).to.be.revertedWith("Institute with token already exists");
});
it("Checks atleast one course is added or not", async function () {
await expect(
institution.addInstitute(
mockInstitute_acc.address,
mockInstitute.institute_name,
mockInstitute.institute_acronym,
mockInstitute.institute_link,
[],
{ from: mockOwner_acc }
)
).to.be.revertedWith("Atleast one course must be added");
});
});
describe("Fetching Institute Data", () => {
it("Only owner can request for Institute Data", async function () {
const institute_data = await institution.getInstituteData(
mockInstitute_acc.address// { from: await certification.getAddress() }
);
assert.equal(institute_data[0], mockInstitute.institute_name);
assert.equal(institute_data[1], mockInstitute.institute_acronym);
assert.equal(institute_data[2], mockInstitute.institute_link);
const formattedInstituteCoursesData = institute_data[3].map((x) => {
return { course_name: x.course_name };
});
assert.equal(
JSON.stringify(formattedInstituteCoursesData),
JSON.stringify(mockInstituteCourses),
"the courses of the institute is incorrect"
);
});
});
});
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I am trying to request for Institution details which returns two or more data but getting these error.
Here is my Institution.sol
Here is my test file
Beta Was this translation helpful? Give feedback.
All reactions