b.liquity

This commit is contained in:
yaron velner 2021-08-23 13:06:49 +03:00
parent 7ced8a1279
commit ed791eba47
5 changed files with 324 additions and 0 deletions

View File

@ -0,0 +1,22 @@
pragma solidity ^0.7.6;
contract Events {
/* Stability Pool */
event LogStabilityDeposit(
address indexed borrower,
uint amount,
uint lqtyGain,
uint getDepositId,
uint setDepositId,
uint setLqtyGainId
);
event LogStabilityWithdraw(
address indexed borrower,
uint numShares,
uint lqtyGain,
uint getWithdrawId,
uint setWithdrawId,
uint setLqtyGainId
);
}

View File

@ -0,0 +1,18 @@
pragma solidity ^0.7.6;
import { DSMath } from "../../../common/math.sol";
import { Basic } from "../../../common/basic.sol";
import { TokenInterface } from "../../../common/interfaces.sol";
import {
StabilityPoolLike,
BAMMLike
} from "./interface.sol";
abstract contract Helpers is DSMath, Basic {
StabilityPoolLike internal constant stabilityPool = StabilityPoolLike(0x66017D22b0f8556afDd19FC67041899Eb65a21bb);
TokenInterface internal constant lqtyToken = TokenInterface(0x6DEA81C8171D0bA574754EF6F8b412F2Ed88c54D);
TokenInterface internal constant lusdToken = TokenInterface(0x5f98805A4E8be255a32880FDeC7F6728C6568bA0);
BAMMLike internal constant BAMM = BAMMLike(0x0d3AbAA7E088C2c82f54B2f47613DA438ea8C598);
}

View File

@ -0,0 +1,17 @@
pragma solidity ^0.7.6;
interface StabilityPoolLike {
function provideToSP(uint _amount, address _frontEndTag) external;
function withdrawFromSP(uint _amount) external;
function withdrawETHGainToTrove(address _upperHint, address _lowerHint) external;
function getDepositorETHGain(address _depositor) external view returns (uint);
function getDepositorLQTYGain(address _depositor) external view returns (uint);
function getCompoundedLUSDDeposit(address _depositor) external view returns (uint);
}
interface BAMMLike {
function deposit(uint lusdAmount) external;
function withdraw(uint numShares) external;
function balanceOf(address a) external view returns(uint);
function totalSupply() external view returns(uint);
}

View File

@ -0,0 +1,86 @@
pragma solidity ^0.7.6;
/**
* @title B.Liquity.
* @dev Lending & Borrowing.
*/
import {
StabilityPoolLike,
BAMMLike
} from "./interface.sol";
import { Stores } from "../../../common/stores.sol";
import { Helpers } from "./helpers.sol";
import { Events } from "./events.sol";
abstract contract BLiquityResolver is Events, Helpers {
/* Begin: Stability Pool */
/**
* @dev Deposit LUSD into Stability Pool
* @notice Deposit LUSD into Stability Pool
* @param amount Amount of LUSD to deposit into Stability Pool
* @param getDepositId Optional storage slot to retrieve the amount of LUSD from
* @param setDepositId Optional storage slot to store the final amount of LUSD deposited
* @param setLqtyGainId Optional storage slot to store any LQTY gains in
*/
function deposit(
uint amount,
uint getDepositId,
uint setDepositId,
uint setLqtyGainId
) external payable returns (string memory _eventName, bytes memory _eventParam) {
amount = getUint(getDepositId, amount);
amount = amount == uint(-1) ? lusdToken.balanceOf(address(this)) : amount;
uint lqtyBalanceBefore = lqtyToken.balanceOf(address(this));
lusdToken.approve(address(BAMM), amount);
BAMM.deposit(amount);
uint lqtyBalanceAfter = lqtyToken.balanceOf(address(this));
uint lqtyGain = sub(lqtyBalanceAfter, lqtyBalanceBefore);
setUint(setDepositId, amount);
setUint(setLqtyGainId, lqtyGain);
_eventName = "LogStabilityDeposit(address,uint256,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(address(this), amount,lqtyGain, getDepositId, setDepositId, setLqtyGainId);
}
/**
* @dev Withdraw user deposited LUSD from Stability Pool
* @notice Withdraw LUSD from Stability Pool
* @param numShares amount of shares to withdraw from the BAMM
* @param getWithdrawId Optional storage slot to retrieve the amount of LUSD to withdraw from
* @param setWithdrawId Optional storage slot to store the withdrawn LUSD
* @param setLqtyGainId Optional storage slot to store any LQTY gains in
*/
function withdraw(
uint numShares,
uint getWithdrawId,
uint setWithdrawId,
uint setLqtyGainId
) external payable returns (string memory _eventName, bytes memory _eventParam) {
numShares = getUint(getWithdrawId, numShares);
numShares = numShares == uint(-1) ? BAMM.balanceOf(address(this)) : numShares;
uint lqtyBalanceBefore = lqtyToken.balanceOf(address(this));
BAMM.withdraw(numShares);
uint lqtyBalanceAfter = lqtyToken.balanceOf(address(this));
uint lqtyGain = sub(lqtyBalanceAfter, lqtyBalanceBefore);
setUint(setWithdrawId, numShares);
setUint(setLqtyGainId, lqtyGain);
_eventName = "LogStabilityWithdraw(address,uint256,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(address(this), numShares, lqtyGain, getWithdrawId, setWithdrawId, setLqtyGainId);
}
}
contract ConnectV2BLiquity is BLiquityResolver {
string public name = "B.Liquity-v1";
}

