n8n-nodes-wax
Version:
n8n Community Node Package for the WAX Blockchain
194 lines • 6.17 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.tokenProperties = void 0;
exports.executeTokenOperations = executeTokenOperations;
const axios_1 = __importDefault(require("axios"));
const eosjs_1 = require("eosjs");
const eosjs_jssig_1 = require("eosjs/dist/eosjs-jssig");
const util_1 = require("util");
const util_2 = require("./util");
exports.tokenProperties = [
{
displayName: 'Operation',
name: 'operation',
type: 'hidden',
noDataExpression: true,
displayOptions: {
show: {
resource: ['token'],
},
},
options: [
{
name: 'Get Account Token Balance',
value: 'getBalance',
action: 'Get account token balance',
},
{
name: 'Transfer Tokens',
value: 'transferTokens',
description: 'Transfer tokens to another account',
action: 'Transfer tokens to another account',
},
],
default: 'getBalance',
},
{
displayName: 'Account Name',
name: 'account',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: ['token'],
operation: ['getBalance'],
},
},
description: 'WAX account name',
},
{
displayName: 'Token Contract',
name: 'contract',
type: 'string',
default: 'eosio.token',
required: true,
displayOptions: {
show: {
resource: ['token'],
operation: ['getBalance', 'transferTokens'],
},
},
description: 'Token contract (e.g., "eosio.token" for WAX)',
},
{
displayName: 'Symbol',
name: 'symbol',
type: 'string',
default: 'WAX',
displayOptions: {
show: {
resource: ['token'],
operation: ['getBalance', 'transferTokens'],
},
},
description: 'Token symbol (e.g., "WAX")',
},
{
displayName: 'To Account',
name: 'to',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: ['token'],
operation: ['transferTokens'],
},
},
},
{
displayName: 'Amount',
name: 'amount',
type: 'number',
default: 1,
required: true,
displayOptions: {
show: {
resource: ['token'],
operation: ['transferTokens'],
},
},
description: 'Amount of tokens to transfer (e.g., 1)',
},
{
displayName: 'Precision',
name: 'precision',
type: 'number',
default: 8,
displayOptions: {
show: {
resource: ['token'],
operation: ['transferTokens'],
},
},
description: 'Number of decimal places for the token (default is 8)',
},
{
displayName: 'Memo',
name: 'memo',
type: 'string',
default: '',
displayOptions: {
show: {
resource: ['token'],
operation: ['transferTokens'],
},
},
},
];
async function executeTokenOperations(items, i) {
var _a;
const operation = this.getNodeParameter('operation', i);
const endpoint = this.getNodeParameter('endpoint', i);
if (operation === 'getBalance') {
const account = this.getNodeParameter('account', i);
const contract = this.getNodeParameter('contract', i);
const symbol = this.getNodeParameter('symbol', i);
const payload = { account, code: contract };
if (symbol)
payload.symbol = symbol;
const { data } = await axios_1.default.post(`${endpoint}/v1/chain/get_currency_balance`, payload);
const item = (_a = data.find((item) => item.endsWith(` ${symbol}`))) !== null && _a !== void 0 ? _a : `0 ${symbol}`;
const [_balance, _symbol] = item.split(' ');
const balance = parseFloat(_balance);
return {
returnData: {
json: { account, contract, symbol, balance }
}
};
}
else if (operation === 'transferTokens') {
const credentials = await (0, util_2.getCredentials)(this);
const from = credentials.account;
const key = credentials.privateKey;
const to = this.getNodeParameter('to', i);
const amount = this.getNodeParameter('amount', i);
const symbol = this.getNodeParameter('symbol', i);
const precision = this.getNodeParameter('precision', i) || 8;
const memo = this.getNodeParameter('memo', i);
const contract = this.getNodeParameter('contract', i);
const formattedAmount = amount.toFixed(precision);
const quantity = `${formattedAmount} ${symbol}`;
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,
quantity,
memo,
}
}];
const result = await api.transact({
actions
}, {
blocksBehind: 3,
expireSeconds: 30,
});
return {
returnData: {
json: { result }
}
};
}
return {};
}
//# sourceMappingURL=token.js.map