Skip to content

Commit 81cd2b4

Browse files
author
Enrique Soares
committed
sapi-new-calls
1 parent 02b30d0 commit 81cd2b4

File tree

8 files changed

+447
-514
lines changed

8 files changed

+447
-514
lines changed

.editorconfig

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
# Editor configuration, see https://editorconfig.org
21
root = true
32

43
[*]
5-
charset = utf-8
64
indent_style = space
7-
indent_size = 2
8-
insert_final_newline = true
5+
indent_size = 4
6+
charset = utf-8
97
trim_trailing_whitespace = true
10-
max_line_length = 80
11-
12-
[*.md]
13-
max_line_length = off
14-
trim_trailing_whitespace = false
15-
8+
insert_final_newline = true
9+
end_of_line = lf
10+
rulers = 110, 130

.prettierignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Ignore all MINIFIED files:
2+
*.min.js
3+
*.min.css
4+
*.min.html

.prettierrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"trailingComma": "es5",
3+
"tabWidth": 4,
4+
"semi": true,
5+
"singleQuote": true,
6+
"bracketSpacing": true,
7+
"arrowParens": "always",
8+
"proseWrap": "never",
9+
"printWidth": 130
10+
}

src/lib/poolSapi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const random = require("random");
22

3-
let sapis = ["https://sapi2.smartcash.org"];
3+
let sapis = ["https://sapi2.smartcash.org", "https://sapi.smartcash.cc"];
44

55
module.exports = () => sapis[random.int(0, sapis.length - 1)];

src/lib/sapi.js

Lines changed: 131 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,72 @@
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');
55

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);
128

13-
let fromAddress = key.getAddress().toString();
9+
let fromAddress = key.getAddress().toString();
1410

15-
let transaction = new smartCash.TransactionBuilder();
11+
let transaction = new smartCash.TransactionBuilder();
1612

17-
let sapiUnspent = await getUnspentWithAmount(fromAddress, amount);
13+
let sapiUnspent = await getUnspentWithAmount(fromAddress, amount);
1814

19-
let totalUnspent = _.sumBy(sapiUnspent.utxos, "amount");
15+
let totalUnspent = _.sumBy(sapiUnspent.utxos, 'amount');
2016

21-
let fee = calculateFee(sapiUnspent.utxos);
17+
let fee = calculateFee(sapiUnspent.utxos);
2218

23-
let change = totalUnspent - amount - fee;
19+
let change = totalUnspent - amount - fee;
2420

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!');
2722

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.');
3224

33-
transaction.setLockTime(sapiUnspent.blockHeight);
25+
transaction.setLockTime(sapiUnspent.blockHeight);
3426

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()));
4029

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+
});
5042

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+
}
5647

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;
5954
}
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-
}
6955
}
7056

7157
export function getAddress(privateKey) {
72-
let key = smartCash.ECPair.fromWIF(privateKey);
58+
let key = smartCash.ECPair.fromWIF(privateKey);
7359

74-
return key.getAddress().toString();
60+
return key.getAddress().toString();
7561
}
7662

77-
export function createNewWalletKeyPair(){
63+
export function createNewWalletKeyPair() {
7864
let keyPair = smartCash.ECPair.makeRandom();
7965
let address = keyPair.getAddress();
8066
let key = keyPair.toWIF();
8167
return {
8268
privateKey: key,
83-
address: address
69+
address: address,
8470
};
8571
}
8672

@@ -90,113 +76,120 @@ export function createNewWalletKeyPair(){
9076
After it does call private methods to calculate the fee
9177
*/
9278
export async function getFee(amount, fromAddress) {
93-
let sapiUnspent = await getUnspentWithAmount(fromAddress, amount);
79+
let sapiUnspent = await getUnspentWithAmount(fromAddress, amount);
9480

95-
let fee = calculateFee(sapiUnspent.utxos);
81+
let fee = calculateFee(sapiUnspent.utxos);
9682

97-
return fee;
83+
return fee;
9884
}
9985

10086
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+
}
10894
}
10995

11096
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;
131117
}
132118

133119
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;
154140
}
155141

156142
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+
}
164157
}
165158

166159
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+
}
183176
}
184177

185178
function calculateFee(listUnspent) {
186-
let MIN_FEE = 0.001;
179+
let MIN_FEE = 0.001;
187180

188-
if (_.isUndefined(listUnspent)) return MIN_FEE;
181+
if (_.isUndefined(listUnspent)) return MIN_FEE;
189182

190-
let countUnspent = listUnspent.length;
183+
let countUnspent = listUnspent.length;
191184

192-
let newFee = (0.001 * (countUnspent * 148 + 2 * 34 + 10 + 9) / 1024);
185+
let newFee = (0.001 * (countUnspent * 148 + 2 * 34 + 10 + 9)) / 1024;
193186

194-
if (newFee > MIN_FEE) MIN_FEE = newFee;
187+
if (newFee > MIN_FEE) MIN_FEE = newFee;
195188

196-
return roundUp(MIN_FEE, 5);
189+
return roundUp(MIN_FEE, 5);
197190
}
198191

199192
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;
202195
}

0 commit comments

Comments
 (0)