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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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 @@ export type ContractItem = { contract: Contract, multicallContract: MulticallCon

class Curve implements ICurve {
provider: ethers.BrowserProvider | ethers.JsonRpcProvider;
abstractProvider?: {chainId: number, name: string};
isNoRPC: boolean;
multicallProvider: MulticallProvider;
signer: ethers.Signer | null;
signerAddress: string;
Expand All @@ -109,6 +111,7 @@ class Curve implements ICurve {
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 @@ class Curve implements ICurve {
}

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 @@ -226,11 +229,19 @@ class Curve implements ICurve {
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.abstractProvider = {
chainId: providerSettings.chainId as number,
name: providerSettings.networkName as string,
}
Comment on lines +234 to +238
Copy link
Contributor

@DanielSchiavini DanielSchiavini Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This abstract provider isn't a provider and isn't abstract. Also it's not really used. Why don't we just keep no provider?

Suggested change
this.isNoRPC = true;
this.abstractProvider = {
chainId: providerSettings.chainId as number,
name: providerSettings.networkName as string,
}
this.provider = null

this.signer = null;
} else {
throw Error('Wrong providerType');
}

const network = await this.provider.getNetwork();
const network = this.abstractProvider || 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 @@ -389,8 +400,10 @@ class Curve implements ICurve {
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 @@ class Curve implements ICurve {
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 @@ class Curve implements ICurve {
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 @@ class Curve implements ICurve {
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 @@ class Curve implements ICurve {
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 @@ class Curve implements ICurve {
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 @@ class Curve implements ICurve {
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 @@ class Curve implements ICurve {
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 @@ class Curve implements ICurve {
}

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