UNPKG

atomicals-js

Version:
174 lines (173 loc) 9.31 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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.MintInteractiveDftCommand = void 0; const ecc = __importStar(require("tiny-secp256k1")); const ecpair_1 = require("ecpair"); const bitcoin = require('bitcoinjs-lib'); bitcoin.initEccLib(ecc); const bitcoinjs_lib_1 = require("bitcoinjs-lib"); const command_helpers_1 = require("./command-helpers"); const atomical_format_helpers_1 = require("../utils/atomical-format-helpers"); const atomical_operation_builder_1 = require("../utils/atomical-operation-builder"); const tinysecp = require('tiny-secp256k1'); (0, bitcoinjs_lib_1.initEccLib)(tinysecp); const ECPair = (0, ecpair_1.ECPairFactory)(tinysecp); class MintInteractiveDftCommand { constructor(electrumApi, options, address, ticker, fundingWIF, useCurrentBitwork) { this.electrumApi = electrumApi; this.options = options; this.address = address; this.ticker = ticker; this.fundingWIF = fundingWIF; this.useCurrentBitwork = useCurrentBitwork; this.options = (0, atomical_format_helpers_1.checkBaseRequestOptions)(this.options); this.ticker = this.ticker.startsWith('$') ? this.ticker.substring(1) : this.ticker; } run() { return __awaiter(this, void 0, void 0, function* () { // Prepare the keys const filesData = yield (0, command_helpers_1.prepareArgsMetaCtx)({ mint_ticker: this.ticker, }, undefined, undefined); (0, command_helpers_1.logBanner)('Mint Interactive FT (Decentralized)'); console.log("Atomical type:", 'FUNGIBLE (decentralized)', filesData, this.ticker); console.log("Mint for ticker: ", this.ticker); const atomicalIdResult = yield this.electrumApi.atomicalsGetByTicker(this.ticker); const atomicalResponse = yield this.electrumApi.atomicalsGetFtInfo(atomicalIdResult.result.atomical_id); const globalInfo = atomicalResponse.global; const atomicalInfo = atomicalResponse.result; const atomicalDecorated = (0, atomical_format_helpers_1.decorateAtomical)(atomicalInfo); console.log(globalInfo, atomicalDecorated); if (!atomicalDecorated['$ticker'] || atomicalDecorated['$ticker'] != this.ticker) { throw new Error('Ticker being requested does not match the initialized decentralized FT mint: ' + atomicalDecorated); } if (!atomicalDecorated['subtype'] || atomicalDecorated['subtype'] != 'decentralized') { throw new Error('Subtype must be decentralized fungible token type'); } if (atomicalDecorated['$mint_height'] > (globalInfo['height'] + 1)) { throw new Error(`Mint height is invalid. height=${globalInfo['height']}, $mint_height=${atomicalDecorated['$mint_height']}`); } const perAmountMint = atomicalDecorated['$mint_amount']; if (perAmountMint <= 0 || perAmountMint >= 100000000) { throw new Error('Per amount mint must be > 0 and less than or equal to 100,000,000'); } console.log("Per mint amount:", perAmountMint); if (!atomicalDecorated['dft_info']) { throw new Error(`General error no dft_info found`); } const max_mints = atomicalDecorated['$max_mints']; const mint_count = atomicalDecorated['dft_info']['mint_count']; const mint_bitworkc_current = atomicalDecorated['dft_info']['mint_bitworkc_current']; const mint_bitworkc_next = atomicalDecorated['dft_info']['mint_bitworkc_next']; const mint_bitworkc_current_remaining = atomicalDecorated['dft_info']['mint_bitworkc_current_remaining']; const mint_bitworkr_current = atomicalDecorated['dft_info']['mint_bitworkr_current']; const mint_bitworkr_next = atomicalDecorated['dft_info']['mint_bitworkr_next']; const mint_bitworkr_current_remaining = atomicalDecorated['dft_info']['mint_bitworkr_current_remaining']; const ticker = atomicalDecorated['$ticker']; const isInfiniteMode = atomicalDecorated['$mint_mode'] == 'perpetual'; if (isInfiniteMode) { console.log('Infinite minting mode detected, there is no limit'); } else { if (atomicalDecorated['dft_info']['mint_count'] >= atomicalDecorated['$max_mints']) { throw new Error(`Decentralized mint for ${ticker} completely minted out!`); } else { console.log(`There are already ${mint_count} mints of ${ticker} out of a max total of ${max_mints}.`); } } console.log('atomicalDecorated', atomicalResponse, atomicalDecorated); const atomicalBuilder = new atomical_operation_builder_1.AtomicalOperationBuilder({ electrumApi: this.electrumApi, rbf: this.options.rbf, satsbyte: this.options.satsbyte, address: this.address, disableMiningChalk: this.options.disableMiningChalk, opType: 'dmt', dmtOptions: { mintAmount: perAmountMint, ticker: this.ticker, }, meta: this.options.meta, ctx: this.options.ctx, init: this.options.init, }); // Attach any default data // Attach a container request if (this.options.container) atomicalBuilder.setContainerMembership(this.options.container); // In infinite minting mode we have a moving target difficulty always increasing based on how many minted so far if (isInfiniteMode) { // Mine the current or the next difficulty. By default it is the next if (mint_bitworkc_current) { if (this.useCurrentBitwork) { atomicalBuilder.setBitworkCommit(mint_bitworkc_current); } else { atomicalBuilder.setBitworkCommit(mint_bitworkc_next); } } if (this.useCurrentBitwork) { atomicalBuilder.setBitworkReveal(mint_bitworkr_current); } else { atomicalBuilder.setBitworkReveal(mint_bitworkr_next); } } else { // Attach any requested bitwork OR automatically request bitwork if the parent decentralized ft requires it const mint_bitworkc = atomicalDecorated['$mint_bitworkc'] || this.options.bitworkc; if (mint_bitworkc) { atomicalBuilder.setBitworkCommit(mint_bitworkc); } const mint_bitworkr = atomicalDecorated['$mint_bitworkr'] || this.options.bitworkr; if (mint_bitworkr) { atomicalBuilder.setBitworkReveal(mint_bitworkr); } } // The receiver output of the deploy atomicalBuilder.addOutput({ address: this.address, value: perAmountMint }); const result = yield atomicalBuilder.start(this.fundingWIF); return { success: true, data: result }; }); } } exports.MintInteractiveDftCommand = MintInteractiveDftCommand;