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

Feature/gas estimation optimization #390

2 changes: 2 additions & 0 deletions app/controllers/api/v1/currencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,8 @@ module.exports = function (router: any) {
isDefault: 1,
nonDefaultCurrencyInformation: 1,
createdByusers: 1,
sourceSlippage: 1,
destinationSlippage: 1,
"network._id": 1,
"network.name": 1,
"network.nameInLower": 1,
Expand Down
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;
}
20 changes: 20 additions & 0 deletions app/controllers/api/v1/super-admin/currencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,26 @@ module.exports = function (router: any) {
});
});

router.put("/cabn/update/slippage/:id", async (req: any, res: any) => {
let body: any = {};
let filters: any = { _id: req.params.id };
if (req.body.sourceSlippage) {
body.sourceSlippage = req.body.sourceSlippage;
}
if (req.body.destinationSlippage) {
body.destinationSlippage = req.body.destinationSlippage;
}
let cabn = await db.CurrencyAddressesByNetwork.findOneAndUpdate(
filters,
body,
{ new: true }
);

return res.http200({
cabn: cabn,
});
});

router.post(
"/create/bulk/cabns",
asyncMiddleware(async (req: any, res: any) => {
Expand Down
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;
}
3 changes: 3 additions & 0 deletions app/helpers/multiSwapHelpers/swapTransactionHelper.ts
Original file line number Diff line number Diff line change
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: 0 additions & 1 deletion app/lib/httpCalls/fiberAxiosHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ module.exports = {
body.sourceBridgeAmount = model.sourceBridgeAmount;
body.sourceAssetType = model.sourceAssetType;
body.destinationAssetType = model.destinationAssetType;
body.gasLimit = model.gasPrices?.destination?.gasLimit;
body.destinationOneInchSelector = model.destinationOneInchSelector;

console.log("getWithdrawBody body", body);
Expand Down
2 changes: 2 additions & 0 deletions app/models/currencyAddressesByNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ var schema = mongoose.Schema(
symbol: { type: String, default: "" },
},
isCreatedFromBulk: { type: Boolean, default: false },
sourceSlippage: { type: Number, default: 2 },
destinationSlippage: { type: Number, default: 2 },
createdAt: { type: Date, default: new Date() },
updatedAt: { type: Date, default: new Date() },
},
Expand Down
4 changes: 3 additions & 1 deletion app/models/swapAndWithdrawTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ var schema = mongoose.Schema(
signatureExpiry: { type: Number, default: null },
responseCode: { type: Number, default: "" },
responseMessage: {},
slippage: { type: Number, default: 2 },
sourceSlippage: { type: Number, default: 2 },
destinationSlippage: { type: Number, default: 2 },
settledAmount: { type: String, default: "" },
sourceToken: { type: String, default: "" },
targetToken: { type: String, default: "" },
Expand All @@ -113,6 +114,7 @@ var schema = mongoose.Schema(
},
sourceOneInchSelector: { type: String, default: "" },
destinationOneInchSelector: { type: String, default: "" },
isSameNetworkSwap: { type: Boolean, default: false },
},
{ collection: "swapAndWithdrawTransactions" }
);
Expand Down
Loading