Skip to content

Commit

Permalink
Merge pull request #72 from Alex-Neo-Projects/multipleMatchingFunctions
Browse files Browse the repository at this point in the history
Fix for when contracts have multiple functions with the same name but different inputs
  • Loading branch information
alexreyes authored Feb 24, 2023
2 parents ad46ae9 + ced9b90 commit b3e432d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
28 changes: 24 additions & 4 deletions server/startServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ app.post("/deployContract", async (req, res) => {
const [abis, byteCodes] = await compileContract(contractFilename);

let tempContract;

if (constructor.length > 0) {
let [factory, contract] = await deployContracts(
abis,
Expand All @@ -93,6 +94,7 @@ app.post("/deployContract", async (req, res) => {
let [factory, contract] = await deployContracts(abis, byteCodes, []);
tempContract = contract;
}

const contract = createNewContract(contractFilename, abis, tempContract);

contracts[contractFilename]["historicalChanges"].push(
Expand Down Expand Up @@ -179,6 +181,24 @@ function parseParams(params) {
return returnVal;
}

function getFullFunctionForEthers(functionName, params) {
let fullFunction = functionName + '(';

// Need to get functions in the style: slot0(address, int24)
// Because ethers needs this to differentiate between that example, and the possibility of another function with the same name but different parameters.
// Example: slot0(address, int24) & slot0(address, int24, uint[])
// If we only pass in slot0 to the interface and there are two slot0's, ethers will throw an error.
params.map((item, index) => {
let currType = item[1];
fullFunction += currType

fullFunction += addCommaToStringIfNeeded(params, index);
})

fullFunction += ')'
return fullFunction;
}

app.post("/executeTransaction", async (req, res) => {
try {
const {
Expand Down Expand Up @@ -214,8 +234,8 @@ app.post("/executeTransaction", async (req, res) => {
);

const parsedParams = parseParams(params);

const functionEncodedSignature = iface.encodeFunctionData(functionName, parsedParams);
const fullFunction = getFullFunctionForEthers(functionName, params);
const functionEncodedSignature = iface.encodeFunctionData(fullFunction, parsedParams);

let txRes;
let txReceipt;
Expand All @@ -241,10 +261,10 @@ app.post("/executeTransaction", async (req, res) => {
try {
txRes = await callContractFunction(paramData);

const decodedResult = decodeFunctionResult(iface, functionName, txRes);
const decodedResult = decodeFunctionResult(iface, fullFunction, txRes);

if (decodedResult.length > 0)
output += `Output: ${decodeFunctionResult(iface, functionName, txRes)}\n\n`;
output += `Output: ${decodeFunctionResult(iface, fullFunction, txRes)}\n\n`;
} catch (e) {
errorOutput += e.message;
}
Expand Down
12 changes: 12 additions & 0 deletions testContracts/test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
pragma solidity >0.8.0;

contract TestContract {
function slot0(address pool) public view returns (string memory) {
return 'slot01';
}

function slot0(address pool, uint24 _int) public view returns (string memory) {
return 'slot01';
}

function loadTicksForMultiplePools(address[] memory pools, int24 notRoundedTicksAroundCenter) public view returns (string memory) {
return 'slot01';
}

function loadTicks(address pool, int24 notRoundedTicksAroundCenter) public pure returns (string memory) {
return "hello!";
}
Expand Down

0 comments on commit b3e432d

Please sign in to comment.