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

Modifications #162

Merged
merged 3 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/interfaces/fiberEngineInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,15 @@ export interface WithdrawSignedAndSwapOneInch {
signatureExpiry: number;
signature: string;
}

export interface SwapOneInch {
amountIn: string;
amountOut: string;
targetChainId: string;
targetTokenAddress: string;
destinationWalletAddress: string;
sourceOneInchData: string;
sourceTokenAddress: string;
foundryTokenAddress: string;
withdrawalData: string;
}
37 changes: 36 additions & 1 deletion app/lib/middlewares/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ const { ethers } = require("ethers");
const routerAbiMainnet = require("../../../artifacts/contracts/common/uniswap/IUniswapV2Router02.sol/IUniswapV2Router02.json");
const fundManagerAbiMainnet = require("../../../artifacts/contracts/upgradeable-Bridge/FundManager.sol/FundManager.json");
const fiberRouterAbiMainnet = require("../../../artifacts/contracts/upgradeable-Bridge/FiberRouter.sol/FiberRouter.json");
import { getSlippage } from "../../lib/middlewares/helpers/configurationHelper";
import {
getSlippage,
getNativeTokens,
} from "../../lib/middlewares/helpers/configurationHelper";

module.exports = {
getHashedPassword: function (password: any) {
Expand Down Expand Up @@ -280,4 +283,36 @@ module.exports = {
(global as any).environment.PRI_KEY = this.getPrivateKey();
} catch (e) {}
},

getWrappedNativeTokenAddress: async function (
address: string
): Promise<string> {
let tokens: any = await getNativeTokens();
for (let item of tokens || []) {
if (item?.address.toLowerCase() == address.toLowerCase()) {
return item?.wrappedAddress;
}
}
return address;
},

getOneInchTokenAddress: async function (address: string): Promise<string> {
let tokens: any = await getNativeTokens();
for (let item of tokens || []) {
if (item?.address.toLowerCase() == address.toLowerCase()) {
return item?.oneInchAddress;
}
}
return address;
},

isNativeToken: async function (address: string): Promise<boolean> {
let tokens = await getNativeTokens();
for (let item of tokens || []) {
if (item?.address.toLowerCase() == address.toLowerCase()) {
return true;
}
}
return false;
},
};
5 changes: 5 additions & 0 deletions app/lib/middlewares/helpers/configurationHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ export const getSlippage = async (): Promise<number> => {
let data = await db.Configurations.findOne();
return data?.slippage ? data?.slippage : 2;
};

