ordinalsbot
Version:
Node.js library for OrdinalsBot API
355 lines • 15.2 kB
JavaScript
;
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 () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.MarketPlace = void 0;
const marketplaceClient_1 = require("./marketplaceClient");
const types_1 = require("./types");
const marketplace_types_1 = require("./types/marketplace_types");
const bitcoin = __importStar(require("bitcoinjs-lib"));
const sats_connect_1 = require("sats-connect");
class MarketPlace {
constructor(key = "", environment = types_1.InscriptionEnvNetwork.mainnet, options) {
if (this.marketplaceInstance !== undefined) {
console.error("marketplace constructore was called multiple times");
return;
}
environment = types_1.InscriptionEnvNetwork[environment] ?? types_1.InscriptionEnvNetwork.mainnet;
switch (environment) {
case types_1.InscriptionEnvNetwork.mainnet:
this.network = sats_connect_1.BitcoinNetworkType.Mainnet;
break;
case types_1.InscriptionEnvNetwork.testnet:
this.network = sats_connect_1.BitcoinNetworkType.Testnet;
break;
case types_1.InscriptionEnvNetwork.signet:
this.network = sats_connect_1.BitcoinNetworkType.Signet;
break;
default:
this.network = sats_connect_1.BitcoinNetworkType.Mainnet;
break;
}
this.marketplaceInstance = new marketplaceClient_1.MarketPlaceClient(key, environment, options);
}
createMarketplace(createMarketplaceRequest) {
return this.marketplaceInstance.createMarketPlace(createMarketplaceRequest);
}
async createListing(createListingRequest) {
try {
if (!createListingRequest.walletProvider) {
return await this.marketplaceInstance.createListing(createListingRequest);
}
else if (createListingRequest.walletProvider === marketplace_types_1.WALLET_PROVIDER.xverse) {
const { psbt } = await this.marketplaceInstance.createListing(createListingRequest);
if (!createListingRequest.sellerOrdinalAddress) {
throw new Error('No seller address provided');
}
const inputIndices = createListingRequest.sellerOrdinals.map((item, index) => index);
const sellerInput = {
address: createListingRequest.sellerOrdinalAddress,
signingIndexes: inputIndices,
sigHash: bitcoin.Transaction.SIGHASH_SINGLE |
bitcoin.Transaction.SIGHASH_ANYONECANPAY,
};
const payload = {
network: { type: this.network },
message: 'Sign Seller Transaction',
psbtBase64: psbt,
broadcast: false,
inputsToSign: [sellerInput],
};
return new Promise((resolve, reject) => {
(0, sats_connect_1.signTransaction)({
payload,
onFinish: async (response) => {
try {
const sellerOrdinalIds = createListingRequest.sellerOrdinals.map((item, index) => item.id);
const confirmListingPayload = {
sellerOrdinals: sellerOrdinalIds,
signedListingPSBT: response.psbtBase64,
};
resolve(this.confirmListing(confirmListingPayload));
}
catch (error) {
console.error('Error confirm listing:', error);
reject(error);
}
},
onCancel: () => {
console.log('Transaction canceled');
}
});
});
}
else {
throw new Error("Wallet not supported");
}
}
catch (error) {
console.error('Error in createListing:', error);
throw error;
}
}
async reListing(reListingRequest) {
try {
if (!reListingRequest.walletProvider) {
return await this.marketplaceInstance.reListing(reListingRequest);
}
else if (reListingRequest.walletProvider === marketplace_types_1.WALLET_PROVIDER.xverse) {
const { psbt } = await this.marketplaceInstance.reListing(reListingRequest);
if (!reListingRequest.sellerOrdinalAddress) {
throw new Error('No seller address provided');
}
const sellerInput = {
address: reListingRequest.sellerOrdinalAddress,
signingIndexes: [0],
sigHash: bitcoin.Transaction.SIGHASH_SINGLE |
bitcoin.Transaction.SIGHASH_ANYONECANPAY,
};
const payload = {
network: { type: this.network },
message: 'Sign Seller Transaction',
psbtBase64: psbt,
broadcast: false,
inputsToSign: [sellerInput],
};
return new Promise((resolve, reject) => {
(0, sats_connect_1.signTransaction)({
payload,
onFinish: async (response) => {
try {
const confirmReListingPayload = {
ordinalId: reListingRequest.ordinalId,
signedListingPSBT: response.psbtBase64,
};
resolve(this.confirmReListing(confirmReListingPayload));
}
catch (error) {
console.error('Error confirm relisting:', error);
reject(error);
}
},
onCancel: () => {
console.log('Transaction canceled');
}
});
});
}
else {
throw new Error("Wallet not supported");
}
}
catch (error) {
console.error('Error in reListing:', error);
throw error;
}
}
async createOffer(createOfferRequest) {
if (!createOfferRequest.walletProvider) {
return this.marketplaceInstance.createOffer(createOfferRequest);
}
else if (createOfferRequest.walletProvider === marketplace_types_1.WALLET_PROVIDER.xverse) {
const offer = await this.marketplaceInstance.createOffer(createOfferRequest);
const sellerInput = {
address: createOfferRequest.buyerPaymentAddress,
signingIndexes: offer.buyerInputIndices,
};
const payload = {
network: {
type: this.network,
},
message: 'Sign Buyer Transaction',
psbtBase64: offer.psbt,
broadcast: false,
inputsToSign: [sellerInput],
};
return new Promise((resolve, reject) => {
(0, sats_connect_1.signTransaction)({
payload,
onFinish: async (response) => {
return resolve(response.psbtBase64);
},
onCancel: () => {
console.log('Transaction canceled');
}
});
});
}
else {
throw new Error("Wallet not supported");
}
}
submitOffer(submitOfferRequest) {
return this.marketplaceInstance.submitOffer(submitOfferRequest);
}
confirmPaddingOutputs(confirmPaddingOutputsRequest) {
return this.marketplaceInstance.confirmPaddingOutputs(confirmPaddingOutputsRequest);
}
async setupPaddingOutputs(setupPaddingOutputsRequest) {
if (!setupPaddingOutputsRequest.walletProvider) {
return this.marketplaceInstance.setupPaddingOutputs(setupPaddingOutputsRequest);
}
else if (setupPaddingOutputsRequest.walletProvider === marketplace_types_1.WALLET_PROVIDER.xverse) {
const paddingOutputResponse = await this.marketplaceInstance.setupPaddingOutputs(setupPaddingOutputsRequest);
const buyerInputs = {
address: setupPaddingOutputsRequest.address,
signingIndexes: paddingOutputResponse.buyerInputIndices
};
const payload = {
network: {
type: this.network,
},
message: 'Sign Padding Outputs Transaction',
psbtBase64: paddingOutputResponse.psbt,
broadcast: true,
inputsToSign: [buyerInputs],
};
return new Promise((resolve, reject) => {
(0, sats_connect_1.signTransaction)({
payload,
onFinish: async (response) => response,
onCancel: () => {
console.log('Transaction canceled');
}
});
});
}
else {
throw new Error("Wallet not supported");
}
}
getListing(getListingRequest) {
return this.marketplaceInstance.getListing(getListingRequest);
}
saveListing(saveListingRequest) {
return this.marketplaceInstance.saveListing(saveListingRequest);
}
confirmListing(confirmListingRequest) {
return this.marketplaceInstance.confirmListing(confirmListingRequest);
}
confirmReListing(confirmReListingRequest) {
return this.marketplaceInstance.confirmReListing(confirmReListingRequest);
}
async transfer(transferRequest) {
if (!transferRequest.walletProvider) {
return this.marketplaceInstance.transfer(transferRequest);
}
const transferResponse = await this.marketplaceInstance.transfer(transferRequest);
const inputsToSign = [
{
address: transferRequest.senderOrdinalAddress,
signingIndexes: transferResponse.senderOrdinalInputs,
sigHash: bitcoin.Transaction.SIGHASH_ALL,
},
{
address: transferRequest.senderPaymentAddress,
signingIndexes: transferResponse.senderPaymentInputs,
sigHash: bitcoin.Transaction.SIGHASH_ALL,
},
];
const payload = {
network: { type: this.network },
message: 'Sign Transfer Transaction',
psbtBase64: transferResponse.psbtBase64,
broadcast: true,
inputsToSign,
};
return new Promise((resolve, reject) => {
(0, sats_connect_1.signTransaction)({
payload,
onFinish: async (response) => {
return resolve(response);
},
onCancel: () => {
console.log('Transaction canceled');
}
});
});
}
async deList(deListingRequest) {
if (!deListingRequest.walletProvider) {
return this.marketplaceInstance.deList(deListingRequest);
}
const transferResponse = await this.marketplaceInstance.deList(deListingRequest);
const inputsToSign = [
{
address: deListingRequest.senderOrdinalAddress,
signingIndexes: transferResponse.senderOrdinalInputs,
sigHash: bitcoin.Transaction.SIGHASH_ALL,
},
{
address: deListingRequest.senderPaymentAddress,
signingIndexes: transferResponse.senderPaymentInputs,
sigHash: bitcoin.Transaction.SIGHASH_ALL,
},
];
const payload = {
network: { type: this.network },
message: 'Sign Transfer Transaction',
psbtBase64: transferResponse.psbtBase64,
broadcast: true,
inputsToSign,
};
return new Promise((resolve, reject) => {
(0, sats_connect_1.signTransaction)({
payload,
onFinish: async (response) => {
try {
const confirmDeListingPayload = {
ordinalId: deListingRequest.ordinalId,
sellerPaymentAddress: deListingRequest.senderPaymentAddress,
};
const confirmDeListResponse = await this.confirmDeListing(confirmDeListingPayload);
resolve({
...confirmDeListResponse,
txId: response?.txId,
});
}
catch (error) {
console.error("Error delisting ordinal:", error);
reject(error);
}
},
onCancel: () => {
console.log("Transaction canceled");
},
});
});
}
confirmDeListing(confirmDeListingRequest) {
return this.marketplaceInstance.confirmDeListing(confirmDeListingRequest);
}
}
exports.MarketPlace = MarketPlace;
//# sourceMappingURL=marketplace.js.map