Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

refactor: use Wallet lib instead of getUTXO #62

Merged
merged 8 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 26 additions & 35 deletions lib/test/createClientWithFundedWallet.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
const {
PrivateKey,
} = require('@dashevo/dashcore-lib');

const Dash = require('dash');

const getDAPISeeds = require('./getDAPISeeds');

const fundAddress = require('./fundAddress');
const wait = require('../wait');

const fundAccount = require('./fundAccount');
shumkov marked this conversation as resolved.
Show resolved Hide resolved
/**
* Create and fund DashJS client
* @param {string} [HDPrivateKey]
Expand All @@ -19,47 +12,45 @@ async function createClientWithFundedWallet(HDPrivateKey = undefined) {
const seeds = getDAPISeeds();

// Prepare to fund wallet
const faucetPrivateKey = PrivateKey.fromString(process.env.FAUCET_PRIVATE_KEY);
const faucetAddress = faucetPrivateKey
.toAddress(process.env.NETWORK)
.toString();

const walletOptions = {};
const faucetPrivateKey = process.env.FAUCET_PRIVATE_KEY;

if (HDPrivateKey) {
walletOptions.HDPrivateKey = HDPrivateKey;
}

const dashClient = new Dash.Client({
const clientOpts = {
seeds,
wallet: walletOptions,
network: process.env.NETWORK,
apps: {
dpns: {
contractId: process.env.DPNS_CONTRACT_ID,
},
},
});
}

const account = await dashClient.getWalletAccount();
const walletOptions = {};

const { address: addressToFund } = account.getAddress();
if (HDPrivateKey) {
walletOptions.HDPrivateKey = HDPrivateKey;
}

const amount = 40000;
return new Promise(async (resolve, reject)=> {
const faucetWallet = new Dash.Client({
...clientOpts,
wallet: {
privateKey: faucetPrivateKey
},
});

const newlyCreatedClient = new Dash.Client({
...clientOpts,
wallet: walletOptions,
});

await fundAddress(
dashClient.getDAPIClient(),
faucetAddress,
faucetPrivateKey,
addressToFund,
amount,
);
const faucetAccount = await faucetWallet.getWalletAccount();

do {
await wait(500);
} while (account.getTotalBalance() < amount);
const accountToFund = await newlyCreatedClient.getWalletAccount();

return dashClient;
const amount = 40000;

await fundAccount(faucetAccount, accountToFund, amount);
});
}

module.exports = createClientWithFundedWallet;
43 changes: 43 additions & 0 deletions lib/test/fundAccount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
*
* @param {Account} fundedAccount
* @param {Account} accountToFund
* @param {number} amountInSatoshis
* @return {Promise<Transaction>}
*/
async function fundAccount(
fundedAccount,
accountToFund,
amountInSatoshis,
) {
return new Promise(async (resolve, reject) => {
try {
const { address: addressToFund } = accountToFund.getAddress();

const fundingTransaction = fundedAccount.createTransaction({
recipient: addressToFund,
satoshis: amountInSatoshis,
});
const fundingResult = {
transaction: fundingTransaction,
transactionId: null
};

accountToFund.on('BALANCE_CHANGED', () => {
console.log('Balance changed');
resolve(fundingResult);
});

accountToFund.on('FETCHED_CONFIRMED_TRANSACTION', () => {
console.log('Fetching confirmed tx');
resolve(fundingResult);
});

fundingResult.transactionId = await fundedAccount.broadcastTransaction(fundingTransaction);
} catch (err){
reject(err);
}
});
}

module.exports = fundAccount;
45 changes: 0 additions & 45 deletions lib/test/fundAddress.js

This file was deleted.

32 changes: 0 additions & 32 deletions lib/test/getInputsByAddress.js

This file was deleted.

42 changes: 21 additions & 21 deletions test/functional/core/broadcastTransaction.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
const {
Transaction,
PrivateKey,
} = require('@dashevo/dashcore-lib');
const Dash = require('dash');

const createClientWithoutWallet = require('../../../lib/test/createClientWithoutWallet');
const getInputsByAddress = require('../../../lib/test/getInputsByAddress');
const fundAccount = require('../../../lib/test/fundAccount');

