UNPKG

n8n-nodes-wax

Version:

n8n Community Node Package for the WAX Blockchain

399 lines 15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.assetProperties = void 0; exports.executeAssetOperations = executeAssetOperations; const n8n_workflow_1 = require("n8n-workflow"); const eosjs_1 = require("eosjs"); const eosjs_jssig_1 = require("eosjs/dist/eosjs-jssig"); const util_1 = require("util"); const util_2 = require("./util"); const atomic_1 = require("./atomic"); const dist_1 = require("@waxio/waxjs/dist"); exports.assetProperties = [ { displayName: 'Operation', name: 'operation', type: 'hidden', noDataExpression: true, displayOptions: { show: { resource: ['asset'], }, }, options: [ { name: 'Get Assets', value: 'getAssets', description: 'Get a list of assets owned by an account', action: 'Get a list of assets owned by an account', }, { name: 'Mint Asset', value: 'mintAsset', description: 'Mint an asset from a template', action: 'Mint an asset from a template', }, { name: 'Transfer Assets', value: 'transferAssets', description: 'Transfer assets to another account', action: 'Transfer assets to another account', }, ], default: 'getAssets', }, { displayName: 'Account Name', name: 'account', type: 'string', default: '', required: true, displayOptions: { show: { resource: ['asset'], operation: ['getAssets'], }, }, description: 'WAX account name', }, { displayName: 'Template ID (Optional)', name: 'templateId', type: 'string', default: '', displayOptions: { show: { resource: ['asset'], operation: ['getAssets'], }, }, description: 'Comma-separated list of template IDs', }, { displayName: 'Collection (Optional)', name: 'collection', type: 'string', default: '', displayOptions: { show: { resource: ['asset'], operation: ['getAssets'], }, }, description: 'Comma-separated list of collections', }, { displayName: 'Schema (Optional)', name: 'schema', type: 'string', default: '', displayOptions: { show: { resource: ['asset'], operation: ['getAssets'], }, }, description: 'Comma-separated list of schemas', }, { displayName: 'Code', name: 'code', type: 'string', default: 'atomicassets', required: true, displayOptions: { show: { resource: ['asset'], operation: ['getAssets'], }, }, }, { displayName: 'To Account', name: 'to', type: 'string', default: '', required: true, displayOptions: { show: { resource: ['asset'], operation: ['transferAssets'], }, }, }, { displayName: 'Asset IDs (Comma-Separated)', name: 'assetIds', type: 'string', default: '', required: true, displayOptions: { show: { resource: ['asset'], operation: ['transferAssets'], }, }, }, { displayName: 'Contract', name: 'contract', type: 'string', default: 'atomicassets', displayOptions: { show: { resource: ['asset'], operation: ['transferAssets'], }, }, }, { displayName: 'Memo', name: 'memo', type: 'string', default: '', displayOptions: { show: { resource: ['asset'], operation: ['transferAssets'], }, }, }, { displayName: 'Collection Name', name: 'collectionName', type: 'string', default: '', required: true, displayOptions: { show: { resource: ['asset'], operation: ['mintAsset'], }, }, description: 'AtomicAssets collection that owns the template', }, { displayName: 'Template ID', name: 'templateIdMint', type: 'number', default: 0, required: true, displayOptions: { show: { resource: ['asset'], operation: ['mintAsset'], }, }, description: 'ID of the template to mint from. Schema is derived from the template.', }, { displayName: 'New Asset Owner', name: 'newAssetOwner', type: 'string', default: '', required: true, displayOptions: { show: { resource: ['asset'], operation: ['mintAsset'], }, }, description: 'Account that will receive the minted asset', }, { displayName: 'Contract', name: 'contractMint', type: 'string', default: 'atomicassets', displayOptions: { show: { resource: ['asset'], operation: ['mintAsset'], }, }, description: 'AtomicAssets contract account. Pin to a literal value - this is signed under your active permission.', }, { displayName: 'Immutable Data Override (JSON)', name: 'immutableDataMint', type: 'json', default: '{}', displayOptions: { show: { resource: ['asset'], operation: ['mintAsset'], }, }, description: 'Per-asset immutable data overrides as a JSON object whose keys/types match the schema format. Usually empty when minting from a template.', }, { displayName: 'Mutable Data (JSON)', name: 'mutableDataMint', type: 'json', default: '{}', displayOptions: { show: { resource: ['asset'], operation: ['mintAsset'], }, }, description: 'Per-asset mutable data as a JSON object whose keys/types match the schema format', }, { displayName: 'Back with Assets', name: 'backWithAssets', type: 'string', default: '', displayOptions: { show: { resource: ['asset'], operation: ['mintAsset'], }, }, description: 'Optional comma-separated list of EOSIO asset strings (e.g., "1.00000000 WAX") to back the minted NFT with. Maps to atomicassets::mintasset tokens_to_back.', }, ]; async function executeAssetOperations(items, i) { const operation = this.getNodeParameter('operation', i); const rawEndpoint = this.getNodeParameter('endpoint', i); const signing = operation === 'transferAssets' || operation === 'mintAsset'; const endpoint = (0, util_2.validateEndpoint)(this, rawEndpoint, { signing }); if (operation === 'getAssets') { const account = (0, util_2.requireAccountName)(this, this.getNodeParameter('account', i), 'Account Name'); const templateIdInput = this.getNodeParameter('templateId', i); const collectionInput = this.getNodeParameter('collection', i); const schemaInput = this.getNodeParameter('schema', i); const code = (0, util_2.requireAccountName)(this, this.getNodeParameter('code', i), 'Code'); const templateIds = templateIdInput ? templateIdInput.split(',').map(id => parseInt(id.trim())).filter(id => !isNaN(id)) : []; const collections = collectionInput ? collectionInput.split(',').map(c => c.trim()).filter(c => c !== '') : []; const schemas = schemaInput ? schemaInput.split(',').map(s => s.trim()).filter(s => s !== '') : []; const wax = new dist_1.WaxJS(endpoint); const assets = new Array(); let result = { next_key: null, more: true }; let iterations = 0; let lastKey = null; do { if (++iterations > util_2.MAX_PAGINATION_ITERATIONS) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `get_table_rows pagination exceeded ${util_2.MAX_PAGINATION_ITERATIONS} iterations`); } result = await wax.rpc.get_table_rows({ json: true, code, scope: account, table: 'assets', lower_bound: result.next_key, limit: 1000, reverse: false, show_payer: false, }); if (result.more && result.next_key !== null && result.next_key === lastKey) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'get_table_rows pagination did not advance'); } lastKey = result.next_key; if (!result) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Empty response from get_table_rows'); } if (!result.hasOwnProperty('rows')) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Response missing rows property: ' + JSON.stringify(result)); } if (result.rows && Array.isArray(result.rows)) { result.rows.forEach((asset) => { if ((templateIds.length > 0 && !templateIds.includes(asset.template_id)) || (collections.length > 0 && !collections.includes(asset.collection_name)) || (schemas.length > 0 && !schemas.includes(asset.schema_name))) { return; } assets.push({ asset_id: asset.asset_id, template_id: asset.template_id, collection_name: asset.collection_name, schema_name: asset.schema_name, }); }); } } while (result.more && result.rows); return { returnData: { json: { account, assets } } }; } else if (operation === 'transferAssets') { const credentials = await (0, util_2.getCredentials)(this); const from = (0, util_2.requireAccountName)(this, credentials.account, 'Credential Account Name'); const key = credentials.privateKey; const to = (0, util_2.requireAccountName)(this, this.getNodeParameter('to', i), 'To Account'); const memo = (0, util_2.normalizeMemo)(this, this.getNodeParameter('memo', i), 'Memo'); const assetIds = (0, util_2.requireAssetIds)(this, this.getNodeParameter('assetIds', i), 'Asset IDs'); const contract = (0, util_2.requireAccountName)(this, this.getNodeParameter('contract', i), 'Contract'); const signatureProvider = new eosjs_jssig_1.JsSignatureProvider([key]); const rpc = new eosjs_1.JsonRpc(endpoint, { fetch }); const api = new eosjs_1.Api({ rpc, signatureProvider, textDecoder: new util_1.TextDecoder(), textEncoder: new util_1.TextEncoder() }); const actions = [{ account: contract, name: 'transfer', authorization: [{ actor: from, permission: 'active' }], data: { from, to, asset_ids: assetIds, memo, } }]; const result = await api.transact({ actions }, { blocksBehind: 3, expireSeconds: 30, }); return { returnData: { json: { result } } }; } else if (operation === 'mintAsset') { const credentials = await (0, util_2.getCredentials)(this); const from = (0, util_2.requireAccountName)(this, credentials.account, 'Credential Account Name'); const key = credentials.privateKey; const collectionName = (0, util_2.requireAccountName)(this, this.getNodeParameter('collectionName', i), 'Collection Name'); const templateId = (0, atomic_1.requireTemplateId)(this, this.getNodeParameter('templateIdMint', i), 'Template ID'); const newAssetOwner = (0, util_2.requireAccountName)(this, this.getNodeParameter('newAssetOwner', i), 'New Asset Owner'); const contract = (0, util_2.requireAccountName)(this, this.getNodeParameter('contractMint', i), 'Contract'); const atomicRpc = (0, atomic_1.createAtomicRpc)(this, endpoint, contract); await (0, atomic_1.ensureAuthorized)(this, atomicRpc, collectionName, from); const { schema_name: schemaName } = await (0, atomic_1.ensureTemplateExists)(this, atomicRpc, collectionName, templateId); const format = await (0, atomic_1.fetchSchemaFormat)(this, atomicRpc, collectionName, schemaName); const immutableData = (0, atomic_1.buildAttributeMap)(this, this.getNodeParameter('immutableDataMint', i), format, 'Immutable Data Override'); const mutableData = (0, atomic_1.buildAttributeMap)(this, this.getNodeParameter('mutableDataMint', i), format, 'Mutable Data'); const tokensToBack = (0, atomic_1.parseTokensToBack)(this, this.getNodeParameter('backWithAssets', i), 'Back with Assets'); const generator = (0, atomic_1.createActionGenerator)(this, contract); const actions = await generator.mintasset([{ actor: from, permission: 'active' }], from, collectionName, schemaName, templateId, newAssetOwner, immutableData, mutableData, tokensToBack); const signatureProvider = new eosjs_jssig_1.JsSignatureProvider([key]); const rpc = new eosjs_1.JsonRpc(endpoint, { fetch }); const api = new eosjs_1.Api({ rpc, signatureProvider, textDecoder: new util_1.TextDecoder(), textEncoder: new util_1.TextEncoder(), }); const result = await api.transact({ actions }, { blocksBehind: 3, expireSeconds: 30 }); return { returnData: { json: { success: true, collection_name: collectionName, schema_name: schemaName, template_id: templateId, new_asset_owner: newAssetOwner, transaction: result, }, }, }; } return {}; } //# sourceMappingURL=asset.js.map