Skip to content

Commit

Permalink
Merge pull request #392 from curvefi/feat/old-gauge-factory
Browse files Browse the repository at this point in the history
Feat: old gauge factory
  • Loading branch information
Macket authored Sep 7, 2024
2 parents d24dc05 + 76eb210 commit 84b7ee3
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 124 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/api",
"version": "2.63.3",
"version": "2.63.4",
"description": "JavaScript library for curve.fi",
"main": "lib/index.js",
"author": "Macket",
Expand Down
5 changes: 3 additions & 2 deletions src/constants/aliases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ export const ALIASES_OPTIMISM = lowerCaseValues({

export const ALIASES_XDAI = lowerCaseValues({
"crv": "0x712b3d230f3c1c19db860d80619288b1f0bdd0bd",
"gauge_factory": "0xabC000d88f23Bb45525E447528DBF656A9D55bf5",
"gauge_factory": "0x7BE6BD57A319A7180f71552E58c9d32Da32b6f96",
"gauge_factory_old": "0xabC000d88f23Bb45525E447528DBF656A9D55bf5",
"voting_escrow": "0xefde221f306152971d8e9f181bfe998447975810",
"fee_distributor": "0xA464e6DCda8AC41e03616F95f4BC98a13b8922Dc", // <-- DUMMY
"gauge_controller": "0x2F50D538606Fa9EDD2B11E2446BEb18C9D5846bB",
Expand All @@ -151,7 +152,7 @@ export const ALIASES_XDAI = lowerCaseValues({
"eywa_factory": '0x37F22A0B028f2152e6CAcef210e0C4d3b875f367', // <--- DUMMY
"crypto_factory": '0xF18056Bbd320E96A48e3Fbf8bC061322531aac99', // <--- TODO CHANGE
"twocrypto_factory": '0x98EE851a00abeE0d95D08cF4CA2BdCE32aeaAF7F', // <--- NEW
"tricrypto_factory": '0x0c0e5f2fF0ff18a3be9b835635039256dC4B4963', // <--- TODO CHANGE
"tricrypto_factory": '0xb47988aD49DCE8D909c6f9Cf7B26caF04e1445c8',
"stable_ng_factory": '0xbC0797015fcFc47d9C1856639CaE50D0e69FbEE8', // <--- TODO CHANGE
"factory_admin": "",
});
Expand Down
4 changes: 4 additions & 0 deletions src/curve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,10 @@ class Curve implements ICurve {
const _gaugeFactoryABI = this.chainId === 1 ? gaugeFactoryABI : gaugeFactorySidechainABI
this.setContract(this.constants.ALIASES.gauge_factory, _gaugeFactoryABI);

if ("gauge_factory_old" in this.constants.ALIASES) {
this.setContract(this.constants.ALIASES.gauge_factory_old, _gaugeFactoryABI);
}

if(this.chainId === 1) {
this.setContract(this.constants.ALIASES.minter, minterMainnetABI)
this.setContract(this.constants.ALIASES.gauge_factory_fraxtal, gaugeFactoryForFraxtalABI)
Expand Down
43 changes: 23 additions & 20 deletions src/factory/factory-crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ async function _getLpTokenMap(this: ICurve,factorySwapAddresses: string[]): Prom
return LpTokenMap
}

async function getPoolsData(this: ICurve, factorySwapAddresses: string[]): Promise<[string[], string[], string[][]]> {
async function getPoolsData(this: ICurve, factorySwapAddresses: string[]): Promise<[string[], string[], string[], string[][]]> {
const factoryMulticallContract = this.contracts[this.constants.ALIASES.crypto_factory].multicallContract;
const isfactoryGaugeNull = this.constants.ALIASES.gauge_factory === '0x0000000000000000000000000000000000000000'

const isGaugeFactoryNull = this.constants.ALIASES.gauge_factory === curve.constants.ZERO_ADDRESS;
const isGaugeFactoryOldNull = !("gauge_factory_old" in this.constants.ALIASES);
const calls = [];

if(this.chainId === 1) {
Expand All @@ -82,36 +82,35 @@ async function getPoolsData(this: ICurve, factorySwapAddresses: string[]): Promi
calls.push(factoryMulticallContract.get_gauge(addr));
calls.push(factoryMulticallContract.get_coins(addr));
}

} else {
const factoryMulticallGaugeContract = this.contracts[this.constants.ALIASES.gauge_factory].multicallContract
const gaugeFactoryMulticallContract = this.contracts[this.constants.ALIASES.gauge_factory].multicallContract;
const gaugeFactoryOldMulticallContract = this.contracts[this.constants.ALIASES.gauge_factory_old ?? curve.constants.ZERO_ADDRESS].multicallContract;

const LpTokenMap = await _getLpTokenMap.call(this, factorySwapAddresses)

for (const addr of factorySwapAddresses) {
calls.push(factoryMulticallContract.get_token(addr));
if(!isfactoryGaugeNull) {
calls.push(factoryMulticallGaugeContract.get_gauge_from_lp_token(LpTokenMap[addr]))
}
if(!isGaugeFactoryNull) calls.push(gaugeFactoryMulticallContract.get_gauge_from_lp_token(LpTokenMap[addr]));
if(!isGaugeFactoryOldNull) calls.push(gaugeFactoryOldMulticallContract.get_gauge_from_lp_token(LpTokenMap[addr]));
calls.push(factoryMulticallContract.get_coins(addr));
}
}

const res = await this.multicallProvider.all(calls);

if(isfactoryGaugeNull) {
const tokenAddresses = (res.filter((a, i) => i % 3 == 0) as string[]).map((a) => a.toLowerCase());
const coinAddresses = _handleCoinAddresses.call(this, res.filter((a, i) => i % 3 == 1) as string[][]);
const gaugeAddresses = Array.from(Array(factorySwapAddresses.length)).map(() => '0x0000000000000000000000000000000000000000')
if(isGaugeFactoryNull || isGaugeFactoryOldNull) {
for(let index = 0; index < res.length; index++) {
if(isGaugeFactoryNull && index % 4 == 1) res.splice(index, 0 , curve.constants.ZERO_ADDRESS);
if(isGaugeFactoryOldNull && index % 4 == 2) res.splice(index, 0 , curve.constants.ZERO_ADDRESS);
}
}

return [tokenAddresses, gaugeAddresses, coinAddresses]
} else {
const tokenAddresses = (res.filter((a, i) => i % 3 == 0) as string[]).map((a) => a.toLowerCase());
const gaugeAddresses = (res.filter((a, i) => i % 3 == 1) as string[]).map((a) => a.toLowerCase());
const coinAddresses = _handleCoinAddresses.call(this, res.filter((a, i) => i % 3 == 2) as string[][]);
const tokenAddresses = (res.filter((a, i) => i % 4 == 0) as string[]).map((a) => a.toLowerCase());
const gaugeAddresses = (res.filter((a, i) => i % 4 == 1) as string[]).map((a) => a.toLowerCase());
const gaugeOldAddresses = (res.filter((a, i) => i % 4 == 2) as string[]).map((a) => a.toLowerCase());
const coinAddresses = _handleCoinAddresses.call(this, res.filter((a, i) => i % 4 == 3) as string[][]);

return [tokenAddresses, gaugeAddresses, coinAddresses]
}
return [tokenAddresses, gaugeAddresses, gaugeOldAddresses, coinAddresses]
}

function setCryptoFactorySwapContracts(this: ICurve, factorySwapAddresses: string[]): void {
Expand Down Expand Up @@ -227,7 +226,11 @@ export async function getCryptoFactoryPoolData(this: ICurve, fromIdx = 0, swapAd
: await getCryptoFactoryIdsAndSwapAddresses.call(this, fromIdx);
if (poolIds.length === 0) return {};

const [tokenAddresses, gaugeAddresses, coinAddresses] = await getPoolsData.call(this, swapAddresses);
const [tokenAddresses, rawGaugeAddresses, rawOldGaugeAddresses, coinAddresses] = await getPoolsData.call(this, swapAddresses);
const gaugeAddresses: string[] = [];
for (let i = 0; i < rawGaugeAddresses.length; i++) {
gaugeAddresses.push(rawGaugeAddresses[i] !== curve.constants.ZERO_ADDRESS ? rawGaugeAddresses[i] : rawOldGaugeAddresses[i]);
}
setCryptoFactorySwapContracts.call(this, swapAddresses);
setCryptoFactoryTokenContracts.call(this, tokenAddresses);
setCryptoFactoryGaugeContracts.call(this, gaugeAddresses);
Expand Down
68 changes: 29 additions & 39 deletions src/factory/factory-tricrypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ERC20ABI from "../constants/abis/ERC20.json" assert { type: 'json' };
import tricryptoFactorySwapABI from "../constants/abis/factory-tricrypto/factory-tricrypto-pool.json" assert { type: 'json' };
import factoryGaugeABI from "../constants/abis/gauge_factory.json" assert { type: 'json' };
import gaugeChildABI from "../constants/abis/gauge_child.json" assert { type: 'json' };
import {tricryptoDeployImplementations} from "../constants/tricryptoDeployImplementations.js";
import { tricryptoDeployImplementations } from "../constants/tricryptoDeployImplementations.js";


const deepFlatten = (arr: any[]): any[] => [].concat(...arr.map((v) => (Array.isArray(v) ? deepFlatten(v) : v)));
Expand Down Expand Up @@ -49,53 +49,45 @@ function _handleCoinAddresses(this: ICurve, coinAddresses: string[][]): string[]
));
}

async function getPoolsData(this: ICurve, factorySwapAddresses: string[]): Promise<[string[], string[][], string[]]> {
if(this.chainId === 1) {
const factoryMulticallContract = this.contracts[this.constants.ALIASES.tricrypto_factory].multicallContract;
async function getPoolsData(this: ICurve, factorySwapAddresses: string[]): Promise<[string[], string[], string[][], string[]]> {
const factoryMulticallContract = this.contracts[this.constants.ALIASES.tricrypto_factory].multicallContract;
const isGaugeFactoryNull = this.constants.ALIASES.gauge_factory === curve.constants.ZERO_ADDRESS;
const isGaugeFactoryOldNull = !("gauge_factory_old" in this.constants.ALIASES);
const calls = [];

const calls = [];
if(this.chainId === 1) {
for (const addr of factorySwapAddresses) {
calls.push(factoryMulticallContract.get_gauge(addr));
calls.push(factoryMulticallContract.get_coins(addr));
calls.push(factoryMulticallContract.get_implementation_address(addr));
}

const res = await this.multicallProvider.all(calls);
const gaugeAddresses = (res.filter((a, i) => i % 3 == 0) as string[]).map((a) => a.toLowerCase());
const coinAddresses = _handleCoinAddresses.call(this, res.filter((a, i) => i % 3 == 1) as string[][]);
const implementationAddresses = (res.filter((a, i) => i % 3 == 2) as string[]).map((a) => a.toLowerCase());

return [gaugeAddresses, coinAddresses, implementationAddresses]
} else {
const factoryMulticallContract = this.contracts[this.constants.ALIASES.tricrypto_factory].multicallContract;
const isFactoryGaugeNull = this.constants.ALIASES.gauge_factory === '0x0000000000000000000000000000000000000000'
const factoryMulticallGaugeContract = this.contracts[this.constants.ALIASES.gauge_factory].multicallContract

const calls = [];
const gaugeFactoryMulticallContract = this.contracts[this.constants.ALIASES.gauge_factory].multicallContract;
const gaugeFactoryOldMulticallContract = this.contracts[this.constants.ALIASES.gauge_factory_old ?? curve.constants.ZERO_ADDRESS].multicallContract;

for (const addr of factorySwapAddresses) {
if(!isFactoryGaugeNull) {
calls.push(factoryMulticallGaugeContract.get_gauge_from_lp_token(addr))
}
if (!isGaugeFactoryNull) calls.push(gaugeFactoryMulticallContract.get_gauge_from_lp_token(addr));
if (!isGaugeFactoryOldNull) calls.push(gaugeFactoryOldMulticallContract.get_gauge_from_lp_token(addr));
calls.push(factoryMulticallContract.get_coins(addr));
calls.push(factoryMulticallContract.get_implementation_address(addr));
}
}

const res = await this.multicallProvider.all(calls);
if(!isFactoryGaugeNull) {
const gaugeAddresses = (res.filter((a, i) => i % 3 == 0) as string[]).map((a) => a.toLowerCase());
const coinAddresses = _handleCoinAddresses.call(this, res.filter((a, i) => i % 3 == 1) as string[][]);
const implementationAddresses = (res.filter((a, i) => i % 3 == 2) as string[]).map((a) => a.toLowerCase());

return [gaugeAddresses, coinAddresses, implementationAddresses]
} else {
const coinAddresses = _handleCoinAddresses.call(this, res.filter((a, i) => i % 2 == 0) as string[][]);
const gaugeAddresses = Array.from(Array(factorySwapAddresses.length)).map(() => '0x0000000000000000000000000000000000000000')
const implementationAddresses = (res.filter((a, i) => i % 2 == 1) as string[]).map((a) => a.toLowerCase());
const res = await this.multicallProvider.all(calls);

return [gaugeAddresses, coinAddresses, implementationAddresses]
if(isGaugeFactoryNull || isGaugeFactoryOldNull) {
for(let index = 0; index < res.length; index++) {
if(isGaugeFactoryNull && index % 4 == 0) res.splice(index, 0 , curve.constants.ZERO_ADDRESS);
if(isGaugeFactoryOldNull && index % 4 == 1) res.splice(index, 0 , curve.constants.ZERO_ADDRESS);
}
}

const gaugeAddresses = (res.filter((a, i) => i % 4 == 0) as string[]).map((a) => a.toLowerCase());
const gaugeOldAddresses = (res.filter((a, i) => i % 4 == 1) as string[]).map((a) => a.toLowerCase());
const coinAddresses = _handleCoinAddresses.call(this, res.filter((a, i) => i % 4 == 2) as string[][]);
const implementationAddresses = (res.filter((a, i) => i % 4 == 3) as string[]).map((a) => a.toLowerCase());

return [gaugeAddresses, gaugeOldAddresses, coinAddresses, implementationAddresses]
}

function setCryptoFactorySwapContracts(this: ICurve, factorySwapAddresses: string[]): void {
Expand All @@ -104,12 +96,6 @@ function setCryptoFactorySwapContracts(this: ICurve, factorySwapAddresses: strin
});
}

function setCryptoFactoryTokenContracts(this: ICurve, factoryTokenAddresses: string[]): void {
factoryTokenAddresses.forEach((addr) => {
this.setContract(addr, ERC20ABI);
});
}

function setCryptoFactoryGaugeContracts(this: ICurve, factoryGaugeAddresses: string[]): void {
factoryGaugeAddresses.filter((addr) => addr !== curve.constants.ZERO_ADDRESS).forEach((addr, i) => {
this.setContract(addr, this.chainId === 1 ? factoryGaugeABI : gaugeChildABI);
Expand Down Expand Up @@ -206,7 +192,11 @@ export async function getTricryptoFactoryPoolData(this: ICurve, fromIdx = 0, swa
: await getCryptoFactoryIdsAndSwapAddresses.call(this, fromIdx);
if (poolIds.length === 0) return {};

const [gaugeAddresses, coinAddresses, implementationAddresses] = await getPoolsData.call(this, swapAddresses);
const [rawGaugeAddresses, rawOldGaugeAddresses, coinAddresses, implementationAddresses] = await getPoolsData.call(this, swapAddresses);
const gaugeAddresses: string[] = [];
for (let i = 0; i < rawGaugeAddresses.length; i++) {
gaugeAddresses.push(rawGaugeAddresses[i] !== curve.constants.ZERO_ADDRESS ? rawGaugeAddresses[i] : rawOldGaugeAddresses[i]);
}
setCryptoFactorySwapContracts.call(this, swapAddresses);
setCryptoFactoryGaugeContracts.call(this, gaugeAddresses);
setCryptoFactoryCoinsContracts.call(this, coinAddresses);
Expand Down
61 changes: 27 additions & 34 deletions src/factory/factory-twocrypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,48 +48,43 @@ function _handleCoinAddresses(this: ICurve, coinAddresses: string[][]): string[]
));
}

async function getPoolsData(this: ICurve, factorySwapAddresses: string[]): Promise<[string[], string[][]]> {
if(this.chainId === 1) {
const factoryMulticallContract = this.contracts[this.constants.ALIASES.twocrypto_factory].multicallContract;
async function getPoolsData(this: ICurve, factorySwapAddresses: string[]): Promise<[string[], string[], string[][]]> {
const factoryMulticallContract = this.contracts[this.constants.ALIASES.twocrypto_factory].multicallContract;
const isGaugeFactoryNull = this.constants.ALIASES.gauge_factory === curve.constants.ZERO_ADDRESS;
const isGaugeFactoryOldNull = !("gauge_factory_old" in this.constants.ALIASES);
const calls = [];

const calls = [];
if(this.chainId === 1) {
for (const addr of factorySwapAddresses) {
calls.push(factoryMulticallContract.get_gauge(addr));
calls.push(factoryMulticallContract.get_coins(addr));
}

const res = await this.multicallProvider.all(calls);
const gaugeAddresses = (res.filter((a, i) => i % 2 == 0) as string[]).map((a) => a.toLowerCase());
const coinAddresses = _handleCoinAddresses.call(this, res.filter((a, i) => i % 2 == 1) as string[][]);

return [gaugeAddresses, coinAddresses]
} else {
const factoryMulticallContract = this.contracts[this.constants.ALIASES.twocrypto_factory].multicallContract;
const isFactoryGaugeNull = this.constants.ALIASES.gauge_factory === '0x0000000000000000000000000000000000000000'
const factoryMulticallGaugeContract = this.contracts[this.constants.ALIASES.gauge_factory].multicallContract

const calls = [];
const gaugeFactoryMulticallContract = this.contracts[this.constants.ALIASES.gauge_factory].multicallContract;
const gaugeFactoryOldMulticallContract = this.contracts[this.constants.ALIASES.gauge_factory_old ?? curve.constants.ZERO_ADDRESS].multicallContract;

for (const addr of factorySwapAddresses) {
if(!isFactoryGaugeNull) {
calls.push(factoryMulticallGaugeContract.get_gauge_from_lp_token(addr))
}
if (!isGaugeFactoryNull) calls.push(gaugeFactoryMulticallContract.get_gauge_from_lp_token(addr));
if (!isGaugeFactoryOldNull) calls.push(gaugeFactoryOldMulticallContract.get_gauge_from_lp_token(addr));
calls.push(factoryMulticallContract.get_coins(addr));
}
}

const res = await this.multicallProvider.all(calls);
if(!isFactoryGaugeNull) {
const gaugeAddresses = (res.filter((a, i) => i % 2 == 0) as string[]).map((a) => a.toLowerCase());
const coinAddresses = _handleCoinAddresses.call(this, res.filter((a, i) => i % 2 == 1) as string[][]);

return [gaugeAddresses, coinAddresses]
} else {
const coinAddresses = _handleCoinAddresses.call(this, res.filter((a, i) => i % 2 == 0) as string[][]);
const gaugeAddresses = Array.from(Array(factorySwapAddresses.length)).map(() => '0x0000000000000000000000000000000000000000')
const res = await this.multicallProvider.all(calls);

return [gaugeAddresses, coinAddresses]
if(isGaugeFactoryNull || isGaugeFactoryOldNull) {
for(let index = 0; index < res.length; index++) {
if(isGaugeFactoryNull && index % 3 == 0) res.splice(index, 0 , curve.constants.ZERO_ADDRESS);
if(isGaugeFactoryOldNull && index % 3 == 1) res.splice(index, 0 , curve.constants.ZERO_ADDRESS);
}
}

const gaugeAddresses = (res.filter((a, i) => i % 3 == 0) as string[]).map((a) => a.toLowerCase());
const gaugeOldAddresses = (res.filter((a, i) => i % 3 == 1) as string[]).map((a) => a.toLowerCase());
const coinAddresses = _handleCoinAddresses.call(this, res.filter((a, i) => i % 3 == 2) as string[][]);

return [gaugeAddresses, gaugeOldAddresses, coinAddresses]
}

function setTwocryptoFactorySwapContracts(this: ICurve, factorySwapAddresses: string[]): void {
Expand All @@ -98,12 +93,6 @@ function setTwocryptoFactorySwapContracts(this: ICurve, factorySwapAddresses: st
});
}

function setTwocryptoFactoryTokenContracts(this: ICurve, factoryTokenAddresses: string[]): void {
factoryTokenAddresses.forEach((addr) => {
this.setContract(addr, ERC20ABI);
});
}

function setTwocryptoFactoryGaugeContracts(this: ICurve, factoryGaugeAddresses: string[]): void {
factoryGaugeAddresses.filter((addr) => addr !== curve.constants.ZERO_ADDRESS).forEach((addr, i) => {
this.setContract(addr, this.chainId === 1 ? factoryGaugeABI : gaugeChildABI);
Expand Down Expand Up @@ -204,7 +193,11 @@ export async function getTwocryptoFactoryPoolData(this: ICurve, fromIdx = 0, swa
: await getTwocryptoFactoryIdsAndSwapAddresses.call(this, fromIdx);
if (poolIds.length === 0) return {};

const [gaugeAddresses, coinAddresses] = await getPoolsData.call(this, swapAddresses);
const [rawGaugeAddresses, rawOldGaugeAddresses, coinAddresses] = await getPoolsData.call(this, swapAddresses);
const gaugeAddresses: string[] = [];
for (let i = 0; i < rawGaugeAddresses.length; i++) {
gaugeAddresses.push(rawGaugeAddresses[i] !== curve.constants.ZERO_ADDRESS ? rawGaugeAddresses[i] : rawOldGaugeAddresses[i]);
}
setTwocryptoFactorySwapContracts.call(this, swapAddresses);
setTwocryptoFactoryGaugeContracts.call(this, gaugeAddresses);
setTwocryptoFactoryCoinsContracts.call(this, coinAddresses);
Expand Down
Loading

0 comments on commit 84b7ee3

Please sign in to comment.