n8n-nodes-wax
Version:
n8n Community Node Package for the WAX Blockchain
215 lines • 7.7 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.accountProperties = void 0;
exports.executeAccountOperations = executeAccountOperations;
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.accountProperties = [
{
displayName: 'Operation',
name: 'operation',
type: 'hidden',
noDataExpression: true,
displayOptions: {
show: {
resource: ['account'],
},
},
options: [
{
name: 'Buy RAM',
value: 'buyRam',
action: 'Buy RAM',
},
{
name: 'Get Account Info',
value: 'getAccountInfo',
description: 'Get account information',
action: 'Get account information',
},
{
name: 'Stake CPU',
value: 'stakeCpu',
description: 'Stake WAX for CPU resources',
action: 'Stake WAX for CPU resources',
},
{
name: 'Stake NET',
value: 'stakeNet',
description: 'Stake WAX for NET resources',
action: 'Stake WAX for NET resources',
},
{
name: 'Verify Account',
value: 'verifyAccount',
description: 'Verify an account exists',
action: 'Verify an account exists',
},
],
default: 'getAccountInfo',
},
{
displayName: 'Account Name',
name: 'account',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: ['account'],
operation: ['buyRam', 'getAccountInfo', 'stakeCpu', 'stakeNet', 'verifyAccount'],
},
},
description: 'WAX account name',
},
{
displayName: 'Amount (WAX)',
name: 'amount',
type: 'number',
default: 1,
required: true,
displayOptions: {
show: {
resource: ['account'],
operation: ['buyRam', 'stakeCpu', 'stakeNet'],
},
},
description: 'Amount of WAX',
},
{
displayName: 'Transfer Stake to New Account',
name: 'transfer',
type: 'boolean',
default: false,
required: true,
displayOptions: {
show: {
resource: ['account'],
operation: ['stakeCpu', 'stakeNet'],
},
},
description: 'Whether to transfer ownership of the staked tokens to the new account',
},
];
async function executeAccountOperations(items, i) {
var _a, _b;
const operation = this.getNodeParameter('operation', i);
const endpoint = this.getNodeParameter('endpoint', i);
const account = this.getNodeParameter('account', i);
if (operation === 'getAccountInfo' || operation === 'verifyAccount') {
if (operation === 'getAccountInfo') {
const response = await axios_1.default.post(`${endpoint}/v1/chain/get_account`, {
account_name: account,
});
return { returnData: { json: response.data } };
}
else if (operation === 'verifyAccount') {
try {
const result = await axios_1.default.post(`${endpoint}/v1/chain/get_account`, {
account_name: account,
});
return {
returnData: {
json: {
account,
exists: true,
created: result.data.created,
},
},
};
}
catch (error) {
if (axios_1.default.isAxiosError(error) && [400, 404].includes((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.status) !== null && _b !== void 0 ? _b : 0)) {
return {
returnData: {
json: {
account,
exists: false,
error: 'Account does not exist on the WAX blockchain',
},
},
};
}
throw new Error(`Failed to verify account: ${error.message}`);
}
}
}
else if (operation === 'buyRam' || operation === 'stakeCpu' || operation === 'stakeNet') {
const credentials = await (0, util_2.getCredentials)(this);
const from = credentials.account;
const amount = this.getNodeParameter('amount', i);
let actions = [];
let formattedAmount = '';
if (operation === 'buyRam') {
formattedAmount = amount.toFixed(8);
actions = [{
account: 'eosio',
name: 'buyram',
authorization: [{ actor: from, permission: 'active' }],
data: {
payer: from,
receiver: account,
quant: `${formattedAmount} WAX`,
}
}];
}
else if (operation === 'stakeCpu' || operation === 'stakeNet') {
formattedAmount = amount.toFixed(8);
const transfer = this.getNodeParameter('transfer', i);
const cpuAmount = operation === 'stakeCpu' ? formattedAmount : '0.00000000';
const netAmount = operation === 'stakeNet' ? formattedAmount : '0.00000000';
actions = [{
account: 'eosio',
name: 'delegatebw',
authorization: [{ actor: from, permission: 'active' }],
data: {
from: from,
receiver: account,
stake_net_quantity: `${netAmount} WAX`,
stake_cpu_quantity: `${cpuAmount} WAX`,
transfer: transfer,
}
}];
}
try {
const key = credentials.privateKey;
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,
operation,
from,
receiver: account,
amount: formattedAmount,
transaction: result
}
}
};
}
catch (error) {
throw new Error(`Failed to execute ${operation}: ${error.message}`);
}
}
return {};
}
//# sourceMappingURL=account.js.map