parsec-node
Version:
https://parseclabs.readthedocs.io/en/latest/
48 lines (42 loc) • 1.26 kB
JavaScript
/**
* Copyright (c) 2018-present, Parsec Labs (parseclabs.org)
*
* This source code is licensed under the Mozilla Public License Version 2.0
* found in the LICENSE file in the root directory of this source tree.
*/
const { helpers, Tx, Outpoint } = require('parsec-lib');
const { unspentForAddress } = require('../utils');
/*
* Creates transfer tx based on address unspent outputs
*/
// ToDo: solve subset sum/knapsack problem for optimal outputs usage
module.exports = async function makeTransfer(
{ balances, unspent },
from,
to,
amount,
color,
privKey
) {
let fromAddr = from.toLowerCase(); // eslint-disable-line
to = to.toLowerCase(); // eslint-disable-line
const colorBalances = balances[color] || {};
const balance = colorBalances[fromAddr] || 0;
if (balance < amount) {
throw new Error('Insufficient balance');
}
const senderUnspent = unspentForAddress(unspent, from, color).map(u => ({
output: u.output,
outpoint: Outpoint.fromRaw(u.outpoint),
}));
const inputs = helpers.calcInputs(senderUnspent, from, amount, color);
const outputs = helpers.calcOutputs(
senderUnspent,
inputs,
fromAddr,
to,
amount,
color
);
return Tx.transfer(inputs, outputs).signAll(privKey);
};