Skip to content

Commit

Permalink
feat: eth collateral
Browse files Browse the repository at this point in the history
  • Loading branch information
Macket committed Jun 14, 2023
1 parent 643649a commit f4135a0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
13 changes: 8 additions & 5 deletions src/crvusd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Crvusd implements Icrvusd {
NETWORK_NAME: "ethereum",
FACTORY: string,
PEG_KEEPERS: string[],
WETH: string,
};

constructor() {
Expand All @@ -52,13 +53,14 @@ class Crvusd implements Icrvusd {
COINS: {},
DECIMALS: {},
NETWORK_NAME: "ethereum",
FACTORY: "0xC9332fdCB1C491Dcc683bAe86Fe3cb70360738BC",
FACTORY: "0xC9332fdCB1C491Dcc683bAe86Fe3cb70360738BC".toLowerCase(),
PEG_KEEPERS: [
'0xaA346781dDD7009caa644A4980f044C50cD2ae22'.toLowerCase(),
'0xE7cd2b4EB1d98CD6a4A48B6071D46401Ac7DC5C8'.toLowerCase(),
'0x6B765d07cf966c745B340AdCa67749fE75B5c345'.toLowerCase(),
'0x1ef89Ed0eDd93D1EC09E4c07373f69C49f4dcCae'.toLowerCase(),
],
WETH: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2".toLowerCase(),
};
}

