11import BigNumber from 'bignumber.js'
2+ import * as Bitcoin from 'bitcoinjs-lib'
23import { SEND_FORM } from 'blockchain-wallet-v4-frontend/src/modals/SendCrypto/model'
34import { SendFormType } from 'blockchain-wallet-v4-frontend/src/modals/SendCrypto/types'
5+ import { nth } from 'ramda'
46import { call , delay , put , select } from 'redux-saga/effects'
57import secp256k1 from 'secp256k1'
68
7- import { Exchange } from '@core'
9+ import { Exchange , utils } from '@core'
810import { convertCoinToCoin , convertFiatToCoin } from '@core/exchange'
911import { APIType } from '@core/network/api'
1012import { BuildTxIntentType , BuildTxResponseType } from '@core/network/api/coin/types'
@@ -19,6 +21,7 @@ import { Analytics } from 'data/types'
1921import { AccountType } from 'middleware/analyticsMiddleware/types'
2022import { promptForSecondPassword } from 'services/sagas'
2123
24+ import coinSagas from '../../coins/sagas'
2225import sendSagas from '../send/sagas'
2326import * as S from './selectors'
2427import { actions as A } from './slice'
@@ -39,6 +42,12 @@ export default ({ api, coreSagas, networks }: { api: APIType; coreSagas: any; ne
3942 networks
4043 } )
4144
45+ const { getNextReceiveAddressForCoin } = coinSagas ( {
46+ api,
47+ coreSagas,
48+ networks
49+ } )
50+
4251 const initializeSend = function * ( ) {
4352 const totalBalanceR = yield select ( selectors . balances . getTotalWalletBalanceNotFormatted )
4453 const totalBalance = totalBalanceR . getOrElse ( { total : 0 } )
@@ -70,9 +79,70 @@ export default ({ api, coreSagas, networks }: { api: APIType; coreSagas: any; ne
7079 if ( nonMigratedCoins . includes ( coin ) ) {
7180 // TODO simulate buildTx logic for non migrated coins
7281 // only PK accounts will go here
82+ // return
83+
84+ // console.log('all details', { account, baseCryptoAmt, destination, fee, memo })
85+ const accountsR = yield select ( selectors . core . common . btc . getAccountsBalances )
86+ // const txS = yield select(selectors.core.common.btc.getWalletTransactions)
87+ // console.log('txS', txS)
88+ if ( account . accountIndex !== undefined ) {
89+ // const defaultAccount = accountsR.map(nth(account?.accountIndex)).getOrElse({})
90+ // console.log('defaultAccountR', defaultAccount)
91+ // const pubb = Bitcoin.payments.p2pkh({
92+ // pubkey: Bitcoin.bip32.fromBase58(defaultAccount.xpub).derive(0).derive(1).publicKey
93+ // })
94+ // const addressDefault = yield call(getNextReceiveAddressForCoin, coin)
95+ // console.log('addresst', pubb.address)
96+ // console.log('address6', addressDefault)
97+ // const addresst = utils.btc.keyPairToAddress(defaultAccount.xpub)
98+ // console.log('addresst', addresst)
99+ // const defaultAccount = allAccount[account.accountIndex]
100+ // console.log('defaultAccount', defaultAccount)
101+ }
102+
103+ // console.log('defaultAccount', defaultAccount)
104+
105+ const tx = { } as BuildTxResponseType
106+
107+ if ( coin === 'BTC' ) {
108+ // const txBTC = new Bitcoin.TransactionBuilder()
109+
110+ let payment = coreSagas . payment . btc . create ( {
111+ network : networks . btc
112+ } )
113+ payment = yield payment . init ( )
114+
115+ payment = yield payment . from ( account . accountIndex , ADDRESS_TYPES . ACCOUNT )
116+
117+ // console.log('payment', payment)
118+
119+ // from
120+ payment = yield payment . from ( account . index , account . type )
121+ // to
122+ payment = yield payment . to ( destination , account . type )
123+ // amount
124+ const satAmount = Exchange . convertCoinToCoin ( {
125+ baseToStandard : false ,
126+ coin,
127+ value : baseCryptoAmt
128+ } )
129+ payment = yield payment . amount ( parseInt ( satAmount ) )
130+
131+ // build transaction
132+ try {
133+ payment = yield payment . build ( )
134+
135+ // console.log('payment ready', payment.value())
136+ } catch ( e ) {
137+ yield put ( actions . logs . logErrorMessage ( logLocation , 'formChanged' , e ) )
138+ }
139+ // console.log('pubKey', pubKey)
140+ // console.log('txBTC', txBTC)
141+ }
142+
143+ yield put ( A . buildTxSuccess ( tx ) )
73144 return
74145 }
75-
76146 const tx : ReturnType < typeof api . buildTx > = yield call ( api . buildTx , {
77147 id : {
78148 guid,
0 commit comments