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

add gaslimit oterwise tx throws out of gas error #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
142 changes: 64 additions & 78 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,93 +1,79 @@
const { ethers } = require("ethers")
const Ganache = require("ganache-core")
const { ethers } = require("ethers");
const Ganache = require("ganache-core");

const MAINNET_NODE_URL = "<insert mainnet node url here>"
const PRIV_KEY = "<insert ANY private key here>"
const MAINNET_NODE_URL = "<insert mainnet node url here>";
const PRIV_KEY = "<insert ANY private key here>";
const GAS_LIMIT = 2000000;

const startChain = async () => {
const ganache = Ganache.provider({
fork: MAINNET_NODE_URL,
network_id: 1,
accounts: [
{
secretKey: PRIV_KEY,
balance: ethers.utils.hexlify(ethers.utils.parseEther("1000")),
},
],
})
const ganache = Ganache.provider({
fork: MAINNET_NODE_URL,
network_id: 1,
accounts: [
{
secretKey: PRIV_KEY,
balance: ethers.utils.hexlify(ethers.utils.parseEther("1000")),
},
],
gasLimit: GAS_LIMIT,
});

const provider = new ethers.providers.Web3Provider(ganache)
const wallet = new ethers.Wallet(PRIV_KEY, provider)
const provider = new ethers.providers.Web3Provider(ganache);
const wallet = new ethers.Wallet(PRIV_KEY, provider);

return wallet
}
return wallet;
};

jest.setTimeout(100000)
const uniswap = require("@studydefi/money-legos/uniswap")
const erc20 = require("@studydefi/money-legos/erc20")
jest.setTimeout(100000);
const uniswap = require("@studydefi/money-legos/uniswap");
const erc20 = require("@studydefi/money-legos/erc20");

describe("do some tests", () => {
let wallet
let wallet;

beforeAll(async () => {
wallet = await startChain()
})
beforeAll(async () => {
wallet = await startChain();
});

test("initial DAI balance of 0", async () => {
const daiContract = new ethers.Contract(
erc20.dai.address,
erc20.dai.abi,
wallet,
)
const daiBalanceWei = await daiContract.balanceOf(wallet.address)
const daiBalance = ethers.utils.formatUnits(daiBalanceWei, 18)
expect(parseFloat(daiBalance)).toBe(0)
})
test("initial DAI balance of 0", async () => {
const daiContract = new ethers.Contract(erc20.dai.address, erc20.dai.abi, wallet);
const daiBalanceWei = await daiContract.balanceOf(wallet.address);
const daiBalance = ethers.utils.formatUnits(daiBalanceWei, 18);
expect(parseFloat(daiBalance)).toBe(0);
});

test("initial ETH balance of 1000 ETH", async () => {
const ethBalanceWei = await wallet.getBalance()
const ethBalance = ethers.utils.formatEther(ethBalanceWei)
expect(parseFloat(ethBalance)).toBe(1000)
})
test("initial ETH balance of 1000 ETH", async () => {
const ethBalanceWei = await wallet.getBalance();
const ethBalance = ethers.utils.formatEther(ethBalanceWei);
expect(parseFloat(ethBalance)).toBe(1000);
});

test("buy DAI from Uniswap", async () => {
// 1. instantiate contracts
const daiContract = new ethers.Contract(
erc20.dai.address,
erc20.dai.abi,
wallet,
)
const uniswapFactoryContract = new ethers.Contract(
uniswap.factory.address,
uniswap.factory.abi,
wallet,
)
const daiExchangeAddress = await uniswapFactoryContract.getExchange(
erc20.dai.address,
)
const daiExchangeContract = new ethers.Contract(
daiExchangeAddress,
uniswap.exchange.abi,
wallet,
)
test("buy DAI from Uniswap", async () => {
// 1. instantiate contracts
const daiContract = new ethers.Contract(erc20.dai.address, erc20.dai.abi, wallet);
const uniswapFactoryContract = new ethers.Contract(uniswap.factory.address, uniswap.factory.abi, wallet);
const daiExchangeAddress = await uniswapFactoryContract.getExchange(erc20.dai.address, {
gasLimit: GAS_LIMIT,
});
const daiExchangeContract = new ethers.Contract(daiExchangeAddress, uniswap.exchange.abi, wallet);

// 2. do the actual swapping
await daiExchangeContract.ethToTokenSwapInput(
1, // min amount of token retrieved
2525644800, // random timestamp in the future (year 2050)
{
gasLimit: 4000000,
value: ethers.utils.parseEther("5"),
},
)
// 2. do the actual swapping
await daiExchangeContract.ethToTokenSwapInput(
1, // min amount of token retrieved
2525644800, // random timestamp in the future (year 2050)
{
gasLimit: GAS_LIMIT,
value: ethers.utils.parseEther("5"),
}
);

// util function
const fromWei = (x) => ethers.utils.formatUnits(x, 18)
// util function
const fromWei = (x) => ethers.utils.formatUnits(x, 18);

// 3. check DAI balance
const daiBalanceWei = await daiContract.balanceOf(wallet.address)
const daiBalance = parseFloat(fromWei(daiBalanceWei))
expect(daiBalance).toBeGreaterThan(0)
console.log("daiBalance", daiBalance)
})
})
// 3. check DAI balance
const daiBalanceWei = await daiContract.balanceOf(wallet.address);
const daiBalance = parseFloat(fromWei(daiBalanceWei));
expect(daiBalance).toBeGreaterThan(0);
console.log("daiBalance", daiBalance);
});
});