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

Settled amount and destinationAmountIn decoupled #55

Merged
merged 1 commit into from
May 15, 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
9 changes: 9 additions & 0 deletions src/constants/FiberRouter.json
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,15 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{ "internalType": "uint256", "name": "cdPtr", "type": "uint256" }
],
"name": "isCctp",
"outputs": [{ "internalType": "bool", "name": "cctp", "type": "bool" }],
"stateMutability": "pure",
"type": "function"
},
{
"inputs": [],
"name": "owner",
Expand Down
12 changes: 6 additions & 6 deletions src/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ export const NETWORKS = [
},
{
chainId: "42161",
fundManagerAddress: "0x5dBF50235fdD9D4D5669E6e0B3Bef3655dEAffe7",
fiberRouterAddress: "0xaDE10E8522AeCadBBe02279c133Fc46E45f92194",
fundManagerAddress: "0xA893673e9264195D8F5192975ABc3D3821de789d",
fiberRouterAddress: "0x6e84E6c702E6748C255A7A46347B01Db7CA95Bf3",
foundaryTokenAddress: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
aggregateRouterContractAddress:
"0x111111125421ca6dc452d289314280a0f8842a65",
cctpFundManager: "0x82496D3bDE9168dfeF6bc8dF0439B51790ff42DF",
cctpFundManager: "0xBe6D480a7f5519BFD9D4EAf6E4Fb86A217736168",
},
{
chainId: "10",
Expand All @@ -49,12 +49,12 @@ export const NETWORKS = [
},
{
chainId: "8453",
fundManagerAddress: "0x1938b36110e4016c54647de123a07F4dB13026C7",
fiberRouterAddress: "0x851930502D363D75C4a0C0c8366ADc43980aA048",
fundManagerAddress: "0x488aEa3F75271e9A79B86392E40de2Fbed468b2B",
fiberRouterAddress: "0xc74819b4e1Dd4B19A545a417378eB0bA7aD96ddb",
foundaryTokenAddress: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
aggregateRouterContractAddress:
"0x111111125421ca6dc452d289314280a0f8842a65",
cctpFundManager: "0x59C1CF95a57F63094637Af70008c55452fFD1b9f",
cctpFundManager: "0x648Abb2c787C2ED5E33506B729Bab5971541D08C",
},
{
chainId: "324",
Expand Down
6 changes: 5 additions & 1 deletion src/constants/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ export const removeExponential = function (n: any) {
};

export const numberIntoDecimals = function (amount: any, decimal: any) {
let formattedValue = ethers.utils.parseUnits(amount.toString(), decimal);
amount = Big(amount);
decimal = Big(10 ** Number(decimal));
let formattedValue = amount.mul(decimal);
formattedValue = removeExponential(formattedValue.toString());
formattedValue = parseInt(formattedValue);
formattedValue = removeExponential(formattedValue.toString());
return formattedValue;
};
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/job.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface JobRequestBody {
destinationChaibId: string;
slippage: number;
isCCTP: boolean;
minDestinationAmountIn: string;
}

export interface UpdateJobRequestBody {
Expand Down
62 changes: 39 additions & 23 deletions src/services/signature.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ import {
pubToAddress,
bufferToHex,
} from "ethereumjs-util";
import { decimals, decimalsIntoNumber, withSlippage } from "../constants/utils";
import {
decimals,
decimalsIntoNumber,
numberIntoDecimals,
withSlippage,
} from "../constants/utils";
import { getAggregateRouterTokenAddress } from "./web3.service";

export const getDataForSignature = async (
Expand Down Expand Up @@ -75,26 +80,27 @@ export const getValidWithdrawalData = async (
let latestHash = Web3.utils.keccak256(
data.sourceOneInchData +
data.destinationOneInchData +
data.destinationAmountIn +
data.minDestinationAmountIn +
data.destinationAmountOut +
data.sourceAssetType +
data.destinationAssetType
);
if (
latestHash == decodedData.withdrawalData &&
(await isValidSettledAmount(
data.slippage,
decodedData.sourceChainId,
decodedData.targetChainId,
data.destinationAmountIn,
decodedData.settledAmount,
distributedFee
))
) {
const { isValid, destinationAmountIn } = await isValidSettledAmount(
data.slippage,
decodedData.sourceChainId,
decodedData.targetChainId,
data.destinationAmountIn,
data.minDestinationAmountIn,
decodedData.settledAmount,
distributedFee
);
console.log({ isValid, destinationAmountIn });
console.log(latestHash, decodedData?.withdrawalData);
if (latestHash == decodedData.withdrawalData && isValid) {
return {
sourceOneInchData: data.sourceOneInchData,
destinationOneInchData: data.destinationOneInchData,
destinationAmountIn: data.destinationAmountIn,
destinationAmountIn: destinationAmountIn,
destinationAmountOut: data.destinationAmountOut,
sourceAssetType: data.sourceAssetType,
destinationAssetType: data.destinationAssetType,
Expand All @@ -108,15 +114,17 @@ export const isValidSettledAmount = async (
slippage: number,
sourceChainId: string,
destinationChainId: string,
destinationAmountIn: any,
destinationAmountIn: string,
minDestinationAmountIn: any,
settledAmount: any,
distributedFee: string
): Promise<boolean> => {
): Promise<any> => {
console.log(
slippage,
sourceChainId,
destinationChainId,
destinationAmountIn,
minDestinationAmountIn,
settledAmount
);
const sWeb3 = new Web3(rpcNodeService.getRpcNodeByChainId(sourceChainId).url);
Expand All @@ -134,12 +142,22 @@ export const isValidSettledAmount = async (
settledAmount = decimalsIntoNumber(settledAmount, sDecimal);
distributedFee = decimalsIntoNumber(distributedFee, sDecimal);
destinationAmountIn = decimalsIntoNumber(destinationAmountIn, dDecimal);
minDestinationAmountIn = decimalsIntoNumber(minDestinationAmountIn, dDecimal);
let sdAmount = Big(settledAmount).add(Big(distributedFee));
console.log(settledAmount, destinationAmountIn, sdAmount?.toString());
if (sdAmount.gte(Big(destinationAmountIn))) {
return true;
console.log(
settledAmount,
minDestinationAmountIn,
sdAmount?.toString(),
destinationAmountIn
);
if (
sdAmount.gte(Big(minDestinationAmountIn)) &&
Big(destinationAmountIn).eq(Big(settledAmount))
) {
destinationAmountIn = numberIntoDecimals(settledAmount, dDecimal);
return { isValid: true, destinationAmountIn };
}
return false;
return { isValid: false, destinationAmountIn };
};

export const createSignedPayment = (
Expand Down Expand Up @@ -248,7 +266,7 @@ export const produceOneInchHash = (
): any => {
const methodHash = Web3.utils.keccak256(
Web3.utils.utf8ToHex(
"withdrawSignedAndSwapRouter(address to,uint256 amountIn,uint256 minAmountOut,address foundryToken,address targetToken,address router,bytes32 routerCalldata,bytes32 salt,uint256 expiry)"
"withdrawSignedAndSwapRouter(address to,uint256 amountIn,uint256 minAmountOut,address foundryToken,address targetToken,address router,bytes32 salt,uint256 expiry)"
)
);
const params = [
Expand All @@ -260,7 +278,6 @@ export const produceOneInchHash = (
"address",
"address",
"bytes32",
"bytes32",
"uint256",
];
const structure = web3.eth.abi.encodeParameters(params, [
Expand All @@ -271,7 +288,6 @@ export const produceOneInchHash = (
foundryToken,
targetToken,
aggregateRouterContractAddress,
Web3.utils.keccak256(routerCalldata),
salt,
expiry,
]);
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 @@ -32,6 +32,7 @@ export async function fetchChainDataFromNetwork(tx: any) {
destinationChaibId: destinationNetwork.chainId,
slippage: tx.slippage,
isCCTP: tx?.isCCTP ? tx?.isCCTP : false,
minDestinationAmountIn: tx?.minDestinationAmountIn,
};

let job: any = { data: data, transaction: tx };
Expand Down Expand Up @@ -76,6 +77,7 @@ async function verifyAndCreateSignature(job: any) {
await updateTransaction(job, signedData, tx);
} catch (error) {
console.error("error occured", error);
await updateTransaction(job, null, null);
}
}

Expand Down