@okxweb3/coin-stellar
Version:
@okxweb3/coin-stellar is a Stellar SDK for building Web3 wallets and applications. It supports Stellar and PI blockchains, enabling private key management, address generation, transaction signing, trustline creation, and asset transfers
34 lines • 1.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Soroban = void 0;
class Soroban {
static formatTokenAmount(amount, decimals) {
if (amount.includes('.')) {
throw new TypeError('No decimals are allowed');
}
let formatted = amount;
if (decimals > 0) {
if (decimals > formatted.length) {
formatted = ['0', formatted.toString().padStart(decimals, '0')].join('.');
}
else {
formatted = [
formatted.slice(0, -decimals),
formatted.slice(-decimals)
].join('.');
}
}
return formatted.replace(/(\.\d*?)0+$/, '$1');
}
static parseTokenAmount(value, decimals) {
var _a;
const [whole, fraction, ...rest] = value.split('.').slice();
if (rest.length) {
throw new Error(`Invalid decimal value: ${value}`);
}
const shifted = BigInt(whole + ((_a = fraction === null || fraction === void 0 ? void 0 : fraction.padEnd(decimals, '0')) !== null && _a !== void 0 ? _a : '0'.repeat(decimals)));
return shifted.toString();
}
}
exports.Soroban = Soroban;
//# sourceMappingURL=soroban.js.map