Turn setup-swap into basic module; add task for it

This commit is contained in:
Oskar Thoren 2021-02-08 21:22:16 +08:00
parent db40672991
commit feb07ef01f
3 changed files with 75 additions and 58 deletions

View File

@ -2,6 +2,8 @@ require("@nomiclabs/hardhat-web3");
require("@nomiclabs/hardhat-waffle");
let swap = require("./src/setup-swap");
// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async () => {
@ -21,6 +23,15 @@ task("balance", "Prints an account's balance")
console.log(web3.utils.fromWei(balance, "ether"), "ETH");
});
task("setupSwap", "Setup Swap")
.setAction(async taskArgs => {
await swap.setupSwap();
// TODO Should return JSON
console.log("NYI")
//console.log(web3.utils.fromWei(balance, "ether"), "ETH");
});
// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more

View File

@ -1,64 +1,8 @@
const hre = require("hardhat");
let swap = require("../src/setup-swap");
async function main() {
// Basic setup and definitions
var signers = await ethers.getSigners();
var aliceSigner = signers[0];
var bobSigner = signers[0];
var aliceAddress = signers[0].address;
var bobAddress = signers[1].address;
var DEFAULT_HARDDEPOSIT_DECREASE_TIMEOUT = ethers.BigNumber.from(86400)
// Deploy ERC20 Token
const ERC20 = await hre.ethers.getContractFactory("ERC20PresetMinterPauser");
var erc20 = await ERC20.deploy("TestToken", "TEST");
await erc20.deployed();
// Deploy SwapFactory
const SimpleSwapFactory = await hre.ethers.getContractFactory("SimpleSwapFactory");
var simpleSwapFactory = await SimpleSwapFactory.deploy(erc20.address);
await simpleSwapFactory.deployed();
// Setup Swap contract interface to parse logs
var swapFactoryArtifact = await artifacts.readArtifact("SimpleSwapFactory");
var swapFactoryInterface = new ethers.utils.Interface(swapFactoryArtifact.abi);
// Deploy Alice Swap and get address
var resp = await simpleSwapFactory.deploySimpleSwap(aliceAddress, DEFAULT_HARDDEPOSIT_DECREASE_TIMEOUT)
var logs = await ethers.provider.getLogs({address: simpleSwapFactory.address});
var aliceSwapAddress = swapFactoryInterface.parseLog(logs[0]).args.contractAddress;
// Deploy Bob Swap and get address
var resp = await simpleSwapFactory.deploySimpleSwap(bobAddress, DEFAULT_HARDDEPOSIT_DECREASE_TIMEOUT)
var logs = await ethers.provider.getLogs({address: simpleSwapFactory.address});
var bobSwapAddress = swapFactoryInterface.parseLog(logs[0]).args.contractAddress;
// Mint ERC20 tokens
// Alice is default account so issuer, which means she can mint tokens
// XXX Difference with getContractFactory above?
var erc20artifact = await artifacts.readArtifact("ERC20PresetMinterPauser");
var erc20contract = new ethers.Contract(erc20.address, erc20artifact.abi, aliceSigner);
await erc20contract.mint(aliceAddress, 10000);
await erc20contract.mint(bobAddress, 10000);
// Get ERC20 Balance
var aliceBalance = (await erc20contract.balanceOf(aliceAddress)).toNumber();
var bobBalance = (await erc20contract.balanceOf(bobAddress)).toNumber();
// Transfer money to Alice Swap address
await erc20contract.transfer(aliceSwapAddress, 5000);
var aliceSwapBalance = (await erc20contract.balanceOf(aliceSwapAddress)).toNumber();
// Print stuff
console.log("ERC20:", erc20.address);
console.log("SimpleSwapFactory:", simpleSwapFactory.address);
console.log("Alice address:", aliceAddress);
console.log("Bob address:", bobAddress);
console.log("AliceSwapAddress:", aliceSwapAddress)
console.log("BobSwapAddress:", bobSwapAddress)
console.log("Alice ERC20 balance:", aliceBalance);
console.log("Bob ERC20 balance:", bobBalance);
console.log("Alice Swap balance:", aliceSwapBalance);
await swap.setupSwap();
}
// We recommend this pattern to be able to use async/await everywhere

62
src/setup-swap.js Normal file
View File

@ -0,0 +1,62 @@
async function setupSwap() {
// Basic setup and definitions
var signers = await ethers.getSigners();
var aliceSigner = signers[0];
var bobSigner = signers[0];
var aliceAddress = signers[0].address;
var bobAddress = signers[1].address;
var DEFAULT_HARDDEPOSIT_DECREASE_TIMEOUT = ethers.BigNumber.from(86400)
// Deploy ERC20 Token
const ERC20 = await hre.ethers.getContractFactory("ERC20PresetMinterPauser");
var erc20 = await ERC20.deploy("TestToken", "TEST");
await erc20.deployed();
// Deploy SwapFactory
const SimpleSwapFactory = await hre.ethers.getContractFactory("SimpleSwapFactory");
var simpleSwapFactory = await SimpleSwapFactory.deploy(erc20.address);
await simpleSwapFactory.deployed();
// Setup Swap contract interface to parse logs
var swapFactoryArtifact = await artifacts.readArtifact("SimpleSwapFactory");
var swapFactoryInterface = new ethers.utils.Interface(swapFactoryArtifact.abi);
// Deploy Alice Swap and get address
var resp = await simpleSwapFactory.deploySimpleSwap(aliceAddress, DEFAULT_HARDDEPOSIT_DECREASE_TIMEOUT)
var logs = await ethers.provider.getLogs({address: simpleSwapFactory.address});
var aliceSwapAddress = swapFactoryInterface.parseLog(logs[0]).args.contractAddress;
// Deploy Bob Swap and get address
var resp = await simpleSwapFactory.deploySimpleSwap(bobAddress, DEFAULT_HARDDEPOSIT_DECREASE_TIMEOUT)
var logs = await ethers.provider.getLogs({address: simpleSwapFactory.address});
var bobSwapAddress = swapFactoryInterface.parseLog(logs[0]).args.contractAddress;
// Mint ERC20 tokens
// Alice is default account so issuer, which means she can mint tokens
// XXX Difference with getContractFactory above?
var erc20artifact = await artifacts.readArtifact("ERC20PresetMinterPauser");
var erc20contract = new ethers.Contract(erc20.address, erc20artifact.abi, aliceSigner);
await erc20contract.mint(aliceAddress, 10000);
await erc20contract.mint(bobAddress, 10000);
// Get ERC20 Balance
var aliceBalance = (await erc20contract.balanceOf(aliceAddress)).toNumber();
var bobBalance = (await erc20contract.balanceOf(bobAddress)).toNumber();
// Transfer money to Alice Swap address
await erc20contract.transfer(aliceSwapAddress, 5000);
var aliceSwapBalance = (await erc20contract.balanceOf(aliceSwapAddress)).toNumber();
// Print stuff
console.log("ERC20:", erc20.address);
console.log("SimpleSwapFactory:", simpleSwapFactory.address);
console.log("Alice address:", aliceAddress);
console.log("Bob address:", bobAddress);
console.log("AliceSwapAddress:", aliceSwapAddress)
console.log("BobSwapAddress:", bobSwapAddress)
console.log("Alice ERC20 balance:", aliceBalance);
console.log("Bob ERC20 balance:", bobBalance);
// console.log("Alice Swap balance:", aliceSwapBalance);
}
module.exports.setupSwap = setupSwap;