1
- const smartCash = require ( " smartcashjs-lib" ) ;
2
- const request = require ( " request-promise" ) ;
3
- const _ = require ( " lodash" ) ;
4
- let getSapiUrl = require ( " ./poolSapi" ) ;
1
+ const smartCash = require ( ' smartcashjs-lib' ) ;
2
+ const request = require ( ' request-promise' ) ;
3
+ const _ = require ( ' lodash' ) ;
4
+ let getSapiUrl = require ( ' ./poolSapi' ) ;
5
5
6
- export async function createAndSendRawTransaction (
7
- toAddress ,
8
- amount ,
9
- keyString
10
- ) {
11
- let key = smartCash . ECPair . fromWIF ( keyString ) ;
6
+ export async function createAndSendRawTransaction ( toAddress , amount , keyString ) {
7
+ let key = smartCash . ECPair . fromWIF ( keyString ) ;
12
8
13
- let fromAddress = key . getAddress ( ) . toString ( ) ;
9
+ let fromAddress = key . getAddress ( ) . toString ( ) ;
14
10
15
- let transaction = new smartCash . TransactionBuilder ( ) ;
11
+ let transaction = new smartCash . TransactionBuilder ( ) ;
16
12
17
- let sapiUnspent = await getUnspentWithAmount ( fromAddress , amount ) ;
13
+ let sapiUnspent = await getUnspentWithAmount ( fromAddress , amount ) ;
18
14
19
- let totalUnspent = _ . sumBy ( sapiUnspent . utxos , " amount" ) ;
15
+ let totalUnspent = _ . sumBy ( sapiUnspent . utxos , ' amount' ) ;
20
16
21
- let fee = calculateFee ( sapiUnspent . utxos ) ;
17
+ let fee = calculateFee ( sapiUnspent . utxos ) ;
22
18
23
- let change = totalUnspent - amount - fee ;
19
+ let change = totalUnspent - amount - fee ;
24
20
25
- if ( totalUnspent < amount + fee )
26
- throw new Error ( "The amount exceeds your balance!" ) ;
21
+ if ( totalUnspent < amount + fee ) throw new Error ( 'The amount exceeds your balance!' ) ;
27
22
28
- if ( amount < 0.001 )
29
- throw new Error (
30
- "The amount is smaller than the minimum accepted. Minimum amount: 0.001."
31
- ) ;
23
+ if ( amount < 0.001 ) throw new Error ( 'The amount is smaller than the minimum accepted. Minimum amount: 0.001.' ) ;
32
24
33
- transaction . setLockTime ( sapiUnspent . blockHeight ) ;
25
+ transaction . setLockTime ( sapiUnspent . blockHeight ) ;
34
26
35
- //SEND TO
36
- transaction . addOutput (
37
- toAddress ,
38
- parseFloat ( smartCash . amount ( amount . toString ( ) ) . toString ( ) )
39
- ) ;
27
+ //SEND TO
28
+ transaction . addOutput ( toAddress , parseFloat ( smartCash . amount ( amount . toString ( ) ) . toString ( ) ) ) ;
40
29
41
- if ( change >= fee ) {
42
- //Change TO
43
- transaction . addOutput (
44
- fromAddress ,
45
- parseFloat ( smartCash . amount ( change . toString ( ) ) . toString ( ) )
46
- ) ;
47
- } else {
48
- fee = change ;
49
- }
30
+ if ( change >= fee ) {
31
+ //Change TO
32
+ transaction . addOutput ( fromAddress , parseFloat ( smartCash . amount ( change . toString ( ) ) . toString ( ) ) ) ;
33
+ } else {
34
+ fee = change ;
35
+ }
36
+
37
+ //Add unspent and sign them all
38
+ if ( ! _ . isUndefined ( sapiUnspent . utxos ) && sapiUnspent . utxos . length > 0 ) {
39
+ sapiUnspent . utxos . forEach ( ( element ) => {
40
+ transaction . addInput ( element . txid , element . index ) ;
41
+ } ) ;
50
42
51
- //Add unspent and sign them all
52
- if ( ! _ . isUndefined ( sapiUnspent . utxos ) && sapiUnspent . utxos . length > 0 ) {
53
- sapiUnspent . utxos . forEach ( ( element ) => {
54
- transaction . addInput ( element . txid , element . index ) ;
55
- } ) ;
43
+ for ( let i = 0 ; i < sapiUnspent . utxos . length ; i += 1 ) {
44
+ transaction . sign ( i , key ) ;
45
+ }
46
+ }
56
47
57
- for ( let i = 0 ; i < sapiUnspent . utxos . length ; i += 1 ) {
58
- transaction . sign ( i , key ) ;
48
+ try {
49
+ let signedTransaction = transaction . build ( ) . toHex ( ) ;
50
+ return await sendTransaction ( signedTransaction ) ;
51
+ } catch ( err ) {
52
+ console . error ( err ) ;
53
+ throw err ;
59
54
}
60
- }
61
-
62
- try {
63
- let signedTransaction = transaction . build ( ) . toHex ( ) ;
64
- return await sendTransaction ( signedTransaction ) ;
65
- } catch ( err ) {
66
- console . error ( err ) ;
67
- throw err ;
68
- }
69
55
}
70
56
71
57
export function getAddress ( privateKey ) {
72
- let key = smartCash . ECPair . fromWIF ( privateKey ) ;
58
+ let key = smartCash . ECPair . fromWIF ( privateKey ) ;
73
59
74
- return key . getAddress ( ) . toString ( ) ;
60
+ return key . getAddress ( ) . toString ( ) ;
75
61
}
76
62
77
- export function createNewWalletKeyPair ( ) {
63
+ export function createNewWalletKeyPair ( ) {
78
64
let keyPair = smartCash . ECPair . makeRandom ( ) ;
79
65
let address = keyPair . getAddress ( ) ;
80
66
let key = keyPair . toWIF ( ) ;
81
67
return {
82
68
privateKey : key ,
83
- address : address
69
+ address : address ,
84
70
} ;
85
71
}
86
72
@@ -90,113 +76,120 @@ export function createNewWalletKeyPair(){
90
76
After it does call private methods to calculate the fee
91
77
*/
92
78
export async function getFee ( amount , fromAddress ) {
93
- let sapiUnspent = await getUnspentWithAmount ( fromAddress , amount ) ;
79
+ let sapiUnspent = await getUnspentWithAmount ( fromAddress , amount ) ;
94
80
95
- let fee = calculateFee ( sapiUnspent . utxos ) ;
81
+ let fee = calculateFee ( sapiUnspent . utxos ) ;
96
82
97
- return fee ;
83
+ return fee ;
98
84
}
99
85
100
86
export async function getBalance ( _address ) {
101
- try {
102
- return await request . get ( `${ getSapiUrl ( ) } /v1/address/balance/${ _address } ` , {
103
- json : true ,
104
- } ) ;
105
- } catch ( err ) {
106
- throw err ;
107
- }
87
+ try {
88
+ return await request . get ( `${ getSapiUrl ( ) } /v1/address/balance/${ _address } ` , {
89
+ json : true ,
90
+ } ) ;
91
+ } catch ( err ) {
92
+ throw err ;
93
+ }
108
94
}
109
95
110
96
export async function getUnspentWithAmount ( _address , _amount ) {
111
- let utxos = { } ;
112
-
113
- let options = {
114
- method : " POST" ,
115
- uri : `${ getSapiUrl ( ) } /v1/address/unspent/amount` ,
116
- body : {
117
- address : _address ,
118
- amount : _amount ,
119
- random : true ,
120
- instantpay : false ,
121
- } ,
122
- json : true ,
123
- } ;
124
-
125
- try {
126
- utxos = await request . post ( options ) ;
127
- } catch ( err ) {
128
- utxos = { } ;
129
- }
130
- return utxos ;
97
+ let utxos = { } ;
98
+
99
+ let options = {
100
+ method : ' POST' ,
101
+ uri : `${ getSapiUrl ( ) } /v1/address/unspent/amount` ,
102
+ body : {
103
+ address : _address ,
104
+ amount : _amount ,
105
+ random : true ,
106
+ instantpay : false ,
107
+ } ,
108
+ json : true ,
109
+ } ;
110
+
111
+ try {
112
+ utxos = await request . post ( options ) ;
113
+ } catch ( err ) {
114
+ utxos = { } ;
115
+ }
116
+ return utxos ;
131
117
}
132
118
133
119
export async function getUnspent ( _address ) {
134
- let utxos = { } ;
135
-
136
- let options = {
137
- method : " POST" ,
138
- uri : `${ getSapiUrl ( ) } /v1/address/unspent` ,
139
- body : {
140
- address : _address ,
141
- pageNumber : 1 ,
142
- pageSize : 10 ,
143
- ascending : false ,
144
- } ,
145
- json : true ,
146
- } ;
147
-
148
- try {
149
- utxos = await request . post ( options ) ;
150
- } catch ( err ) {
151
- utxos = { } ;
152
- }
153
- return utxos ;
120
+ let utxos = { } ;
121
+
122
+ let options = {
123
+ method : ' POST' ,
124
+ uri : `${ getSapiUrl ( ) } /v1/address/unspent` ,
125
+ body : {
126
+ address : _address ,
127
+ pageNumber : 1 ,
128
+ pageSize : 10 ,
129
+ ascending : false ,
130
+ } ,
131
+ json : true ,
132
+ } ;
133
+
134
+ try {
135
+ utxos = await request . post ( options ) ;
136
+ } catch ( err ) {
137
+ utxos = { } ;
138
+ }
139
+ return utxos ;
154
140
}
155
141
156
142
export async function getTransactionHistory ( _address ) {
157
- try {
158
- return await request . get (
159
- `https://insight.smartcash.cc/api/txs/apps/?address=${ _address } &limit=5`
160
- ) ;
161
- } catch ( err ) {
162
- throw err ;
163
- }
143
+ try {
144
+ var options = {
145
+ method : 'POST' ,
146
+ uri : `${ getSapiUrl ( ) } /v1/address/transactions/${ _address } ` ,
147
+ body : {
148
+ "pageNumber" : 1 ,
149
+ "pageSize" : 5
150
+ } ,
151
+ json : true , // Automatically stringifies the body to JSON
152
+ } ;
153
+ return await request . post ( options ) . then ( ( res ) => res . data ) ;
154
+ } catch ( err ) {
155
+ throw err ;
156
+ }
164
157
}
165
158
166
159
export async function sendTransaction ( hex ) {
167
- var options = {
168
- method : " POST" ,
169
- uri : `${ getSapiUrl ( ) } /v1/transaction/send` ,
170
- body : {
171
- data : `${ hex } ` ,
172
- instantpay : false ,
173
- overrideFees : false ,
174
- } ,
175
- json : true , // Automatically stringifies the body to JSON
176
- } ;
177
-
178
- try {
179
- return await request . post ( options ) ;
180
- } catch ( err ) {
181
- throw err ;
182
- }
160
+ var options = {
161
+ method : ' POST' ,
162
+ uri : `${ getSapiUrl ( ) } /v1/transaction/send` ,
163
+ body : {
164
+ data : `${ hex } ` ,
165
+ instantpay : false ,
166
+ overrideFees : false ,
167
+ } ,
168
+ json : true , // Automatically stringifies the body to JSON
169
+ } ;
170
+
171
+ try {
172
+ return await request . post ( options ) ;
173
+ } catch ( err ) {
174
+ throw err ;
175
+ }
183
176
}
184
177
185
178
function calculateFee ( listUnspent ) {
186
- let MIN_FEE = 0.001 ;
179
+ let MIN_FEE = 0.001 ;
187
180
188
- if ( _ . isUndefined ( listUnspent ) ) return MIN_FEE ;
181
+ if ( _ . isUndefined ( listUnspent ) ) return MIN_FEE ;
189
182
190
- let countUnspent = listUnspent . length ;
183
+ let countUnspent = listUnspent . length ;
191
184
192
- let newFee = ( 0.001 * ( countUnspent * 148 + 2 * 34 + 10 + 9 ) / 1024 ) ;
185
+ let newFee = ( 0.001 * ( countUnspent * 148 + 2 * 34 + 10 + 9 ) ) / 1024 ;
193
186
194
- if ( newFee > MIN_FEE ) MIN_FEE = newFee ;
187
+ if ( newFee > MIN_FEE ) MIN_FEE = newFee ;
195
188
196
- return roundUp ( MIN_FEE , 5 ) ;
189
+ return roundUp ( MIN_FEE , 5 ) ;
197
190
}
198
191
199
192
function roundUp ( num , precision ) {
200
- precision = Math . pow ( 10 , precision ) ;
201
- return Math . ceil ( num * precision ) / precision ;
193
+ precision = Math . pow ( 10 , precision ) ;
194
+ return Math . ceil ( num * precision ) / precision ;
202
195
}
0 commit comments