Skip to content

Commit

Permalink
reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
mantasfam committed Sep 24, 2024
1 parent e2022ee commit 1f9fa86
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 6 deletions.
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 1f9fa86

Please sign in to comment.