@ton.js/core
Version:
TonWeb - JavaScript API for TON blockchain
128 lines • 6.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NftCollection = void 0;
const cell_1 = require("../../../boc/cell");
const base64_1 = require("../../../utils/base64");
const contract_1 = require("../../contract");
const utils_1 = require("./utils");
const CODE_HEX = ('B5EE9C72410213010001FE000114FF00F4A413F4BCF2C80B0102016202030202CD04050201200D0E02012006070201480B0C03ED420C700915BE001D0D3030171B0915BE0FA4030ED44D0FA40D33FD4D4D43006D31FD33F8210693D39505230BA8E29165F0602D0128210A8CB00AD708010C8CB055005CF1624FA0214CB6A13CB1FCB3F01CF16C98040FB00E0315165C705F2E19120C001E30220C002E3023403C003E3025F05840FF2F0808090A002D501C8CB3FF828CF16C97020C8CB0113F400F400CB00C9800623004D33F5313BBF2E1925313BA01FA00D43027103459F0058E1301A4443302C85005CF1613CB3FCCCCCCC9ED54925F05E200A2307005D4308E378040F4966FA5208E2908A4208100FABE93F2C18FDE81019321A05327BBF2F402FA00D43022544630F00525BA9304A404DE06926C21E2B3E630344034C85005CF1613CB3FCCCCCCC9ED54002803FA40304334C85005CF1613CB3FCCCCCCC9ED54001B3E401D3232C084B281F2FFF27420003D16BC00DC087C011DE0063232C15633C594013E8084F2DAC4B333325C7EC0200201200F100025BC82DF6A2687D20699FEA6A6A182DE86A182C40043B8B5D31ED44D0FA40D33FD4D4D43010245F04D0D431D430D071C8CB0701CF16CCC980201201112002FB5DAFDA89A1F481A67FA9A9A860D883A1A61FA61FF480610002DB4F47DA89A1F481A67FA9A9A86028BE09E006E003E00901654EE64');
/**
* NFT Release Candidate - may still change slightly.
*/
class NftCollection extends contract_1.Contract {
constructor(provider, options) {
options.wc = 0;
options.code = (options.code || cell_1.Cell.oneFromBoc(CODE_HEX));
if (options.royalty > 1) {
throw new Error(`"royalty" option can't be greater than one`);
}
super(provider, options);
this.royaltyBase = 1000;
this.royaltyFactor = Math.floor(this.options.royalty *
this.royaltyBase);
this.methods.getCollectionData = (() => this.getCollectionData());
this.methods.getNftItemAddressByIndex = (index => this.getNftItemAddressByIndex(index));
this.methods.getNftItemContent = (nftItem => this.getNftItemContent(nftItem));
this.methods.getRoyaltyParams = (() => this.getRoyaltyParams());
}
createMintBody(params) {
const body = new cell_1.Cell();
body.bits.writeUint(1, 32); // OP deploy new nft
body.bits.writeUint(params.queryId || 0, 64); // query_id
body.bits.writeUint(params.itemIndex, 64);
body.bits.writeCoins(params.amount);
const nftItemContent = new cell_1.Cell();
nftItemContent.bits.writeAddress(params.itemOwnerAddress);
const uriContent = new cell_1.Cell();
uriContent.bits.writeBytes((0, utils_1.serializeUri)(params.itemContentUri));
nftItemContent.refs[0] = uriContent;
body.refs[0] = nftItemContent;
return body;
}
createGetRoyaltyParamsBody(params) {
const body = new cell_1.Cell();
body.bits.writeUint(0x693d3950, 32); // OP
body.bits.writeUint(params.queryId || 0, 64); // query_id
return body;
}
createChangeOwnerBody(params) {
const body = new cell_1.Cell();
body.bits.writeUint(3, 32); // OP
body.bits.writeUint(params.queryId || 0, 64); // query_id
body.bits.writeAddress(params.newOwnerAddress);
return body;
}
async getCollectionData() {
const myAddress = await this.getAddress();
const result = await this.provider.call2(myAddress.toString(), 'get_collection_data');
const nextItemIndex = result[0].toNumber();
const collectionContentUri = (0, utils_1.parseOffchainUriCell)(result[1]);
const ownerAddress = (0, utils_1.parseAddress)(result[2]);
return {
nextItemIndex,
ownerAddress,
collectionContentUri,
};
}
async getNftItemContent(nftItem) {
const myAddress = await this.getAddress();
const itemData = await nftItem.getData();
const itemContent = {
isInitialized: itemData.isInitialized,
index: itemData.index,
collectionAddress: itemData.collectionAddress,
ownerAddress: itemData.ownerAddress,
};
if (itemData.isInitialized) {
const result = await this.provider.call2(myAddress.toString(), 'get_nft_content', [
['num', itemData.index],
['tvm.Cell', (0, base64_1.bytesToBase64)(await itemData.contentCell.toBoc(false))],
]);
itemContent.contentUri = (0, utils_1.parseOffchainUriCell)(result);
}
return itemContent;
}
async getNftItemAddressByIndex(index) {
const myAddress = await this.getAddress();
const result = await this.provider.call2(myAddress.toString(), 'get_nft_address_by_index', [['num', index]]);
return (0, utils_1.parseAddress)(result);
}
async getRoyaltyParams() {
const myAddress = await this.getAddress();
const result = await this.provider.call2(myAddress.toString(), 'royalty_params');
const royaltyFactor = result[0].toNumber();
const royaltyBase = result[1].toNumber();
const royalty = royaltyFactor / royaltyBase;
const royaltyAddress = (0, utils_1.parseAddress)(result[2]);
return {
royalty,
royaltyBase,
royaltyFactor,
royaltyAddress,
};
}
/**
* Returns cell that contains NFT collection data.
*/
createDataCell() {
const cell = new cell_1.Cell();
cell.bits.writeAddress(this.options.ownerAddress);
cell.bits.writeUint(0, 64); // next_item_index
const collectionContentCell = (0, utils_1.createOffchainUriCell)(this.options.collectionContentUri);
const commonContentCell = new cell_1.Cell();
commonContentCell.bits.writeBytes((0, utils_1.serializeUri)(this.options.nftItemContentBaseUri));
const contentCell = new cell_1.Cell();
contentCell.refs[0] = collectionContentCell;
contentCell.refs[1] = commonContentCell;
cell.refs[0] = contentCell;
cell.refs[1] = cell_1.Cell.oneFromBoc(this.options.nftItemCodeHex);
const royaltyCell = new cell_1.Cell();
royaltyCell.bits.writeUint(this.royaltyFactor, 16);
royaltyCell.bits.writeUint(this.royaltyBase, 16);
royaltyCell.bits.writeAddress(this.options.royaltyAddress);
cell.refs[2] = royaltyCell;
return cell;
}
}
exports.NftCollection = NftCollection;
//# sourceMappingURL=nft-collection.js.map