Skip to content

Commit

Permalink
Merge pull request #391 from ferrumnet/feature/gas-estimation-optimiz…
Browse files Browse the repository at this point in the history
…ation

Feature/gas estimation optimization
  • Loading branch information
zikriya authored May 1, 2024
2 parents 2bd44d7 + 277fa4b commit fd6d223
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 7 deletions.
109 changes: 109 additions & 0 deletions app/controllers/api/v1/super-admin/analytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
var mongoose = require("mongoose");
import moment from "moment";
var uab = require("unique-array-objects");

module.exports = function (router: any) {
// this is a rough script (which created quickly)
router.get("/", async (req: any, res: any) => {
try {
let totalMinutes = 0;
let totalSeconds = 0;
let totalUSDCVolume = 0;
let sourceWalletAddress: any = [];
let filters: any = {
version: "v3",
};
if (req.query.status) {
filters.status = { $gte: req.query.status };
}
if (req.query.dateTime) {
filters.createdAt = { $gte: req.query.dateTime };
}

let transactions = await db.SwapAndWithdrawTransactions.find(filters)
.populate("sourceCabn")
.populate("sourceNetwork");
for (let item of transactions || []) {
let dt1 = moment(item.createdAt).utc();
let dt2 = moment(item.updatedAt).utc();
var resultInSeconds = dt2.diff(dt1, "seconds");
if (resultInSeconds > 60) {
resultInSeconds = 60;
}
totalSeconds = totalSeconds + resultInSeconds;
console.log(
"resultInSeconds",
resultInSeconds,
item?.receiveTransactionId
);
sourceWalletAddress.push(item?.sourceWalletAddress);
}
let avgTimeInSeconds = totalSeconds / transactions.length;
console.log("===============================");
console.log("Avg time in seconds: ", avgTimeInSeconds);
console.log("Total transactions: ", transactions.length);
console.log("Total volume (USDC): ", totalUSDCVolume * 2);
if (sourceWalletAddress && sourceWalletAddress.length > 0) {
console.log(
"Number of unique source wallet addresses: ",
await uab(sourceWalletAddress).length
);
}
await getVolumePerNetwork(filters);
console.log("===============================");
return res.http200({ message: "" });
} catch (e) {
console.log(e);
}
});
};

async function getSettledAmount(item: any) {
let settledAmount = 0;
try {
settledAmount = await (global as any).swapUtilsHelper.amountToHuman_(
item.sourceNetwork,
item.sourceCabn,
item.settledAmount
);
} catch (e) {
settledAmount = await (global as any).swapUtilsHelper.amountToHuman(
item.settledAmount,
item.sourceCabn.decimals
);
}
return settledAmount;
}

async function getVolumePerNetwork(filters: any) {
let data: any = [];
let networks = await db.Networks.find({ isAllowedOnMultiSwap: true });
console.log("Total networks", networks.length);
let grandTotalUSDCVolume = 0;

for (let item of networks || []) {
let innerFilters = { ...filters };
innerFilters.sourceNetwork = item?._id;
let transactions = await db.SwapAndWithdrawTransactions.find(innerFilters)
.populate("sourceCabn")
.populate("sourceNetwork");
let totalUSDCVolume = 0;
for (let tx of transactions || []) {
if (tx.settledAmount) {
let settledAmount = await getSettledAmount(tx);
if (settledAmount) {
totalUSDCVolume = totalUSDCVolume + Number(settledAmount);
}
}
}
let network: any = {};
network.networkName = item?.name;
network.networkChainId = item?.chainId;
network.totalVolumeInUSDC = totalUSDCVolume * 2;
data.push(network);
grandTotalUSDCVolume = grandTotalUSDCVolume + totalUSDCVolume;
}
console.log("USDC Volume per network: ", data);
console.log("Total USDC Volume: ", grandTotalUSDCVolume * 2);
return data;
}
58 changes: 54 additions & 4 deletions app/helpers/multiSwapHelpers/generatorNodeHelper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { handleFiberRequest } from "../../helpers/multiSwapHelpers/fiberHelper";

