Skip to content

Commit

Permalink
Merge branch 'master' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
mantasfam committed Sep 24, 2024
2 parents 5fade36 + a3fd41c commit c555447
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 14 deletions.
24 changes: 24 additions & 0 deletions src/factory/providers/bsc/aavev3/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import formatter from '../../../../util/formatter';
import { ITvlParams, ITvlReturn } from '../../../../interfaces/ITvl';
import aaveV3 from '../../../../util/calculators/aaveV3';

const START_BLOCK = 33571625;
const POOL_DATA_PROVIDER_V3 = '0x41585C50524fb8c3899B43D7D797d9486AAc94DB';

async function tvl(params: ITvlParams): Promise<Partial<ITvlReturn>> {
const { block, chain, provider, web3 } = params;
if (block < START_BLOCK) {
return {};
}

const balances = await aaveV3.getTvl(
POOL_DATA_PROVIDER_V3,
block,
chain,
web3,
);

formatter.convertBalancesToFixed(balances);
return { balances };
}
export { tvl };
2 changes: 1 addition & 1 deletion src/factory/providers/bsc/pancakeswapv3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const THEGRAPTH_ENDPOINT = `https://gateway-arbitrum.network.thegraph.com/api/${

async function tvl(params: ITvlParams): Promise<Partial<ITvlReturn>> {
const { chain, provider, web3 } = params;
const block = params.block - 100;
const block = params.block - 1000;

if (block < START_BLOCK) {
return { balances: {} };
Expand Down
7 changes: 0 additions & 7 deletions src/factory/providers/optimism/aavev3/data.json

This file was deleted.

1 change: 1 addition & 0 deletions src/factory/providers/solana/raydium/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const RAYDIUM_POOLS_ENDPOINT =

async function tvl(params: ITvlParams): Promise<Partial<ITvlReturn>> {
const { web3 } = params;

const balances = {};
const pools = await fetch(RAYDIUM_POOLS_ENDPOINT).then((res) => res.json());
const RAYDIUM_POOLS = pools.official;
Expand Down
24 changes: 24 additions & 0 deletions src/factory/providers/zksync-era/aavev3/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import formatter from '../../../../util/formatter';
import { ITvlParams, ITvlReturn } from '../../../../interfaces/ITvl';
import aaveV3 from '../../../../util/calculators/aaveV3';

const START_BLOCK = 43708965;
const POOL_DATA_PROVIDER_V3 = '0x48B96565291d1B23a014bb9f68E07F4B2bb3Cd6D';

async function tvl(params: ITvlParams): Promise<Partial<ITvlReturn>> {
const { block, chain, provider, web3 } = params;
if (block < START_BLOCK) {
return {};
}

const balances = await aaveV3.getTvl(
POOL_DATA_PROVIDER_V3,
block,
chain,
web3,
);

formatter.convertBalancesToFixed(balances);
return { balances };
}
export { tvl };
34 changes: 31 additions & 3 deletions src/util/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,37 @@ import { getLogger, configure } from 'log4js';
const logger = getLogger();

interface ILogger {
info: ({ message, endpoint }) => void;
warning: ({ message, stack, detail, endpoint }) => void;
error: ({ message, stack, detail, endpoint }) => void;
info: ({
message,
detail,
endpoint,
}: {
message?: string;
detail?: string;
endpoint?: string;
}) => void;
warning: ({
message,
stack,
detail,
endpoint,
}: {
message?: string;
stack?: string;
detail?: string;
endpoint?: string;
}) => void;
error: ({
message,
stack,
detail,
endpoint,
}: {
message?: string;
stack?: string;
detail?: string;
endpoint?: string;
}) => void;
}

configure({
Expand Down
94 changes: 91 additions & 3 deletions src/web3Provider/solana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import BigNumber from 'bignumber.js';
import { Injectable } from '@nestjs/common';
import { config, nodeUrls } from '../app.config';
import Bottleneck from 'bottleneck';
import { log } from '../util/logger/logger';

const nodeUrl = nodeUrls.SOLANA_NODE_URL;
const solanaBottleNeckMinTime = config.SOLANA_BOTTLENECK_MIN_TIME;
Expand All @@ -13,12 +14,25 @@ const limiter = new Bottleneck({
@Injectable()
export class Solana {
getNodeUrl() {
log.info({
message: 'Fetching Solana node URL',
detail: 'solana - Fetched Solana node URL.',
endpoint: 'solana.getNodeUrl',
});
return nodeUrl;
}

async call(method, params) {
return limiter.schedule(async () => {
try {
log.info({
message: `Calling method: ${method} with params: ${JSON.stringify(
params,
)}`,
detail: `solana - API call initiated.`,
endpoint: 'solana.call',
});

const res = await fetch(nodeUrl, {
method: 'post',
headers: { 'Content-Type': 'application/json' },
Expand All @@ -31,31 +45,59 @@ export class Solana {
}).then((res) => res.json());

if (res.error) {
log.error({
message: `Error in method ${method}: ${res.error.message}`,
detail: `solana - API call encountered an error.`,
endpoint: 'solana.call',
});
throw res.error;
}

log.info({
message: `Response from method ${method}: ${JSON.stringify(
res.result,
)}`,
detail: `solana - API call returned successfully.`,
endpoint: 'solana.call',
});

return res.result;
} catch {
} catch (e) {
log.error({
message: `Exception in method ${method}: ${e.message}`,
detail: `solana - API call failed with exception.`,
endpoint: 'solana.call',
});
return null;
}
});
}

async getBlockNumber() {
const res = await this.call('getSlot', []);
log.info({
message: `Fetched block number: ${res}`,
detail: `solana - Fetched block number using getSlot.`,
endpoint: 'solana.getBlockNumber',
});
return res;
}

async getBlock(slotNumber) {
let slot = slotNumber || 0;
if (slotNumber == 'latest') {
slot = await module.exports.eth.getBlockNumber();
slot = await this.getBlockNumber();
}
let res;
while (true) {
res = await this.call('getBlockTime', [slot]);

if (res && !res.error) {
log.info({
message: `Found block at slot ${slot} with timestamp: ${res}`,
detail: `solana - Block found successfully.`,
endpoint: 'solana.getBlock',
});
break;
}
slot += 1;
Expand All @@ -76,11 +118,24 @@ class Contract {
constructor(abi, address) {
this.abi = abi;
this.address = address;
log.info({
message: `Contract instance created for address: ${address}`,
detail: `solana - Contract creation logged.`,
endpoint: 'solana.Contract.constructor',
});
}

async call(method, params) {
return limiter.schedule(async () => {
try {
log.info({
message: `Calling method: ${method} with params: ${JSON.stringify(
params,
)} for contract ${this.address}`,
detail: `solana - Contract API call initiated.`,
endpoint: 'solana.Contract.call',
});

const res = await fetch(nodeUrl, {
method: 'post',
headers: { 'Content-Type': 'application/json' },
Expand All @@ -93,11 +148,29 @@ class Contract {
}).then((res) => res.json());

if (res.error) {
log.error({
message: `Error in method ${method}: ${res.error.message} for contract ${this.address}`,
detail: `solana - Contract API call encountered an error.`,
endpoint: 'solana.Contract.call',
});
throw res.error;
}

log.info({
message: `Response from method ${method}: ${JSON.stringify(
res.result,
)} for contract ${this.address}`,
detail: `solana - Contract API call returned successfully.`,
endpoint: 'solana.Contract.call',
});

return res.result;
} catch {
} catch (e) {
log.error({
message: `Exception in method ${method}: ${e.message} for contract ${this.address}`,
detail: `solana - Contract API call failed with exception.`,
endpoint: 'solana.Contract.call',
});
return null;
}
});
Expand All @@ -109,6 +182,11 @@ class Contract {
return {
call: async () => {
const res = await this.call('getTokenSupply', [this.address]);
log.info({
message: `Total supply fetched: ${res.value.amount} for contract ${this.address}`,
detail: `solana - Fetched total supply of contract.`,
endpoint: 'solana.Contract.methods.totalSupply.call',
});
return res.value.amount;
},
};
Expand All @@ -125,6 +203,7 @@ class Contract {
encoding: 'jsonParsed',
},
]);

let balance = BigNumber(0);
res.value.forEach((value) => {
if (value && value.account && value.account.data) {
Expand All @@ -133,6 +212,15 @@ class Contract {
);
}
});

log.info({
message: `Balance fetched for account ${account}: ${balance.toFixed()} for contract ${
this.address
}`,
detail: `solana - Balance fetched successfully.`,
endpoint: 'solana.Contract.methods.balanceOf.call',
});

return balance.toFixed();
},
};
Expand Down

0 comments on commit c555447

Please sign in to comment.