Skip to content

Commit

Permalink
extract log sub title to one place
Browse files Browse the repository at this point in the history
  • Loading branch information
wuminzhe committed Apr 1, 2024
1 parent 067e2a0 commit 29311a4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
33 changes: 17 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ console.log = (function() {
var console_log = console.log;

return function() {
var time = "[" + new Date().toUTCString() + "]";
const logSubTitle = httpContext.get('logTitle');
if (logSubTitle) {
var title = "[" + new Date().toUTCString() + "] - [" + logSubTitle + "]";
} else {
var title = "[" + new Date().toUTCString() + "]";
}
var args = [];
args.push(time + ':');
args.push(title + ':');
for(var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
Expand Down Expand Up @@ -74,15 +79,14 @@ app.get("/:protocol/fee", async (req: Request, res: Response) => {
const refundAddress: string = req.query.refund_address as string;
const extra: string = req.query.extra as string; // extra=[[1, 10]]

const logTitle = `${protocol}:${fromChainId}>${toChainId}`
httpContext.set('logTitle', logTitle)
httpContext.set('logTitle', `${protocol}:${fromChainId}>${toChainId}`)

console.log(`${logTitle} - ==============================================================================================================`);
console.log(`${logTitle} - fromAddress: ${fromAddress}, toAddress: ${toAddress}`)
console.log(`${logTitle} - payload: ${payload}`);
console.log(`${logTitle} - gasLimit: ${gasLimit}`)
console.log(`${logTitle} - refundAddress: ${refundAddress}`)
console.log(`${logTitle} - extra: ${extra}`);
console.log(`==============================================================================================================`);
console.log(`fromAddress: ${fromAddress}, toAddress: ${toAddress}`)
console.log(`payload: ${payload}`);
console.log(`gasLimit: ${gasLimit}`)
console.log(`refundAddress: ${refundAddress}`)
console.log(`extra: ${extra}`);

if (
!fromChainId ||
Expand Down Expand Up @@ -131,12 +135,11 @@ app.get("/:protocol/fee", async (req: Request, res: Response) => {
});

async function getEstimateFeeFunction(protocol: string) {
const logTitle = httpContext.get('logTitle');
try {
const buildEstimateFee = await import(`./${protocol}/estimateFee`)
return buildEstimateFee.default();
} catch (e) {
console.log(`${logTitle} - `, e);
console.log(e);
throw new Error(`${protocol} - ${e.message}`);
}
}
Expand All @@ -152,7 +155,6 @@ async function estimateFee(
refundAddress: string,
extraParams: any
) {
const logTitle = httpContext.get('logTitle');
try {
const estimateFee = await getEstimateFeeFunction(protocol);
return await estimateFee(
Expand All @@ -167,7 +169,7 @@ async function estimateFee(
);
} catch (e) {
const err = ensureError(e);
console.error(`${logTitle} - `, err);
console.error(err);
throw err;
}
}
Expand Down Expand Up @@ -210,8 +212,7 @@ function parseExtraParams(extra: string) {
return []
}
} catch (e: any) {
const logTitle = httpContext.get('logTitle');
console.error(`${logTitle} - `, e.message);
console.error(e.message);
return [];
}
}
Expand Down
21 changes: 9 additions & 12 deletions src/ormp/doEstimateFee.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { ethers } from "ethers";
import { blockNumber, getContract, estimateGas, callFunction } from "../utils/evmChainsUtils";
import { FeestimiError } from "../errors";
var httpContext = require('express-http-context');

const srcOrmpLineAbi = [
"function fee(uint256 toChainId, address toDapp, bytes calldata message, bytes calldata params) external view returns (uint256)"
];

async function doEstimateFee(params, config): Promise<[string, string]> {
const logTitle = httpContext.get('logTitle');

const fromChainId = params.fromChainId;
const toChainId = params.toChainId;
const payload = params.payload;
Expand All @@ -18,22 +15,22 @@ async function doEstimateFee(params, config): Promise<[string, string]> {
const refundAddress = params.refundAddress;
let gasLimit = params.gasLimit;

console.log(`${logTitle} - DO ESTIMATE GAS AT SOURCE BLOCK NUMBER: ${await blockNumber(fromChainId)} ...`)
console.log(`DO ESTIMATE GAS AT SOURCE BLOCK NUMBER: ${await blockNumber(fromChainId)} ...`)

const portAddresses = config.portAddresses;
const ormpAddresses = config.ormpAddresses;

// PARAMS PREPARATION
const srcPortAddress = portAddresses[fromChainId]
console.log(`${logTitle} - srcPortAddress: ${srcPortAddress}`);
console.log(`srcPortAddress: ${srcPortAddress}`);
if (!srcPortAddress) {
throw new FeestimiError(`srcPortAddress not found`, {
context: { fromChainId },
});
}

const tgtPortAddress = portAddresses[toChainId]
console.log(`${logTitle} - tgtPortAddress: ${tgtPortAddress}`);
console.log(`tgtPortAddress: ${tgtPortAddress}`);
if (!tgtPortAddress) {
throw new FeestimiError(`tgtPortAddress not found`, {
context: { toChainId },
Expand All @@ -46,21 +43,21 @@ async function doEstimateFee(params, config): Promise<[string, string]> {
context: { toChainId },
});
}
console.log(`${logTitle} - tgtOrmpAddress: ${tgtOrmpAddress}`);
console.log(`tgtOrmpAddress: ${tgtOrmpAddress}`);

// BUILD FULL PAYLOAD
const fullPayload = buildFullPayload(fromUAAddress, toUAAddress, payload, fromChainId, srcPortAddress)
console.log(`${logTitle} - fullPayload: ${fullPayload}`);
console.log(`fullPayload: ${fullPayload}`);

// GAS ESTIMATION IF NOT PROVIDED
if (!gasLimit) {
console.log(`${logTitle} - TARGET GASLIMIT NOT PROVIDED, ESTIMATING AT TARGET BLOCK NUMBER: ${await blockNumber(toChainId)} ...`)
console.log(`TARGET GASLIMIT NOT PROVIDED, ESTIMATING AT TARGET BLOCK NUMBER: ${await blockNumber(toChainId)} ...`)
gasLimit = await estimateGas(toChainId, tgtOrmpAddress, tgtPortAddress, fullPayload);
console.log(`${logTitle} - - gasLimit: ${gasLimit}, fullPayload`)
console.log(`- gasLimit: ${gasLimit}, fullPayload`)

const arb = isArb(toChainId);
const baseGas = arb ? (await fetchBaseGas(toChainId)) : 0;
console.log(`${logTitle} - - baseGas: ${baseGas}${arb ? '(arb)' : ''}`)
console.log(`- baseGas: ${baseGas}${arb ? '(arb)' : ''}`)

let m = 1.2;
if (toChainId == 44) {
Expand All @@ -69,7 +66,7 @@ async function doEstimateFee(params, config): Promise<[string, string]> {
} else {
gasLimit = Math.round((baseGas + gasLimit) * m);
}
console.log(`${logTitle} - - gasLimit total: ${gasLimit}, (gasLimit+baseGas)*${m}`)
console.log(`- gasLimit total: ${gasLimit}, (gasLimit+baseGas)*${m}`)
}

// 1. BUILD PARAMS STR FOR UA TO CALL ORMP
Expand Down

0 comments on commit 29311a4

Please sign in to comment.