Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: use staticcall instead of call #383

Merged
merged 1 commit into from
May 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/StdCheats.sol
Original file line number Diff line number Diff line change
Expand Up @@ -572,15 +572,15 @@ abstract contract StdCheats is StdCheatsSafe {

function deal(address token, address to, uint256 give, bool adjust) internal virtual {
// get current balance
(, bytes memory balData) = token.call(abi.encodeWithSelector(0x70a08231, to));
(, bytes memory balData) = token.staticcall(abi.encodeWithSelector(0x70a08231, to));
uint256 prevBal = abi.decode(balData, (uint256));

// update balance
stdstore.target(token).sig(0x70a08231).with_key(to).checked_write(give);

// update total supply
if (adjust) {
(, bytes memory totSupData) = token.call(abi.encodeWithSelector(0x18160ddd));
(, bytes memory totSupData) = token.staticcall(abi.encodeWithSelector(0x18160ddd));
uint256 totSup = abi.decode(totSupData, (uint256));
if (give < prevBal) {
totSup -= (prevBal - give);
Expand All @@ -593,15 +593,15 @@ abstract contract StdCheats is StdCheatsSafe {

function dealERC1155(address token, address to, uint256 id, uint256 give, bool adjust) internal virtual {
// get current balance
(, bytes memory balData) = token.call(abi.encodeWithSelector(0x00fdd58e, to, id));
(, bytes memory balData) = token.staticcall(abi.encodeWithSelector(0x00fdd58e, to, id));
uint256 prevBal = abi.decode(balData, (uint256));

// update balance
stdstore.target(token).sig(0x00fdd58e).with_key(to).with_key(id).checked_write(give);

// update total supply
if (adjust) {
(, bytes memory totSupData) = token.call(abi.encodeWithSelector(0xbd85b039, id));
(, bytes memory totSupData) = token.staticcall(abi.encodeWithSelector(0xbd85b039, id));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this PR seems like it should be ok: ERC-20 requires that balanceOf and totalSupply are view methods, and ERC-1155 requires that balanceOf is view also

Interestingly I didn't see the totalSupply specified in 1155 though—@wirew0lf any idea on if using a staticcall for the 1155 methods here might be problematic for some reason?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! yeah I agree it's better to use staticcall, good call. About the ERC115 @mds1 ERC1155 doesn't have a total supply by default, it is an extension ERC1155Supply.

On the cheatcode I acocunted for this and revert if adjust is set to true but contract is no ERC115Supply with the message target contract is not ERC1155Supply. But yeah on ERC1155Supply totalSupply() is also a view function so no problem by using staticcall.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah perfect, thanks @wirew0lf for the info and thanks @PaulRBerg for the PR!

require(
totSupData.length != 0,
"StdCheats deal(address,address,uint,uint,bool): target contract is not ERC1155Supply."
Expand All @@ -622,11 +622,12 @@ abstract contract StdCheats is StdCheatsSafe {
require(successMinted, "StdCheats deal(address,address,uint,bool): id not minted.");

// get owner current balance
(, bytes memory fromBalData) = token.call(abi.encodeWithSelector(0x70a08231, abi.decode(ownerData, (address))));
(, bytes memory fromBalData) =
token.staticcall(abi.encodeWithSelector(0x70a08231, abi.decode(ownerData, (address))));
uint256 fromPrevBal = abi.decode(fromBalData, (uint256));

// get new user current balance
(, bytes memory toBalData) = token.call(abi.encodeWithSelector(0x70a08231, to));
(, bytes memory toBalData) = token.staticcall(abi.encodeWithSelector(0x70a08231, to));
uint256 toPrevBal = abi.decode(toBalData, (uint256));

// update balances
Expand Down