Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
refactor(Shift): more generic creation of shift payments
Browse files Browse the repository at this point in the history
  • Loading branch information
Thore3 committed Oct 23, 2017
1 parent 88fe9c8 commit 01998a4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
5 changes: 5 additions & 0 deletions src/bch/bch-account.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable semi */
const BchSpendable = require('./bch-spendable')
const BchShiftPayment = require('../shift/bch-payment');

const ACCOUNT_LABEL_PREFIX = 'Bitcoin Cash - '

Expand Down Expand Up @@ -50,6 +51,10 @@ class BchAccount extends BchSpendable {
createPayment () {
return super.createPayment().from(this.index, this.changeAddress)
}

createShiftPayment (wallet) {
return BchShiftPayment.fromWallet(wallet, this)
}
}

module.exports = BchAccount
5 changes: 5 additions & 0 deletions src/eth/eth-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const EthTxBuilder = require('./eth-tx-builder');
const EthWalletTx = require('./eth-wallet-tx');
const API = require('../api');
const { toBigNumber, toWei, fromWei } = require('../helpers');
const EthShiftPayment = require('../shift/eth-payment');

class EthAccount {
constructor (obj) {
Expand Down Expand Up @@ -143,6 +144,10 @@ class EthAccount {
});
}

createShiftPayment (wallet) {
return EthShiftPayment.fromWallet(wallet, this);
}

static privateKeyToAddress (privateKey) {
return ethUtil.toChecksumAddress(ethUtil.privateToAddress(privateKey).toString('hex'));
}
Expand Down
5 changes: 5 additions & 0 deletions src/hd-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var MyWallet = require('./wallet'); // This cyclic import should be avoided once
var API = require('./api');
var Transaction = require('./transaction');
var constants = require('./constants');
var BtcShiftPayment = require('./shift/btc-payment');

// HDAccount Class

Expand Down Expand Up @@ -318,3 +319,7 @@ HDAccount.prototype.getAvailableBalance = function (feeType) {
return { amount, fee: fees[feeType] };
});
};

HDAccount.prototype.createShiftPayment = function (wallet) {
return BtcShiftPayment.fromWallet(wallet, this);
};
22 changes: 2 additions & 20 deletions src/shift/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ const { delay, asyncOnce, trace } = require('../helpers')
const Api = require('./api')
const Trade = require('./trade')
const Quote = require('./quote')
const BtcPayment = require('./btc-payment')
const EthPayment = require('./eth-payment')
const BchPayment = require('./bch-payment')

const METADATA_TYPE_SHAPE_SHIFT = 6;

Expand Down Expand Up @@ -53,28 +50,13 @@ class ShapeShift {

buildPayment (quote, fee, fromAccount) {
trace('building payment')
let payment
if (quote.depositAddress == null) {
throw new Error('Quote is missing deposit address')
}
if (fromAccount != null && fromAccount.coinCode !== quote.fromCurrency) {
if (fromAccount.coinCode !== quote.fromCurrency) {
throw new Error('Sending account currency does not match quote deposit currency')
}
if (quote.fromCurrency === 'btc') {
let account = fromAccount || this._wallet.hdwallet.defaultAccount
payment = BtcPayment.fromWallet(this._wallet, account)
}
if (quote.fromCurrency === 'eth') {
let account = fromAccount || this._wallet.eth.defaultAccount
payment = EthPayment.fromWallet(this._wallet, account)
}
if (quote.fromCurrency === 'bch') {
let account = fromAccount || this._wallet.bch.defaultAccount
payment = BchPayment.fromWallet(this._wallet, account)
}
if (payment == null) {
throw new Error(`Tried to build for unsupported currency ${quote.fromCurrency}`)
}
let payment = fromAccount.createShiftPayment(this._wallet)
return payment.setFromQuote(quote, fee)
}

Expand Down

0 comments on commit 01998a4

Please sign in to comment.