UNPKG

3.85 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.AtomicAssetsService = void 0;
4const antelope_1 = require("@wharfkit/antelope");
5const atomicassets_1 = require("atomicassets");
6/**
7 * AtomicAssetsService
8 *
9 * This service is used to interact with the atomicassets contract.
10 *
11 * Read more on the atomicassets contracts here:
12 * https://github.com/pinknetworkx/atomicassets-contract/wiki/
13 */
14class AtomicAssetsService {
15 constructor(client) {
16 this.client = client;
17 /**
18 * Retrieve atomic assets from the atomicassets contract for the given account
19 * @param account eosio account name
20 * @returns {Promise<AtomicAsset[]>} Returns an array of atomic assets
21 */
22 this.getAccountAssets = async (account) => {
23 const { rows } = await this.client.eos.v1.chain.get_table_rows({
24 code: this.client.config.atomicAssetsContract,
25 scope: account,
26 table: 'assets',
27 limit: 100
28 });
29 return rows;
30 };
31 /**
32 * Retrieve atomic assets from the atomicassets contract
33 * @param account eosio account name
34 * @param assetId
35 * @returns {Promise<AtomicAsset>} Returns the atomic asset config
36 */
37 this.getAsset = async (account, assetId, deserializeAsset = true) => {
38 try {
39 const { rows } = await this.client.eos.v1.chain.get_table_rows({
40 code: this.client.config.atomicAssetsContract,
41 scope: account,
42 table: 'assets',
43 limit: 1,
44 lower_bound: antelope_1.UInt128.from(assetId),
45 upper_bound: antelope_1.UInt128.from(assetId)
46 });
47 const [asset] = rows;
48 if (deserializeAsset) {
49 const schema = await this.getSchema(asset.collection_name, asset.schema_name);
50 const objectSchema = (0, atomicassets_1.ObjectSchema)(schema.format);
51 const mutable_deserialized_data = (0, atomicassets_1.deserialize)(asset.mutable_serialized_data, objectSchema);
52 const immutable_deserialized_data = (0, atomicassets_1.deserialize)(asset.immutable_serialized_data, objectSchema);
53 return { ...asset, immutable_deserialized_data, mutable_deserialized_data };
54 }
55 else {
56 return asset;
57 }
58 }
59 catch (error) {
60 console.error(error);
61 throw error;
62 }
63 };
64 // TODO: Figure out if there is a collection that ever has a schema with more than 100 rows
65 this.getCollection = async (collectionName) => {
66 const { rows } = await this.client.eos.v1.chain.get_table_rows({
67 code: this.client.config.atomicAssetsContract,
68 scope: collectionName,
69 table: 'collections',
70 limit: 100,
71 });
72 // console.debug('getCollection', rows)
73 return rows;
74 };
75 this.getSchema = async (collectionName, schemaName) => {
76 const { rows } = await this.client.eos.v1.chain.get_table_rows({
77 code: this.client.config.atomicAssetsContract,
78 scope: collectionName,
79 table: 'schemas',
80 limit: 100
81 });
82 const schema = rows.find((schema) => schema.schema_name === schemaName);
83 return schema;
84 };
85 /**
86 * TODO
87 * Mint an asset to the given account
88 */
89 this.mintAsset = async () => { };
90 }
91}
92exports.AtomicAssetsService = AtomicAssetsService;
93//# sourceMappingURL=atomic.js.map
\No newline at end of file