@bithomp/xrpl-api
Version:
A Bithomp JavaScript/TypeScript library for interacting with the XRP Ledger
200 lines (199 loc) • 6.57 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.getAccountNfts = getAccountNfts;
exports.findAccountNfts = findAccountNfts;
exports.getAccountNftSellOffers = getAccountNftSellOffers;
exports.getAccountNftBuyOffers = getAccountNftBuyOffers;
const Client = __importStar(require("../client"));
const account_nfts_1 = require("../models/account_nfts");
const utils_1 = require("../common/utils");
const common_1 = require("../common");
async function getAccountNfts(account, options = {}) {
const { hash, marker } = (0, utils_1.parseMarker)(options.marker);
options.marker = marker;
const connection = Client.findConnection(undefined, undefined, undefined, hash);
if (!connection) {
throw new Error("There is no connection");
}
const response = await connection.request({
command: "account_nfts",
account,
ledger_index: options.ledgerIndex || "validated",
marker: options.marker,
});
if (!response) {
return {
account,
status: "error",
error: "invalidResponse",
};
}
if (response.error) {
const { error, error_code, error_message, error_exception, status, validated } = response;
return (0, common_1.removeUndefined)({
account,
error,
error_code,
error_message,
error_exception,
status,
validated,
});
}
const result = response?.result;
if (!result) {
return {
account,
status: "error",
error: "invalidResponse",
};
}
if (Array.isArray(result.account_nfts)) {
result.account_nfts = result.account_nfts.sort(account_nfts_1.sortHelperAccountNFToken);
}
const newMarker = (0, utils_1.createMarker)(connection.hash, result.marker);
if (newMarker) {
result.marker = newMarker;
}
return result;
}
async function findAccountNfts(account, options = { timeout: 15000 }) {
const timeStart = new Date();
const limit = options.limit;
let response;
const accountNfts = [];
while (true) {
const currentTime = new Date();
if (options.timeout && currentTime.getTime() - timeStart.getTime() > options.timeout) {
options.timeout = currentTime.getTime() - timeStart.getTime();
break;
}
if (options.limit && limit) {
options.limit = limit - accountNfts.length;
}
response = await getAccountNfts(account, options);
if (!response || response.error) {
return response;
}
accountNfts.push(...response.account_nfts);
if (limit && accountNfts.length >= limit) {
response.limit = accountNfts.length;
break;
}
if (response.marker) {
options.marker = response.marker;
}
else {
break;
}
}
if (response.error) {
const { error, error_code, error_message, error_exception, status, validated } = response;
return (0, common_1.removeUndefined)({
account,
error,
error_code,
error_message,
error_exception,
status,
validated,
});
}
response.account_nfts = accountNfts;
return response;
}
async function getAccountNftSellOffers(nftID, options = {}) {
const connection = Client.findConnection("!clio");
if (!connection) {
throw new Error("There is no connection");
}
const response = await connection.request({
command: "nft_sell_offers",
nft_id: nftID,
ledger_index: options.ledgerIndex || "validated",
});
if (!response) {
return {
nft_id: nftID,
status: "error",
error: "invalidResponse",
};
}
if (response.error) {
const { error, error_code, error_message, error_exception, status, validated } = response;
return (0, common_1.removeUndefined)({
nft_id: nftID,
error,
error_code,
error_message,
error_exception,
status,
validated,
});
}
return response?.result;
}
async function getAccountNftBuyOffers(nftID, options = {}) {
const connection = Client.findConnection("!clio");
if (!connection) {
throw new Error("There is no connection");
}
const response = await connection.request({
command: "nft_buy_offers",
nft_id: nftID,
ledger_index: options.ledgerIndex || "validated",
});
if (!response) {
return {
nft_id: nftID,
status: "error",
error: "invalidResponse",
};
}
if (response.error) {
const { error, error_code, error_message, error_exception, status, validated } = response;
return (0, common_1.removeUndefined)({
nft_id: nftID,
error,
error_code,
error_message,
error_exception,
status,
validated,
});
}
return response?.result;
}
;