@broxus/js-core
Version:
MobX-based JavaScript Core library
256 lines (255 loc) • 14 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DexStablePairUtils = void 0;
const constants_1 = require("../../constants");
const contracts_1 = require("../../models/dex-stable-pair/contracts");
const tvm_token_1 = require("../../models/tvm-token");
const utils_1 = require("../../utils");
class DexStablePairUtils {
static async getDetails(connection, pairAddress, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, pairAddress);
const [balances, roots, wallets, type, isActive] = await Promise.all([
DexStablePairUtils.getBalances(connection, pairAddress, state),
DexStablePairUtils.getTokenRoots(connection, pairAddress, state),
DexStablePairUtils.getTokenWallets(connection, pairAddress, state),
DexStablePairUtils.getPoolType(connection, pairAddress, state),
DexStablePairUtils.isActive(connection, pairAddress, state),
]);
const [lpToken, leftToken, rightToken, lpWallet, leftWallet, rightWallet] = await Promise.allSettled([
tvm_token_1.TvmToken.create(connection, roots.lp, { sync: true }),
tvm_token_1.TvmToken.create(connection, roots.left, { sync: true }),
tvm_token_1.TvmToken.create(connection, roots.right, { sync: true }),
tvm_token_1.TvmToken.Wallet.create(connection, { address: wallets.lp }, { sync: true }),
tvm_token_1.TvmToken.Wallet.create(connection, { address: wallets.left }, { sync: true }),
tvm_token_1.TvmToken.Wallet.create(connection, { address: wallets.right }, { sync: true }),
]).then(responses => responses.map(response => (response.status === 'fulfilled' ? response.value : undefined)));
if (!lpToken || !leftToken || !rightToken) {
throw new Error();
}
return {
address: (0, utils_1.resolveTvmAddress)(pairAddress),
balances,
contractState: state,
isActive,
lpToken,
lpWallet,
tokens: [leftToken, rightToken],
type,
wallets: [leftWallet, rightWallet],
};
}
static async getBalances(connection, pairAddress, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, pairAddress);
const [roots, balances] = await Promise.all([
DexStablePairUtils.getTokenRoots(connection, pairAddress, state),
(0, contracts_1.dexStablePairContract)(connection, pairAddress)
.methods.getBalances({ answerId: 0 })
.call({ cachedState: state, responsible: true })
.then(result => result.value0),
]);
return new Map([
[roots.left.toString(), balances.left_balance],
[roots.lp.toString(), balances.lp_supply],
[roots.right.toString(), balances.right_balance],
]);
}
static async getFeeParams(connection, pairAddress, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, pairAddress);
const { value0 } = await (0, contracts_1.dexStablePairContract)(connection, pairAddress)
.methods.getFeeParams({ answerId: 0 })
.call({ cachedState: state, responsible: true });
return {
beneficiaryAddress: value0.beneficiary,
beneficiaryNumerator: Number(value0.beneficiary_numerator),
denominator: Number(value0.denominator),
numerator: Number(value0.pool_numerator),
threshold: value0.threshold,
};
}
static async isActive(connection, pairAddress, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, pairAddress);
const result = await (0, contracts_1.dexStablePairContract)(connection, pairAddress)
.methods.isActive({ answerId: 0 })
.call({ cachedState: state, responsible: true });
return result.value0;
}
static async getTokenRoots(connection, pairAddress, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, pairAddress);
return (0, contracts_1.dexStablePairContract)(connection, pairAddress)
.methods.getTokenRoots({ answerId: 0 })
.call({ cachedState: state, responsible: true });
}
static async getTokenWallets(connection, pairAddress, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, pairAddress);
return (0, contracts_1.dexStablePairContract)(connection, pairAddress)
.methods.getTokenWallets({ answerId: 0 })
.call({ cachedState: state, responsible: true });
}
static async getPoolType(connection, pairAddress, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, pairAddress);
const result = await (0, contracts_1.dexStablePairContract)(connection, pairAddress)
.methods.getPoolType({ answerId: 0 })
.call({ cachedState: state, responsible: true });
return result.value0;
}
static async getRoot(connection, pairAddress, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, pairAddress);
const result = await (0, contracts_1.dexStablePairContract)(connection, pairAddress)
.methods.getRoot({ answerId: 0 })
.call({ cachedState: state, responsible: true });
return result.dex_root;
}
static async getVault(connection, pairAddress, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, pairAddress);
const result = await (0, contracts_1.dexStablePairContract)(connection, pairAddress)
.methods.getVault({ answerId: 0 })
.call({ cachedState: state, responsible: true });
return result.value0;
}
static async getAccumulatedFees(connection, pairAddress, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, pairAddress);
const result = await (0, contracts_1.dexStablePairContract)(connection, pairAddress)
.methods.getAccumulatedFees({ answerId: 0 })
.call({ cachedState: state, responsible: true });
return result.accumulatedFees;
}
static async buildCrossPairExchangePayload(connection, pairAddress, params, cachedState) {
const result = await (0, contracts_1.dexStablePairContract)(connection, pairAddress)
.methods.buildCrossPairExchangePayload({
deploy_wallet_grams: params.deployWalletGrams || 0,
expected_amount: params.expectedAmount,
id: params.callId ?? (0, utils_1.getRandomInt)(),
steps: params.steps.map(step => ({
amount: step.amount,
root: (0, utils_1.resolveTvmAddress)(step.root),
})),
})
.call({ cachedState });
return result.value0;
}
static async buildCrossPairExchangePayloadV2(connection, pairAddress, params, cachedState) {
const result = await (0, contracts_1.dexStablePairContract)(connection, pairAddress)
.methods.buildCrossPairExchangePayloadV2({
_cancelPayload: params.cancelPayload ?? null,
_deployWalletGrams: params.deployWalletGrams || 0,
_expectedAmount: params.expectedAmount,
_id: params.callId ?? (0, utils_1.getRandomInt)(),
_nextStepIndices: params.nextStepIndices,
_outcoming: (0, utils_1.resolveTvmAddress)(params.outcoming),
_recipient: (0, utils_1.resolveTvmAddress)(params.recipient),
_referrer: (0, utils_1.resolveTvmAddress)(params.referrer ?? constants_1.ZeroAddress),
_steps: params.steps.map(step => ({
amount: step.amount,
nextStepIndices: step.nextStepIndices,
numerator: step.numerator,
outcoming: (0, utils_1.resolveTvmAddress)(step.outcoming),
roots: step.roots.map(utils_1.resolveTvmAddress),
})),
_successPayload: params.successPayload ?? null,
_toNative: params.toNative ?? null,
})
.call({ cachedState });
return result.value0;
}
static async buildExchangePayload(connection, pairAddress, params, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, pairAddress);
const result = await (0, contracts_1.dexStablePairContract)(connection, pairAddress)
.methods.buildExchangePayload({
deploy_wallet_grams: params.deployWalletGrams || 0,
expected_amount: params.expectedAmount,
id: params.callId ?? (0, utils_1.getRandomInt)(),
})
.call({ cachedState: state });
return result.value0;
}
static async buildExchangePayloadV2(connection, pairAddress, params, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, pairAddress);
const result = await (0, contracts_1.dexStablePairContract)(connection, pairAddress)
.methods.buildExchangePayloadV2({
_cancelPayload: params.cancelPayload ?? null,
_deployWalletGrams: params.deployWalletGrams || 0,
_expectedAmount: params.expectedAmount,
_id: params.callId ?? (0, utils_1.getRandomInt)(),
_recipient: (0, utils_1.resolveTvmAddress)(params.recipient),
_referrer: (0, utils_1.resolveTvmAddress)(params.referrer ?? constants_1.ZeroAddress),
_successPayload: params.successPayload ?? null,
_toNative: params.toNative ?? null,
})
.call({ cachedState: state });
return result.value0;
}
static async buildDepositLiquidityPayload(connection, pairAddress, params, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, pairAddress);
const result = await (0, contracts_1.dexStablePairContract)(connection, pairAddress)
.methods.buildDepositLiquidityPayload({
deploy_wallet_grams: params.deployWalletGrams ?? 0,
id: params.callId ?? (0, utils_1.getRandomInt)(),
})
.call({ cachedState: state });
return result.value0;
}
static async buildDepositLiquidityPayloadV2(connection, pairAddress, params, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, pairAddress);
const result = await (0, contracts_1.dexStablePairContract)(connection, pairAddress)
.methods.buildDepositLiquidityPayloadV2({
_cancelPayload: params.cancelPayload ?? null,
_deployWalletGrams: params.deployWalletGrams || 0,
_expectedAmount: params.expectedAmount,
_id: params.callId ?? (0, utils_1.getRandomInt)(),
_recipient: (0, utils_1.resolveTvmAddress)(params.recipient),
_referrer: (0, utils_1.resolveTvmAddress)(params.referrer ?? constants_1.ZeroAddress),
_successPayload: params.successPayload ?? null,
})
.call({ cachedState: state });
return result.value0;
}
static async buildWithdrawLiquidityPayload(connection, pairAddress, params, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, pairAddress);
const result = await (0, contracts_1.dexStablePairContract)(connection, pairAddress)
.methods.buildWithdrawLiquidityPayload({
deploy_wallet_grams: params.deployWalletGrams || 0,
id: params.callId ?? (0, utils_1.getRandomInt)(),
})
.call({ cachedState: state });
return result.value0;
}
static async expectedDepositLiquidityV2(connection, pairAddress, params, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, pairAddress);
const result = await (0, contracts_1.dexStablePairContract)(connection, pairAddress)
.methods.expectedDepositLiquidityV2({
amounts: params.amounts,
answerId: 0,
})
.call({ cachedState: state, responsible: true });
return result.value0;
}
static async expectedExchange(connection, pairAddress, params, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, pairAddress);
const result = await (0, contracts_1.dexStablePairContract)(connection, pairAddress)
.methods.expectedExchange({
amount: params.amount,
answerId: 0,
spent_token_root: (0, utils_1.resolveTvmAddress)(params.spentTokenAddress),
})
.call({ cachedState: state, responsible: true });
return {
expectedAmount: result.expected_amount,
expectedFee: result.expected_fee,
};
}
static async expectedSpendAmount(connection, pairAddress, params, cachedState) {
const state = cachedState ?? await (0, utils_1.getFullContractState)(connection, pairAddress);
const result = await (0, contracts_1.dexStablePairContract)(connection, pairAddress)
.methods.expectedSpendAmount({
answerId: 0,
receive_amount: params.receiveAmount,
receive_token_root: (0, utils_1.resolveTvmAddress)(params.receiveTokenAddress),
})
.call({ cachedState: state, responsible: true });
return {
expectedAmount: result.expected_amount,
expectedFee: result.expected_fee,
};
}
}
exports.DexStablePairUtils = DexStablePairUtils;