Files
xahau.js/src/api/transaction/settings.js

113 lines
3.2 KiB
JavaScript

/* @flow */
'use strict';
const assert = require('assert');
const BigNumber = require('bignumber.js');
const utils = require('./utils');
const validate = utils.common.validate;
const AccountFlagIndices = utils.common.constants.AccountFlagIndices;
const AccountFields = utils.common.constants.AccountFields;
import type {Instructions, Prepare} from './types.js';
import type {Settings} from './settings-types.js';
// Emptry string passed to setting will clear it
const CLEAR_SETTING = null;
function setTransactionFlags(txJSON: Object, values: Settings) {
const keys = Object.keys(values);
assert(keys.length === 1, 'ERROR: can only set one setting per transaction');
const flagName = keys[0];
const value = values[flagName];
const index = AccountFlagIndices[flagName];
if (index !== undefined) {
if (value) {
txJSON.SetFlag = index;
} else {
txJSON.ClearFlag = index;
}
}
}
function setTransactionFields(txJSON: Object, input: Settings) {
const fieldSchema = AccountFields;
for (const fieldName in fieldSchema) {
const field = fieldSchema[fieldName];
let value = input[field.name];
if (value === undefined) {
continue;
}
// The value required to clear an account root field varies
if (value === CLEAR_SETTING && field.hasOwnProperty('defaults')) {
value = field.defaults;
}
if (field.encoding === 'hex' && !field.length) {
// This is currently only used for Domain field
value = new Buffer(value, 'ascii').toString('hex').toUpperCase();
}
txJSON[fieldName] = value;
}
}
/**
* Note: A fee of 1% requires 101% of the destination to be sent for the
* destination to receive 100%.
* The transfer rate is specified as the input amount as fraction of 1.
* To specify the default rate of 0%, a 100% input amount, specify 1.
* To specify a rate of 1%, a 101% input amount, specify 1.01
*
* @param {Number|String} transferRate
*
* @returns {Number|String} numbers will be converted while strings
* are returned
*/
function convertTransferRate(transferRate: number | string): number | string {
return (new BigNumber(transferRate)).shift(9).toNumber();
}
function createSettingsTransaction(account: string, settings: Settings
): Object {
validate.address(account);
validate.settings(settings);
if (settings.regularKey) {
return {
TransactionType: 'SetRegularKey',
Account: account,
RegularKey: settings.regularKey
};
}
const txJSON: Object = {
TransactionType: 'AccountSet',
Account: account
};
setTransactionFlags(txJSON, settings);
setTransactionFields(txJSON, settings);
if (txJSON.TransferRate !== undefined) {
txJSON.TransferRate = convertTransferRate(txJSON.TransferRate);
}
return txJSON;
}
function prepareSettingsAsync(account: string, settings: Settings,
instructions: Instructions, callback
) {
const txJSON = createSettingsTransaction(account, settings);
utils.prepareTransaction(txJSON, this, instructions, callback);
}
function prepareSettings(account: string, settings: Object,
instructions: Instructions = {}
): Promise<Prepare> {
return utils.promisify(prepareSettingsAsync.bind(this))(
account, settings, instructions);
}
module.exports = prepareSettings;