90 lines
No EOL
3.1 KiB
JavaScript
90 lines
No EOL
3.1 KiB
JavaScript
const Wallet = require('./index');
|
|
const TransactionPool = require('./transaction-pool');
|
|
const Blockchain = require('../blockchain');
|
|
const { INITIAL_BALANCE } = require('../config');
|
|
|
|
describe('Wallet', () => {
|
|
let wallet, tp, bc;
|
|
|
|
beforeEach(() => {
|
|
wallet = new Wallet();
|
|
tp = new TransactionPool();
|
|
bc = new Blockchain();
|
|
});
|
|
|
|
describe('creating a transaction', () => {
|
|
let transaction, sendAmount, recipient;
|
|
|
|
beforeEach(() => {
|
|
sendAmount = 50;
|
|
recipient = 'r4nd0m-4ddr355';
|
|
Geo = 20;
|
|
Std = 9014;
|
|
Name = 'temp';
|
|
MetaHash = '123abcd';
|
|
transaction = wallet.createCoinTransaction(recipient, sendAmount,Geo, Std, Name,MetaHash, bc, tp);
|
|
});
|
|
|
|
describe('and doing the same transaction', () => {
|
|
beforeEach(() => {
|
|
wallet.createCoinTransaction(recipient, sendAmount,Geo, Std, Name,MetaHash, bc, tp);
|
|
});
|
|
|
|
it('doubles the `sendAmount` subtracted from the wallet balance', () => {
|
|
expect(transaction.outputs.find(output => output.address === wallet.publicKey).amount)
|
|
.toEqual(wallet.balance - sendAmount * 2);
|
|
});
|
|
|
|
it('clones the `sendAmount` output for the recipient', () => {
|
|
expect(transaction.outputs.filter(output => output.address === recipient)
|
|
.map(output => output.amount)).toEqual([sendAmount, sendAmount]);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('calculating a balance', () => {
|
|
let addBalance, repeatAdd, senderWallet;
|
|
|
|
beforeEach(() => {
|
|
senderWallet = new Wallet();
|
|
addBalance = 100;
|
|
repeatAdd = 3;
|
|
for (let i=0; i<repeatAdd; i++) {
|
|
senderWallet.createCoinTransaction(wallet.publicKey, addBalance,Geo, Std, Name,MetaHash, bc, tp);
|
|
}
|
|
bc.addBlock(tp.paymenttransactions);
|
|
});
|
|
|
|
it('calculates the balance for blockchain transactions matching the recipient', () => {
|
|
expect(wallet.calculateBalance(bc)).toEqual(INITIAL_BALANCE + (addBalance * repeatAdd));
|
|
});
|
|
|
|
it('calculates the balance for blockchain transactions matching the sender', () => {
|
|
expect(senderWallet.calculateBalance(bc)).toEqual(INITIAL_BALANCE - (addBalance * repeatAdd));
|
|
});
|
|
|
|
describe('and the recipient conducts a transaction', () => {
|
|
let subtractBalance, recipientBalance;
|
|
|
|
beforeEach(() => {
|
|
tp.clear();
|
|
subtractBalance = 60;
|
|
recipientBalance = wallet.calculateBalance(bc);
|
|
wallet.createCoinTransaction(senderWallet.publicKey, subtractBalance,20,9014,'temp','123abc', bc, tp);
|
|
bc.addBlock(tp.paymenttransactions);
|
|
});
|
|
|
|
describe('and the sender sends another transaction to the recipient', () => {
|
|
beforeEach(() => {
|
|
tp.clear();
|
|
senderWallet.createCoinTransaction(wallet.publicKey, addBalance, 20,9014,'temp','123abc',bc, tp);
|
|
bc.addBlock(tp.paymenttransactions);
|
|
});
|
|
|
|
it('calculate the recipient balance only using transactions since its most recent one', () => {
|
|
expect(wallet.calculateBalance(bc)).toEqual(recipientBalance - subtractBalance + addBalance);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
}); |