add tests to ensure getId/setIds all work

This commit is contained in:
Edward Mulraney 2021-06-08 16:57:36 +01:00
parent 5be5cb6866
commit 7588733d98
4 changed files with 1086 additions and 479 deletions

View File

@ -14,7 +14,6 @@ import {
import { Stores } from "../../common/stores.sol"; import { Stores } from "../../common/stores.sol";
import { Helpers } from "./helpers.sol"; import { Helpers } from "./helpers.sol";
import { Events } from "./events.sol"; import { Events } from "./events.sol";
import "hardhat/console.sol";
abstract contract LiquityResolver is Events, Helpers { abstract contract LiquityResolver is Events, Helpers {
BorrowerOperationsLike internal constant borrowerOperations = BorrowerOperationsLike internal constant borrowerOperations =
@ -48,7 +47,7 @@ abstract contract LiquityResolver is Events, Helpers {
* @param borrowAmount The amount of LUSD to borrow * @param borrowAmount The amount of LUSD to borrow
* @param upperHint Address of the Trove near the upper bound of where the user's Trove will now sit in the ordered Trove list * @param upperHint Address of the Trove near the upper bound of where the user's Trove will now sit in the ordered Trove list
* @param lowerHint Address of the Trove near the lower bound of where the user's Trove will now sit in the ordered Trove list * @param lowerHint Address of the Trove near the lower bound of where the user's Trove will now sit in the ordered Trove list
* @param getId Optional storage slot to retrieve ETH instead of receiving it from msg.value * @param getId Optional storage slot to retrieve ETH from
* @param setId Optional storage slot to store the LUSD borrowed against * @param setId Optional storage slot to store the LUSD borrowed against
*/ */
function open( function open(
@ -60,13 +59,6 @@ abstract contract LiquityResolver is Events, Helpers {
uint getId, uint getId,
uint setId uint setId
) external payable returns (string memory _eventName, bytes memory _eventParam) { ) external payable returns (string memory _eventName, bytes memory _eventParam) {
/*
User has three options for depositing ETH collateral to open a Trove:
- Send ETH directly with this function call
- Have ETH collected from a previous spell
- Have ETH collected from the DSA's existing balance
*/
if (getId != 0 && depositAmount != 0) { if (getId != 0 && depositAmount != 0) {
revert("open(): Cannot supply a depositAmount if a non-zero getId is supplied"); revert("open(): Cannot supply a depositAmount if a non-zero getId is supplied");
} }
@ -225,7 +217,6 @@ abstract contract LiquityResolver is Events, Helpers {
if (getRepayId != 0 && repayAmount != 0) { if (getRepayId != 0 && repayAmount != 0) {
revert("adjust(): Cannot supply a repayAmount if a non-zero getRepayId is supplied"); revert("adjust(): Cannot supply a repayAmount if a non-zero getRepayId is supplied");
} }
AdjustTrove memory adjustTrove; AdjustTrove memory adjustTrove;
adjustTrove.maxFeePercentage = maxFeePercentage; adjustTrove.maxFeePercentage = maxFeePercentage;
@ -235,7 +226,7 @@ abstract contract LiquityResolver is Events, Helpers {
adjustTrove.repayAmount = getUint(getRepayId, repayAmount); adjustTrove.repayAmount = getUint(getRepayId, repayAmount);
adjustTrove.isBorrow = borrowAmount > 0; adjustTrove.isBorrow = borrowAmount > 0;
borrowerOperations.adjustTrove{value: depositAmount}( borrowerOperations.adjustTrove{value: adjustTrove.depositAmount}(
adjustTrove.maxFeePercentage, adjustTrove.maxFeePercentage,
adjustTrove.withdrawAmount, adjustTrove.withdrawAmount,
adjustTrove.borrowAmount, adjustTrove.borrowAmount,
@ -288,6 +279,7 @@ abstract contract LiquityResolver is Events, Helpers {
uint setLqtyGainId uint setLqtyGainId
) external returns (string memory _eventName, bytes memory _eventParam) { ) external returns (string memory _eventName, bytes memory _eventParam) {
amount = getUint(getDepositId, amount); amount = getUint(getDepositId, amount);
uint ethGain = stabilityPool.getDepositorETHGain(address(this)); uint ethGain = stabilityPool.getDepositorETHGain(address(this));
uint lqtyGain = stabilityPool.getDepositorLQTYGain(address(this)); uint lqtyGain = stabilityPool.getDepositorLQTYGain(address(this));

View File

@ -60,6 +60,7 @@ const LQTY_TOKEN_ADDRESS = "0x6DEA81C8171D0bA574754EF6F8b412F2Ed88c54D";
const LQTY_TOKEN_ABI = [ const LQTY_TOKEN_ABI = [
"function balanceOf(address account) external view returns (uint256)", "function balanceOf(address account) external view returns (uint256)",
"function transfer(address _to, uint256 _value) public returns (bool success)", "function transfer(address _to, uint256 _value) public returns (bool success)",
"function approve(address spender, uint256 amount) external returns (bool)",
]; ];
const COLL_SURPLUS_ADDRESS = "0x3D32e8b97Ed5881324241Cf03b2DA5E2EBcE5521"; const COLL_SURPLUS_ADDRESS = "0x3D32e8b97Ed5881324241Cf03b2DA5E2EBcE5521";

View File

@ -16,12 +16,16 @@ const connectV2LiquityArtifacts = require("../../artifacts/contracts/mainnet/con
const connectV2BasicV1Artifacts = require("../../artifacts/contracts/mainnet/connectors/basic/main.sol/ConnectV2Basic.json"); const connectV2BasicV1Artifacts = require("../../artifacts/contracts/mainnet/connectors/basic/main.sol/ConnectV2Basic.json");
const { ethers } = require("hardhat"); const { ethers } = require("hardhat");
const CONNECTOR_NAME = "LIQUITY-v1-TEST"; // Instadapp uses a fake address to represent native ETH
const { eth_addr: ETH_ADDRESS } = require("../../scripts/constant/constant");
const LIQUITY_CONNECTOR = "LIQUITY-v1-TEST";
const LUSD_GAS_COMPENSATION = hre.ethers.utils.parseUnits("200", 18); // 200 LUSD gas compensation repaid after loan repayment const LUSD_GAS_COMPENSATION = hre.ethers.utils.parseUnits("200", 18); // 200 LUSD gas compensation repaid after loan repayment
const LIQUIDATABLE_TROVES_BLOCK_NUMBER = 12478159; // Deterministic block number for tests to run against, if you change this, tests will break. const LIQUIDATABLE_TROVES_BLOCK_NUMBER = 12478159; // Deterministic block number for tests to run against, if you change this, tests will break.
const JUSTIN_SUN_ADDRESS = "0x903d12bf2c57a29f32365917c706ce0e1a84cce3"; // LQTY whale address const JUSTIN_SUN_ADDRESS = "0x903d12bf2c57a29f32365917c706ce0e1a84cce3"; // LQTY whale address
const LIQUIDATABLE_TROVE_ADDRESS = "0xafbeb4cb97f3b08ec2fe07ef0dac15d37013a347"; // Trove which is liquidatable at blockNumber: LIQUIDATABLE_TROVES_BLOCK_NUMBER const LIQUIDATABLE_TROVE_ADDRESS = "0xafbeb4cb97f3b08ec2fe07ef0dac15d37013a347"; // Trove which is liquidatable at blockNumber: LIQUIDATABLE_TROVES_BLOCK_NUMBER
const MAX_GAS = hardhatConfig.networks.hardhat.blockGasLimit; // Maximum gas limit (12000000) const MAX_GAS = hardhatConfig.networks.hardhat.blockGasLimit; // Maximum gas limit (12000000)
const INSTADAPP_BASIC_V1_CONNECTOR = "Basic-v1";
const openTroveSpell = async ( const openTroveSpell = async (
dsa, dsa,
@ -38,7 +42,7 @@ const openTroveSpell = async (
} }
const openTroveSpell = { const openTroveSpell = {
connector: CONNECTOR_NAME, connector: LIQUITY_CONNECTOR,
method: "open", method: "open",
args: [ args: [
depositAmount, depositAmount,
@ -60,8 +64,7 @@ const openTroveSpell = async (
const createDsaTrove = async ( const createDsaTrove = async (
dsa, dsa,
signer, signer,
hintHelpers, liquity,
sortedTroves,
depositAmount = hre.ethers.utils.parseEther("5"), depositAmount = hre.ethers.utils.parseEther("5"),
borrowAmount = hre.ethers.utils.parseUnits("2000", 18) borrowAmount = hre.ethers.utils.parseUnits("2000", 18)
) => { ) => {
@ -69,8 +72,7 @@ const createDsaTrove = async (
const { upperHint, lowerHint } = await getTroveInsertionHints( const { upperHint, lowerHint } = await getTroveInsertionHints(
depositAmount, depositAmount,
borrowAmount, borrowAmount,
hintHelpers, liquity
sortedTroves
); );
return await openTroveSpell( return await openTroveSpell(
dsa, dsa,
@ -90,7 +92,9 @@ const sendToken = async (token, amount, from, to) => {
}); });
const signer = await hre.ethers.provider.getSigner(from); const signer = await hre.ethers.provider.getSigner(from);
return await token.connect(signer).transfer(to, amount); return await token.connect(signer).transfer(to, amount, {
gasPrice: 0,
});
}; };
const resetInitialState = async (walletAddress, contracts, isDebug = false) => { const resetInitialState = async (walletAddress, contracts, isDebug = false) => {
@ -138,13 +142,13 @@ const deployAndConnect = async (contracts, isDebug = false) => {
instadappAddresses.core.connectorsV2 instadappAddresses.core.connectorsV2
); );
const connector = await deployAndEnableConnector({ const connector = await deployAndEnableConnector({
connectorName: CONNECTOR_NAME, connectorName: LIQUITY_CONNECTOR,
contractArtifact: connectV2LiquityArtifacts, contractArtifact: connectV2LiquityArtifacts,
signer: masterSigner, signer: masterSigner,
connectors: instaConnectorsV2, connectors: instaConnectorsV2,
}); });
isDebug && isDebug &&
console.log(`${CONNECTOR_NAME} Connector address`, connector.address); console.log(`${LIQUITY_CONNECTOR} Connector address`, connector.address);
const basicConnector = await deployAndEnableConnector({ const basicConnector = await deployAndEnableConnector({
connectorName: "Basic-v1", connectorName: "Basic-v1",
@ -250,28 +254,24 @@ const deployAndConnect = async (contracts, isDebug = false) => {
return liquity; return liquity;
}; };
const getTroveInsertionHints = async ( const getTroveInsertionHints = async (depositAmount, borrowAmount, liquity) => {
depositAmount, const nominalCR = await liquity.hintHelpers.computeNominalCR(
borrowAmount,
hintHelpers,
sortedTroves
) => {
const nominalCR = await hintHelpers.computeNominalCR(
depositAmount, depositAmount,
borrowAmount borrowAmount
); );
const { hintAddress, latestRandomSeed } = await hintHelpers.getApproxHint( const {
nominalCR, hintAddress,
50, latestRandomSeed,
1298379, } = await liquity.hintHelpers.getApproxHint(nominalCR, 50, 1298379, {
{ gasLimit: MAX_GAS,
gasLimit: MAX_GAS, });
}
);
randomSeed = latestRandomSeed; randomSeed = latestRandomSeed;
const { 0: upperHint, 1: lowerHint } = await sortedTroves.findInsertPosition( const {
0: upperHint,
1: lowerHint,
} = await liquity.sortedTroves.findInsertPosition(
nominalCR, nominalCR,
hintAddress, hintAddress,
hintAddress, hintAddress,
@ -288,19 +288,17 @@ const getTroveInsertionHints = async (
let randomSeed = 4223; let randomSeed = 4223;
const getRedemptionHints = async ( const getRedemptionHints = async (amount, liquity) => {
amount, const ethPrice = await liquity.priceFeed.callStatic.fetchPrice();
hintHelpers,
sortedTroves,
priceFeed
) => {
const ethPrice = await priceFeed.callStatic.fetchPrice();
const [ const [
firstRedemptionHint, firstRedemptionHint,
partialRedemptionHintNicr, partialRedemptionHintNicr,
] = await hintHelpers.getRedemptionHints(amount, ethPrice, 0); ] = await liquity.hintHelpers.getRedemptionHints(amount, ethPrice, 0);
const { hintAddress, latestRandomSeed } = await hintHelpers.getApproxHint( const {
hintAddress,
latestRandomSeed,
} = await liquity.hintHelpers.getApproxHint(
partialRedemptionHintNicr, partialRedemptionHintNicr,
50, 50,
randomSeed, randomSeed,
@ -310,7 +308,10 @@ const getRedemptionHints = async (
); );
randomSeed = latestRandomSeed; randomSeed = latestRandomSeed;
const { 0: upperHint, 1: lowerHint } = await sortedTroves.findInsertPosition( const {
0: upperHint,
1: lowerHint,
} = await liquity.sortedTroves.findInsertPosition(
partialRedemptionHintNicr, partialRedemptionHintNicr,
hintAddress, hintAddress,
hintAddress, hintAddress,
@ -334,12 +335,7 @@ const redeem = async (amount, from, wallet, liquity) => {
firstRedemptionHint, firstRedemptionHint,
upperHint, upperHint,
lowerHint, lowerHint,
} = await getRedemptionHints( } = await getRedemptionHints(amount, liquity);
amount,
liquity.hintHelpers,
liquity.sortedTroves,
liquity.priceFeed
);
const maxFeePercentage = ethers.utils.parseUnits("0.5", 18); // 0.5% max fee const maxFeePercentage = ethers.utils.parseUnits("0.5", 18); // 0.5% max fee
return await liquity.troveManager return await liquity.troveManager
@ -366,9 +362,11 @@ module.exports = {
getTroveInsertionHints, getTroveInsertionHints,
getRedemptionHints, getRedemptionHints,
redeem, redeem,
CONNECTOR_NAME, LIQUITY_CONNECTOR,
LUSD_GAS_COMPENSATION, LUSD_GAS_COMPENSATION,
JUSTIN_SUN_ADDRESS, JUSTIN_SUN_ADDRESS,
LIQUIDATABLE_TROVE_ADDRESS, LIQUIDATABLE_TROVE_ADDRESS,
MAX_GAS, MAX_GAS,
INSTADAPP_BASIC_V1_CONNECTOR,
ETH_ADDRESS,
}; };

File diff suppressed because it is too large Load Diff