Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow pools without rpc #428

Merged
merged 4 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import curve from "@curvefi/api";

// 3. Web3 provider
await curve.init('Web3', { externalProvider: <WEB3_PROVIDER> }, { chainId: 1 });

// 4. Without RPC
await curve.init('NoRPC', {chainId: 1, networkName: 'ETHEREUM'});

// Fetch factory pools
await curve.factory.fetchPools();
Expand Down
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.65.29",
"version": "2.66.0",
"description": "JavaScript library for curve.fi",
"main": "lib/index.js",
"author": "Macket",
Expand Down
57 changes: 46 additions & 11 deletions src/curve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@

class Curve implements ICurve {
provider: ethers.BrowserProvider | ethers.JsonRpcProvider;
chainOptions?: {chainId: number, name: string};
isNoRPC: boolean;
multicallProvider: MulticallProvider;
signer: ethers.Signer | null;
signerAddress: string;
Expand All @@ -109,6 +111,7 @@
this.provider = null;
// @ts-ignore
this.signer = null;
this.isNoRPC = false;
this.signerAddress = '';
this.chainId = 1;
this.isLiteChain = false;
Expand Down Expand Up @@ -143,8 +146,8 @@
}

async init(
providerType: 'JsonRpc' | 'Web3' | 'Infura' | 'Alchemy',
providerSettings: { url?: string, privateKey?: string, batchMaxCount? : number } | { externalProvider: ethers.Eip1193Provider } | { network?: Networkish, apiKey?: string },
providerType: 'JsonRpc' | 'Web3' | 'Infura' | 'Alchemy' | 'NoRPC',
providerSettings: { url?: string, privateKey?: string, batchMaxCount? : number } | { externalProvider: ethers.Eip1193Provider } | { network?: Networkish, apiKey?: string } | {chainId: number, networkName: string},
options: { gasPrice?: number, maxFeePerGas?: number, maxPriorityFeePerGas?: number, chainId?: number } = {} // gasPrice in Gwei
): Promise<void> {
// @ts-ignore
Expand Down Expand Up @@ -207,7 +210,7 @@
} else if (!providerSettings.url?.startsWith("https://rpc.gnosischain.com")) {
try {
this.signer = await this.provider.getSigner();
} catch (e) {

Check warning on line 213 in src/curve.ts

View workflow job for this annotation

GitHub Actions / test

'e' is defined but never used
this.signer = null;
}
}
Expand All @@ -226,11 +229,19 @@
providerSettings = providerSettings as { network?: Networkish, apiKey?: string };
this.provider = new ethers.AlchemyProvider(providerSettings.network, providerSettings.apiKey);
this.signer = null;
} else if (providerType.toLowerCase() === 'NoRPC'.toLowerCase()) {
providerSettings = providerSettings as { chainId: number, networkName: string };
this.isNoRPC = true;
this.chainOptions = {
chainId: providerSettings.chainId as number,
name: providerSettings.networkName as string,
}
this.signer = null;
} else {
throw Error('Wrong providerType');
}

const network = await this.provider.getNetwork();
const network = this.chainOptions || await this.provider.getNetwork();
console.log("CURVE-JS IS CONNECTED TO NETWORK:", { name: network.name.toUpperCase(), chainId: Number(network.chainId) });
this.chainId = Number(network.chainId) === 133 || Number(network.chainId) === 31337 ? 1 : Number(network.chainId) as IChainId;

Expand Down Expand Up @@ -278,7 +289,7 @@
if (this.signer) {
try {
this.signerAddress = await this.signer.getAddress();
} catch (err) {

Check warning on line 292 in src/curve.ts

View workflow job for this annotation

GitHub Actions / test

'err' is defined but never used
this.signer = null;
}
} else {
Expand Down Expand Up @@ -389,8 +400,10 @@
this.setContract(this.constants.ALIASES.factory, factoryABI);

const factoryContract = this.contracts[this.constants.ALIASES.factory].contract;
this.constants.ALIASES.factory_admin = (await factoryContract.admin(this.constantOptions) as string).toLowerCase();
this.setContract(this.constants.ALIASES.factory_admin, factoryAdminABI);
if(!this.isNoRPC) {
this.constants.ALIASES.factory_admin = (await factoryContract.admin(this.constantOptions) as string).toLowerCase();
this.setContract(this.constants.ALIASES.factory_admin, factoryAdminABI);
}
}

