Skip to content

Commit

Permalink
Reward Transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
15Dkatz committed Sep 28, 2022
1 parent 17c57ba commit 7185319
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
12 changes: 11 additions & 1 deletion config.js
Expand Up @@ -12,4 +12,14 @@ const GENESIS_DATA = {

const STARTING_BALANCE = 1000;

module.exports = { GENESIS_DATA, MINE_RATE, STARTING_BALANCE };
const REWARD_INPUT = { address: '*authorized-reward*' };

const MINING_REWARD = 50;

module.exports = {
GENESIS_DATA,
MINE_RATE,
STARTING_BALANCE,
REWARD_INPUT,
MINING_REWARD
};
14 changes: 11 additions & 3 deletions wallet/transaction.js
@@ -1,11 +1,12 @@
const uuid = require('uuid/v1');
const { verifySignature } = require('../util');
const { REWARD_INPUT, MINING_REWARD } = require('../config');

class Transaction {
constructor({ senderWallet, recipient, amount }) {
constructor({ senderWallet, recipient, amount, outputMap, input }) {
this.id = uuid();
this.outputMap = this.createOutputMap({ senderWallet, recipient, amount });
this.input = this.createInput({ senderWallet, outputMap: this.outputMap });
this.outputMap = outputMap || this.createOutputMap({ senderWallet, recipient, amount });
this.input = input || this.createInput({ senderWallet, outputMap: this.outputMap });
}

createOutputMap({ senderWallet, recipient, amount }) {
Expand Down Expand Up @@ -62,6 +63,13 @@ class Transaction {

return true;
}

static rewardTransaction({ minerWallet }) {
return new this({
input: REWARD_INPUT,
outputMap: { [minerWallet.publicKey]: MINING_REWARD }
});
}
}

module.exports = Transaction;
18 changes: 18 additions & 0 deletions wallet/transaction.test.js
@@ -1,6 +1,7 @@
const Transaction = require('./transaction');
const Wallet = require('./index');
const { verifySignature } = require('../util');
const { REWARD_INPUT, MINING_REWARD } = require('../config');

describe('Transaction', () => {
let transaction, senderWallet, recipient, amount;
Expand Down Expand Up @@ -163,5 +164,22 @@ describe('Transaction', () => {
});
});
});

describe('rewardTransaction()', () => {
let rewardTransaction, minerWallet;

beforeEach(() => {
minerWallet = new Wallet();
rewardTransaction = Transaction.rewardTransaction({ minerWallet });
});

it('creates a transaction with the reward input', () => {
expect(rewardTransaction.input).toEqual(REWARD_INPUT);
});

it('creates one transaction for the miner with the `MINING_REWARD`', () => {
expect(rewardTransaction.outputMap[minerWallet.publicKey]).toEqual(MINING_REWARD);
});
});
});

0 comments on commit 7185319

Please sign in to comment.