Skip to content

Commit

Permalink
Fetch ABI of impl
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementWalter committed Jul 20, 2023
1 parent 3a46481 commit 524b282
Show file tree
Hide file tree
Showing 7 changed files with 1,219 additions and 551 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1,432 changes: 917 additions & 515 deletions packages/starksheet-solidity/out/Evmsheet.sol/Evmsheet.json

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions packages/starksheet-solidity/src/Evmsheet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,11 @@ contract Evmsheet is Ownable {
function getSheets() public view returns (address[] memory) {
return sheets;
}

function withdraw() public onlyOwner {
(bool success,) = msg.sender.call{value: address(this).balance}("");
require(success, "Withdrawal failed");
}

receive() external payable {}
}
27 changes: 22 additions & 5 deletions packages/starksheet-solidity/test/Evmsheet.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ contract EvmsheetTest is Test {
ICellRenderer public renderer;
MultiSendCallOnly public multicall;

uint256 price = 0.01 ether;
uint256 _price = 0.01 ether;

receive() external payable {}

function setUp() public {
renderer = new BasicCellRenderer();
evmsheet = new Evmsheet(address(renderer), price);
evmsheet = new Evmsheet(address(renderer), _price);
multicall = new MultiSendCallOnly();
}

Expand All @@ -40,7 +42,7 @@ contract EvmsheetTest is Test {
}

function testAddSheet() public {
evmsheet.addSheet{value: price}("name", "SMB", 0);
evmsheet.addSheet{value: _price}("name", "SMB", 0);
address newSheet = evmsheet.sheets(0);
assertEq(ISheet(newSheet).name(), "name");
assertEq(ISheet(newSheet).symbol(), "SMB");
Expand All @@ -52,14 +54,29 @@ contract EvmsheetTest is Test {
bytes memory transaction = bytes.concat(
bytes1(0x00), // operation
bytes20(address(evmsheet)), // address
bytes32(price), // value
bytes32(_price), // value
bytes32(encodedCall.length), // calldata len
encodedCall // calldata
);
multicall.multiSend{value: price}(transaction);
multicall.multiSend{value: _price}(transaction);
address newSheet = evmsheet.sheets(0);
assertEq(ISheet(newSheet).name(), "Sheet0");
assertEq(ISheet(newSheet).symbol(), "SHT0");
assertEq(ISheet(newSheet).owner(), tx.origin);
}

function testWithdraw() public {
evmsheet.addSheet{value: _price}("name", "SMB", 0);
uint256 prevBalance = address(this).balance;
evmsheet.withdraw();
uint256 newBalance = address(this).balance;
assertEq(newBalance - prevBalance, _price);
}

function testWithdrawShouldRevert() public {
evmsheet.addSheet{value: _price}("name", "SMB", 0);
vm.expectRevert();
vm.prank(address(0xDEAD));
evmsheet.withdraw();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ export function toPlainTextFormula(
.map((arg) => {
if (isDependency(arg)) {
const placeholder = ethers.BigNumber.from(
ethers.utils.randomBytes(32)
)._hex.slice(2);
ethers.utils.randomBytes(20)
)
._hex.slice(2)
.padStart(64, "0");
mapping[placeholder] = tokenIdToCellName(
arg.sub(number.toBN(1)).div(number.toBN(2)).toNumber()
);
Expand Down Expand Up @@ -145,12 +147,16 @@ const customStringify =
"}"
);
} else if (isBigNumber(input)) {
const ret =
mapping[input._hex.slice(2)] === undefined
? input.toString()
: mapping[input._hex.slice(2)];
const key = input._hex.slice(2).padStart(64, "0");
const ret = mapping[key] === undefined ? input.toString() : mapping[key];
return ret;
} else return `${input}`;
} else if (typeof input === "string") {
const key = input.slice(2).padStart(64, "0").toLowerCase();
if (mapping[key] !== undefined) {
return mapping[key];
}
}
return `"${input}"`;
};

export function parseContractCall(
Expand Down Expand Up @@ -201,7 +207,7 @@ export function parse(
// Add global brackets if user input is just a comma separated list
`[${rawCall.args}]`
// Quote cell names before eval
.replace(/([A-O][0-9]+)/gi, '"$1"')
.replace(/([, [(]+)([A-O]{1}[0-9]{1,2})([, \])])/gi, '$1"$2"$3')
) as any[];

// retrieve function and corresponding abi
Expand Down Expand Up @@ -230,17 +236,18 @@ export function parse(
calldata = flattenWithLen(encodedArgs).slice(1) as BN[];
} else if (chainType === ChainType.EVM) {
const m: Record<string, string> = {};
const mappedArgs = mapCellsToRandom(m)(args);
calldata = (ethers.utils.defaultAbiCoder
.encode(
selectorAbi.inputs.map((i) => i.type),
mapCellsToRandom(m)(args)
mappedArgs
)
.slice(2)
.match(/.{1,64}/g)
?.map((bytes32) => {
const parsedWord = number.toBN("0x" + bytes32).toString();
if (m[parsedWord] !== undefined) {
const cellName = m[parsedWord];
const key = "0x" + bytes32.replace(/^0+/, "");
if (m[key] !== undefined) {
const cellName = m[key];
return encodeTokenId(cellNameToTokenId(cellName));
}
return encodeConst("0x" + bytes32);
Expand Down Expand Up @@ -378,8 +385,9 @@ const mapCellsToRandom =
input.replace('"', "").match(CELL_NAME_REGEX)
) {
const placeholder = ethers.BigNumber.from(
ethers.utils.randomBytes(32)
).toString();
// restricted to 20 instead of 32 to handle addresses (bytes20)
ethers.utils.randomBytes(20)
)._hex;
mapping[placeholder] = input;
return placeholder;
} else return input;
Expand Down
29 changes: 27 additions & 2 deletions packages/starksheet-webapp/src/provider/EVMProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class EVMProvider implements ChainProvider {
const url = new URL(this.config.explorerApiUrl!);
url.search = params.toString();

let abi = [];
let abi: Abi = [];
try {
const rawAbi = await fetch(url)
// check the response is not an error and decode its content to json
Expand Down Expand Up @@ -111,10 +111,35 @@ export class EVMProvider implements ChainProvider {

// parse the raw abi and return it
abi = JSON.parse(rawAbi);
const iface = new ethers.utils.Interface(abi);
const others = await Promise.all(
iface.fragments
.filter(
(f: any) =>
f.type === "function" &&
f.stateMutability === "view" &&
f.inputs.length === 0 &&
f.name.toLowerCase().includes("impl")
)
.map(async (f) => {
const implementationAddress = (
await this.provider.call({
to: address,
data: iface.getSighash(f),
})
)
.slice(2)
.replace(/^0+/, "");
return Object.values(
(await this.getAbi("0x" + implementationAddress)) || {}
) as Abi;
})
);
abi = [...abi, ...others.flat()];
} catch (error) {
abi = [];
}
return abi;
return abi as Abi;
}

parseAbi(abi: Abi): ContractAbi {
Expand Down

0 comments on commit 524b282

Please sign in to comment.