describe('Core', () => {
describe('broadcastTransaction', () => {
Expand All @@ -21,29 +18,32 @@ describe('Core', () => {
});

it('should sent transaction and return transaction ID', async () => {
const faucetPrivateKey = PrivateKey.fromString(process.env.FAUCET_PRIVATE_KEY);
const faucetAddress = faucetPrivateKey
.toAddress(process.env.NETWORK)
.toString();

const address = new PrivateKey()
.toAddress(process.env.NETWORK)
.toString();
const faucetPrivateKey = process.env.FAUCET_PRIVATE_KEY;

const amount = 10000;

const inputs = await getInputsByAddress(client.getDAPIClient(), faucetAddress);
const clientOpts = {
network: process.env.NETWORK,
}

const transaction = new Transaction();
const faucetWallet = new Dash.Client({
...clientOpts,
wallet: {
privateKey: faucetPrivateKey
},
});
const faucetAccount = await faucetWallet.getWalletAccount();

transaction.from(inputs.slice(-1)[0])
.to(address, amount)
.change(faucetAddress)
.sign(faucetPrivateKey);
const walletToFund = new Dash.Client({
...clientOpts,
wallet: {
privateKey: null,
},
});

const serializedTransaction = Buffer.from(transaction.serialize(), 'hex');
const accountToFund = await walletToFund.getWalletAccount();

const result = await client.getDAPIClient().core.broadcastTransaction(serializedTransaction);
const { transactionId: result} = await fundAccount(faucetAccount, accountToFund, amount);

expect(result).to.be.a('string');
});
Expand Down
49 changes: 29 additions & 20 deletions test/functional/core/getAddressSummary.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
const {
PrivateKey,
} = require('@dashevo/dashcore-lib');
const Dash = require('dash');

const createClientWithoutWallet = require('../../../lib/test/createClientWithoutWallet');

const fundAddress = require('../../../lib/test/fundAddress');
const fundAccount = require('../../../lib/test/fundAccount');

describe('Core', () => {
describe('getAddressSummary', () => {
Expand All @@ -22,22 +20,33 @@ describe('Core', () => {
});

before(async () => {
const faucetPrivateKey = PrivateKey.fromString(process.env.FAUCET_PRIVATE_KEY);
const faucetAddress = faucetPrivateKey
.toAddress(process.env.NETWORK)
.toString();

address = new PrivateKey()
.toAddress(process.env.NETWORK)
.toString();

await fundAddress(
client.getDAPIClient(),
faucetAddress,
faucetPrivateKey,
address,
20000,
);
const faucetPrivateKey = process.env.FAUCET_PRIVATE_KEY;

const clientOpts = {
network: process.env.NETWORK,
}

const faucetWallet = new Dash.Client({
...clientOpts,
wallet: {
privateKey: faucetPrivateKey
},
});
const faucetAccount = await faucetWallet.getWalletAccount();

const walletToFund = new Dash.Client({
...clientOpts,
wallet: {
privateKey: null,
},
});

const accountToFund = await walletToFund.getWalletAccount();

const amount = 20000;

await fundAccount(faucetAccount, accountToFund, amount);

});

it('should return address summary', async () => {
Expand Down
46 changes: 28 additions & 18 deletions test/functional/core/getTransaction.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const Dash = require('dash');

const {
Transaction,
PrivateKey,
} = require('@dashevo/dashcore-lib');

const createClientWithoutWallet = require('../../../lib/test/createClientWithoutWallet');

const fundAddress = require('../../../lib/test/fundAddress');
const fundAccount = require('../../../lib/test/fundAccount');

describe('Core', () => {
describe('getTransaction', () => {
Expand All @@ -22,22 +23,31 @@ describe('Core', () => {
});

it('should respond with a transaction by it\'s ID', async () => {
const faucetPrivateKey = PrivateKey.fromString(process.env.FAUCET_PRIVATE_KEY);
const faucetAddress = faucetPrivateKey
.toAddress(process.env.NETWORK)
.toString();

const address = new PrivateKey()
.toAddress(process.env.NETWORK)
.toString();

const transactionId = await fundAddress(
client.getDAPIClient(),
faucetAddress,
faucetPrivateKey,
address,
20000,
);
const faucetPrivateKey = process.env.FAUCET_PRIVATE_KEY;

const amount = 20000;

const clientOpts = {
network: process.env.NETWORK,
}

const faucetWallet = new Dash.Client({
...clientOpts,
wallet: {
privateKey: faucetPrivateKey
},
});
const faucetAccount = await faucetWallet.getWalletAccount();

const walletToFund = new Dash.Client({
...clientOpts,
wallet: {
privateKey: null,
},
});
const accountToFund = await walletToFund.getWalletAccount();

const { transactionId } = await fundAccount(faucetAccount, accountToFund, amount);

const result = await client.getDAPIClient().core.getTransaction(transactionId);
const receivedTx = new Transaction(Buffer.from(result));
Expand Down