Skip to content

Instantly share code, notes, and snippets.

@xiaoming9090
Created May 25, 2022 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiaoming9090/c4fcd4e967bd7d6940429e5d8e39004d to your computer and use it in GitHub Desktop.
Save xiaoming9090/c4fcd4e967bd7d6940429e5d8e39004d to your computer and use it in GitHub Desktop.
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
async function getOrderBook() {
let DAIInstance = await DAI.deployed();
let WETHInstance = await WETH.deployed();
let rubiconRouterInstance = await RubiconRouter.deployed();
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]));
}
}
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("bath tokens have the right name", async function () {
assert.equal(await bathAssetInstance.symbol(), "bathWETH");
assert.equal(await bathQuoteInstance.symbol(), "bathDAI");
});
it("User can deposit asset funds with custom weights and receive bathTokens", async function () {
await WETHInstance.deposit({
from: accounts[1],
value: web3.utils.toWei((1).toString()),
});
await WETHInstance.approve(
bathAssetInstance.address,
web3.utils.toWei((1).toString()),
{ from: accounts[1] }
);
logIndented(bathAssetInstance.functions);
await bathAssetInstance.methods["deposit(uint256)"](
web3.utils.toWei((1).toString()),
{ from: accounts[1] }
);
assert.equal(
(await bathAssetInstance.balanceOf(accounts[1])).toString(),
web3.utils.toWei((1).toString())
);
});
it("User 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((100).toString()),
{ from: accounts[2] }
);
await bathQuoteInstance.methods["deposit(uint256)"](
web3.utils.toWei((100).toString()),
{
from: accounts[2],
}
);
assert.equal(
(await bathQuoteInstance.balanceOf(accounts[2])).toString(),
web3.utils.toWei((100).toString())
);
});
it("[Debug]", async function () {
const getPoolStatus = async () => {
console.log(
"bathAssetInstance: underlyingBalance() = %s WETH, balanceOf = %s WETH, Outstanding Amount = %s WETH",
web3.utils.fromWei(await bathAssetInstance.underlyingBalance()),
web3.utils.fromWei(await WETHInstance.balanceOf(bathAssetInstance.address)),
web3.utils.fromWei(await bathAssetInstance.outstandingAmount())
);
console.log(
"bathQuoteInstance: underlyingBalance() = %s DAI, balanceOf = %s DAI, Outstanding Amount = %s DAI",
web3.utils.fromWei(await bathQuoteInstance.underlyingBalance()),
web3.utils.fromWei(await DAIInstance.balanceOf(bathQuoteInstance.address)),
web3.utils.fromWei(await bathQuoteInstance.outstandingAmount())
);
};
await getOrderBook();
await getPoolStatus();
const askNumerator = web3.utils.toWei((0.9).toString()); // In WETH - payamt - This will be escrow in OrderBook - Price 200
const askDenominator = web3.utils.toWei((180).toString()); // In DAI - buyamt
const bidNumerator = web3.utils.toWei((90).toString()); // In DAI - payamt - This will be escrow in OrderBook - Price 100
const bidDenominator = web3.utils.toWei((0.9).toString()); // In WETH - buyamt
await bathPairInstance.placeMarketMakingTrades(
[WETHInstance.address, DAIInstance.address],
askNumerator,
askDenominator,
bidNumerator,
bidDenominator
);
console.log("After Placing Order");
await getOrderBook();
await getPoolStatus();
});
});
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment