-
-
Save xiaoming9090/f26e0d3ea365edc89b257d6c0aa54ac9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const BathHouse = artifacts.require("BathHouse"); | |
const BathPair = artifacts.require("BathPair"); | |
const BathToken = artifacts.require("BathToken"); | |
const RubiconMarket = artifacts.require("RubiconMarket"); | |
const DAI = artifacts.require("TokenWithFaucet"); | |
const WETH = artifacts.require("WETH9"); | |
const RubiconRouter = artifacts.require("RubiconRouter"); | |
const TokenWithFaucet = artifacts.require("TokenWithFaucet"); | |
const { parseUnits } = require("ethers/lib/utils.js"); | |
const helper = require("../testHelpers/timeHelper.js"); | |
function logIndented(...args) { | |
console.log(" ", ...args); | |
} | |
// ganache-cli --gasLimit=0x1fffffffffffff --gasPrice=0x1 --allowUnlimitedContractSize --defaultBalanceEther 9000 | |
// ganache-cli --gasLimit=9000000 --gasPrice=0x1 --defaultBalanceEther 9000 --allowUnlimitedContractSize | |
contract( | |
"Rubicon Exchange and Pools Original Tests", | |
async function (accounts) { | |
let bathPairInstance; | |
let bathAssetInstance; | |
let bathQuoteInstance; | |
let bathHouseInstance; | |
let bathTokenImplementation; | |
let rubiconRouterInstance; | |
describe("Deployment", async function () { | |
it("is deployed", async function () { | |
rubiconMarketInstance = await RubiconMarket.deployed(); | |
bathHouseInstance = await BathHouse.deployed(); | |
DAIInstance = await DAI.deployed(); | |
WETHInstance = await WETH.deployed(); | |
bathPairInstance = await BathPair.deployed(); | |
bathTokenImplementation = await BathToken.new(); | |
rubiconRouterInstance = await RubiconRouter.deployed(); | |
}); | |
}); | |
describe("Bath House Initialization of Bath Pair and Bath Tokens", async function () { | |
it("Bath House is deployed and initialized", async function () { | |
// Call initialize on Bath house | |
return await bathHouseInstance.initialize( | |
rubiconMarketInstance.address, | |
80, | |
10, | |
bathTokenImplementation.address, | |
accounts[9] // Proxy admin | |
// 20 | |
); | |
}); | |
it("Bath Token for asset is deployed and initialized", async function () { | |
await bathHouseInstance.createBathToken( | |
WETHInstance.address, | |
accounts[0] | |
); | |
let newBathTokenAddr = await bathHouseInstance.tokenToBathToken( | |
WETHInstance.address | |
); | |
logIndented("new bathWETH!", newBathTokenAddr.toString()); | |
bathAssetInstance = await BathToken.at(newBathTokenAddr); | |
assert.equal( | |
await bathAssetInstance.RubiconMarketAddress(), | |
rubiconMarketInstance.address | |
); | |
}); | |
it("Bath Token for quote is deployed and initialized", async function () { | |
await bathHouseInstance.createBathToken( | |
DAIInstance.address, | |
accounts[0] | |
); | |
let newBathTokenAddr = await bathHouseInstance.tokenToBathToken( | |
DAIInstance.address | |
); | |
// logIndented("new addr!",await bathHouseInstance.tokenToBathToken(WETHInstance.address)); | |
bathQuoteInstance = await BathToken.at(newBathTokenAddr); | |
assert.equal( | |
await bathQuoteInstance.RubiconMarketAddress(), | |
rubiconMarketInstance.address | |
); | |
}); | |
// Now is initialized from the BathHouse itself | |
it("Bath Pair is deployed and initialized w/ BathHouse", async function () { | |
await bathHouseInstance.initBathPair(bathPairInstance.address, 500, -5); // 90% reserve ratio and 3 days cancel delay | |
livePair = await bathHouseInstance.approvedPairContract(); | |
assert.equal(livePair.toString(), bathPairInstance.address); | |
}); | |
it("can correctly spawn bathWETH and bathDAI", async function () { | |
let assetName = await bathAssetInstance.symbol(); | |
let quoteName = await bathQuoteInstance.symbol(); | |
assert.equal(assetName, "bathWETH"); | |
assert.equal(quoteName, "bathDAI"); | |
}); | |
it("Alice can deposit asset funds with custom weights and receive bathTokens", async function () { | |
await WETHInstance.deposit({ | |
from: accounts[1], | |
value: web3.utils.toWei((50).toString()), | |
}); | |
await WETHInstance.approve( | |
bathAssetInstance.address, | |
web3.utils.toWei((50).toString()), | |
{ from: accounts[1] } | |
); | |
logIndented(bathAssetInstance.functions); | |
await bathAssetInstance.methods["deposit(uint256)"]( | |
web3.utils.toWei((50).toString()), | |
{ from: accounts[1] } | |
); | |
console.log("bathAssetInstance.totalSupply = %s WETH", web3.utils.fromWei(await bathAssetInstance.totalSupply())); | |
assert.equal( | |
(await bathAssetInstance.balanceOf(accounts[1])).toString(), | |
web3.utils.toWei((50).toString()) | |
); | |
}); | |
it("Alice can deposit quote funds with custom weights and receive bathTokens", async function () { | |
// Faucets 1000 | |
await DAIInstance.faucet({ from: accounts[1] }); | |
await DAIInstance.approve( | |
bathQuoteInstance.address, | |
web3.utils.toWei((50).toString()), | |
{ from: accounts[1] } | |
); | |
await bathQuoteInstance.methods["deposit(uint256)"]( | |
web3.utils.toWei((50).toString()), | |
{ | |
from: accounts[1], | |
} | |
); | |
console.log("bathQuoteInstance.totalSupply = %s DAI", web3.utils.fromWei(await bathQuoteInstance.totalSupply())); | |
assert.equal( | |
(await bathQuoteInstance.balanceOf(accounts[1])).toString(), | |
web3.utils.toWei((50).toString()) | |
); | |
}); | |
it("Bob can deposit asset funds with custom weights and receive bathTokens", async function () { | |
await WETHInstance.deposit({ | |
from: accounts[2], | |
value: web3.utils.toWei((50).toString()), | |
}); | |
await WETHInstance.approve( | |
bathAssetInstance.address, | |
web3.utils.toWei((50).toString()), | |
{ from: accounts[2] } | |
); | |
logIndented(bathAssetInstance.functions); | |
await bathAssetInstance.methods["deposit(uint256)"]( | |
web3.utils.toWei((50).toString()), | |
{ from: accounts[2] } | |
); | |
console.log("bathAssetInstance.totalSupply = %s WETH", web3.utils.fromWei(await bathAssetInstance.totalSupply())); | |
assert.equal( | |
(await bathAssetInstance.balanceOf(accounts[2])).toString(), | |
web3.utils.toWei((50).toString()) | |
); | |
}); | |
it("Bob can deposit quote funds with custom weights and receive bathTokens", async function () { | |
// Faucets 1000 | |
await DAIInstance.faucet({ from: accounts[2] }); | |
await DAIInstance.approve( | |
bathQuoteInstance.address, | |
web3.utils.toWei((50).toString()), | |
{ from: accounts[2] } | |
); | |
await bathQuoteInstance.methods["deposit(uint256)"]( | |
web3.utils.toWei((50).toString()), | |
{ | |
from: accounts[2], | |
} | |
); | |
console.log("bathQuoteInstance.totalSupply = %s DAI", web3.utils.fromWei(await bathQuoteInstance.totalSupply())); | |
assert.equal( | |
(await bathQuoteInstance.balanceOf(accounts[2])).toString(), | |
web3.utils.toWei((50).toString()) | |
); | |
}); | |
it("[Debug]", async function () { | |
const getOrderBook = async () => { | |
let output = await rubiconRouterInstance.getBookFromPair( | |
WETHInstance.address, | |
DAIInstance.address, | |
4 | |
); | |
let asks = output[0]; | |
let bids = output[1]; | |
console.log("--------------- Order Book ---------------") | |
for (let i = 0; i < output[0].length; i++) { | |
console.log("[-] asks index %s: ask_pay_amt = %s, ask_buy_amt = %s", i, web3.utils.fromWei(output[0][i][0]), web3.utils.fromWei(output[0][i][1])); | |
} | |
for (let i = 0; i < output[1].length; i++) { | |
console.log("[+] bids index %s: bid_pay_amt = %s, bid_buy_amt = %s", i, web3.utils.fromWei(output[1][i][0]), web3.utils.fromWei(output[1][i][1])); | |
} | |
}; | |
const getPoolStatus = async () => { | |
console.log( | |
"bathAssetInstance: underlyingBalance() = %s WETH, balanceOf = %s WETH, Outstanding Amount = %s WETH | %s DAI pending rebalance", | |
web3.utils.fromWei(await bathAssetInstance.underlyingBalance()), | |
web3.utils.fromWei(await WETHInstance.balanceOf(bathAssetInstance.address)), | |
web3.utils.fromWei(await bathAssetInstance.outstandingAmount()), | |
web3.utils.fromWei(await DAIInstance.balanceOf(bathAssetInstance.address)), | |
); | |
console.log( | |
"bathQuoteInstance: underlyingBalance() = %s DAI, balanceOf = %s DAI, Outstanding Amount = %s DAI | %s WETH pending rebalance", | |
web3.utils.fromWei(await bathQuoteInstance.underlyingBalance()), | |
web3.utils.fromWei(await DAIInstance.balanceOf(bathQuoteInstance.address)), | |
web3.utils.fromWei(await bathQuoteInstance.outstandingAmount()), | |
web3.utils.fromWei(await WETHInstance.balanceOf(bathQuoteInstance.address)), | |
); | |
console.log( | |
"Order Book Escrowed %s WETH and %s DAI", | |
web3.utils.fromWei(await WETHInstance.balanceOf(rubiconMarketInstance.address)), | |
web3.utils.fromWei(await DAIInstance.balanceOf(rubiconMarketInstance.address)), | |
) | |
}; | |
await getOrderBook(); | |
await getPoolStatus(); | |
const askNumerator = web3.utils.toWei((40).toString()); // In WETH - payamt - This will be escrow in OrderBook - Price 20 | |
const askDenominator = web3.utils.toWei((800).toString()); // In DAI - buyamt | |
const bidNumerator = web3.utils.toWei((40).toString()); // In DAI - payamt - This will be escrow in OrderBook - Price 10 | |
const bidDenominator = web3.utils.toWei((4).toString()); // In WETH - buyamt | |
await bathPairInstance.placeMarketMakingTrades( | |
[WETHInstance.address, DAIInstance.address], | |
askNumerator, | |
askDenominator, | |
bidNumerator, | |
bidDenominator | |
); | |
console.log("After Placing Order"); | |
await getOrderBook(); | |
await getPoolStatus(); | |
let bestAskID = ( | |
await rubiconMarketInstance.getBestOffer( | |
WETHInstance.address, | |
DAIInstance.address | |
) | |
).toNumber(); | |
console.log("Get Best Ask Order = %s", bestAskID); | |
// Charles The Buyer | |
await DAIInstance.faucet({ from: accounts[3] }); | |
await DAIInstance.approve( | |
rubiconMarketInstance.address, | |
web3.utils.toWei((1000).toString()), | |
{ from: accounts[3] } | |
); | |
await rubiconMarketInstance.buy(bestAskID, web3.utils.toWei((40).toString()), { | |
from: accounts[3], | |
}); | |
console.log("After Charles brought the best ask order") | |
await getOrderBook(); | |
await getPoolStatus(); | |
console.log("Strategist scrub his trade order"); | |
await bathPairInstance.scrubStrategistTrade(1); | |
await getOrderBook(); | |
await getPoolStatus(); | |
console.log( | |
"Alice (accounts[1]) has %s bathWETH and %s bathDAI", | |
web3.utils.fromWei(await bathAssetInstance.balanceOf(accounts[1])), | |
web3.utils.fromWei(await bathQuoteInstance.balanceOf(accounts[1])) | |
) | |
console.log( | |
"Alice (accounts[1]) has %s WETH and %s DAI", | |
web3.utils.fromWei(await WETHInstance.balanceOf(accounts[1])), | |
web3.utils.fromWei(await DAIInstance.balanceOf(accounts[1])), | |
) | |
/* | |
console.log( | |
"Bob (accounts[2]) has %s bathWETH and %s bathDAI", | |
web3.utils.fromWei(await bathAssetInstance.balanceOf(accounts[2])), | |
web3.utils.fromWei(await bathQuoteInstance.balanceOf(accounts[2])) | |
) | |
*/ | |
console.log("Alice Attempting to withdraw all %s bathWETH token", web3.utils.fromWei(await bathAssetInstance.balanceOf(accounts[1]))) | |
await bathAssetInstance.withdraw(await bathAssetInstance.balanceOf(accounts[1]), { | |
from: accounts[1], | |
}); | |
console.log("Alice Succeed in withdrawing all bathWETH token") | |
console.log( | |
"Alice (accounts[1]) has %s WETH and %s DAI", | |
web3.utils.fromWei(await WETHInstance.balanceOf(accounts[1])), | |
web3.utils.fromWei(await DAIInstance.balanceOf(accounts[1])), | |
) | |
}); | |
}); | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment