UNPKG

frakt-client

Version:

Client library for interacting with FRAKT solana program

836 lines (835 loc) 27.7 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.getReservationList = exports.getMetadata = exports.getEdition = exports.mintPrintingTokens = exports.signMetadata = exports.createReservationList = exports.updatePrimarySaleHappenedViaToken = exports.mintNewEditionFromMasterEditionViaToken = exports.createMasterEdition = exports.createMetadata = exports.updateMetadata = exports.decodeMasterEdition = exports.decodeEdition = exports.decodeMetadata = exports.METADATA_SCHEMA = exports.Metadata = exports.Data = exports.Creator = exports.ReservationList = exports.Reservation = exports.Edition = exports.MasterEdition = exports.MetadataCategory = exports.MetadataKey = exports.MAX_MASTER_EDITION_KEN = exports.MAX_METADATA_LEN = exports.MAX_CREATOR_LEN = exports.MAX_CREATOR_LIMIT = exports.MAX_URI_LENGTH = exports.MAX_SYMBOL_LENGTH = exports.MAX_NAME_LENGTH = exports.RESERVATION = exports.EDITION = exports.METADATA_PREFIX = exports.findProgramAddress = exports.extendBorsh = void 0; const web3_js_1 = require("@solana/web3.js"); const ids_1 = require("./utils/ids"); const borsh_1 = require("borsh"); exports.extendBorsh = () => { borsh_1.BinaryReader.prototype.readPubkey = function () { const reader = this; const array = reader.readFixedArray(32); return new web3_js_1.PublicKey(array); }; borsh_1.BinaryWriter.prototype.writePubkey = function (value) { const writer = this; writer.writeFixedArray(value.toBuffer()); }; }; exports.extendBorsh(); const BN = require('bn.js'); // import { findProgramAddress } from '../utils'; exports.findProgramAddress = (seeds, programId) => __awaiter(void 0, void 0, void 0, function* () { const key = 'pda-' + seeds.reduce((agg, item) => agg + item.toString('hex'), '') + programId.toString(); // let cached = localStorage.getItem(key); // if (cached) { // const value = JSON.parse(cached); // return [new PublicKey(value.key), parseInt(value.nonce)] as [ // PublicKey, // number, // ]; // } const result = yield web3_js_1.PublicKey.findProgramAddress(seeds, programId); // localStorage.setItem( // key, // JSON.stringify({ // key: result[0].toBase58(), // nonce: result[1], // }), // ); return result; }); exports.METADATA_PREFIX = 'metadata'; exports.EDITION = 'edition'; exports.RESERVATION = 'reservation'; exports.MAX_NAME_LENGTH = 32; exports.MAX_SYMBOL_LENGTH = 10; exports.MAX_URI_LENGTH = 200; exports.MAX_CREATOR_LIMIT = 5; exports.MAX_CREATOR_LEN = 32 + 1 + 1; exports.MAX_METADATA_LEN = 1 + 32 + 32 + exports.MAX_NAME_LENGTH + exports.MAX_SYMBOL_LENGTH + exports.MAX_URI_LENGTH + exports.MAX_CREATOR_LIMIT * exports.MAX_CREATOR_LEN + 1 + 1 + 200; exports.MAX_MASTER_EDITION_KEN = 1 + 9 + 8 + 32 + 32; var MetadataKey; (function (MetadataKey) { MetadataKey[MetadataKey["Uninitialized"] = 0] = "Uninitialized"; MetadataKey[MetadataKey["MetadataV1"] = 4] = "MetadataV1"; MetadataKey[MetadataKey["EditionV1"] = 1] = "EditionV1"; MetadataKey[MetadataKey["MasterEditionV1"] = 2] = "MasterEditionV1"; MetadataKey[MetadataKey["ReservationListV1"] = 3] = "ReservationListV1"; })(MetadataKey = exports.MetadataKey || (exports.MetadataKey = {})); var MetadataCategory; (function (MetadataCategory) { MetadataCategory["Audio"] = "audio"; MetadataCategory["Video"] = "video"; MetadataCategory["Image"] = "image"; MetadataCategory["VR"] = "vr"; })(MetadataCategory = exports.MetadataCategory || (exports.MetadataCategory = {})); class MasterEdition { constructor(args) { this.key = MetadataKey.MasterEditionV1; this.supply = args.supply; this.maxSupply = args.maxSupply; this.printingMint = args.printingMint; this.oneTimePrintingAuthorizationMint = args.oneTimePrintingAuthorizationMint; } } exports.MasterEdition = MasterEdition; class Edition { constructor(args) { this.key = MetadataKey.EditionV1; this.parent = args.parent; this.edition = args.edition; } } exports.Edition = Edition; class Reservation { constructor(args) { this.address = args.address; this.spotsRemaining = args.spotsRemaining; this.totalSpots = args.totalSpots; } } exports.Reservation = Reservation; class ReservationList { constructor(args) { this.key = MetadataKey.ReservationListV1; this.key = MetadataKey.EditionV1; this.masterEdition = args.masterEdition; this.supplySnapshot = args.supplySnapshot; this.reservations = args.reservations; this.totalReservationSpots = args.totalReservationSpots; } } exports.ReservationList = ReservationList; class Creator { constructor(args) { this.address = args.address; this.verified = args.verified; this.share = args.share; } } exports.Creator = Creator; class Data { constructor(args) { this.name = args.name; this.symbol = args.symbol; this.uri = args.uri; this.sellerFeeBasisPoints = args.sellerFeeBasisPoints; this.creators = args.creators; } } exports.Data = Data; class Metadata { constructor(args) { this.key = MetadataKey.MetadataV1; this.updateAuthority = args.updateAuthority; this.mint = args.mint; this.data = args.data; this.primarySaleHappened = args.primarySaleHappened; this.isMutable = args.isMutable; } init() { return __awaiter(this, void 0, void 0, function* () { const edition = yield getEdition(this.mint); this.edition = edition; this.masterEdition = edition; }); } } exports.Metadata = Metadata; class CreateMetadataArgs { constructor(args) { this.instruction = 0; this.data = args.data; this.isMutable = args.isMutable; } } class UpdateMetadataArgs { constructor(args) { this.instruction = 1; this.data = args.data ? args.data : null; this.updateAuthority = args.updateAuthority ? new web3_js_1.PublicKey(args.updateAuthority) : null; this.primarySaleHappened = args.primarySaleHappened; } } class CreateMasterEditionArgs { constructor(args) { this.instruction = 2; this.maxSupply = args.maxSupply; } } class MintPrintingTokensArgs { constructor(args) { this.instruction = 9; this.supply = args.supply; } } // export const TEST_SCHEMA = new Map<any, any>([ // [ // CreateMetadataArgs, // { // kind: 'struct', // fields: [ // ['instruction', 'u8'], // ['data', Data], // ['isMutable', 'u8'], // bool // ], // }, // ], // ]) exports.METADATA_SCHEMA = new Map([ [ CreateMetadataArgs, { kind: 'struct', fields: [ ['instruction', 'u8'], ['data', Data], ['isMutable', 'u8'], ], }, ], [ UpdateMetadataArgs, { kind: 'struct', fields: [ ['instruction', 'u8'], ['data', { kind: 'option', type: Data }], ['updateAuthority', { kind: 'option', type: 'pubkey' }], ['primarySaleHappened', { kind: 'option', type: 'u8' }], ], }, ], [ CreateMasterEditionArgs, { kind: 'struct', fields: [ ['instruction', 'u8'], ['maxSupply', { kind: 'option', type: 'u64' }], ], }, ], [ MintPrintingTokensArgs, { kind: 'struct', fields: [ ['instruction', 'u8'], ['supply', 'u64'], ], }, ], [ MasterEdition, { kind: 'struct', fields: [ ['key', 'u8'], ['supply', 'u64'], ['maxSupply', { kind: 'option', type: 'u64' }], ['printingMint', 'pubkey'], ['oneTimePrintingAuthorizationMint', 'pubkey'], ], }, ], [ Edition, { kind: 'struct', fields: [ ['key', 'u8'], ['parent', 'pubkey'], ['edition', 'u64'], ], }, ], [ Data, { kind: 'struct', fields: [ ['name', 'string'], ['symbol', 'string'], ['uri', 'string'], ['sellerFeeBasisPoints', 'u16'], ['creators', { kind: 'option', type: [Creator] }], ], }, ], [ Creator, { kind: 'struct', fields: [ ['address', 'pubkey'], ['verified', 'u8'], ['share', 'u8'], ], }, ], [ Metadata, { kind: 'struct', fields: [ ['key', 'u8'], ['updateAuthority', 'pubkey'], ['mint', 'pubkey'], ['data', Data], ['primarySaleHappened', 'u8'], ['isMutable', 'u8'], ], }, ], [ Reservation, { kind: 'struct', fields: [ ['address', 'pubkey'], ['spotsRemaining', 'u8'], ['totalSpots', 'u8'], ], }, ], [ ReservationList, { kind: 'struct', fields: [ ['key', 'u8'], ['masterEdition', 'pubkey'], ['supplySnapshot', { kind: 'option', type: 'u64' }], ['reservations', [Reservation]], ['totalReservationSpots', 'u64'], ], }, ], ]); exports.decodeMetadata = (buffer) => { const metadata = borsh_1.deserializeUnchecked(exports.METADATA_SCHEMA, Metadata, buffer); return metadata; }; exports.decodeEdition = (buffer) => { return borsh_1.deserializeUnchecked(exports.METADATA_SCHEMA, Edition, buffer); }; exports.decodeMasterEdition = (buffer) => { return borsh_1.deserializeUnchecked(exports.METADATA_SCHEMA, MasterEdition, buffer); }; function updateMetadata(data, newUpdateAuthority, primarySaleHappened, mintKey, updateAuthority, instructions, metadataAccount) { return __awaiter(this, void 0, void 0, function* () { const metadataProgramId = ids_1.programIds().metadata; metadataAccount = metadataAccount || (yield exports.findProgramAddress([ Buffer.from('metadata'), metadataProgramId.toBuffer(), mintKey.toBuffer(), ], metadataProgramId))[0]; const value = new UpdateMetadataArgs({ data, updateAuthority: !newUpdateAuthority ? undefined : newUpdateAuthority, primarySaleHappened: primarySaleHappened === null || primarySaleHappened === undefined ? null : primarySaleHappened, }); const txnData = Buffer.from(borsh_1.serialize(exports.METADATA_SCHEMA, value)); const keys = [ { pubkey: metadataAccount, isSigner: false, isWritable: true, }, { pubkey: updateAuthority, isSigner: true, isWritable: false, }, ]; instructions.push(new web3_js_1.TransactionInstruction({ keys, programId: metadataProgramId, data: txnData, })); return metadataAccount; }); } exports.updateMetadata = updateMetadata; class Test { constructor(args) { this.x = args.x; this.y = args.y; this.z = args.z; this.q = args.q; } } function createMetadata(data, updateAuthority, mintKey, mintAuthorityKey, instructions, payer) { return __awaiter(this, void 0, void 0, function* () { const metadataProgramId = ids_1.programIds().metadata; const metadataAccount = (yield exports.findProgramAddress([ Buffer.from('metadata'), metadataProgramId.toBuffer(), mintKey.toBuffer(), ], metadataProgramId))[0]; console.log('Data', data); // const value = new Test({ x: 255, y: 20, z: '123', q: [1, 2, 3] }); // const schema = new Map([[Test, { kind: 'struct', fields: [['x', 'u8'], ['y', 'u64'], ['z', 'string'], ['q', [3]]] }]]); // const buffer = serialize(schema, value); // console.log('buffer: ', buffer) const value = new CreateMetadataArgs({ data, isMutable: true }); console.log('value: ', value); // console.log('borsh ser: ', serialize(METADATA_SCHEMA, value)) const txnData = Buffer.from(borsh_1.serialize(exports.METADATA_SCHEMA, value)); console.log('txnData: ', txnData); const keys = [ { pubkey: metadataAccount, isSigner: false, isWritable: true, }, { pubkey: mintKey, isSigner: false, isWritable: false, }, { pubkey: mintAuthorityKey, isSigner: true, isWritable: false, }, { pubkey: payer, isSigner: true, isWritable: false, }, { pubkey: updateAuthority, isSigner: false, isWritable: false, }, { pubkey: web3_js_1.SystemProgram.programId, isSigner: false, isWritable: false, }, { pubkey: web3_js_1.SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false, }, ]; instructions.push(new web3_js_1.TransactionInstruction({ keys, programId: metadataProgramId, data: txnData, })); return metadataAccount; }); } exports.createMetadata = createMetadata; function createMasterEdition(maxSupply, mintKey, printingMintKey, oneTimePrintingAuthorizationMint, updateAuthorityKey, mintAuthorityKey, instructions, payer, printingMintAuthority, oneTimePrintingAuthorizationMintAuthority) { return __awaiter(this, void 0, void 0, function* () { const metadataProgramId = ids_1.programIds().metadata; const metadataAccount = (yield exports.findProgramAddress([ Buffer.from(exports.METADATA_PREFIX), metadataProgramId.toBuffer(), mintKey.toBuffer(), ], metadataProgramId))[0]; const editionAccount = (yield exports.findProgramAddress([ Buffer.from(exports.METADATA_PREFIX), metadataProgramId.toBuffer(), mintKey.toBuffer(), Buffer.from(exports.EDITION), ], metadataProgramId))[0]; const value = new CreateMasterEditionArgs({ maxSupply: maxSupply || null }); const data = Buffer.from(borsh_1.serialize(exports.METADATA_SCHEMA, value)); const keys = [ { pubkey: editionAccount, isSigner: false, isWritable: true, }, { pubkey: mintKey, isSigner: false, isWritable: true, }, { pubkey: printingMintKey, isSigner: false, isWritable: false, }, { pubkey: oneTimePrintingAuthorizationMint, isSigner: false, isWritable: true, }, { pubkey: updateAuthorityKey, isSigner: true, isWritable: false, }, { pubkey: printingMintAuthority, isSigner: true, isWritable: false, }, { pubkey: mintAuthorityKey, isSigner: true, isWritable: false, }, { pubkey: metadataAccount, isSigner: false, isWritable: false, }, { pubkey: payer, isSigner: true, isWritable: false, }, { pubkey: ids_1.programIds().token, isSigner: false, isWritable: false, }, { pubkey: web3_js_1.SystemProgram.programId, isSigner: false, isWritable: false, }, { pubkey: web3_js_1.SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false, }, ]; if (oneTimePrintingAuthorizationMintAuthority) keys.push({ pubkey: oneTimePrintingAuthorizationMintAuthority, isSigner: true, isWritable: false, }); instructions.push(new web3_js_1.TransactionInstruction({ keys, programId: metadataProgramId, data, })); }); } exports.createMasterEdition = createMasterEdition; function mintNewEditionFromMasterEditionViaToken(newMint, tokenMint, newMintAuthority, printingMint, authorizationTokenHoldingAccount, burnAuthority, updateAuthorityOfMaster, reservationList, instructions, payer) { return __awaiter(this, void 0, void 0, function* () { const metadataProgramId = ids_1.programIds().metadata; const newMetadataKey = yield getMetadata(newMint); const masterMetadataKey = yield getMetadata(tokenMint); const newEdition = yield getEdition(newMint); const masterEdition = yield getEdition(tokenMint); const data = Buffer.from([3]); const keys = [ { pubkey: newMetadataKey, isSigner: false, isWritable: true, }, { pubkey: newEdition, isSigner: false, isWritable: true, }, { pubkey: masterEdition, isSigner: false, isWritable: true, }, { pubkey: newMint, isSigner: false, isWritable: true, }, { pubkey: newMintAuthority, isSigner: true, isWritable: false, }, { pubkey: printingMint, isSigner: false, isWritable: true, }, { pubkey: authorizationTokenHoldingAccount, isSigner: false, isWritable: true, }, { pubkey: burnAuthority, isSigner: true, isWritable: false, }, { pubkey: payer, isSigner: true, isWritable: false, }, { pubkey: updateAuthorityOfMaster, isSigner: false, isWritable: false, }, { pubkey: masterMetadataKey, isSigner: false, isWritable: false, }, { pubkey: ids_1.programIds().token, isSigner: false, isWritable: false, }, { pubkey: web3_js_1.SystemProgram.programId, isSigner: false, isWritable: false, }, { pubkey: web3_js_1.SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false, }, ]; if (reservationList) { keys.push({ pubkey: reservationList, isSigner: false, isWritable: true, }); } instructions.push(new web3_js_1.TransactionInstruction({ keys, programId: metadataProgramId, data, })); }); } exports.mintNewEditionFromMasterEditionViaToken = mintNewEditionFromMasterEditionViaToken; function updatePrimarySaleHappenedViaToken(metadata, owner, tokenAccount, instructions) { return __awaiter(this, void 0, void 0, function* () { const metadataProgramId = ids_1.programIds().metadata; const data = Buffer.from([4]); const keys = [ { pubkey: metadata, isSigner: false, isWritable: true, }, { pubkey: owner, isSigner: true, isWritable: false, }, { pubkey: tokenAccount, isSigner: false, isWritable: false, }, ]; instructions.push(new web3_js_1.TransactionInstruction({ keys, programId: metadataProgramId, data, })); }); } exports.updatePrimarySaleHappenedViaToken = updatePrimarySaleHappenedViaToken; function createReservationList(metadata, masterEdition, resource, updateAuthority, payer, instructions) { return __awaiter(this, void 0, void 0, function* () { const metadataProgramId = ids_1.programIds().metadata; const reservationList = yield getReservationList(masterEdition, resource); const data = Buffer.from([6]); const keys = [ { pubkey: reservationList, isSigner: false, isWritable: true, }, { pubkey: payer, isSigner: true, isWritable: false, }, { pubkey: updateAuthority, isSigner: true, isWritable: false, }, { pubkey: masterEdition, isSigner: false, isWritable: false, }, { pubkey: resource, isSigner: false, isWritable: false, }, { pubkey: metadata, isSigner: false, isWritable: false, }, { pubkey: web3_js_1.SystemProgram.programId, isSigner: false, isWritable: false, }, { pubkey: web3_js_1.SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false, }, ]; instructions.push(new web3_js_1.TransactionInstruction({ keys, programId: metadataProgramId, data, })); }); } exports.createReservationList = createReservationList; function signMetadata(metadata, creator, instructions) { return __awaiter(this, void 0, void 0, function* () { const metadataProgramId = ids_1.programIds().metadata; const data = Buffer.from([7]); const keys = [ { pubkey: metadata, isSigner: false, isWritable: true, }, { pubkey: creator, isSigner: true, isWritable: false, }, ]; instructions.push(new web3_js_1.TransactionInstruction({ keys, programId: metadataProgramId, data, })); }); } exports.signMetadata = signMetadata; function mintPrintingTokens(destination, printingMint, updateAuthority, metadata, masterEdition, supply, instructions) { return __awaiter(this, void 0, void 0, function* () { const PROGRAM_IDS = ids_1.programIds(); const metadataProgramId = PROGRAM_IDS.metadata; const value = new MintPrintingTokensArgs({ supply }); const data = Buffer.from(borsh_1.serialize(exports.METADATA_SCHEMA, value)); const keys = [ { pubkey: destination, isSigner: false, isWritable: true, }, { pubkey: printingMint, isSigner: false, isWritable: true, }, { pubkey: updateAuthority, isSigner: true, isWritable: false, }, { pubkey: metadata, isSigner: false, isWritable: false, }, { pubkey: masterEdition, isSigner: false, isWritable: false, }, { pubkey: PROGRAM_IDS.token, isSigner: false, isWritable: false, }, { pubkey: web3_js_1.SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false, }, ]; instructions.push(new web3_js_1.TransactionInstruction({ keys, programId: metadataProgramId, data, })); }); } exports.mintPrintingTokens = mintPrintingTokens; function getEdition(tokenMint) { return __awaiter(this, void 0, void 0, function* () { const PROGRAM_IDS = ids_1.programIds(); return (yield exports.findProgramAddress([ Buffer.from(exports.METADATA_PREFIX), PROGRAM_IDS.metadata.toBuffer(), tokenMint.toBuffer(), Buffer.from(exports.EDITION), ], PROGRAM_IDS.metadata))[0]; }); } exports.getEdition = getEdition; function getMetadata(tokenMint) { return __awaiter(this, void 0, void 0, function* () { const PROGRAM_IDS = ids_1.programIds(); return (yield exports.findProgramAddress([ Buffer.from(exports.METADATA_PREFIX), PROGRAM_IDS.metadata.toBuffer(), tokenMint.toBuffer(), ], PROGRAM_IDS.metadata))[0]; }); } exports.getMetadata = getMetadata; function getReservationList(masterEdition, resource) { return __awaiter(this, void 0, void 0, function* () { const PROGRAM_IDS = ids_1.programIds(); return (yield exports.findProgramAddress([ Buffer.from(exports.METADATA_PREFIX), PROGRAM_IDS.metadata.toBuffer(), masterEdition.toBuffer(), Buffer.from(exports.RESERVATION), resource.toBuffer(), ], PROGRAM_IDS.metadata))[0]; }); } exports.getReservationList = getReservationList;