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

feat: add erc20 balance #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion create.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ async function main() {
await fork.init(FORK_NETWORK_ID, CHAIN_ID);
console.log("rpcUrl", fork.get_rpc_url());
console.log("chainId", CHAIN_ID);
console.log("address", ETH_ADDRESS);
console.log("");
if (ETH_ADDRESS) {
console.log(`Funding ${ETH_ADDRESS} with 10000 of the native currency.`);
await fork.fund_account(ETH_ADDRESS, 10000);
// await fork.fund_account(ETH_ADDRESS, 10000);
await fork.deal(ETH_ADDRESS, 100);
} else {
console.log("No ETH_ADDRESS was provided so funding is skipped.");
}
Expand Down
89 changes: 89 additions & 0 deletions tenderly.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
require("dotenv").config();
const axios = require("axios");
const ethers = require("ethers");
const { hexStripZeros, hexZeroPad } = require("@ethersproject/bytes");
const { keccak256 } = require("@ethersproject/keccak256");
const { defaultAbiCoder } = require("@ethersproject/abi");

const TENDERLY_KEY = process.env.TENDERLY_KEY;
const TENDERLY_ACCOUNT = process.env.TENDERLY_ACCOUNT;
Expand Down Expand Up @@ -30,6 +34,16 @@ class TenderlyFork {
}
);
this.fork_id = response.data.simulation_fork.id;
this.forkNetworkId = forkNetworkId;
this.chainId = chainId;
}

async getHead() {
const response = await tenderly.get(
`account/${TENDERLY_ACCOUNT}/project/${TENDERLY_PROJECT}/fork/${this.fork_id}`
);

return response.data.simulation_fork.global_head;
}

async fund_account(address, amount) {
Expand All @@ -40,6 +54,59 @@ class TenderlyFork {
);
}

async deal(address, amount) {
const head = await this.getHead();
if (!this.fork_id) throw new Error("Fork not initialized!");
const tokens = [
//{ address: "0xae7ab96520de3a18e5e111b5eaab095312d7fe84", slot: 1 }, // stETH - doesn't work
{ address: "0x6b175474e89094c44da98b954eedeac495271d0f", slot: 2 }, // DAI (key slot) 2
{ address: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", slot: 9 }, // USDC (key, slot) 9
// "0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae9", // AAVE (key, slot) 0
// "0xba100000625a3754423978a60c9317c58a424e3d", // BAL (key slot) 1
];
const storage = tokens.reduce(
(acc, token) => {
acc[token.address] = {
storage: {
[getSolidityStoageSlotUint256(token.slot, address)]: hexZeroPad(
ethers.utils.parseEther(String(amount)).toHexString(),
32
),
},
};
return acc;
},
{
[address]: {
balance: ethers.utils.parseEther(String(amount)).toString(),
},
}
);
console.log("Funding various tokens");
const response = await tenderly.post(
`account/${TENDERLY_ACCOUNT}/project/${TENDERLY_PROJECT}/fork/${this.fork_id}/simulate`,
{
from: "0x0000000000000000000000000000000000000000",
to: "0xBd770416a3345F91E4B34576cb804a576fa48EB1",
input: "0xef690cc0",
gas: 1000000,
gas_price: "0",
value: 0,
save: true,
state_objects: storage,
root: head,
}
);

await tenderly.put(
`account/${TENDERLY_ACCOUNT}/project/${TENDERLY_PROJECT}/fork/${this.fork_id}`,
{
fork_head: response.data.simulation.id,
}
);
console.log("Funding complete");
}

get_rpc_url() {
if (!this.fork_id) throw new Error("Fork not initialized!");
return `https://rpc.tenderly.co/fork/${this.fork_id}`;
Expand All @@ -52,4 +119,26 @@ class TenderlyFork {
}
}

/**
* @notice Returns the storage slot for a Solidity mapping with bytes32 keys, given the slot of the mapping itself
* @dev Read more at https://docs.soliditylang.org/en/latest/internals/layout_in_storage.html#mappings-and-dynamic-arrays
* @param mappingSlot Mapping slot in storage
* @param key Mapping key to find slot for
* @returns Storage slot
*/
function getSolidityStorageSlotBytes(mappingSlot, key) {
const slot = hexZeroPad(mappingSlot, 32);
return hexStripZeros(
keccak256(defaultAbiCoder.encode(["address", "uint256"], [key, slot]))
);
}

function getSolidityStoageSlotUint256(mappingSlot, key) {
return hexStripZeros(
keccak256(
defaultAbiCoder.encode(["uint256", "uint256"], [key, mappingSlot])
)
);
}

module.exports = { TenderlyFork };