Expand Down Expand Up @@ -153,7 +155,7 @@ class Crvusd implements Icrvusd {
for (let i = N1; i < N2; i++) {
calls.push(factoryMulticallContract.collaterals(i));
}
const collaterals: string[] = await this.multicallProvider.all(calls);
const collaterals: string[] = (await this.multicallProvider.all(calls) as string[]).map((c) => c.toLowerCase());

if (collaterals.length > 0) {
for (const collateral of collaterals) this.setContract(collateral, ERC20ABI);
Expand All @@ -173,17 +175,18 @@ class Crvusd implements Icrvusd {
});

for (const collateral_address of collaterals) {
const is_eth = collateral_address === this.constants.WETH;
const [collateral_symbol, collateral_decimals, amm_address, controller_address] = res.splice(0, 4) as [string, number, string, string];
this.setContract(amm_address, llammaABI);
this.setContract(controller_address, controllerABI);
const monetary_policy_address = await this.contracts[controller_address].contract.monetary_policy(this.constantOptions);
this.setContract(monetary_policy_address, MonetaryPolicy2ABI);
this.constants.LLAMMAS[collateral_symbol.toLowerCase()] = {
this.constants.LLAMMAS[is_eth ? "eth" : collateral_symbol.toLowerCase()] = {
amm_address,
controller_address,
monetary_policy_address,
collateral_address,
collateral_symbol,
collateral_address: is_eth ? "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" : collateral_address,
collateral_symbol: is_eth ? "ETH" : collateral_symbol,
collateral_decimals,
min_bands: 4,
max_bands: 50,
Expand Down
33 changes: 17 additions & 16 deletions src/llammas/LlammaTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,12 +667,13 @@ export class LlammaTemplate {
const _collateral = parseUnits(collateral, this.collateralDecimals);
const _debt = parseUnits(debt);
const contract = crvusd.contracts[this.controller].contract;
const gas = await contract.estimateGas.create_loan(_collateral, _debt, range, crvusd.constantOptions);
const value = isEth(this.collateral) ? _collateral : crvusd.parseUnits("0");
const gas = await contract.estimateGas.create_loan(_collateral, _debt, range, { ...crvusd.constantOptions, value });
if (estimateGas) return gas.toNumber();

await crvusd.updateFeeData();
const gasLimit = gas.mul(130).div(100);
return (await contract.create_loan(_collateral, _debt, range, { ...crvusd.options, gasLimit })).hash
return (await contract.create_loan(_collateral, _debt, range, { ...crvusd.options, gasLimit, value })).hash
}

public async createLoanEstimateGas(collateral: number | string, debt: number | string, range: number): Promise<number> {
Expand Down Expand Up @@ -756,12 +757,13 @@ export class LlammaTemplate {
const _collateral = parseUnits(collateral, this.collateralDecimals);
const _debt = parseUnits(debt);
const contract = crvusd.contracts[this.controller].contract;
const gas = await contract.estimateGas.borrow_more(_collateral, _debt, crvusd.constantOptions);
const value = isEth(this.collateral) ? _collateral : crvusd.parseUnits("0");
const gas = await contract.estimateGas.borrow_more(_collateral, _debt, { ...crvusd.constantOptions, value });
if (estimateGas) return gas.toNumber();

await crvusd.updateFeeData();
const gasLimit = gas.mul(130).div(100);
return (await contract.borrow_more(_collateral, _debt, { ...crvusd.options, gasLimit })).hash
return (await contract.borrow_more(_collateral, _debt, { ...crvusd.options, gasLimit, value })).hash
}

public async borrowMoreEstimateGas(collateral: number | string, debt: number | string): Promise<number> {
Expand Down Expand Up @@ -831,12 +833,13 @@ export class LlammaTemplate {

const _collateral = parseUnits(collateral, this.collateralDecimals);
const contract = crvusd.contracts[this.controller].contract;
const gas = await contract.estimateGas.add_collateral(_collateral, address, crvusd.constantOptions);
const value = isEth(this.collateral) ? _collateral : crvusd.parseUnits("0");
const gas = await contract.estimateGas.add_collateral(_collateral, address, { ...crvusd.constantOptions, value });
if (estimateGas) return gas.toNumber();

await crvusd.updateFeeData();
const gasLimit = gas.mul(130).div(100);
return (await contract.add_collateral(_collateral, address, { ...crvusd.options, gasLimit })).hash
return (await contract.add_collateral(_collateral, address, { ...crvusd.options, gasLimit, value })).hash
}

public async addCollateralEstimateGas(collateral: number | string, address = ""): Promise<number> {
Expand Down Expand Up @@ -903,12 +906,12 @@ export class LlammaTemplate {

const _collateral = parseUnits(collateral, this.collateralDecimals);
const contract = crvusd.contracts[this.controller].contract;
const gas = await contract.estimateGas.remove_collateral(_collateral, false, crvusd.constantOptions);
const gas = await contract.estimateGas.remove_collateral(_collateral, isEth(this.collateral), crvusd.constantOptions);
if (estimateGas) return gas.toNumber();

await crvusd.updateFeeData();
const gasLimit = gas.mul(130).div(100);
return (await contract.remove_collateral(_collateral, false, { ...crvusd.options, gasLimit })).hash
return (await contract.remove_collateral(_collateral, isEth(this.collateral), { ...crvusd.options, gasLimit })).hash
}

public async removeCollateralEstimateGas(collateral: number | string): Promise<number> {
Expand Down Expand Up @@ -978,12 +981,12 @@ export class LlammaTemplate {
const [_, n1] = await this.userBands(address);
const { stablecoin } = await this.userState(address);
const n = (BN(stablecoin).gt(0)) ? MAX_ACTIVE_BAND : n1 - 1; // In liquidation mode it doesn't matter if active band moves
const gas = await contract.estimateGas.repay(_debt, address, n, false, crvusd.constantOptions);
const gas = await contract.estimateGas.repay(_debt, address, n, isEth(this.collateral), crvusd.constantOptions);
if (estimateGas) return gas.toNumber();

await crvusd.updateFeeData();
const gasLimit = gas.mul(130).div(100);
return (await contract.repay(_debt, address, n, false, { ...crvusd.options, gasLimit })).hash
return (await contract.repay(_debt, address, n, isEth(this.collateral), { ...crvusd.options, gasLimit })).hash
}

public async repayEstimateGas(debt: number | string, address = ""): Promise<number> {
Expand Down Expand Up @@ -1124,20 +1127,18 @@ export class LlammaTemplate {
private async _swap(i: number, j: number, amount: number | string, slippage: number, estimateGas: boolean): Promise<string | number> {
if (!(i === 0 && j === 1) && !(i === 1 && j === 0)) throw Error("Wrong index");

const inCoinAddress = this.coinAddresses[i];
const [inDecimals, outDecimals] = [this.coinDecimals[i], this.coinDecimals[j]];
const _amount = parseUnits(amount, inDecimals);
const _expected = await this._swapExpected(i, j, _amount);
const minRecvAmountBN: BigNumber = toBN(_expected, outDecimals).times(100 - slippage).div(100);
const _minRecvAmount = fromBN(minRecvAmountBN);
const value = isEth(inCoinAddress) ? _amount : ethers.BigNumber.from(0);
const contract = crvusd.contracts[this.address].contract;
const gas = await contract.estimateGas.exchange(i, j, _amount, _minRecvAmount, { ...crvusd.constantOptions, value });
const gas = await contract.estimateGas.exchange(i, j, _amount, _minRecvAmount, crvusd.constantOptions);
if (estimateGas) return gas.toNumber();

await crvusd.updateFeeData();
const gasLimit = gas.mul(130).div(100);
return (await contract.exchange(i, j, _amount, _minRecvAmount, { ...crvusd.options, value, gasLimit })).hash
return (await contract.exchange(i, j, _amount, _minRecvAmount, { ...crvusd.options, gasLimit })).hash
}

public async swapEstimateGas(i: number, j: number, amount: number | string, slippage = 0.5): Promise<number> {
Expand Down Expand Up @@ -1184,12 +1185,12 @@ export class LlammaTemplate {
const minAmountBN: BigNumber = BN(stablecoin).times(100 - slippage).div(100);
const _minAmount = fromBN(minAmountBN);
const contract = crvusd.contracts[this.controller].contract;
const gas = (await contract.estimateGas.liquidate(address, _minAmount, false, crvusd.constantOptions))
const gas = (await contract.estimateGas.liquidate(address, _minAmount, isEth(this.collateral), crvusd.constantOptions))
if (estimateGas) return gas.toNumber();

await crvusd.updateFeeData();
const gasLimit = gas.mul(130).div(100);
return (await contract.liquidate(address, _minAmount, false, { ...crvusd.options, gasLimit })).hash
return (await contract.liquidate(address, _minAmount, isEth(this.collateral), { ...crvusd.options, gasLimit })).hash
}

public async liquidateEstimateGas(address: string, slippage = 0.5): Promise<number> {
Expand Down

0 comments on commit f4135a0

Please sign in to comment.