Skip to content

Commit

Permalink
Merge pull request #2 from zikriya/develop
Browse files Browse the repository at this point in the history
getTokenCategorizedAndQouteInformation api
  • Loading branch information
zikriya committed Dec 10, 2022
2 parents c70a4f9 + 4ba632b commit cd90278
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 126 deletions.
23 changes: 6 additions & 17 deletions app/controllers/api/v1/multiswap.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
var Category = require("../../../../scripts/FiberNode");
module.exports = function (router: any) {

router.get('/token/categorized/info', asyncMiddleware(async (req: any, res: any) => {

const category = new Category()

// Params
// sourceChainId, sourcetokenAddress, targetChainId, targetTokenAddress, inputAmount

category.categoriseSwapAssets(5, "0x93e7a4C6FF5f5D786a33076c8F9D380E1bbA7E90", 97, "0x8834b57Fb0162977011C9D11dFF1d24b93073DA6", 12).then(console.log);
router.get('/token/categorized/qoute/info', asyncMiddleware(async (req: any, res: any) => {

if (!req.query.sourceTokenContractAddress || !req.query.sourceNetworkChainId
|| !req.query.sourceAmount || !req.query.destinationTokenContractAddress
|| !req.query.destinationNetworkChainId ) {
return res.http401('sourceTokenContractAddress & sourceNetworkChainId & sourceAmount & destinationTokenContractAddress & destinationNetworkChainId are missing');
}

return res.http200({
data: await multiSwapHelper.getTokenCategorizedInformation(req)
});

}));

router.get('/approval/signed', asyncMiddleware(async (req: any, res: any) => {

return res.http200({
data: await multiSwapHelper.getApprovalSigned(req)
});

}));

router.get('/swap/signed', asyncMiddleware(async (req: any, res: any) => {

return res.http200({
Expand Down
40 changes: 16 additions & 24 deletions app/lib/middlewares/helpers/multiSwapHelper.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
var HDWalletProvider = require("@truffle/hdwallet-provider");
var Web3= require("web3");
import crypto from 'crypto';
var CryptoJS = require("crypto-js");

module.exports = {

getTokenCategorizedInformation: async function (req: any) {
let data: any = {};

let sourceTokenCategorizedInfo: any = {};
sourceTokenCategorizedInfo.isFoundary = true;
sourceTokenCategorizedInfo.isRefinary = true;
sourceTokenCategorizedInfo.isIonic = true;
sourceTokenCategorizedInfo.sourceAmount = req.query.sourceAmount;

let destinationTokenCategorizedInfo: any = {};
destinationTokenCategorizedInfo.isFoundary = true;
destinationTokenCategorizedInfo.isRefinary = true;
destinationTokenCategorizedInfo.isIonic = true;
destinationTokenCategorizedInfo.sourceAmount = 20;

data.sourceTokenCategorizedInfo = sourceTokenCategorizedInfo;
data.destinationTokenCategorizedInfo = destinationTokenCategorizedInfo;
let categorizedInfo = await fiberNode.categoriseSwapAssets(req.query.sourceNetworkChainId, req.query.sourceTokenContractAddress, req.query.destinationNetworkChainId, req.query.destinationTokenContractAddress, req.query.sourceAmount);
console.log(categorizedInfo);
let data: any = {};

return data;
},
if(categorizedInfo){
let sourceTokenCategorizedInfo: any = {};
sourceTokenCategorizedInfo.type = categorizedInfo.sourceAssetType;
sourceTokenCategorizedInfo.sourceAmount = req.query.sourceAmount;

let destinationTokenCategorizedInfo: any = {};
destinationTokenCategorizedInfo.type = categorizedInfo.targetAssetType;
destinationTokenCategorizedInfo.sourceAmount = 20;

data.sourceTokenCategorizedInfo = sourceTokenCategorizedInfo;
data.destinationTokenCategorizedInfo = destinationTokenCategorizedInfo;

}

getApprovalSigned: async function (req: any) {
let data: any = {};
return data;
},

Expand Down
3 changes: 2 additions & 1 deletion app/lib/middlewares/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ commonFunctions: any,
stringHelper: any,
authHelper: any,
signatureHelper: any,
multiSwapHelper: any
multiSwapHelper: any,
fiberNode: any



Expand Down
3 changes: 3 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ if (
// crons
(global as any).removeRandomKeyJob = require("./app/lib/crons/removeRandomKeyJob");

// fiber
(global as any).fiberNode = require("./scripts/fiberNode");


(global as any).kraken = app.kraken;

Expand Down
163 changes: 79 additions & 84 deletions scripts/fiberNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,96 +97,91 @@ async function isTargetRefineryAsset(targetNetwork, tokenAddress, amount) {
return false;
}
}
class Category {
constructor() { }
async categoriseSwapAssets(
sourceChainId,
sourcetokenAddress,
targetChainId,
targetTokenAddress,
inputAmount
) {
// mapping source and target networs (go to Network.js file)
const sourceNetwork = networks[sourceChainId];
const targetNetwork = networks[targetChainId];
// source token contract
const sourceTokenContract = new ethers.Contract(
sourcetokenAddress,
tokenAbi.abi,
sourceNetwork.provider
);
//convert to wei
const sourceTokenDecimal = await sourceTokenContract.decimals();
const amount = (inputAmount * 10 ** Number(sourceTokenDecimal)).toString();

//check source token type
let sourceAssetType;
let sourceBridgeAmount;

const isSourceTokenFoundryAsset = await sourceFACCheck(
sourceNetwork,
sourcetokenAddress,
amount
);
const isSourceTokenRefineryAsset = await isSourceRefineryAsset(
sourceNetwork,
sourcetokenAddress,
amount
);
module.exports = {

if (isSourceTokenFoundryAsset) {
sourceAssetType = "Foundry";
sourceBridgeAmount = amount;
} else if (!isSourceTokenFoundryAsset && isSourceTokenRefineryAsset) {
sourceAssetType = "Refinery";
// get bridge foundry amount afeter swap refinery to foundry
let path = [sourcetokenAddress, sourceNetwork.foundryTokenAddress];
const amounts = await sourceNetwork.dexContract.getAmountsOut(amount, path);
const amountsOut = amounts[1];
sourceBridgeAmount = amountsOut;
} else if (!isSourceTokenFoundryAsset && !isSourceTokenRefineryAsset) {
sourceAssetType = "Ionic";
// get bridge foundry amount after swap ionic to foundry
let path = [
categoriseSwapAssets: async function (
sourceChainId,
sourcetokenAddress,
targetChainId,
targetTokenAddress,
inputAmount
) {
// mapping source and target networs (go to Network.js file)
const sourceNetwork = networks[sourceChainId];
const targetNetwork = networks[targetChainId];
// source token contract
const sourceTokenContract = new ethers.Contract(
sourcetokenAddress,
sourceNetwork.weth,
sourceNetwork.foundryTokenAddress,
];
const amounts = await sourceNetwork.dexContract.getAmountsOut(amount, path);
const amountsOut = amounts[amounts.length - 1];
sourceBridgeAmount = amountsOut;
}
tokenAbi.abi,
sourceNetwork.provider
);
//convert to wei
const sourceTokenDecimal = await sourceTokenContract.decimals();
const amount = (inputAmount * 10 ** Number(sourceTokenDecimal)).toString();

//check target token type
let targetAssetType;
//check source token type
let sourceAssetType;
let sourceBridgeAmount;

const isTargetTokenFoundryAsset = await targetFACCheck(
targetNetwork,
targetTokenAddress,
sourceBridgeAmount
);
const isTargetTokenRefineryAsset = await isTargetRefineryAsset(
targetNetwork,
targetTokenAddress,
sourceBridgeAmount
);
if (isTargetTokenFoundryAsset) {
targetAssetType = "Foundry";
} else if (!isTargetTokenFoundryAsset && isTargetTokenRefineryAsset) {
targetAssetType = "Refinery";
} else if (!isTargetTokenFoundryAsset && !isTargetTokenRefineryAsset) {
targetAssetType = "Ionic";
}
const isSourceTokenFoundryAsset = await sourceFACCheck(
sourceNetwork,
sourcetokenAddress,
amount
);
const isSourceTokenRefineryAsset = await isSourceRefineryAsset(
sourceNetwork,
sourcetokenAddress,
amount
);

return {
sourceAssetType,
targetAssetType,
};
}
}
if (isSourceTokenFoundryAsset) {
sourceAssetType = "Foundry";
sourceBridgeAmount = amount;
} else if (!isSourceTokenFoundryAsset && isSourceTokenRefineryAsset) {
sourceAssetType = "Refinery";
// get bridge foundry amount afeter swap refinery to foundry
let path = [sourcetokenAddress, sourceNetwork.foundryTokenAddress];
const amounts = await sourceNetwork.dexContract.getAmountsOut(amount, path);
const amountsOut = amounts[1];
sourceBridgeAmount = amountsOut;
} else if (!isSourceTokenFoundryAsset && !isSourceTokenRefineryAsset) {
sourceAssetType = "Ionic";
// get bridge foundry amount after swap ionic to foundry
let path = [
sourcetokenAddress,
sourceNetwork.weth,
sourceNetwork.foundryTokenAddress,
];
const amounts = await sourceNetwork.dexContract.getAmountsOut(amount, path);
const amountsOut = amounts[amounts.length - 1];
sourceBridgeAmount = amountsOut;
}

module.exports = Category;
//check target token type
let targetAssetType;

const category = new Category()
const isTargetTokenFoundryAsset = await targetFACCheck(
targetNetwork,
targetTokenAddress,
sourceBridgeAmount
);
const isTargetTokenRefineryAsset = await isTargetRefineryAsset(
targetNetwork,
targetTokenAddress,
sourceBridgeAmount
);
if (isTargetTokenFoundryAsset) {
targetAssetType = "Foundry";
} else if (!isTargetTokenFoundryAsset && isTargetTokenRefineryAsset) {
targetAssetType = "Refinery";
} else if (!isTargetTokenFoundryAsset && !isTargetTokenRefineryAsset) {
targetAssetType = "Ionic";
}

category.categoriseSwapAssets(5, goerliAda, 97, bscAave, 12).then(console.log);
return {
sourceAssetType,
targetAssetType,
};
}
}

0 comments on commit cd90278

Please sign in to comment.