UNPKG

@rainfi/sdk

Version:

This package is used to interact with Rain.fi protocol on Solana

186 lines 9.51 kB
"use strict"; 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.WrapperConnection = exports.toMetadataFromReadApiAsset = exports.toMetadataArgsFromReadApiAsset = exports.ReadApiError = exports.Pda = void 0; const web3_js_1 = require("@solana/web3.js"); class Pda extends web3_js_1.PublicKey { constructor(value, bump) { super(value); this.bump = bump; } static find(programId, seeds) { const [publicKey, bump] = web3_js_1.PublicKey.findProgramAddressSync(seeds, programId); return new Pda(publicKey, bump); } } exports.Pda = Pda; /** @group Errors */ class ReadApiError { constructor(message, cause) { this.name = "ReadApiError"; } } exports.ReadApiError = ReadApiError; const toMetadataArgsFromReadApiAsset = (input) => { var _a, _b, _c, _d, _e, _f, _g; const { content, royalty, creators, grouping, supply, ownership: { owner, delegate }, mutable, } = input; const coll = (_a = grouping.find((g) => g.group_key === "collection")) === null || _a === void 0 ? void 0 : _a.group_value; const metadata = { name: (_c = (_b = content === null || content === void 0 ? void 0 : content.metadata) === null || _b === void 0 ? void 0 : _b.name) !== null && _c !== void 0 ? _c : "", symbol: (_e = (_d = content === null || content === void 0 ? void 0 : content.metadata) === null || _d === void 0 ? void 0 : _d.symbol) !== null && _e !== void 0 ? _e : " ", uri: (_f = content === null || content === void 0 ? void 0 : content.json_uri) !== null && _f !== void 0 ? _f : "", sellerFeeBasisPoints: royalty.basis_points, creators: creators.map((creator) => ({ address: new web3_js_1.PublicKey(creator.address), share: creator.share, verified: creator.verified, })), primarySaleHappened: royalty.primary_sale_happened, isMutable: mutable, editionNonce: (_g = supply === null || supply === void 0 ? void 0 : supply.edition_nonce) !== null && _g !== void 0 ? _g : null, tokenStandard: null, collection: coll ? { key: new web3_js_1.PublicKey(coll), verified: true } : null, uses: null, tokenProgramVersion: 0, }; return metadata; }; exports.toMetadataArgsFromReadApiAsset = toMetadataArgsFromReadApiAsset; const toMetadataFromReadApiAsset = (input) => { var _a, _b, _c, _d, _e, _f, _g; const { compression, content, royalty, creators, grouping, supply, ownership: { owner, delegate }, mutable, } = input; const coll = (_a = grouping.find((g) => g.group_key === "collection")) === null || _a === void 0 ? void 0 : _a.group_value; const metadata = { name: (_c = (_b = content === null || content === void 0 ? void 0 : content.metadata) === null || _b === void 0 ? void 0 : _b.name) !== null && _c !== void 0 ? _c : "", symbol: (_e = (_d = content === null || content === void 0 ? void 0 : content.metadata) === null || _d === void 0 ? void 0 : _d.symbol) !== null && _e !== void 0 ? _e : " ", uri: (_f = content === null || content === void 0 ? void 0 : content.json_uri) !== null && _f !== void 0 ? _f : "", sellerFeeBasisPoints: royalty.basis_points, creators: creators.map((creator) => ({ address: new web3_js_1.PublicKey(creator.address), share: creator.share, verified: creator.verified, })), primarySaleHappened: royalty.primary_sale_happened, isMutable: mutable, editionNonce: (_g = supply === null || supply === void 0 ? void 0 : supply.edition_nonce) !== null && _g !== void 0 ? _g : null, tokenStandard: null, collection: coll ? { key: new web3_js_1.PublicKey(coll), verified: true } : null, uses: null, tokenProgramVersion: 0, }; return metadata; }; exports.toMetadataFromReadApiAsset = toMetadataFromReadApiAsset; /** /** * Wrapper class to add additional methods on top the standard Connection from `@solana/web3.js` * Specifically, adding the RPC methods used by the Digital Asset Standards (DAS) ReadApi * for state compression and compressed NFTs */ class WrapperConnection extends web3_js_1.Connection { constructor(endpoint, commitmentOrConfig) { super(endpoint, commitmentOrConfig); this.callReadApi = (jsonRpcParams) => __awaiter(this, void 0, void 0, function* () { var _a, _b; const response = yield fetch(this.rpcEndpoint, { method: "POST", headers: Object.assign(Object.assign({}, (_a = this === null || this === void 0 ? void 0 : this._commitmentOrConfig) === null || _a === void 0 ? void 0 : _a.httpHeaders), { "Content-Type": "application/json" }), body: JSON.stringify({ jsonrpc: "2.0", method: jsonRpcParams.method, id: (_b = jsonRpcParams.id) !== null && _b !== void 0 ? _b : "rpd-op-123", params: jsonRpcParams.params, }), }); return yield response.json(); }); this._commitmentOrConfig = commitmentOrConfig; } // Asset id can be calculated via Bubblegum#getLeafAssetId // It is a PDA with the following seeds: ["asset", tree, leafIndex] getAsset(assetId) { return __awaiter(this, void 0, void 0, function* () { const { result: asset } = yield this.callReadApi({ method: "getAsset", params: { id: assetId.toBase58(), }, }); if (!asset) throw new ReadApiError("No asset returned"); return asset; }); } // Asset id can be calculated via Bubblegum#getLeafAssetId // It is a PDA with the following seeds: ["asset", tree, leafIndex] getAssetProof(assetId) { return __awaiter(this, void 0, void 0, function* () { const { result: proof } = yield this.callReadApi({ method: "getAssetProof", params: { id: assetId.toBase58(), }, }); if (!proof) throw new ReadApiError("No asset proof returned"); return proof; }); } // getAssetsByGroup(_a) { return __awaiter(this, arguments, void 0, function* ({ groupKey, groupValue, page, limit, sortBy, before, after, }) { // `page` cannot be supplied with `before` or `after` if (typeof page == "number" && (before || after)) throw new ReadApiError("Pagination Error. Only one pagination parameter supported per query."); // a pagination method MUST be selected, but we are defaulting to using `page=0` const { result } = yield this.callReadApi({ method: "getAssetsByGroup", params: { groupKey, groupValue, after: after !== null && after !== void 0 ? after : null, before: before !== null && before !== void 0 ? before : null, limit: limit !== null && limit !== void 0 ? limit : null, page: page !== null && page !== void 0 ? page : 1, sortBy: sortBy !== null && sortBy !== void 0 ? sortBy : null, }, }); if (!result) throw new ReadApiError("No results returned"); return result; }); } // getAssetsByOwner(_a) { return __awaiter(this, arguments, void 0, function* ({ ownerAddress, page, limit, sortBy, before, after, }) { // `page` cannot be supplied with `before` or `after` if (typeof page == "number" && (before || after)) throw new ReadApiError("Pagination Error. Only one pagination parameter supported per query."); // a pagination method MUST be selected, but we are defaulting to using `page=0` const { result } = yield this.callReadApi({ method: "getAssetsByOwner", params: { ownerAddress, after: after !== null && after !== void 0 ? after : null, before: before !== null && before !== void 0 ? before : null, limit: limit !== null && limit !== void 0 ? limit : null, page: page !== null && page !== void 0 ? page : 1, sortBy: sortBy !== null && sortBy !== void 0 ? sortBy : null, }, }); if (!result) throw new ReadApiError("No results returned"); return result; }); } } exports.WrapperConnection = WrapperConnection; //# sourceMappingURL=compressed.utils.js.map