export const handleGeneratorRequest = async (data: any, swapTxHash: string) => {
try {
let filter: any = {
Expand All @@ -19,10 +21,18 @@ export const handleGeneratorRequest = async (data: any, swapTxHash: string) => {
transactionReceipt?.status == true &&
data?.signedData
) {
transaction = getGeneratorSignedData(transaction, data?.signedData);
transaction = await getTransactionDetail(transaction, data?.signedData);
transaction.status =
utils.swapAndWithdrawTransactionStatuses.generatorSignatureCreated;
if (data?.signedData?.isSameNetworkSwap) {
await handleSameNetworkSwap(transaction, data?.signedData);
return;
} else {
transaction = getGeneratorSignedData(transaction, data?.signedData);
transaction = await getTransactionDetail(
transaction,
data?.signedData
);
transaction.status =
utils.swapAndWithdrawTransactionStatuses.generatorSignatureCreated;
}
} else {
transaction.status =
utils.swapAndWithdrawTransactionStatuses.generatorSignatureFailed;
Expand Down Expand Up @@ -96,3 +106,43 @@ async function getAmount(transaction: any) {

return amount;
}

async function getSettledAmount(transaction: any, settledAmount: any) {
let amount;
try {
amount = await swapUtilsHelper.amountToHuman_(
transaction.destinationNetwork,
transaction.destinationCabn,
settledAmount
);
} catch (e) {
console.log("for nativ tokens");
amount = await swapUtilsHelper.amountToHuman(
settledAmount,
transaction.destinationCabn.decimals
);
}

return amount;
}

async function handleSameNetworkSwap(transaction: any, signedData: any) {
try {
if (signedData.settledAmount) {
let destinationAmount = await getSettledAmount(
transaction,
signedData.settledAmount
);
let data = {
data: transaction?.receiveTransactionId,
withdraw: { destinationAmount: destinationAmount },
responseCode: 200,
responseMessage: "",
};
await handleFiberRequest(data, data?.data);
}
} catch (e) {
console.log(e);
}
return transaction;
}
7 changes: 5 additions & 2 deletions app/helpers/multiSwapHelpers/swapTransactionHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ module.exports = {
!req.query.sourceCabnId ||
!req.query.destinationCabnId ||
!req.body.sourceAssetType ||
!req.body.destinationAssetType
// !req.body.gasPrices
!req.body.destinationAssetType ||
!req.body.gasPrices
) {
throw "swapTxId & sourceNetworkId & destinationNetworkId & sourceCabnId & destinationCabnId & sourceAssetType & destinationAssetType & gasPrices are required.";
}
Expand Down Expand Up @@ -80,6 +80,9 @@ module.exports = {
: "";

let body: any = {};
if (sourceNetwork._id == destinationNetwork._id) {
body.isSameNetworkSwap = true;
}
body.receiveTransactionId = req.params.swapTxId;
body.version = "v3";
body.createdByUser = req.user._id;
Expand Down
1 change: 1 addition & 0 deletions app/lib/httpCalls/fiberAxiosHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ module.exports = {
body.sourceBridgeAmount = model.sourceBridgeAmount;
body.sourceAssetType = model.sourceAssetType;
body.destinationAssetType = model.destinationAssetType;
body.destinationOneInchSelector = model.destinationOneInchSelector;

console.log("getWithdrawBody body", body);
return body;
Expand Down
3 changes: 3 additions & 0 deletions app/models/swapAndWithdrawTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ var schema = mongoose.Schema(
gasLimit: { type: String, default: "" },
},
},
sourceOneInchSelector: { type: String, default: "" },
destinationOneInchSelector: { type: String, default: "" },
isSameNetworkSwap: { type: Boolean, default: false },
},
{ collection: "swapAndWithdrawTransactions" }
);
Expand Down
26 changes: 25 additions & 1 deletion resources/FiberRouter.json
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,15 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{ "internalType": "address", "name": "_weth", "type": "address" }
],
"name": "setWETH",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{ "internalType": "address", "name": "token", "type": "address" },
Expand Down Expand Up @@ -520,6 +529,11 @@
"internalType": "bytes32",
"name": "withdrawalData",
"type": "bytes32"
},
{
"internalType": "enum FiberRouter.OneInchFunction",
"name": "funcSelector",
"type": "uint8"
}
],
"name": "swapAndCrossOneInch",
Expand Down Expand Up @@ -556,7 +570,12 @@
"name": "withdrawalData",
"type": "bytes32"
},
{ "internalType": "uint256", "name": "gasFee", "type": "uint256" }
{ "internalType": "uint256", "name": "gasFee", "type": "uint256" },
{
"internalType": "enum FiberRouter.OneInchFunction",
"name": "funcSelector",
"type": "uint8"
}
],
"name": "swapAndCrossOneInchETH",
"outputs": [],
Expand Down Expand Up @@ -598,6 +617,11 @@
},
{ "internalType": "address", "name": "targetToken", "type": "address" },
{ "internalType": "bytes", "name": "oneInchData", "type": "bytes" },
{
"internalType": "enum FiberRouter.OneInchFunction",
"name": "funcSelector",
"type": "uint8"
},
{ "internalType": "bytes32", "name": "salt", "type": "bytes32" },
{ "internalType": "uint256", "name": "expiry", "type": "uint256" },
{ "internalType": "bytes", "name": "multiSignature", "type": "bytes" }
Expand Down

0 comments on commit fd6d223

Please sign in to comment.