Skip to content

Commit

Permalink
Merge pull request #13 from curvefi/fix/collateral-duplication
Browse files Browse the repository at this point in the history
Fix: collateral duplication
  • Loading branch information
Macket authored Aug 26, 2023
2 parents d3044d0 + 3c85b0b commit c5dc503
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@curvefi/stablecoin-api",
"version": "1.3.4",
"version": "1.3.5",
"description": "JavaScript library for Curve Stablecoin",
"main": "lib/index.js",
"author": "Macket",
Expand Down
2 changes: 2 additions & 0 deletions src/constants/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export const lowerCaseLlammasAddresses = (llammas: IDict<ILlamma>): IDict<ILlamm
llamma.amm_address = llamma.amm_address.toLowerCase();
llamma.controller_address = llamma.controller_address.toLowerCase();
llamma.collateral_address = llamma.collateral_address.toLowerCase();
llamma.monetary_policy_address = llamma.monetary_policy_address.toLowerCase();
llamma.leverage_zap = llamma.leverage_zap.toLowerCase();
}

return llammas
Expand Down
35 changes: 20 additions & 15 deletions src/crvusd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class Crvusd implements Icrvusd {
this.setContract(llamma.amm_address, llammaABI);
this.setContract(llamma.controller_address, controllerABI);
const monetary_policy_address = await this.contracts[llamma.controller_address].contract.monetary_policy(this.constantOptions);
llamma.monetary_policy_address = monetary_policy_address;
llamma.monetary_policy_address = monetary_policy_address.toLowerCase();
this.setContract(monetary_policy_address, llamma.monetary_policy_abi);
if (llamma.collateral_address === "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee") {
this.setContract(this.constants.WETH, ERC20ABI);
Expand All @@ -163,9 +163,16 @@ class Crvusd implements Icrvusd {
const N2 = await factoryContract.n_collaterals(this.constantOptions);
let calls = [];
for (let i = N1; i < N2; i++) {
calls.push(factoryMulticallContract.collaterals(i));
calls.push(
factoryMulticallContract.collaterals(i),
factoryMulticallContract.amms(i),
factoryMulticallContract.controllers(i)
);
}
const collaterals: string[] = (await this.multicallProvider.all(calls) as string[]).map((c) => c.toLowerCase());
const res: string[] = (await this.multicallProvider.all(calls) as string[]).map((c) => c.toLowerCase());
const collaterals = res.filter((a, i) => i % 3 == 0) as string[];
const amms = res.filter((a, i) => i % 3 == 1) as string[];
const controllers = res.filter((a, i) => i % 3 == 2) as string[];

if (collaterals.length > 0) {
for (const collateral of collaterals) this.setContract(collateral, ERC20ABI);
Expand All @@ -174,28 +181,26 @@ class Crvusd implements Icrvusd {
for (const collateral of collaterals) {
calls.push(
this.contracts[collateral].multicallContract.symbol(),
this.contracts[collateral].multicallContract.decimals(),
factoryMulticallContract.get_amm(collateral),
factoryMulticallContract.get_controller(collateral)
this.contracts[collateral].multicallContract.decimals()
)
}
const res = (await this.multicallProvider.all(calls)).map((x) => {
if (typeof x === "string") return x.toLowerCase();
return x;
});

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);
for (let i = 0; i < collaterals.length; i++) {
const is_eth = collaterals[i] === this.constants.WETH;
const [collateral_symbol, collateral_decimals] = res.splice(0, 2) as [string, number];
this.setContract(amms[i], llammaABI);
this.setContract(controllers[i], controllerABI);
const monetary_policy_address = (await this.contracts[controllers[i]].contract.monetary_policy(this.constantOptions)).toLowerCase();
this.setContract(monetary_policy_address, MonetaryPolicy2ABI);
this.constants.LLAMMAS[is_eth ? "eth" : collateral_symbol.toLowerCase()] = {
amm_address,
controller_address,
amm_address: amms[i],
controller_address: controllers[i],
monetary_policy_address,
collateral_address: is_eth ? "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" : collateral_address,
collateral_address: is_eth ? "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" : collaterals[i],
leverage_zap: "0x0000000000000000000000000000000000000000",
collateral_symbol: is_eth ? "ETH" : collateral_symbol,
collateral_decimals,
Expand Down
7 changes: 2 additions & 5 deletions src/external-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ export const _getPoolsFromApi = memoize(
)

export const _getUserCollateral = memoize(
async (network: INetworkName, collateral: string, user: string, collateralDecimals = 18): Promise<string> => {
console.log(network, collateral, user);
const url = `https://prices.curve.fi/v1/stablecoin/collateral/events/${network}/${collateral}/${user}`;
console.log(url);
async (network: INetworkName, controller: string, user: string, collateralDecimals = 18): Promise<string> => {
const url = `https://prices.curve.fi/v1/stablecoin/controller/events/${network}/${controller}/${user}`;
const response = await axios.get(url, { validateStatus: () => true });
console.log(response);
return crvusd.formatUnits(crvusd.parseUnits(response.data.total_collateral ?? "0.0", 0), collateralDecimals);
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/llammas/LlammaTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ export class LlammaTemplate {
public async userLoss(userAddress = ""): Promise<{ deposited_collateral: string, current_collateral_estimation: string, loss: string, loss_pct: string }> {
userAddress = _getAddress(userAddress);
const [deposited_collateral, _current_collateral_estimation] = await Promise.all([
_getUserCollateral(crvusd.constants.NETWORK_NAME, this.collateral, userAddress, this.collateralDecimals),
_getUserCollateral(crvusd.constants.NETWORK_NAME, this.controller, userAddress, this.collateralDecimals),
crvusd.contracts[this.address].contract.get_y_up(userAddress),
]);
const current_collateral_estimation = crvusd.formatUnits(_current_collateral_estimation, this.collateralDecimals);
Expand Down

0 comments on commit c5dc503

Please sign in to comment.