n8n-nodes-wax
Version:
n8n Community Node Package for the WAX Blockchain
252 lines • 8.33 kB
JavaScript
"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 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: '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'],
},
},
},
];
async function executeAssetOperations(items, i) {
const operation = this.getNodeParameter('operation', i);
const endpoint = this.getNodeParameter('endpoint', i);
if (operation === 'getAssets') {
const account = this.getNodeParameter('account', i);
const templateIdInput = this.getNodeParameter('templateId', i);
const collectionInput = this.getNodeParameter('collection', i);
const schemaInput = this.getNodeParameter('schema', i);
const code = this.getNodeParameter('code', i);
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 };
do {
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) {
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,
});
});
}
else {
console.log(`Warning: result.rows is not an array: ${JSON.stringify(result.rows)}`);
}
} while (result.more && result.rows);
return {
returnData: {
json: {
account,
assets
}
}
};
}
else if (operation === 'transferAssets') {
const credentials = await (0, util_2.getCredentials)(this);
const from = credentials.account;
const key = credentials.privateKey;
const to = this.getNodeParameter('to', i);
const memo = this.getNodeParameter('memo', i);
const assetIdsString = this.getNodeParameter('assetIds', i);
const contract = this.getNodeParameter('contract', i);
const assetIds = assetIdsString.split(',').map(id => id.trim());
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 }
}
};
}
return {};
}
//# sourceMappingURL=asset.js.map