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

settledAmount flow added #112

Merged
merged 1 commit into from
Dec 28, 2023
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"crypto-js": "^4.1.1",
"dotenv": "^16.0.3",
"ethereumjs-util": "^7.1.5",
"ethers": "^6.3.0",
"ethers": "^5.7.2",
"express": "^4.17.2",
"web3": "^1.8.1",
"web3-utils": "^1.9.0",
Expand Down
36 changes: 8 additions & 28 deletions src/constants/FiberRouter.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@
{
"indexed": false,
"internalType": "uint256",
"name": "swapBridgeAmount",
"name": "settledAmount",
"type": "uint256"
},
{
"indexed": false,
"internalType": "bytes32",
"name": "withdrawlData",
"name": "withdrawalData",
"type": "bytes32"
}
],
Expand Down Expand Up @@ -131,13 +131,13 @@
{
"indexed": false,
"internalType": "uint256",
"name": "swapBridgeAmount",
"name": "settledAmount",
"type": "uint256"
},
{
"indexed": false,
"internalType": "bytes32",
"name": "withdrawlData",
"name": "withdrawalData",
"type": "bytes32"
}
],
Expand Down Expand Up @@ -387,14 +387,9 @@
"name": "targetAddress",
"type": "string"
},
{
"internalType": "uint256",
"name": "swapBridgeAmount",
"type": "uint256"
},
{
"internalType": "bytes32",
"name": "withdrawlData",
"name": "withdrawalData",
"type": "bytes32"
}
],
Expand Down Expand Up @@ -445,14 +440,9 @@
"name": "foundryToken",
"type": "address"
},
{
"internalType": "uint256",
"name": "swapBridgeAmount",
"type": "uint256"
},
{
"internalType": "bytes32",
"name": "withdrawlData",
"name": "withdrawalData",
"type": "bytes32"
}
],
Expand Down Expand Up @@ -560,14 +550,9 @@
"name": "targetAddress",
"type": "address"
},
{
"internalType": "uint256",
"name": "swapBridgeAmount",
"type": "uint256"
},
{
"internalType": "bytes32",
"name": "withdrawlData",
"name": "withdrawalData",
"type": "bytes32"
}
],
Expand Down Expand Up @@ -603,11 +588,6 @@
"name": "crossTargetAddress",
"type": "address"
},
{
"internalType": "uint256",
"name": "swapBridgeAmount",
"type": "uint256"
},
{
"internalType": "bytes",
"name": "oneInchData",
Expand All @@ -625,7 +605,7 @@
},
{
"internalType": "bytes32",
"name": "withdrawlData",
"name": "withdrawalData",
"type": "bytes32"
}
],
Expand Down
8 changes: 4 additions & 4 deletions src/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export const NUMBER_OF_VALIDATORS_SHOULD_BE = 1;
export const NETWORKS = [
{
chainId: '56',
fundManagerAddress: '0xd9B93DCAbaa1e68c1E8cc6c84d44e76040F78973',
fiberRouterAddress: '0xEf062ED0e27Ef97c7E942310Ab6d702321EEA1D9',
fundManagerAddress: '0xbbf9c58D7fbfb3910ec3D22119c89715aca81Ae0',
fiberRouterAddress: '0x956DC094C1Ca37CC22B2047b9Ab13FC6eC124900',
foundaryTokenAddress: '0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d',
},
{
Expand All @@ -33,8 +33,8 @@ export const NETWORKS = [
},
{
chainId: '42161',
fundManagerAddress: '0xcfddF60db000D49d0F2dafd7eDB08Fca177F1A1E',
fiberRouterAddress: '0x0d618f4632C135e05d9fD795bab021e7DD3187c4',
fundManagerAddress: '0xf9d6a8918e41dc36cbd86b30c525401e1f45ce58',
fiberRouterAddress: '0x11c7deb29d992b5deacb4c7b0e85d335e0178794',
foundaryTokenAddress: '0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8',
},
{
Expand Down
103 changes: 71 additions & 32 deletions src/constants/utils.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,84 @@
import erc20Abi from '../constants/IERC20.json';
var { Big } = require("big.js");
const { Big } = require('big.js');
const { ethers } = require('ethers');

export const amountToHuman = async (
web3: any,
token: string,
amount: string
web3: any,
token: string,
amount: string,
) => {
let decimal = await decimals(web3, token);
if (decimal) {
let decimalFactor = 10 ** decimal;
return new Big(amount).div(decimalFactor).toFixed();
}
return null;
let decimal = await decimals(web3, token);
if (decimal) {
let decimalFactor = 10 ** decimal;
return new Big(amount).div(decimalFactor).toFixed();
}
return null;
};

export const amountToMachine = async (
web3: any,
token: string,
amount: string
web3: any,
token: string,
amount: string,
) => {
let decimal = await decimals(web3, token);
let decimalFactor = 10 ** decimal;
return new Big(amount).times(decimalFactor).toFixed(0);
let decimal = await decimals(web3, token);
let decimalFactor = 10 ** decimal;
return new Big(amount).times(decimalFactor).toFixed(0);
};

const erc20 = (
web3: any,
token: string
) => {
return new web3.eth.Contract(erc20Abi as any, token);
const erc20 = (web3: any, token: string) => {
return new web3.eth.Contract(erc20Abi as any, token);
};

const decimals = async (
web3: any,
token: string
) => {
if (web3 && token) {
let con = erc20(web3, token)
if (con) {
return await con.methods.decimals().call();
}
export const decimals = async (web3: any, token: string) => {
if (web3 && token) {
let con = erc20(web3, token);
if (con) {
return await con.methods.decimals().call();
}
return null;
}
}
return null;
};

export const removeExponential = function (n: any) {
var sign = +n < 0 ? '-' : '',
toStr = n.toString();
if (!/e/i.test(toStr)) {
return n;
}
var [lead, decimal, pow] = n
.toString()
.replace(/^-/, '')
.replace(/^([0-9]+)(e.*)/, '$1.$2')
.split(/e|\./);
return +pow < 0
? sign +
'0.' +
'0'.repeat(Math.max(Math.abs(pow) - 1 || 0, 0)) +
lead +
decimal
: sign +
lead +
(+pow >= decimal.length
? decimal + '0'.repeat(Math.max(+pow - decimal.length || 0, 0))
: decimal.slice(0, +pow) + '.' + decimal.slice(+pow));
};

export const numberIntoDecimals = function (amount: any, decimal: any) {
let formattedValue = ethers.utils.parseUnits(amount.toString(), decimal);
formattedValue = removeExponential(formattedValue.toString());
return formattedValue;
};

export const decimalsIntoNumber = function (amount: any, decimal: any) {
const bigNumberValue = ethers.BigNumber.from(amount.toString());
let formattedValue = ethers.utils.formatUnits(bigNumberValue, decimal);
formattedValue = removeExponential(formattedValue.toString());
return formattedValue;
};

export const withSlippage = function (value: any, slippage: number) {
let slippageProportion = 100 - slippage;
let valueWithSlippage = (value * slippageProportion) / 100;
valueWithSlippage = removeExponential(valueWithSlippage.toString());
return valueWithSlippage;
};
2 changes: 2 additions & 0 deletions src/interfaces/job.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface JobRequestBody {
withdrawalData: string;
sourceChainId: string;
destinationChaibId: string;
slippage: number;
}

export interface SignatureData {
Expand All @@ -29,6 +30,7 @@ export interface SignatureData {
targetToken: string;
targetAddress: string;
swapBridgeAmount: string;
settledAmount: string;
}

export interface UpdateJobRequestBody {
Expand Down
60 changes: 55 additions & 5 deletions src/services/signature.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Web3 from 'web3';
import { transactionService, web3Service } from './index';
import { transactionService, web3Service, rpcNodeService } from './index';
import {
NAME,
VERSION,
Expand All @@ -22,10 +22,11 @@ import {
pubToAddress,
bufferToHex,
} from 'ethereumjs-util';
import { decimals, decimalsIntoNumber, withSlippage } from '../constants/utils';

export const getDataForSignature = (job: any): any => {
export const getDataForSignature = async (job: any): Promise<any> => {
let decodedData = job.signatureData;
const withdrawalData = getValidWithdrawalData(job.data);
const withdrawalData = await getValidWithdrawalData(job.data, decodedData);
const txData = {
transactionHash: job.data.txId,
from: decodedData.from,
Expand Down Expand Up @@ -57,7 +58,10 @@ export const getDataForSignature = (job: any): any => {
return txData;
};

export const getValidWithdrawalData = (data: any): any => {
export const getValidWithdrawalData = async (
data: any,
decodedData: any,
): Promise<any> => {
let latestHash = Web3.utils.keccak256(
data.sourceOneInchData +
data.destinationOneInchData +
Expand All @@ -68,7 +72,16 @@ export const getValidWithdrawalData = (data: any): any => {
);
console.log('latestHash', latestHash);
console.log('withdrawlDataHash', data.withdrawalData);
if (latestHash == data.withdrawalData) {
if (
latestHash == data.withdrawalData &&
(await isValidSettledAmount(
data.slippage,
decodedData.sourceChainId,
decodedData.targetChainId,
data.destinationAmountIn,
decodedData.settledAmount,
))
) {
return {
sourceOneInchData: data.sourceOneInchData,
destinationOneInchData: data.destinationOneInchData,
Expand All @@ -81,6 +94,43 @@ export const getValidWithdrawalData = (data: any): any => {
return null;
};

export const isValidSettledAmount = async (
slippage: number,
sourceChainId: string,
destinationChainId: string,
destinationAmountIn: any,
settledAmount: any,
): Promise<boolean> => {
console.log(
slippage,
sourceChainId,
destinationChainId,
destinationAmountIn,
settledAmount,
);
const sWeb3 = new Web3(rpcNodeService.getRpcNodeByChainId(sourceChainId).url);
const dWeb3 = new Web3(
rpcNodeService.getRpcNodeByChainId(destinationChainId).url,
);
let sDecimal = await decimals(
sWeb3,
web3Service.getFoundaryTokenAddress(sourceChainId),
);
let dDecimal = await decimals(
dWeb3,
web3Service.getFoundaryTokenAddress(destinationChainId),
);
settledAmount = decimalsIntoNumber(settledAmount, sDecimal);
destinationAmountIn = decimalsIntoNumber(destinationAmountIn, dDecimal);
let minValue = withSlippage(destinationAmountIn, slippage);
let maxValue = destinationAmountIn;
console.log(minValue, settledAmount, maxValue);
if (settledAmount >= minValue && settledAmount <= maxValue) {
return true;
}
return false;
};

export const createSignedPayment = (
chainId: string,
payee: string,
Expand Down
2 changes: 2 additions & 0 deletions src/services/transaction.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export async function prepareObjectsAndVerifySignatures(tx: any) {
withdrawalData: tx.withdrawalData,
sourceChainId: sourceNetwork.chainId,
destinationChaibId: destinationNetwork.chainId,
slippage: tx.slippage,
};

let signatureData: SignatureData = {
Expand All @@ -40,6 +41,7 @@ export async function prepareObjectsAndVerifySignatures(tx: any) {
targetToken: tx.destinationCabn.tokenContractAddress,
targetAddress: tx.destinationWalletAddress,
swapBridgeAmount: tx.sourceBridgeAmount,
settledAmount: tx.settledAmount,
};
let job: any = {
data: data,
Expand Down