this.setContract(this.constants.ALIASES.crvusd_factory, factoryABI);
Expand Down Expand Up @@ -523,7 +536,7 @@
this.constants.FACTORY_POOLS_DATA = await this._filterHiddenPools(this.constants.FACTORY_POOLS_DATA);
this._updateDecimalsAndGauges(this.constants.FACTORY_POOLS_DATA);

this.constants.FACTORY_GAUGE_IMPLEMENTATIONS["factory"] = await this.contracts[this.constants.ALIASES.factory].contract.gauge_implementation(this.constantOptions);
this.constants.FACTORY_GAUGE_IMPLEMENTATIONS["factory"] = this.isNoRPC ? null : await this.contracts[this.constants.ALIASES.factory].contract.gauge_implementation(this.constantOptions);
}

fetchCrvusdFactoryPools = async (useApi = true): Promise<void> => {
Expand All @@ -532,6 +545,9 @@
if (useApi) {
this.constants.CRVUSD_FACTORY_POOLS_DATA = lowerCasePoolDataAddresses(await getFactoryPoolsDataFromApi.call(this, "factory-crvusd"));
} else {
if (this.isNoRPC) {
throw new Error('RPC connection is required');
}
this.constants.CRVUSD_FACTORY_POOLS_DATA = lowerCasePoolDataAddresses(
await getFactoryPoolData.call(this, 0, undefined, this.constants.ALIASES.crvusd_factory)
);
Expand All @@ -546,6 +562,9 @@
if (useApi) {
this.constants.EYWA_FACTORY_POOLS_DATA = lowerCasePoolDataAddresses(await getFactoryPoolsDataFromApi.call(this, "factory-eywa"));
} else {
if (this.isNoRPC) {
throw new Error('RPC connection is required');
}
this.constants.EYWA_FACTORY_POOLS_DATA = lowerCasePoolDataAddresses(
await getFactoryPoolData.call(this, 0, undefined, this.constants.ALIASES.eywa_factory)
);
Expand All @@ -560,12 +579,15 @@
if (useApi) {
this.constants.CRYPTO_FACTORY_POOLS_DATA = lowerCasePoolDataAddresses(await getFactoryPoolsDataFromApi.call(this, "factory-crypto"));
} else {
if (this.isNoRPC) {
throw new Error('RPC connection is required');
}
this.constants.CRYPTO_FACTORY_POOLS_DATA = lowerCasePoolDataAddresses(await getCryptoFactoryPoolData.call(this));
}
this.constants.CRYPTO_FACTORY_POOLS_DATA = await this._filterHiddenPools(this.constants.CRYPTO_FACTORY_POOLS_DATA);
this._updateDecimalsAndGauges(this.constants.CRYPTO_FACTORY_POOLS_DATA);

this.constants.FACTORY_GAUGE_IMPLEMENTATIONS["factory-crypto"] = await this.contracts[this.constants.ALIASES.crypto_factory].contract.gauge_implementation(this.constantOptions);
this.constants.FACTORY_GAUGE_IMPLEMENTATIONS["factory-crypto"] = this.isNoRPC? null : await this.contracts[this.constants.ALIASES.crypto_factory].contract.gauge_implementation(this.constantOptions);
}

fetchStableNgFactoryPools = async (useApi = true): Promise<void> => {
Expand All @@ -574,6 +596,9 @@
if (useApi) {
this.constants.STABLE_NG_FACTORY_POOLS_DATA = lowerCasePoolDataAddresses(await getFactoryPoolsDataFromApi.call(this, "factory-stable-ng"));
} else {
if (this.isNoRPC) {
throw new Error('RPC connection is required');
}
this.constants.STABLE_NG_FACTORY_POOLS_DATA = lowerCasePoolDataAddresses(await getFactoryPoolData.call(this, 0, undefined, this.constants.ALIASES.stable_ng_factory));
}

Expand All @@ -587,16 +612,19 @@
if (useApi) {
this.constants.TWOCRYPTO_FACTORY_POOLS_DATA = lowerCasePoolDataAddresses(await getFactoryPoolsDataFromApi.call(this, "factory-twocrypto"));
} else {
if (this.isNoRPC) {
throw new Error('RPC connection is required');
}
this.constants.TWOCRYPTO_FACTORY_POOLS_DATA = lowerCasePoolDataAddresses(await getTwocryptoFactoryPoolData.call(this));
}
this.constants.TWOCRYPTO_FACTORY_POOLS_DATA = await this._filterHiddenPools(this.constants.TWOCRYPTO_FACTORY_POOLS_DATA);
this._updateDecimalsAndGauges(this.constants.TWOCRYPTO_FACTORY_POOLS_DATA);

if (this.chainId === 1) {
this.constants.FACTORY_GAUGE_IMPLEMENTATIONS["factory-twocrypto"] =
this.constants.FACTORY_GAUGE_IMPLEMENTATIONS["factory-twocrypto"] = this.isNoRPC ? null :
await this.contracts[this.constants.ALIASES.twocrypto_factory].contract.gauge_implementation(this.constantOptions);
} else {
this.constants.FACTORY_GAUGE_IMPLEMENTATIONS["factory-twocrypto"] =
this.constants.FACTORY_GAUGE_IMPLEMENTATIONS["factory-twocrypto"] = this.isNoRPC ? null :
await this.contracts[this.constants.ALIASES.child_gauge_factory].contract.get_implementation(this.constantOptions);
}
}
Expand All @@ -607,16 +635,19 @@
if (useApi) {
this.constants.TRICRYPTO_FACTORY_POOLS_DATA = lowerCasePoolDataAddresses(await getFactoryPoolsDataFromApi.call(this, "factory-tricrypto"));
} else {
if (this.isNoRPC) {
throw new Error('RPC connection is required');
}
this.constants.TRICRYPTO_FACTORY_POOLS_DATA = lowerCasePoolDataAddresses(await getTricryptoFactoryPoolData.call(this));
}
this.constants.TRICRYPTO_FACTORY_POOLS_DATA = await this._filterHiddenPools(this.constants.TRICRYPTO_FACTORY_POOLS_DATA);
this._updateDecimalsAndGauges(this.constants.TRICRYPTO_FACTORY_POOLS_DATA);

if (this.chainId === 1) {
this.constants.FACTORY_GAUGE_IMPLEMENTATIONS["factory-tricrypto"] =
this.constants.FACTORY_GAUGE_IMPLEMENTATIONS["factory-tricrypto"] = this.isNoRPC ? null :
await this.contracts[this.constants.ALIASES.tricrypto_factory].contract.gauge_implementation(this.constantOptions);
} else {
this.constants.FACTORY_GAUGE_IMPLEMENTATIONS["factory-tricrypto"] =
this.constants.FACTORY_GAUGE_IMPLEMENTATIONS["factory-tricrypto"] = this.isNoRPC ? null :
await this.contracts[this.constants.ALIASES.child_gauge_factory].contract.get_implementation(this.constantOptions);
}
}
Expand Down Expand Up @@ -787,6 +818,10 @@
}

async updateFeeData(): Promise<void> {
if(this.isNoRPC) {
return
}

const feeData = await this.provider.getFeeData();
if (feeData.maxFeePerGas === null || feeData.maxPriorityFeePerGas === null) {
delete this.options.maxFeePerGas;
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ async function init (
this.signerAddress = _curve.signerAddress;
// @ts-ignore
this.chainId = _curve.chainId;
// @ts-ignore
this.isNoRPC = _curve.isNoRPC;
}

function setCustomFeeData (customFeeData: { gasPrice?: number, maxFeePerGas?: number, maxPriorityFeePerGas?: number }): void {
Expand Down Expand Up @@ -187,6 +189,7 @@ const curve = {
getCurveLiteNetworks,
getNetworkConstants: _curve.getNetworkConstants,
getIsLiteChain: _curve.getIsLiteChain,
isNoRPC: _curve.isNoRPC,
factory: {
fetchPools: _curve.fetchFactoryPools,
fetchNewPools: _curve.fetchNewFactoryPools,
Expand Down
Loading