View File

@ -0,0 +1,181 @@
const { expect } = require("chai");
const hre = require("hardhat");
const { web3, deployments, waffle, ethers } = hre;
const { provider, deployContract } = waffle
const deployAndEnableConnector = require("../../scripts/deployAndEnableConnector.js")
const buildDSAv2 = require("../../scripts/buildDSAv2")
const encodeSpells = require("../../scripts/encodeSpells.js")
const getMasterSigner = require("../../scripts/getMasterSigner")
const addresses = require("../../scripts/constant/addresses");
const abis = require("../../scripts/constant/abis");
const constants = require("../../scripts/constant/constant");
const tokens = require("../../scripts/constant/tokens");
const connectorLiquityArtifacts = require("../../artifacts/contracts/mainnet/connectors/b.protocol/liquity/main.sol/ConnectV2BLiquity.json")
const LUSD_WHALE = "0x66017D22b0f8556afDd19FC67041899Eb65a21bb" // stability pool
const BAMM_ADDRESS = "0x0d3AbAA7E088C2c82f54B2f47613DA438ea8C598"
describe("B.Liquity", function () {
const connectorName = "B.LIQUITY-TEST-A"
let dsaWallet0;
let dsaWallet1;
let masterSigner;
let instaConnectorsV2;
let connector;
let manager;
let vat;
let lusd;
let bammToken;
let stabilityPool;
const wallets = provider.getWallets()
const [wallet0, wallet1, wallet2, wallet3] = wallets
before(async () => {
masterSigner = await getMasterSigner(wallet3)
instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2);
connector = await deployAndEnableConnector({
connectorName,
contractArtifact: connectorLiquityArtifacts,
signer: masterSigner,
connectors: instaConnectorsV2
})
lusd = await ethers.getContractAt("../artifacts/contracts/mainnet/common/interfaces.sol:TokenInterface", "0x5f98805A4E8be255a32880FDeC7F6728C6568bA0")
bammToken = await ethers.getContractAt("../artifacts/contracts/mainnet/connectors/b.protocol/liquity/interface.sol:BAMMLike", BAMM_ADDRESS)
stabilityPool = await ethers.getContractAt("../artifacts/contracts/mainnet/connectors/b.protocol/liquity/interface.sol:StabilityPoolLike", "0x66017D22b0f8556afDd19FC67041899Eb65a21bb")
console.log("Connector address", connector.address)
})
it("test veryClose.", async function () {
expect(veryClose(1000001, 1000000)).to.be.true
expect(veryClose(1000000, 1000001)).to.be.true
expect(veryClose(1003000, 1000001)).to.be.false
expect(veryClose(1000001, 1000300)).to.be.false
});
it("Should have contracts deployed.", async function () {
expect(!!instaConnectorsV2.address).to.be.true;
expect(!!connector.address).to.be.true;
expect(!!masterSigner.address).to.be.true;
expect(await connector.name()).to.be.equal("B.Liquity-v1");
});
describe("DSA wallet setup", function () {
it("Should build DSA v2", async function () {
dsaWallet0 = await buildDSAv2(wallet0.address)
expect(!!dsaWallet0.address).to.be.true;
dsaWallet1 = await buildDSAv2(wallet1.address)
expect(!!dsaWallet1.address).to.be.true;
});
it("Deposit LUSD into DSA wallet", async function () {
await hre.network.provider.request({
method: "hardhat_impersonateAccount",
params: [LUSD_WHALE],
});
const signer = await hre.ethers.provider.getSigner(LUSD_WHALE);
await lusd.connect(signer).transfer(dsaWallet0.address, ethers.utils.parseEther("100000"))
expect(await lusd.balanceOf(dsaWallet0.address)).to.equal(ethers.utils.parseEther("100000"));
});
});
describe("Main", function () {
it("should deposit 10k LUSD", async function () {
const totalSupplyBefore = await bammToken.totalSupply();
const lusdBalanceBefore = await stabilityPool.getCompoundedLUSDDeposit(BAMM_ADDRESS);
const amount = ethers.utils.parseEther("10000");
const spells = [
{
connector: connectorName,
method: "deposit",
args: [amount, 0, 0, 0]
}
]
const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address)
const receipt = await tx.wait()
const expectedBalance = totalSupplyBefore.mul(amount).div(lusdBalanceBefore)
expect(veryClose(expectedBalance, await bammToken.balanceOf(dsaWallet0.address))).to.be.true
});
it("should deposit all LUSD", async function () {
const totalSupplyBefore = await bammToken.totalSupply();
const lusdBalanceBefore = await stabilityPool.getCompoundedLUSDDeposit(BAMM_ADDRESS);
const amount = web3.utils.toBN("2").pow(web3.utils.toBN("256")).sub(web3.utils.toBN("1"));
const balanceBefore = await bammToken.balanceOf(dsaWallet0.address)
const spells = [
{
connector: connectorName,
method: "deposit",
args: [amount, 0, 0, 0]
}
]
const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address)
const receipt = await tx.wait()
const expectedBalance = (totalSupplyBefore.mul(ethers.utils.parseEther("90000")).div(lusdBalanceBefore)).add(balanceBefore)
expect(veryClose(expectedBalance, await bammToken.balanceOf(dsaWallet0.address))).to.be.true
});
it("should withdraw half of the shares", async function () {
const balanceBefore = await bammToken.balanceOf(dsaWallet0.address)
const halfBalance = balanceBefore.div("2")
const spells = [
{
connector: connectorName,
method: "withdraw",
args: [halfBalance, 0, 0, 0]
}
]
const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address)
const receipt = await tx.wait()
expect(veryClose(halfBalance, await bammToken.balanceOf(dsaWallet0.address))).to.be.true
expect(veryClose(ethers.utils.parseEther("50000"), await lusd.balanceOf(dsaWallet0.address))).to.be.true
});
it("should withdraw all the shares", async function () {
const amount = web3.utils.toBN("2").pow(web3.utils.toBN("256")).sub(web3.utils.toBN("1"));
const spells = [
{
connector: connectorName,
method: "withdraw",
args: [amount, 0, 0, 0]
}
]
const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address)
const receipt = await tx.wait()
expect(veryClose(ethers.utils.parseEther("100000"), await lusd.balanceOf(dsaWallet0.address))).to.be.true
});
})
})
function veryClose(n1, n2) {
n1 = web3.utils.toBN(n1)
n2 = web3.utils.toBN(n2)
_10000 = web3.utils.toBN(10000)
_9999 = web3.utils.toBN(9999)
if(n1.mul(_10000).lt(n2.mul(_9999))) return false
if(n2.mul(_10000).lt(n1.mul(_9999))) return false
return true
}