diff --git a/scripts/resetHardhat.js b/scripts/resetHardhat.js new file mode 100644 index 0000000..6b712cd --- /dev/null +++ b/scripts/resetHardhat.js @@ -0,0 +1,52 @@ +const axios = require('axios'); +const fs = require("fs"); + +const takeSnapshot = async () => { + try{ + const [remoteHardhatUrl] = process.argv.slice(2); + const {data} = await axios.post(remoteHardhatUrl, { + "jsonrpc":"2.0", + "method":"evm_snapshot", + "params":[], + "id":1 + }) + console.log('snapshotId: ', data.result) + fs.writeFileSync('./tests/snapshot-id.txt', data.result) + console.log('all done') + process.exit(0) + } catch (e) { + console.error(e) + process.exit(1) + } +}; + +const run = async () => { + try{ + const [remoteHardhatUrl] = process.argv.slice(2); + if(!remoteHardhatUrl){ + throw new Error('please include the remote hardhat url as an argument') + } + console.log('remote hardhat url: ', remoteHardhatUrl) + const snapshotId = fs.readFileSync('./tests/snapshot-id.txt', 'utf8') + if (!snapshotId){ + + } + console.log('snapshotId: ', snapshotId) + + const {data} = await axios.post(remoteHardhatUrl, { + "jsonrpc":"2.0", + "method":"evm_revert", + "params":[snapshotId], + "id":1 + }) + + console.log('revert success: ', data.result) + console.log('all done') + process.exit(0) + } catch (e) { + console.error(e) + process.exit(1) + } +}; + +run(); \ No newline at end of file diff --git a/scripts/revertSnapshot.js b/scripts/revertSnapshot.js new file mode 100644 index 0000000..311b0c5 --- /dev/null +++ b/scripts/revertSnapshot.js @@ -0,0 +1,30 @@ +const axios = require('axios'); +const fs = require("fs"); + +const run = async () => { + try{ + const [remoteHardhatUrl] = process.argv.slice(2); + if(!remoteHardhatUrl){ + throw new Error('please include the remote hardhat url as an argument') + } + console.log('remote hardhat url: ', remoteHardhatUrl) + const snapshotId = fs.readFileSync('./tests/snapshot-id.txt', 'utf8') + console.log('snapshotId: ', snapshotId) + + const {data} = await axios.post(remoteHardhatUrl, { + "jsonrpc":"2.0", + "method":"evm_revert", + "params":[snapshotId], + "id":1 + }) + + console.log('revert success: ', data.result) + console.log('all done') + process.exit(0) + } catch (e) { + console.error(e) + process.exit(1) + } +}; + +run(); \ No newline at end of file diff --git a/scripts/takeShanpshot.js b/scripts/takeShanpshot.js new file mode 100644 index 0000000..b0be119 --- /dev/null +++ b/scripts/takeShanpshot.js @@ -0,0 +1,27 @@ +const axios = require('axios'); +const fs = require("fs"); + +const run = async () => { + try{ + const [remoteHardhatUrl] = process.argv.slice(2); + if(!remoteHardhatUrl){ + throw new Error('please include the remote hardhat url as an argument') + } + console.log('remote hardhat url: ', remoteHardhatUrl) + const {data} = await axios.post(remoteHardhatUrl, { + "jsonrpc":"2.0", + "method":"evm_snapshot", + "params":[], + "id":1 + }) + console.log('snapshotId: ', data.result) + fs.writeFileSync('./tests/snapshot-id.txt', data.result) + console.log('all done') + process.exit(0) + } catch (e) { + console.error(e) + process.exit(1) + } +}; + +run(); \ No newline at end of file diff --git a/tests/e2e/specs/_reset-hardhat-spec.js b/tests/e2e/specs/_reset-hardhat-spec.js new file mode 100644 index 0000000..ca0d127 --- /dev/null +++ b/tests/e2e/specs/_reset-hardhat-spec.js @@ -0,0 +1,98 @@ +describe('intial setup', () => { + before('resets hardhat', () => { + //read snapshot + cy.readFile('./tests/snapshot-id.txt') + .then(snapshotId => { + // revert to snapshot + return cy.request("POST", "http://localhost:8545", { + "jsonrpc":"2.0", + "method":"evm_revert", + "params":[snapshotId], + "id":1 + }) + }) + .then(({body})=> { + cy.log("revert success: " + body.result) + }) + + cy.wait(4*1000) + // take new snapshot + cy.request("POST", "http://localhost:8545", { + "jsonrpc":"2.0", + "method":"eth_blockNumber", + "params":[], + "id":1 + }) + .then(({body})=> { + cy.log("eth_blockNumber: " + body.result) + return cy.request("POST", "http://localhost:8545", { + "jsonrpc":"2.0", + "method":"evm_snapshot", + "params":[body.result], + "id":1 + }) + }) + .then(({body})=> { + cy.log("snapshot-id.txt: " + body.result) + // write snapshot to disk + return cy.writeFile('./tests/snapshot-id.txt', body.result) + }) + + cy.log("snapshot done! ") + + cy.request("POST", "http://localhost:8545", { + "jsonrpc":"2.0", + "method":"mine_block", + "params":[], + "id":1 + }) + + cy.request("POST", "http://localhost:8545", { + "jsonrpc":"2.0", + "method":"mine_block", + "params":[], + "id":1 + }) + + cy.request("POST", "http://localhost:8545", { + "jsonrpc":"2.0", + "method":"mine_block", + "params":[], + "id":1 + }) + + cy.request("POST", "http://localhost:8545", { + "jsonrpc":"2.0", + "method":"mine_block", + "params":[], + "id":1 + }) + + cy.request("POST", "http://localhost:8545", { + "jsonrpc":"2.0", + "method":"mine_block", + "params":[], + "id":1 + }) + cy.log("mined 5 blocks") + }) + + it('validates address', () => { + cy.getMetamaskWalletAddress().then(address => { + expect(address).to.equal("0xF994Eba5f3F33EbC5a03d9B5e195765eeb29a923") + }) + }) + + it('validates balance', () => { + cy.resetMetamaskAccount() // fix + cy.visit('/') + cy.contains('Connect').click() + cy.contains('Meta Mask').click() + cy.wait(4 * 1000) + cy.acceptMetamaskAccess() + cy.wait(4 * 1000) + cy.contains("Deposit").click() + cy.wait(4 * 1000) + cy.contains("1111 ETH") + }) +}) \ No newline at end of file