rubic-sdk
Version:
Simplify dApp creation
64 lines • 3.04 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OrbiterUtils = void 0;
const errors_1 = require("../../../../../../common/errors");
const blockchain_1 = require("../../../../../../common/utils/blockchain");
const blockchain_name_1 = require("../../../../../../core/blockchain/models/blockchain-name");
const blockchain_id_1 = require("../../../../../../core/blockchain/utils/blockchains-info/constants/blockchain-id");
const get_from_without_fee_1 = require("../../../../../common/utils/get-from-without-fee");
const web3_1 = __importDefault(require("web3"));
class OrbiterUtils {
static compareChainId(orbiterChainId, blockchainName) {
if (blockchainName === blockchain_name_1.BLOCKCHAIN_NAME.STARKNET) {
return orbiterChainId === 'SN_MAIN';
}
if (blockchainName === blockchain_name_1.BLOCKCHAIN_NAME.TRON) {
return orbiterChainId === '728126428';
}
return orbiterChainId === blockchain_id_1.blockchainId[blockchainName].toString();
}
static getQuoteConfig({ configs, from, to }) {
const config = configs.find(conf => {
return (this.compareChainId(conf.srcChain, from.blockchain) &&
this.compareChainId(conf.tgtChain, to.blockchain) &&
(0, blockchain_1.compareAddresses)(conf.srcToken, from.address) &&
(0, blockchain_1.compareAddresses)(conf.tgtToken, to.address));
});
if (!config) {
throw new errors_1.RubicSdkError('[ORBITER] Unsupported pair of tokens!');
}
return config;
}
/**
*
* @param code Orbiter identification code of chain(9001, 9002 etc), equals quoteConfig.vc
* @param receiverAddress
* @returns data argument for orbiter-abi methods as hex string
*/
static getHexDataArg(code, receiverAddress) {
const value = `c=${code}&t=${receiverAddress}`;
const hexString = web3_1.default.utils.toHex(value);
return hexString;
}
static getAmountWithVcCode(fromWeiAmount, config) {
const regex = new RegExp(`\\d{${config.vc.length}}$`, 'g');
const total = fromWeiAmount.replace(regex, config.vc);
return total;
}
/**
* @example for native transfer
* 1000000 - 2%(rubicPercentFee) -> convertToCode -> 998015
* amountJore - 998015 + 2%
* When Jora subtracts amountJore - 2%, he will get value with orbiter-vc-code
*/
static getFromAmountWithoutFeeWithCode(from, feeInfo, quoteConfig) {
const fromWithoutFee = (0, get_from_without_fee_1.getFromWithoutFee)(from, feeInfo.rubicProxy?.platformFee?.percent);
const transferAmount = OrbiterUtils.getAmountWithVcCode(fromWithoutFee.stringWeiAmount, quoteConfig);
return transferAmount;
}
}
exports.OrbiterUtils = OrbiterUtils;
//# sourceMappingURL=orbiter-utils.js.map