export const getNativeTokens = async (): Promise<any> => {
let data = await db.Configurations.findOne();
return data?.nativeTokens ? data?.nativeTokens : [];
};
88 changes: 80 additions & 8 deletions app/lib/middlewares/helpers/fiberEngineHelper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
SwapOneInch,
WithdrawSigned,
WithdrawSignedAndSwapOneInch,
} from "../../../interfaces/fiberEngineInterface";
Expand Down Expand Up @@ -57,16 +58,19 @@ export const doFoundaryWithdraw = async (
obj: WithdrawSigned,
targetNetwork: any,
targetSigner: any,
targetChainId: any
targetChainId: any,
isDynamicGasLimit = true
): Promise<any> => {
let result;
try {
let gasLimit;
if (await isAllowedDynamicGasValues(targetChainId)) {
if ((await isAllowedDynamicGasValues(targetChainId)) && isDynamicGasLimit) {
gasLimit = await targetNetwork.fiberRouterContract
.connect(targetSigner)
.estimateGas.withdrawSigned(
obj.targetTokenAddress,
await (global as any).commonFunctions.getOneInchTokenAddress(
obj.targetTokenAddress
),
obj.destinationWalletAddress,
obj.destinationAmountIn,
obj.salt,
Expand All @@ -78,7 +82,9 @@ export const doFoundaryWithdraw = async (
result = await targetNetwork.fiberRouterContract
.connect(targetSigner)
.withdrawSigned(
obj.targetTokenAddress,
await (global as any).commonFunctions.getOneInchTokenAddress(
obj.targetTokenAddress
),
obj.destinationWalletAddress,
obj.destinationAmountIn,
obj.salt,
Expand All @@ -88,6 +94,15 @@ export const doFoundaryWithdraw = async (
);
} catch (e) {
console.log(e);
if (isDynamicGasLimit) {
result = await doFoundaryWithdraw(
obj,
targetNetwork,
targetSigner,
targetChainId,
false
);
}
}
return result;
};
Expand All @@ -96,20 +111,23 @@ export const doOneInchWithdraw = async (
obj: WithdrawSignedAndSwapOneInch,
targetNetwork: any,
targetSigner: any,
targetChainId: any
targetChainId: any,
isDynamicGasLimit = true
): Promise<any> => {
let result;
try {
let gasLimit;
if (await isAllowedDynamicGasValues(targetChainId)) {
if ((await isAllowedDynamicGasValues(targetChainId)) && isDynamicGasLimit) {
gasLimit = await targetNetwork.fiberRouterContract
.connect(targetSigner)
.estimateGas.withdrawSignedAndSwapOneInch(
obj.destinationWalletAddress,
obj.destinationAmountIn,
obj.destinationAmountOut,
obj.targetFoundryTokenAddress,
obj.targetTokenAddress,
await (global as any).commonFunctions.getOneInchTokenAddress(
obj.targetTokenAddress
),
obj.destinationOneInchData,
obj.salt,
obj.signatureExpiry,
Expand All @@ -124,13 +142,67 @@ export const doOneInchWithdraw = async (
obj.destinationAmountIn,
obj.destinationAmountOut,
obj.targetFoundryTokenAddress,
obj.targetTokenAddress,
await (global as any).commonFunctions.getOneInchTokenAddress(
obj.targetTokenAddress
),
obj.destinationOneInchData,
obj.salt,
obj.signatureExpiry,
obj.signature,
await getGasForWithdraw(targetChainId, gasLimit)
);
} catch (e) {
console.log(e);
if (isDynamicGasLimit) {
result = await doOneInchWithdraw(
obj,
targetNetwork,
targetSigner,
targetChainId,
false
);
}
}
return result;
};

export const doOneInchSwap = async (
obj: SwapOneInch,
fiberRouter: any
): Promise<any> => {
let result;
try {
if (
await (global as any).commonFunctions.isNativeToken(
obj.sourceTokenAddress
)
) {
result = fiberRouter.methods.swapAndCrossOneInchETH(
obj.amountOut,
obj.targetChainId,
await (global as any).commonFunctions.getOneInchTokenAddress(
obj.targetTokenAddress
),
obj.destinationWalletAddress,
obj.sourceOneInchData,
obj.foundryTokenAddress,
obj.withdrawalData
);
} else {
result = fiberRouter.methods.swapAndCrossOneInch(
obj.amountIn,
obj.amountOut,
obj.targetChainId,
await (global as any).commonFunctions.getOneInchTokenAddress(
obj.targetTokenAddress
),
obj.destinationWalletAddress,
obj.sourceOneInchData,
obj.sourceTokenAddress,
obj.foundryTokenAddress,
obj.withdrawalData
);
}
} catch (e) {
console.log(e);
}
Expand Down
2 changes: 1 addition & 1 deletion app/lib/middlewares/helpers/gasEstimationHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const getGasForWithdraw = async (
Web3.utils.toWei(maxPriorityFeePerGas, "gwei")
);

data.gasLimit = item?.isAllowedDynamicGasLimit
data.gasLimit = dynamicGasLimit
? dynamicGasLimit?.toString()
: staticGasLimit;
}
Expand Down
12 changes: 6 additions & 6 deletions app/lib/middlewares/helpers/multiSwapHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ module.exports = {
let data: any = {};
data = await fiberEngine.swapForAbi(
req.query.sourceWalletAddress,
req.query.sourceTokenContractAddress, // goerli ada
req.query.destinationTokenContractAddress, // bsc ada
req.query.sourceNetworkChainId, // source chain id (goerli)
req.query.destinationNetworkChainId, // target chain id (bsc)
req.query.sourceAmount, //source token amount
req.query.destinationWalletAddress, // destination wallet address
req.query.sourceTokenContractAddress,
req.query.destinationTokenContractAddress,
req.query.sourceNetworkChainId,
req.query.destinationNetworkChainId,
req.query.sourceAmount,
req.query.destinationWalletAddress,
req.query
);
return data;
Expand Down
2 changes: 1 addition & 1 deletion app/lib/middlewares/helpers/withdrawResponseHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const createEVMResponse = (tx: any): Response => {
if (tx != null && tx.status != null && tx.status == true) {
responseCode = CODE_200;
responseMessage = SUCCESS;
} else if (tx.code == CODE_701) {
} else if (tx?.code == CODE_701) {
responseCode = CODE_701;
responseMessage = IN_SUFFICIENT_LIQUIDITY_ERROR;
}
Expand Down
7 changes: 7 additions & 0 deletions app/models/configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ var collectionName = "configurations";
var schema = mongoose.Schema(
{
slippage: { type: Number, default: 0 },
nativeTokens: [
{
address: { type: String, default: "" },
wrappedAddress: { type: String, default: "" },
oneInchAddress: { type: String, default: "" },
},
],
isActive: { type: Boolean, default: true },
createdAt: { type: Date, default: new Date() },
updatedAt: { type: Date, default: new Date() },
Expand Down
Loading