@funded-labs/plug-controller
Version:
Internet Computer Plug wallet's controller
71 lines (70 loc) • 4.41 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildSonicData = exports.SONIC_SWAP_CANISTER_ID = void 0;
const dab_js_1 = require("@funded-labs/dab-js");
const object_1 = require("../../../object");
exports.SONIC_SWAP_CANISTER_ID = '3xwpq-ziaaa-aaaah-qcn4a-cai';
const getHandledTokenInfo = (principal, canistersInfo) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b;
if (!principal)
return;
const canisterId = (0, object_1.parsePrincipal)(principal);
if ((_a = canistersInfo[canisterId]) === null || _a === void 0 ? void 0 : _a.tokenRegistryInfo)
return (_b = canistersInfo[canisterId]) === null || _b === void 0 ? void 0 : _b.tokenRegistryInfo;
else {
const registry = new dab_js_1.TokenRegistry();
const data = yield registry.get(canisterId);
return data;
}
});
const buildSonicData = ({ canisterId, details, operation, canistersInfo, tokenId, }) => __awaiter(void 0, void 0, void 0, function* () {
var _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
if (canisterId !== exports.SONIC_SWAP_CANISTER_ID)
return;
const { amount, amountIn, amountOut } = details || {};
const isSwap = (_e = (_d = (_c = operation === null || operation === void 0 ? void 0 : operation.toLowerCase) === null || _c === void 0 ? void 0 : _c.call(operation)) === null || _d === void 0 ? void 0 : _d.includes) === null || _e === void 0 ? void 0 : _e.call(_d, 'swap');
const isLiquidity = (_h = (_g = (_f = operation === null || operation === void 0 ? void 0 : operation.toLowerCase) === null || _f === void 0 ? void 0 : _f.call(operation)) === null || _g === void 0 ? void 0 : _g.includes) === null || _h === void 0 ? void 0 : _h.call(_g, 'liquidity');
let data = {
token: (_j = canistersInfo[tokenId]) === null || _j === void 0 ? void 0 : _j.tokenRegistryInfo,
amount,
};
const formatSwapData = (data) => ((data === null || data === void 0 ? void 0 : data.tokenRegistryInfo) || {
name: data === null || data === void 0 ? void 0 : data.name,
thumbnail: data === null || data === void 0 ? void 0 : data.logo_url,
logo: data === null || data === void 0 ? void 0 : data.logo_url,
description: data === null || data === void 0 ? void 0 : data.description,
});
const from = formatSwapData(canistersInfo[details === null || details === void 0 ? void 0 : details.from]);
const to = formatSwapData(canistersInfo[details === null || details === void 0 ? void 0 : details.to]);
if (isSwap) {
data.swap = {
from,
to,
amountIn,
amountOut,
};
delete data.amount;
}
if (isLiquidity) {
const token0 = (_k = canistersInfo[details === null || details === void 0 ? void 0 : details.token0]) === null || _k === void 0 ? void 0 : _k.tokenRegistryInfo;
const token1 = (_l = canistersInfo[details === null || details === void 0 ? void 0 : details.token1]) === null || _l === void 0 ? void 0 : _l.tokenRegistryInfo;
const pair = `${(_m = token0 === null || token0 === void 0 ? void 0 : token0.details) === null || _m === void 0 ? void 0 : _m.symbol}/${(_o = token1 === null || token1 === void 0 ? void 0 : token1.details) === null || _o === void 0 ? void 0 : _o.symbol}`;
data.liquidity = {
pair,
token0: { token: token0, amount: details === null || details === void 0 ? void 0 : details.amount0 },
token1: { token: token1, amount: details === null || details === void 0 ? void 0 : details.amount1 },
};
delete data.amount;
}
return data;
});
exports.buildSonicData = buildSonicData;