@xchainjs/xchain-bitcoin 0.18.0 → 0.18.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -40
- package/lib/client.d.ts +15 -2
- package/lib/const.d.ts +2 -0
- package/lib/index.esm.js +649 -933
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +650 -932
- package/lib/index.js.map +1 -1
- package/package.json +5 -5
- package/CHANGELOG.md +0 -292
package/lib/index.esm.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { Network,
|
|
1
|
+
import { Network, standardFeeRates, FeeOption, calcFees, UTXOClient, TxType, checkFeeBounds } from '@xchainjs/xchain-client';
|
|
2
2
|
import buffer from 'buffer';
|
|
3
3
|
import events from 'events';
|
|
4
4
|
import stream from 'stream';
|
|
5
5
|
import string_decoder$1 from 'string_decoder';
|
|
6
6
|
import crypto$1$1 from 'crypto';
|
|
7
7
|
import readableStream from 'readable-stream';
|
|
8
|
-
import axios from 'axios';
|
|
8
|
+
import axios$1 from 'axios';
|
|
9
9
|
|
|
10
10
|
/*! *****************************************************************************
|
|
11
11
|
Copyright (c) Microsoft Corporation.
|
|
@@ -21,33 +21,6 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
21
21
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
22
22
|
PERFORMANCE OF THIS SOFTWARE.
|
|
23
23
|
***************************************************************************** */
|
|
24
|
-
/* global Reflect, Promise */
|
|
25
|
-
|
|
26
|
-
var extendStatics = function(d, b) {
|
|
27
|
-
extendStatics = Object.setPrototypeOf ||
|
|
28
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
29
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
30
|
-
return extendStatics(d, b);
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
function __extends(d, b) {
|
|
34
|
-
if (typeof b !== "function" && b !== null)
|
|
35
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
36
|
-
extendStatics(d, b);
|
|
37
|
-
function __() { this.constructor = d; }
|
|
38
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
var __assign = function() {
|
|
42
|
-
__assign = Object.assign || function __assign(t) {
|
|
43
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
44
|
-
s = arguments[i];
|
|
45
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
46
|
-
}
|
|
47
|
-
return t;
|
|
48
|
-
};
|
|
49
|
-
return __assign.apply(this, arguments);
|
|
50
|
-
};
|
|
51
24
|
|
|
52
25
|
function __awaiter(thisArg, _arguments, P, generator) {
|
|
53
26
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
@@ -57,34 +30,6 @@ function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
57
30
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
58
31
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
59
32
|
});
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function __generator(thisArg, body) {
|
|
63
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
64
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
65
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
66
|
-
function step(op) {
|
|
67
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
68
|
-
while (_) try {
|
|
69
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
70
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
71
|
-
switch (op[0]) {
|
|
72
|
-
case 0: case 1: t = op; break;
|
|
73
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
74
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
75
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
76
|
-
default:
|
|
77
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
78
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
79
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
80
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
81
|
-
if (t[2]) _.ops.pop();
|
|
82
|
-
_.trys.pop(); continue;
|
|
83
|
-
}
|
|
84
|
-
op = body.call(thisArg, _);
|
|
85
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
86
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
87
|
-
}
|
|
88
33
|
}
|
|
89
34
|
|
|
90
35
|
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
@@ -61913,7 +61858,7 @@ var sha256$2 = createCommonjsModule(function (module, exports) {
|
|
|
61913
61858
|
* @param {string} phrase
|
|
61914
61859
|
* @returns {boolean} `true` or `false`
|
|
61915
61860
|
*/
|
|
61916
|
-
|
|
61861
|
+
const validatePhrase = (phrase) => {
|
|
61917
61862
|
return src_6(phrase);
|
|
61918
61863
|
};
|
|
61919
61864
|
/**
|
|
@@ -61924,7 +61869,7 @@ var validatePhrase = function (phrase) {
|
|
|
61924
61869
|
*
|
|
61925
61870
|
* @throws {"Invalid BIP39 phrase"} Thrown if phrase is an invalid one.
|
|
61926
61871
|
*/
|
|
61927
|
-
|
|
61872
|
+
const getSeed = (phrase) => {
|
|
61928
61873
|
if (!validatePhrase(phrase)) {
|
|
61929
61874
|
throw new Error('Invalid BIP39 phrase');
|
|
61930
61875
|
}
|
|
@@ -64863,7 +64808,7 @@ var bignumber$1 = createCommonjsModule$1(function (module) {
|
|
|
64863
64808
|
* }
|
|
64864
64809
|
* ```
|
|
64865
64810
|
*/
|
|
64866
|
-
|
|
64811
|
+
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
64867
64812
|
|
|
64868
64813
|
/**
|
|
64869
64814
|
* Shortcut to create a BigNumber
|
|
@@ -64871,14 +64816,14 @@ var delay = function (ms) { return new Promise(function (resolve) { return setTi
|
|
|
64871
64816
|
* @param {string | number | BigNumber.Instance} value
|
|
64872
64817
|
* @returns {BigNumber} The BigNumber interface from the given value.
|
|
64873
64818
|
*/
|
|
64874
|
-
|
|
64819
|
+
const bn$1 = (value) => new bignumber$1(value);
|
|
64875
64820
|
/**
|
|
64876
64821
|
* Helper to check whether a BigNumber is valid or not
|
|
64877
64822
|
*
|
|
64878
64823
|
* @param {BigNumber} value
|
|
64879
64824
|
* @returns {boolean} `true` or `false`.
|
|
64880
64825
|
* */
|
|
64881
|
-
|
|
64826
|
+
const isValidBN = (value) => !value.isNaN();
|
|
64882
64827
|
/**
|
|
64883
64828
|
* The enumuration for symbol position.
|
|
64884
64829
|
* `before` or `after`
|
|
@@ -64896,14 +64841,12 @@ var SymbolPosition;
|
|
|
64896
64841
|
* @param {number} decimalPlaces The decimal place. (optional)
|
|
64897
64842
|
* @returns {BigNumber} The BigNumber interface from the given value and decimal.
|
|
64898
64843
|
* */
|
|
64899
|
-
|
|
64900
|
-
|
|
64901
|
-
|
|
64902
|
-
var fixedBN = isValidBN(n) ? n.toFixed(decimalPlaces) : bn$1(0).toFixed(decimalPlaces);
|
|
64844
|
+
const fixedBN = (value, decimalPlaces = 2) => {
|
|
64845
|
+
const n = bn$1(value || 0);
|
|
64846
|
+
const fixedBN = isValidBN(n) ? n.toFixed(decimalPlaces) : bn$1(0).toFixed(decimalPlaces);
|
|
64903
64847
|
return bn$1(fixedBN);
|
|
64904
64848
|
};
|
|
64905
64849
|
|
|
64906
|
-
var _a;
|
|
64907
64850
|
var Chain;
|
|
64908
64851
|
(function (Chain) {
|
|
64909
64852
|
Chain["Binance"] = "BNB";
|
|
@@ -64917,38 +64860,38 @@ var Chain;
|
|
|
64917
64860
|
Chain["Terra"] = "TERRA";
|
|
64918
64861
|
Chain["Doge"] = "DOGE";
|
|
64919
64862
|
})(Chain || (Chain = {}));
|
|
64920
|
-
|
|
64921
|
-
|
|
64922
|
-
|
|
64923
|
-
|
|
64924
|
-
|
|
64925
|
-
|
|
64926
|
-
|
|
64927
|
-
|
|
64928
|
-
|
|
64929
|
-
|
|
64863
|
+
const BNBChain = Chain.Binance;
|
|
64864
|
+
const BTCChain = Chain.Bitcoin;
|
|
64865
|
+
const ETHChain = Chain.Ethereum;
|
|
64866
|
+
const THORChain = Chain.THORChain;
|
|
64867
|
+
const CosmosChain = Chain.Cosmos;
|
|
64868
|
+
const PolkadotChain = Chain.Polkadot;
|
|
64869
|
+
const BCHChain = Chain.BitcoinCash;
|
|
64870
|
+
const LTCChain = Chain.Litecoin;
|
|
64871
|
+
const TerraChain = Chain.Terra;
|
|
64872
|
+
const DOGEChain = Chain.Doge;
|
|
64930
64873
|
/**
|
|
64931
64874
|
* Convert chain to string.
|
|
64932
64875
|
*
|
|
64933
64876
|
* @param {Chain} chainId.
|
|
64934
64877
|
* @returns {string} The string based on the given chain type.
|
|
64935
64878
|
*/
|
|
64936
|
-
|
|
64879
|
+
const chainToString = Object.assign((chainId) => {
|
|
64937
64880
|
if (!(chainId in chainToString))
|
|
64938
64881
|
return 'unknown chain';
|
|
64939
64882
|
return chainToString[chainId];
|
|
64940
|
-
},
|
|
64941
|
-
|
|
64942
|
-
|
|
64943
|
-
|
|
64944
|
-
|
|
64945
|
-
|
|
64946
|
-
|
|
64947
|
-
|
|
64948
|
-
|
|
64949
|
-
|
|
64950
|
-
|
|
64951
|
-
|
|
64883
|
+
}, {
|
|
64884
|
+
[Chain.THORChain]: 'Thorchain',
|
|
64885
|
+
[Chain.Bitcoin]: 'Bitcoin',
|
|
64886
|
+
[Chain.BitcoinCash]: 'Bitcoin Cash',
|
|
64887
|
+
[Chain.Litecoin]: 'Litecoin',
|
|
64888
|
+
[Chain.Ethereum]: 'Ethereum',
|
|
64889
|
+
[Chain.Binance]: 'Binance Chain',
|
|
64890
|
+
[Chain.Cosmos]: 'Cosmos',
|
|
64891
|
+
[Chain.Polkadot]: 'Polkadot',
|
|
64892
|
+
[Chain.Terra]: 'Terra',
|
|
64893
|
+
[Chain.Doge]: 'Dogecoin',
|
|
64894
|
+
});
|
|
64952
64895
|
|
|
64953
64896
|
var Denomination;
|
|
64954
64897
|
(function (Denomination) {
|
|
@@ -64968,9 +64911,7 @@ var Denomination;
|
|
|
64968
64911
|
* @param {unknown} v
|
|
64969
64912
|
* @returns {boolean} `true` or `false`.
|
|
64970
64913
|
* */
|
|
64971
|
-
|
|
64972
|
-
return typeof v === 'string' || typeof v === 'number' || v instanceof bignumber$1;
|
|
64973
|
-
};
|
|
64914
|
+
const isBigNumberValue = (v) => typeof v === 'string' || typeof v === 'number' || v instanceof bignumber$1;
|
|
64974
64915
|
/**
|
|
64975
64916
|
* Default number of asset decimals
|
|
64976
64917
|
* For history reason and by starting the project on Binance chain assets, it's 8 decimal.
|
|
@@ -64981,7 +64922,7 @@ var isBigNumberValue = function (v) {
|
|
|
64981
64922
|
* 0.00000001 RUNE == 1 ð (tor)
|
|
64982
64923
|
* ```
|
|
64983
64924
|
* */
|
|
64984
|
-
|
|
64925
|
+
const ASSET_DECIMAL = 8;
|
|
64985
64926
|
/**
|
|
64986
64927
|
* Factory to create values of assets (e.g. RUNE)
|
|
64987
64928
|
*
|
|
@@ -64990,34 +64931,21 @@ var ASSET_DECIMAL = 8;
|
|
|
64990
64931
|
* @returns {AssetAmount} The asset amount from the given value and decimal.
|
|
64991
64932
|
*
|
|
64992
64933
|
**/
|
|
64993
|
-
|
|
64994
|
-
|
|
64995
|
-
var amount = fixedBN(value, decimal);
|
|
64934
|
+
const assetAmount = (value, decimal = ASSET_DECIMAL) => {
|
|
64935
|
+
const amount = fixedBN(value, decimal);
|
|
64996
64936
|
return {
|
|
64997
64937
|
type: Denomination.Asset,
|
|
64998
|
-
amount:
|
|
64999
|
-
plus:
|
|
65000
|
-
|
|
65001
|
-
|
|
65002
|
-
|
|
65003
|
-
|
|
65004
|
-
|
|
65005
|
-
|
|
65006
|
-
|
|
65007
|
-
|
|
65008
|
-
|
|
65009
|
-
return assetAmount(amount.times(isBigNumberValue(v) ? v : v.amount()), d);
|
|
65010
|
-
},
|
|
65011
|
-
div: function (v, d) {
|
|
65012
|
-
if (d === void 0) { d = decimal; }
|
|
65013
|
-
return assetAmount(amount.div(isBigNumberValue(v) ? v : v.amount()), d);
|
|
65014
|
-
},
|
|
65015
|
-
lt: function (v) { return amount.lt(isBigNumberValue(v) ? v : v.amount()); },
|
|
65016
|
-
lte: function (v) { return amount.lte(isBigNumberValue(v) ? v : v.amount()); },
|
|
65017
|
-
gt: function (v) { return amount.gt(isBigNumberValue(v) ? v : v.amount()); },
|
|
65018
|
-
gte: function (v) { return amount.gte(isBigNumberValue(v) ? v : v.amount()); },
|
|
65019
|
-
eq: function (v) { return amount.eq(isBigNumberValue(v) ? v : v.amount()); },
|
|
65020
|
-
decimal: decimal,
|
|
64938
|
+
amount: () => amount,
|
|
64939
|
+
plus: (v, d = decimal) => assetAmount(amount.plus(isBigNumberValue(v) ? v : v.amount()), d),
|
|
64940
|
+
minus: (v, d = decimal) => assetAmount(amount.minus(isBigNumberValue(v) ? v : v.amount()), d),
|
|
64941
|
+
times: (v, d = decimal) => assetAmount(amount.times(isBigNumberValue(v) ? v : v.amount()), d),
|
|
64942
|
+
div: (v, d = decimal) => assetAmount(amount.div(isBigNumberValue(v) ? v : v.amount()), d),
|
|
64943
|
+
lt: (v) => amount.lt(isBigNumberValue(v) ? v : v.amount()),
|
|
64944
|
+
lte: (v) => amount.lte(isBigNumberValue(v) ? v : v.amount()),
|
|
64945
|
+
gt: (v) => amount.gt(isBigNumberValue(v) ? v : v.amount()),
|
|
64946
|
+
gte: (v) => amount.gte(isBigNumberValue(v) ? v : v.amount()),
|
|
64947
|
+
eq: (v) => amount.eq(isBigNumberValue(v) ? v : v.amount()),
|
|
64948
|
+
decimal,
|
|
65021
64949
|
};
|
|
65022
64950
|
};
|
|
65023
64951
|
/**
|
|
@@ -65027,34 +64955,21 @@ var assetAmount = function (value, decimal) {
|
|
|
65027
64955
|
* @param {number} decimal The decimal places of its associated AssetAmount. (optional)
|
|
65028
64956
|
* @returns {BaseAmount} The base amount from the given value and decimal.
|
|
65029
64957
|
**/
|
|
65030
|
-
|
|
65031
|
-
|
|
65032
|
-
var amount = fixedBN(value, 0);
|
|
64958
|
+
const baseAmount = (value, decimal = ASSET_DECIMAL) => {
|
|
64959
|
+
const amount = fixedBN(value, 0);
|
|
65033
64960
|
return {
|
|
65034
64961
|
type: Denomination.Base,
|
|
65035
|
-
amount:
|
|
65036
|
-
plus:
|
|
65037
|
-
|
|
65038
|
-
|
|
65039
|
-
|
|
65040
|
-
|
|
65041
|
-
|
|
65042
|
-
|
|
65043
|
-
|
|
65044
|
-
|
|
65045
|
-
|
|
65046
|
-
return baseAmount(amount.times(isBigNumberValue(v) ? v : v.amount()), d);
|
|
65047
|
-
},
|
|
65048
|
-
div: function (v, d) {
|
|
65049
|
-
if (d === void 0) { d = decimal; }
|
|
65050
|
-
return baseAmount(amount.div(isBigNumberValue(v) ? v : v.amount()).decimalPlaces(0, bignumber$1.ROUND_DOWN), d);
|
|
65051
|
-
},
|
|
65052
|
-
lt: function (v) { return amount.lt(isBigNumberValue(v) ? v : v.amount()); },
|
|
65053
|
-
lte: function (v) { return amount.lte(isBigNumberValue(v) ? v : v.amount()); },
|
|
65054
|
-
gt: function (v) { return amount.gt(isBigNumberValue(v) ? v : v.amount()); },
|
|
65055
|
-
gte: function (v) { return amount.gte(isBigNumberValue(v) ? v : v.amount()); },
|
|
65056
|
-
eq: function (v) { return amount.eq(isBigNumberValue(v) ? v : v.amount()); },
|
|
65057
|
-
decimal: decimal,
|
|
64962
|
+
amount: () => amount,
|
|
64963
|
+
plus: (v, d = decimal) => baseAmount(amount.plus(isBigNumberValue(v) ? v : v.amount()), d),
|
|
64964
|
+
minus: (v, d = decimal) => baseAmount(amount.minus(isBigNumberValue(v) ? v : v.amount()), d),
|
|
64965
|
+
times: (v, d = decimal) => baseAmount(amount.times(isBigNumberValue(v) ? v : v.amount()), d),
|
|
64966
|
+
div: (v, d = decimal) => baseAmount(amount.div(isBigNumberValue(v) ? v : v.amount()).decimalPlaces(0, bignumber$1.ROUND_DOWN), d),
|
|
64967
|
+
lt: (v) => amount.lt(isBigNumberValue(v) ? v : v.amount()),
|
|
64968
|
+
lte: (v) => amount.lte(isBigNumberValue(v) ? v : v.amount()),
|
|
64969
|
+
gt: (v) => amount.gt(isBigNumberValue(v) ? v : v.amount()),
|
|
64970
|
+
gte: (v) => amount.gte(isBigNumberValue(v) ? v : v.amount()),
|
|
64971
|
+
eq: (v) => amount.eq(isBigNumberValue(v) ? v : v.amount()),
|
|
64972
|
+
decimal,
|
|
65058
64973
|
};
|
|
65059
64974
|
};
|
|
65060
64975
|
/**
|
|
@@ -65063,8 +64978,8 @@ var baseAmount = function (value, decimal) {
|
|
|
65063
64978
|
* @param {AssetAmount} asset
|
|
65064
64979
|
* @returns {BaseAmount} The base amount from the given AssetAmount.
|
|
65065
64980
|
* */
|
|
65066
|
-
|
|
65067
|
-
|
|
64981
|
+
const assetToBase = (asset) => {
|
|
64982
|
+
const value = asset
|
|
65068
64983
|
.amount()
|
|
65069
64984
|
.multipliedBy(Math.pow(10, asset.decimal))
|
|
65070
64985
|
.integerValue();
|
|
@@ -65076,78 +64991,72 @@ var assetToBase = function (asset) {
|
|
|
65076
64991
|
* Based on definition in Thorchain `common`
|
|
65077
64992
|
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24
|
|
65078
64993
|
*/
|
|
65079
|
-
|
|
64994
|
+
const AssetBNB = { chain: Chain.Binance, symbol: 'BNB', ticker: 'BNB', synth: false };
|
|
65080
64995
|
/**
|
|
65081
64996
|
* Base "chain" asset on bitcoin main net.
|
|
65082
64997
|
*
|
|
65083
64998
|
* Based on definition in Thorchain `common`
|
|
65084
64999
|
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24
|
|
65085
65000
|
*/
|
|
65086
|
-
|
|
65001
|
+
const AssetBTC = { chain: Chain.Bitcoin, symbol: 'BTC', ticker: 'BTC', synth: false };
|
|
65087
65002
|
/**
|
|
65088
65003
|
* Base "chain" asset on bitcoin cash main net.
|
|
65089
65004
|
*
|
|
65090
65005
|
* Based on definition in Thorchain `common`
|
|
65091
65006
|
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24
|
|
65092
65007
|
*/
|
|
65093
|
-
|
|
65008
|
+
const AssetBCH = { chain: Chain.BitcoinCash, symbol: 'BCH', ticker: 'BCH', synth: false };
|
|
65094
65009
|
/**
|
|
65095
65010
|
* Base "chain" asset on litecoin main net.
|
|
65096
65011
|
*
|
|
65097
65012
|
* Based on definition in Thorchain `common`
|
|
65098
65013
|
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24
|
|
65099
65014
|
*/
|
|
65100
|
-
|
|
65015
|
+
const AssetLTC = { chain: Chain.Litecoin, symbol: 'LTC', ticker: 'LTC', synth: false };
|
|
65101
65016
|
/**
|
|
65102
65017
|
* Dogecoin asset
|
|
65103
65018
|
* Based on definition in Thorchain
|
|
65104
65019
|
* @see https://gitlab.com/thorchain/thornode/-/blob/781-add-doge-chain/common/asset.go#L24
|
|
65105
65020
|
*/
|
|
65106
|
-
|
|
65107
|
-
|
|
65108
|
-
* Luna asset
|
|
65109
|
-
* Based on definition in Thorchain
|
|
65110
|
-
* @see TBD
|
|
65111
|
-
*/
|
|
65112
|
-
var AssetLUNA = { chain: Chain.Terra, symbol: 'LUNA', ticker: 'LUNA', synth: false };
|
|
65113
|
-
var RUNE_TICKER = 'RUNE';
|
|
65021
|
+
const AssetDOGE = { chain: Chain.Doge, symbol: 'DOGE', ticker: 'DOGE', synth: false };
|
|
65022
|
+
const RUNE_TICKER = 'RUNE';
|
|
65114
65023
|
/**
|
|
65115
65024
|
* Base "chain" asset on ethereum main net.
|
|
65116
65025
|
*
|
|
65117
65026
|
* Based on definition in Thorchain `common`
|
|
65118
65027
|
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24
|
|
65119
65028
|
*/
|
|
65120
|
-
|
|
65029
|
+
const AssetETH = { chain: Chain.Ethereum, symbol: 'ETH', ticker: 'ETH', synth: false };
|
|
65121
65030
|
/**
|
|
65122
65031
|
* Base "chain" asset for RUNE-67C on Binance test net.
|
|
65123
65032
|
*
|
|
65124
65033
|
* Based on definition in Thorchain `common`
|
|
65125
65034
|
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24
|
|
65126
65035
|
*/
|
|
65127
|
-
|
|
65036
|
+
const AssetRune67C = { chain: Chain.Binance, symbol: 'RUNE-67C', ticker: RUNE_TICKER, synth: false };
|
|
65128
65037
|
/**
|
|
65129
65038
|
* Base "chain" asset for RUNE-B1A on Binance main net.
|
|
65130
65039
|
*
|
|
65131
65040
|
* Based on definition in Thorchain `common`
|
|
65132
65041
|
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24
|
|
65133
65042
|
*/
|
|
65134
|
-
|
|
65043
|
+
const AssetRuneB1A = { chain: Chain.Binance, symbol: 'RUNE-B1A', ticker: RUNE_TICKER, synth: false };
|
|
65135
65044
|
/**
|
|
65136
65045
|
* Base "chain" asset on thorchain main net.
|
|
65137
65046
|
*
|
|
65138
65047
|
* Based on definition in Thorchain `common`
|
|
65139
65048
|
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24
|
|
65140
65049
|
*/
|
|
65141
|
-
|
|
65050
|
+
const AssetRuneNative = { chain: Chain.THORChain, symbol: RUNE_TICKER, ticker: RUNE_TICKER, synth: false };
|
|
65142
65051
|
/**
|
|
65143
65052
|
* Base "chain" asset for RUNE on ethereum main net.
|
|
65144
65053
|
*
|
|
65145
65054
|
* Based on definition in Thorchain `common`
|
|
65146
65055
|
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24
|
|
65147
65056
|
*/
|
|
65148
|
-
|
|
65057
|
+
const AssetRuneERC20 = {
|
|
65149
65058
|
chain: Chain.Ethereum,
|
|
65150
|
-
symbol: RUNE_TICKER
|
|
65059
|
+
symbol: `${RUNE_TICKER}-0x3155ba85d5f96b2d030a4966af206230e46849cb`,
|
|
65151
65060
|
ticker: RUNE_TICKER,
|
|
65152
65061
|
synth: false,
|
|
65153
65062
|
};
|
|
@@ -65157,12 +65066,33 @@ var AssetRuneERC20 = {
|
|
|
65157
65066
|
* Based on definition in Thorchain `common`
|
|
65158
65067
|
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24
|
|
65159
65068
|
*/
|
|
65160
|
-
|
|
65069
|
+
const AssetRuneERC20Testnet = {
|
|
65161
65070
|
chain: Chain.Ethereum,
|
|
65162
|
-
symbol: RUNE_TICKER
|
|
65071
|
+
symbol: `${RUNE_TICKER}-0xd601c6A3a36721320573885A8d8420746dA3d7A0`,
|
|
65163
65072
|
ticker: RUNE_TICKER,
|
|
65164
65073
|
synth: false,
|
|
65165
65074
|
};
|
|
65075
|
+
const SYNTH_DELIMITER = '/';
|
|
65076
|
+
const NON_SYNTH_DELIMITER = '.';
|
|
65077
|
+
/**
|
|
65078
|
+
* Returns an `Asset` as a string using following naming convention:
|
|
65079
|
+
*
|
|
65080
|
+
* `AAA.BBB-CCC`
|
|
65081
|
+
* where
|
|
65082
|
+
* chain: `AAA`
|
|
65083
|
+
* ticker (optional): `BBB`
|
|
65084
|
+
* symbol: `BBB-CCC` or `CCC` (if no ticker available)
|
|
65085
|
+
* symbol (synth): `BBB/CCC` or `CCC` (if no ticker available)
|
|
65086
|
+
*
|
|
65087
|
+
* @see https://docs.thorchain.org/developers/transaction-memos#asset-notation
|
|
65088
|
+
*
|
|
65089
|
+
* @param {Asset} asset The given asset.
|
|
65090
|
+
* @returns {string} The string from the given asset.
|
|
65091
|
+
*/
|
|
65092
|
+
const assetToString = ({ chain, symbol, synth }) => {
|
|
65093
|
+
const delimiter = synth ? SYNTH_DELIMITER : NON_SYNTH_DELIMITER;
|
|
65094
|
+
return `${chain}${delimiter}${symbol}`;
|
|
65095
|
+
};
|
|
65166
65096
|
/**
|
|
65167
65097
|
* Currency symbols currently supported
|
|
65168
65098
|
*/
|
|
@@ -65173,7 +65103,77 @@ var AssetCurrencySymbol;
|
|
|
65173
65103
|
AssetCurrencySymbol["SATOSHI"] = "\u26A1";
|
|
65174
65104
|
AssetCurrencySymbol["ETH"] = "\u039E";
|
|
65175
65105
|
AssetCurrencySymbol["USD"] = "$";
|
|
65176
|
-
})(AssetCurrencySymbol || (AssetCurrencySymbol = {}));
|
|
65106
|
+
})(AssetCurrencySymbol || (AssetCurrencySymbol = {}));
|
|
65107
|
+
|
|
65108
|
+
/*! *****************************************************************************
|
|
65109
|
+
Copyright (c) Microsoft Corporation.
|
|
65110
|
+
|
|
65111
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
65112
|
+
purpose with or without fee is hereby granted.
|
|
65113
|
+
|
|
65114
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
65115
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
65116
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
65117
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
65118
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
65119
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
65120
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
65121
|
+
***************************************************************************** */
|
|
65122
|
+
|
|
65123
|
+
function __awaiter$1(thisArg, _arguments, P, generator) {
|
|
65124
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
65125
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
65126
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
65127
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
65128
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
65129
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
65130
|
+
});
|
|
65131
|
+
}
|
|
65132
|
+
|
|
65133
|
+
const axios = require('axios');
|
|
65134
|
+
const midgardBaseUrls = {
|
|
65135
|
+
[Network.Testnet]: ['https://testnet.midgard.thorchain.info'],
|
|
65136
|
+
[Network.Mainnet]: ['https://midgard.ninerealms.com', 'https://midgard.thorswap.net'],
|
|
65137
|
+
};
|
|
65138
|
+
const getMimirDetails = (network = Network.Mainnet) => __awaiter$1(void 0, void 0, void 0, function* () {
|
|
65139
|
+
const path = '/v2/thorchain/mimir';
|
|
65140
|
+
for (const baseUrl of midgardBaseUrls[network]) {
|
|
65141
|
+
try {
|
|
65142
|
+
const { data } = yield axios.get(`${baseUrl}${path}`);
|
|
65143
|
+
return data;
|
|
65144
|
+
}
|
|
65145
|
+
catch (e) {
|
|
65146
|
+
console.error(e);
|
|
65147
|
+
}
|
|
65148
|
+
}
|
|
65149
|
+
throw new Error('Midgard not responding');
|
|
65150
|
+
});
|
|
65151
|
+
const getAllInboundDetails = (network = Network.Mainnet) => __awaiter$1(void 0, void 0, void 0, function* () {
|
|
65152
|
+
const path = '/v2/thorchain/inbound_addresses';
|
|
65153
|
+
for (const baseUrl of midgardBaseUrls[network]) {
|
|
65154
|
+
try {
|
|
65155
|
+
const { data } = yield axios.get(`${baseUrl}${path}`);
|
|
65156
|
+
return data;
|
|
65157
|
+
}
|
|
65158
|
+
catch (e) {
|
|
65159
|
+
console.error(e);
|
|
65160
|
+
}
|
|
65161
|
+
}
|
|
65162
|
+
throw new Error('Midgard not responding');
|
|
65163
|
+
});
|
|
65164
|
+
const getInboundDetails = (chain, network = Network.Mainnet) => __awaiter$1(void 0, void 0, void 0, function* () {
|
|
65165
|
+
const [mimirDetails, allInboundDetails] = yield Promise.all([getMimirDetails(network), getAllInboundDetails(network)]);
|
|
65166
|
+
const inboundDetail = allInboundDetails === null || allInboundDetails === void 0 ? void 0 : allInboundDetails.find((item) => item.chain === chain);
|
|
65167
|
+
const details = {
|
|
65168
|
+
vault: (inboundDetail === null || inboundDetail === void 0 ? void 0 : inboundDetail.address) || '',
|
|
65169
|
+
haltedChain: (inboundDetail === null || inboundDetail === void 0 ? void 0 : inboundDetail.halted) || !!mimirDetails[`HALT${chain}CHAIN`] || !!mimirDetails['HALTCHAINGLOBAL'],
|
|
65170
|
+
haltedTrading: !!mimirDetails['HALTTRADING'] || !!mimirDetails[`HALT${chain}TRADING`],
|
|
65171
|
+
haltedLP: !!mimirDetails['PAUSELP'] || !!mimirDetails[`PAUSELP${chain}`],
|
|
65172
|
+
};
|
|
65173
|
+
if (inboundDetail === null || inboundDetail === void 0 ? void 0 : inboundDetail.router)
|
|
65174
|
+
details.router = inboundDetail.router;
|
|
65175
|
+
return details;
|
|
65176
|
+
});
|
|
65177
65177
|
|
|
65178
65178
|
var inherits_browser$1 = createCommonjsModule$1(function (module) {
|
|
65179
65179
|
if (typeof Object.create === 'function') {
|
|
@@ -85151,11 +85151,13 @@ var src_12 = src$3.TransactionBuilder;
|
|
|
85151
85151
|
* 1000 satoshi/kB (similar to current `minrelaytxfee`)
|
|
85152
85152
|
* @see https://github.com/bitcoin/bitcoin/blob/db88db47278d2e7208c50d16ab10cb355067d071/src/validation.h#L56
|
|
85153
85153
|
*/
|
|
85154
|
-
|
|
85155
|
-
|
|
85154
|
+
const MIN_TX_FEE = 1000;
|
|
85155
|
+
const BTC_DECIMAL = 8;
|
|
85156
|
+
const LOWER_FEE_BOUND = 1;
|
|
85157
|
+
const UPPER_FEE_BOUND = 500;
|
|
85156
85158
|
|
|
85157
|
-
|
|
85158
|
-
|
|
85159
|
+
const DEFAULT_SUGGESTED_TRANSACTION_FEE = 127;
|
|
85160
|
+
const toSochainNetwork = (network) => {
|
|
85159
85161
|
switch (network) {
|
|
85160
85162
|
case Network.Mainnet:
|
|
85161
85163
|
case Network.Stagenet:
|
|
@@ -85174,23 +85176,12 @@ var toSochainNetwork = function (network) {
|
|
|
85174
85176
|
* @param {string} address
|
|
85175
85177
|
* @returns {BtcAddressDTO}
|
|
85176
85178
|
*/
|
|
85177
|
-
|
|
85178
|
-
|
|
85179
|
-
|
|
85180
|
-
|
|
85181
|
-
|
|
85182
|
-
|
|
85183
|
-
case 0:
|
|
85184
|
-
url = sochainUrl + "/address/" + toSochainNetwork(network) + "/" + address;
|
|
85185
|
-
return [4 /*yield*/, axios.get(url)];
|
|
85186
|
-
case 1:
|
|
85187
|
-
response = _b.sent();
|
|
85188
|
-
addressResponse = response.data;
|
|
85189
|
-
return [2 /*return*/, addressResponse.data];
|
|
85190
|
-
}
|
|
85191
|
-
});
|
|
85192
|
-
});
|
|
85193
|
-
};
|
|
85179
|
+
const getAddress = ({ sochainUrl, network, address }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85180
|
+
const url = `${sochainUrl}/address/${toSochainNetwork(network)}/${address}`;
|
|
85181
|
+
const response = yield axios$1.get(url);
|
|
85182
|
+
const addressResponse = response.data;
|
|
85183
|
+
return addressResponse.data;
|
|
85184
|
+
});
|
|
85194
85185
|
/**
|
|
85195
85186
|
* Get transaction by hash.
|
|
85196
85187
|
*
|
|
@@ -85201,23 +85192,12 @@ var getAddress = function (_a) {
|
|
|
85201
85192
|
* @param {string} hash The transaction hash.
|
|
85202
85193
|
* @returns {Transactions}
|
|
85203
85194
|
*/
|
|
85204
|
-
|
|
85205
|
-
|
|
85206
|
-
|
|
85207
|
-
|
|
85208
|
-
|
|
85209
|
-
|
|
85210
|
-
case 0:
|
|
85211
|
-
url = sochainUrl + "/get_tx/" + toSochainNetwork(network) + "/" + hash;
|
|
85212
|
-
return [4 /*yield*/, axios.get(url)];
|
|
85213
|
-
case 1:
|
|
85214
|
-
response = _b.sent();
|
|
85215
|
-
tx = response.data;
|
|
85216
|
-
return [2 /*return*/, tx.data];
|
|
85217
|
-
}
|
|
85218
|
-
});
|
|
85219
|
-
});
|
|
85220
|
-
};
|
|
85195
|
+
const getTx = ({ sochainUrl, network, hash }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85196
|
+
const url = `${sochainUrl}/get_tx/${toSochainNetwork(network)}/${hash}`;
|
|
85197
|
+
const response = yield axios$1.get(url);
|
|
85198
|
+
const tx = response.data;
|
|
85199
|
+
return tx.data;
|
|
85200
|
+
});
|
|
85221
85201
|
/**
|
|
85222
85202
|
* Get address balance.
|
|
85223
85203
|
*
|
|
@@ -85229,27 +85209,16 @@ var getTx = function (_a) {
|
|
|
85229
85209
|
* @param {boolean} confirmedOnly Flag whether to get balances of confirmed txs only or for all
|
|
85230
85210
|
* @returns {number}
|
|
85231
85211
|
*/
|
|
85232
|
-
|
|
85233
|
-
|
|
85234
|
-
|
|
85235
|
-
|
|
85236
|
-
|
|
85237
|
-
|
|
85238
|
-
|
|
85239
|
-
|
|
85240
|
-
|
|
85241
|
-
|
|
85242
|
-
response = _b.sent();
|
|
85243
|
-
balanceResponse = response.data;
|
|
85244
|
-
confirmed = assetAmount(balanceResponse.data.confirmed_balance, BTC_DECIMAL);
|
|
85245
|
-
unconfirmed = assetAmount(balanceResponse.data.unconfirmed_balance, BTC_DECIMAL);
|
|
85246
|
-
netAmt = confirmedOnly ? confirmed : confirmed.plus(unconfirmed);
|
|
85247
|
-
result = assetToBase(netAmt);
|
|
85248
|
-
return [2 /*return*/, result];
|
|
85249
|
-
}
|
|
85250
|
-
});
|
|
85251
|
-
});
|
|
85252
|
-
};
|
|
85212
|
+
const getBalance = ({ sochainUrl, network, address, confirmedOnly, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85213
|
+
const url = `${sochainUrl}/get_address_balance/${toSochainNetwork(network)}/${address}`;
|
|
85214
|
+
const response = yield axios$1.get(url);
|
|
85215
|
+
const balanceResponse = response.data;
|
|
85216
|
+
const confirmed = assetAmount(balanceResponse.data.confirmed_balance, BTC_DECIMAL);
|
|
85217
|
+
const unconfirmed = assetAmount(balanceResponse.data.unconfirmed_balance, BTC_DECIMAL);
|
|
85218
|
+
const netAmt = confirmedOnly ? confirmed : confirmed.plus(unconfirmed);
|
|
85219
|
+
const result = assetToBase(netAmt);
|
|
85220
|
+
return result;
|
|
85221
|
+
});
|
|
85253
85222
|
/**
|
|
85254
85223
|
* Get unspent txs
|
|
85255
85224
|
*
|
|
@@ -85260,43 +85229,34 @@ var getBalance = function (_a) {
|
|
|
85260
85229
|
* @param {string} address
|
|
85261
85230
|
* @returns {BtcAddressUTXO[]}
|
|
85262
85231
|
*/
|
|
85263
|
-
|
|
85264
|
-
|
|
85265
|
-
|
|
85266
|
-
|
|
85267
|
-
|
|
85268
|
-
|
|
85269
|
-
|
|
85270
|
-
|
|
85271
|
-
|
|
85272
|
-
|
|
85273
|
-
|
|
85274
|
-
|
|
85275
|
-
|
|
85276
|
-
|
|
85277
|
-
|
|
85278
|
-
|
|
85279
|
-
|
|
85280
|
-
|
|
85281
|
-
|
|
85282
|
-
|
|
85283
|
-
|
|
85284
|
-
if (!(txs.length === 100)) return [3 /*break*/, 3];
|
|
85285
|
-
lastTxId = txs[99].txid;
|
|
85286
|
-
return [4 /*yield*/, getUnspentTxs({
|
|
85287
|
-
sochainUrl: sochainUrl,
|
|
85288
|
-
network: network,
|
|
85289
|
-
address: address,
|
|
85290
|
-
startingFromTxId: lastTxId,
|
|
85291
|
-
})];
|
|
85292
|
-
case 2:
|
|
85293
|
-
nextBatch = _b.sent();
|
|
85294
|
-
return [2 /*return*/, txs.concat(nextBatch)];
|
|
85295
|
-
case 3: return [2 /*return*/, txs];
|
|
85296
|
-
}
|
|
85232
|
+
const getUnspentTxs = ({ sochainUrl, network, address, startingFromTxId, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85233
|
+
const url = [
|
|
85234
|
+
sochainUrl,
|
|
85235
|
+
'get_tx_unspent',
|
|
85236
|
+
toSochainNetwork(network),
|
|
85237
|
+
address,
|
|
85238
|
+
startingFromTxId ? startingFromTxId : null,
|
|
85239
|
+
]
|
|
85240
|
+
.filter((v) => !!v)
|
|
85241
|
+
.join('/');
|
|
85242
|
+
const resp = yield axios$1.get(url);
|
|
85243
|
+
const response = resp.data;
|
|
85244
|
+
const txs = response.data.txs;
|
|
85245
|
+
if (txs.length === 100) {
|
|
85246
|
+
//fetch the next batch
|
|
85247
|
+
const lastTxId = txs[99].txid;
|
|
85248
|
+
const nextBatch = yield getUnspentTxs({
|
|
85249
|
+
sochainUrl,
|
|
85250
|
+
network,
|
|
85251
|
+
address,
|
|
85252
|
+
startingFromTxId: lastTxId,
|
|
85297
85253
|
});
|
|
85298
|
-
|
|
85299
|
-
}
|
|
85254
|
+
return txs.concat(nextBatch);
|
|
85255
|
+
}
|
|
85256
|
+
else {
|
|
85257
|
+
return txs;
|
|
85258
|
+
}
|
|
85259
|
+
});
|
|
85300
85260
|
/**
|
|
85301
85261
|
* Get Tx Confirmation status
|
|
85302
85262
|
*
|
|
@@ -85307,62 +85267,35 @@ var getUnspentTxs = function (_a) {
|
|
|
85307
85267
|
* @param {string} hash tx id
|
|
85308
85268
|
* @returns {TxConfirmedStatus}
|
|
85309
85269
|
*/
|
|
85310
|
-
|
|
85311
|
-
|
|
85312
|
-
return
|
|
85313
|
-
|
|
85314
|
-
return __generator(this, function (_b) {
|
|
85315
|
-
switch (_b.label) {
|
|
85316
|
-
case 0: return [4 /*yield*/, axios.get(sochainUrl + "/is_tx_confirmed/" + toSochainNetwork(network) + "/" + hash)];
|
|
85317
|
-
case 1:
|
|
85318
|
-
data = (_b.sent()).data;
|
|
85319
|
-
return [2 /*return*/, data.data];
|
|
85320
|
-
}
|
|
85321
|
-
});
|
|
85322
|
-
});
|
|
85323
|
-
};
|
|
85270
|
+
const getIsTxConfirmed = ({ sochainUrl, network, hash }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85271
|
+
const { data } = yield axios$1.get(`${sochainUrl}/is_tx_confirmed/${toSochainNetwork(network)}/${hash}`);
|
|
85272
|
+
return data.data;
|
|
85273
|
+
});
|
|
85324
85274
|
/**
|
|
85325
85275
|
* List of confirmed txs
|
|
85326
85276
|
*
|
|
85327
85277
|
* Stores a list of confirmed txs (hashes) in memory to avoid requesting same data
|
|
85328
85278
|
*/
|
|
85329
|
-
|
|
85279
|
+
const confirmedTxs = [];
|
|
85330
85280
|
/**
|
|
85331
85281
|
* Helper to get `confirmed` status of a tx.
|
|
85332
85282
|
*
|
|
85333
85283
|
* It will get it from cache or try to get it from Sochain (if not cached before)
|
|
85334
85284
|
*/
|
|
85335
|
-
|
|
85336
|
-
|
|
85337
|
-
|
|
85338
|
-
|
|
85339
|
-
|
|
85340
|
-
|
|
85341
|
-
|
|
85342
|
-
|
|
85343
|
-
|
|
85344
|
-
return [2 /*return*/, true
|
|
85345
|
-
// or get status from Sochain
|
|
85346
|
-
];
|
|
85347
|
-
return [4 /*yield*/, getIsTxConfirmed({
|
|
85348
|
-
sochainUrl: sochainUrl,
|
|
85349
|
-
network: network,
|
|
85350
|
-
hash: txHash,
|
|
85351
|
-
})
|
|
85352
|
-
// cache status
|
|
85353
|
-
];
|
|
85354
|
-
case 1: return [4 /*yield*/, _b.sent()
|
|
85355
|
-
// cache status
|
|
85356
|
-
];
|
|
85357
|
-
case 2:
|
|
85358
|
-
is_confirmed = (_b.sent()).is_confirmed;
|
|
85359
|
-
// cache status
|
|
85360
|
-
confirmedTxs.push(txHash);
|
|
85361
|
-
return [2 /*return*/, is_confirmed];
|
|
85362
|
-
}
|
|
85363
|
-
});
|
|
85285
|
+
const getConfirmedTxStatus = ({ txHash, sochainUrl, network, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85286
|
+
// try to get it from cache
|
|
85287
|
+
if (confirmedTxs.includes(txHash))
|
|
85288
|
+
return true;
|
|
85289
|
+
// or get status from Sochain
|
|
85290
|
+
const { is_confirmed } = yield yield getIsTxConfirmed({
|
|
85291
|
+
sochainUrl,
|
|
85292
|
+
network,
|
|
85293
|
+
hash: txHash,
|
|
85364
85294
|
});
|
|
85365
|
-
|
|
85295
|
+
// cache status
|
|
85296
|
+
confirmedTxs.push(txHash);
|
|
85297
|
+
return is_confirmed;
|
|
85298
|
+
});
|
|
85366
85299
|
/**
|
|
85367
85300
|
* Get unspent txs and filter out pending UTXOs
|
|
85368
85301
|
*
|
|
@@ -85373,67 +85306,42 @@ var getConfirmedTxStatus = function (_a) {
|
|
|
85373
85306
|
* @param {string} address
|
|
85374
85307
|
* @returns {BtcAddressUTXO[]}
|
|
85375
85308
|
*/
|
|
85376
|
-
|
|
85377
|
-
|
|
85378
|
-
|
|
85379
|
-
|
|
85380
|
-
|
|
85381
|
-
switch (_b.label) {
|
|
85382
|
-
case 0: return [4 /*yield*/, getUnspentTxs({
|
|
85383
|
-
sochainUrl: sochainUrl,
|
|
85384
|
-
network: network,
|
|
85385
|
-
address: address,
|
|
85386
|
-
})];
|
|
85387
|
-
case 1:
|
|
85388
|
-
txs = _b.sent();
|
|
85389
|
-
confirmedUTXOs = [];
|
|
85390
|
-
return [4 /*yield*/, Promise.all(txs.map(function (tx) { return __awaiter(void 0, void 0, void 0, function () {
|
|
85391
|
-
var confirmed;
|
|
85392
|
-
return __generator(this, function (_a) {
|
|
85393
|
-
switch (_a.label) {
|
|
85394
|
-
case 0: return [4 /*yield*/, getConfirmedTxStatus({
|
|
85395
|
-
sochainUrl: sochainUrl,
|
|
85396
|
-
network: network,
|
|
85397
|
-
txHash: tx.txid,
|
|
85398
|
-
})];
|
|
85399
|
-
case 1:
|
|
85400
|
-
confirmed = _a.sent();
|
|
85401
|
-
if (confirmed) {
|
|
85402
|
-
confirmedUTXOs.push(tx);
|
|
85403
|
-
}
|
|
85404
|
-
return [2 /*return*/];
|
|
85405
|
-
}
|
|
85406
|
-
});
|
|
85407
|
-
}); }))];
|
|
85408
|
-
case 2:
|
|
85409
|
-
_b.sent();
|
|
85410
|
-
return [2 /*return*/, confirmedUTXOs];
|
|
85411
|
-
}
|
|
85412
|
-
});
|
|
85309
|
+
const getConfirmedUnspentTxs = ({ sochainUrl, network, address, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85310
|
+
const txs = yield getUnspentTxs({
|
|
85311
|
+
sochainUrl,
|
|
85312
|
+
network,
|
|
85313
|
+
address,
|
|
85413
85314
|
});
|
|
85414
|
-
|
|
85315
|
+
const confirmedUTXOs = [];
|
|
85316
|
+
yield Promise.all(txs.map((tx) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85317
|
+
const confirmed = yield getConfirmedTxStatus({
|
|
85318
|
+
sochainUrl,
|
|
85319
|
+
network,
|
|
85320
|
+
txHash: tx.txid,
|
|
85321
|
+
});
|
|
85322
|
+
if (confirmed) {
|
|
85323
|
+
confirmedUTXOs.push(tx);
|
|
85324
|
+
}
|
|
85325
|
+
})));
|
|
85326
|
+
return confirmedUTXOs;
|
|
85327
|
+
});
|
|
85415
85328
|
/**
|
|
85416
85329
|
* Get Bitcoin suggested transaction fee.
|
|
85417
85330
|
*
|
|
85418
85331
|
* @returns {number} The Bitcoin suggested transaction fee per bytes in sat.
|
|
85419
85332
|
*/
|
|
85420
|
-
|
|
85421
|
-
|
|
85422
|
-
|
|
85423
|
-
|
|
85424
|
-
|
|
85425
|
-
|
|
85426
|
-
|
|
85427
|
-
|
|
85428
|
-
|
|
85429
|
-
|
|
85430
|
-
|
|
85431
|
-
|
|
85432
|
-
return [2 /*return*/, DEFAULT_SUGGESTED_TRANSACTION_FEE];
|
|
85433
|
-
case 3: return [2 /*return*/];
|
|
85434
|
-
}
|
|
85435
|
-
});
|
|
85436
|
-
}); };
|
|
85333
|
+
const getSuggestedTxFee = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
85334
|
+
//Note: sochain does not provide fee rate related data
|
|
85335
|
+
//So use Bitgo API for fee estimation
|
|
85336
|
+
//Refer: https://app.bitgo.com/docs/#operation/v2.tx.getfeeestimate
|
|
85337
|
+
try {
|
|
85338
|
+
const response = yield axios$1.get('https://app.bitgo.com/api/v2/btc/tx/fee');
|
|
85339
|
+
return response.data.feePerKb / 1000; // feePerKb to feePerByte
|
|
85340
|
+
}
|
|
85341
|
+
catch (error) {
|
|
85342
|
+
return DEFAULT_SUGGESTED_TRANSACTION_FEE;
|
|
85343
|
+
}
|
|
85344
|
+
});
|
|
85437
85345
|
|
|
85438
85346
|
// baseline estimates, used to improve performance
|
|
85439
85347
|
var TX_EMPTY_SIZE = 4 + 1 + 1 + 4;
|
|
@@ -85553,71 +85461,31 @@ var accumulative = function accumulative (utxos, outputs, feeRate) {
|
|
|
85553
85461
|
* Doc (SwaggerHub) https://app.swaggerhub.com/apis/eligecode/blockchain-api/0.0.1-oas3
|
|
85554
85462
|
*
|
|
85555
85463
|
*/
|
|
85556
|
-
|
|
85557
|
-
|
|
85558
|
-
|
|
85559
|
-
|
|
85560
|
-
|
|
85561
|
-
|
|
85562
|
-
|
|
85563
|
-
|
|
85564
|
-
|
|
85565
|
-
|
|
85566
|
-
|
|
85567
|
-
|
|
85568
|
-
|
|
85569
|
-
|
|
85570
|
-
|
|
85571
|
-
|
|
85572
|
-
|
|
85573
|
-
|
|
85574
|
-
return __awaiter(void 0, void 0, void 0, function () {
|
|
85575
|
-
var response;
|
|
85576
|
-
return __generator(this, function (_b) {
|
|
85577
|
-
switch (_b.label) {
|
|
85578
|
-
case 0: return [4 /*yield*/, axios.get(haskoinUrl + "/address/" + address + "/unspent")];
|
|
85579
|
-
case 1:
|
|
85580
|
-
response = (_b.sent()).data;
|
|
85581
|
-
return [2 /*return*/, response];
|
|
85582
|
-
}
|
|
85464
|
+
const getBalance$1 = ({ haskoinUrl, address, confirmedOnly, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85465
|
+
const { data: { confirmed, unconfirmed }, } = yield axios$1.get(`${haskoinUrl}/address/${address}/balance`);
|
|
85466
|
+
const confirmedAmount = baseAmount(confirmed, BTC_DECIMAL);
|
|
85467
|
+
const unconfirmedAmount = baseAmount(unconfirmed, BTC_DECIMAL);
|
|
85468
|
+
return confirmedOnly ? confirmedAmount : confirmedAmount.plus(unconfirmedAmount);
|
|
85469
|
+
});
|
|
85470
|
+
const getUnspentTxs$1 = ({ haskoinUrl, address, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85471
|
+
const { data: response } = yield axios$1.get(`${haskoinUrl}/address/${address}/unspent`);
|
|
85472
|
+
return response;
|
|
85473
|
+
});
|
|
85474
|
+
const getConfirmedUnspentTxs$1 = ({ haskoinUrl, sochainUrl, address, network, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85475
|
+
const allUtxos = yield getUnspentTxs$1({ haskoinUrl, address });
|
|
85476
|
+
const confirmedUTXOs = [];
|
|
85477
|
+
yield Promise.all(allUtxos.map((tx) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85478
|
+
const confirmed = yield getConfirmedTxStatus({
|
|
85479
|
+
sochainUrl,
|
|
85480
|
+
network,
|
|
85481
|
+
txHash: tx.txid,
|
|
85583
85482
|
});
|
|
85584
|
-
|
|
85585
|
-
|
|
85586
|
-
|
|
85587
|
-
|
|
85588
|
-
return
|
|
85589
|
-
|
|
85590
|
-
return __generator(this, function (_b) {
|
|
85591
|
-
switch (_b.label) {
|
|
85592
|
-
case 0: return [4 /*yield*/, getUnspentTxs$1({ haskoinUrl: haskoinUrl, address: address })];
|
|
85593
|
-
case 1:
|
|
85594
|
-
allUtxos = _b.sent();
|
|
85595
|
-
confirmedUTXOs = [];
|
|
85596
|
-
return [4 /*yield*/, Promise.all(allUtxos.map(function (tx) { return __awaiter(void 0, void 0, void 0, function () {
|
|
85597
|
-
var confirmed;
|
|
85598
|
-
return __generator(this, function (_a) {
|
|
85599
|
-
switch (_a.label) {
|
|
85600
|
-
case 0: return [4 /*yield*/, getConfirmedTxStatus({
|
|
85601
|
-
sochainUrl: sochainUrl,
|
|
85602
|
-
network: network,
|
|
85603
|
-
txHash: tx.txid,
|
|
85604
|
-
})];
|
|
85605
|
-
case 1:
|
|
85606
|
-
confirmed = _a.sent();
|
|
85607
|
-
if (confirmed) {
|
|
85608
|
-
confirmedUTXOs.push(tx);
|
|
85609
|
-
}
|
|
85610
|
-
return [2 /*return*/];
|
|
85611
|
-
}
|
|
85612
|
-
});
|
|
85613
|
-
}); }))];
|
|
85614
|
-
case 2:
|
|
85615
|
-
_b.sent();
|
|
85616
|
-
return [2 /*return*/, confirmedUTXOs];
|
|
85617
|
-
}
|
|
85618
|
-
});
|
|
85619
|
-
});
|
|
85620
|
-
};
|
|
85483
|
+
if (confirmed) {
|
|
85484
|
+
confirmedUTXOs.push(tx);
|
|
85485
|
+
}
|
|
85486
|
+
})));
|
|
85487
|
+
return confirmedUTXOs;
|
|
85488
|
+
});
|
|
85621
85489
|
/**
|
|
85622
85490
|
* Broadcast transaction.
|
|
85623
85491
|
*
|
|
@@ -85630,64 +85498,44 @@ var getConfirmedUnspentTxs$1 = function (_a) {
|
|
|
85630
85498
|
* @param {BroadcastTxParams} params
|
|
85631
85499
|
* @returns {TxHash} Transaction hash.
|
|
85632
85500
|
*/
|
|
85633
|
-
|
|
85634
|
-
|
|
85635
|
-
|
|
85636
|
-
|
|
85637
|
-
|
|
85638
|
-
|
|
85639
|
-
|
|
85640
|
-
|
|
85641
|
-
|
|
85642
|
-
|
|
85643
|
-
|
|
85644
|
-
|
|
85645
|
-
|
|
85646
|
-
|
|
85647
|
-
return __generator(this, function (_b) {
|
|
85648
|
-
switch (_b.label) {
|
|
85649
|
-
case 0:
|
|
85650
|
-
config = error.config;
|
|
85651
|
-
if (!(counter < MAX && ((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 500)) return [3 /*break*/, 2];
|
|
85652
|
-
counter++;
|
|
85653
|
-
return [4 /*yield*/, delay(200 * counter)];
|
|
85654
|
-
case 1:
|
|
85655
|
-
_b.sent();
|
|
85656
|
-
return [2 /*return*/, instance.request(config)];
|
|
85657
|
-
case 2: return [2 /*return*/, Promise.reject(error)];
|
|
85658
|
-
}
|
|
85659
|
-
});
|
|
85660
|
-
}); };
|
|
85661
|
-
id = instance.interceptors.response.use(onFullfilled, onRejected);
|
|
85662
|
-
url = haskoinUrl + "/transactions";
|
|
85663
|
-
_b.label = 1;
|
|
85664
|
-
case 1:
|
|
85665
|
-
_b.trys.push([1, 3, , 4]);
|
|
85666
|
-
return [4 /*yield*/, instance.post(url, txHex)
|
|
85667
|
-
// clean up interceptor from axios instance
|
|
85668
|
-
];
|
|
85669
|
-
case 2:
|
|
85670
|
-
txid = (_b.sent()).data.txid;
|
|
85671
|
-
// clean up interceptor from axios instance
|
|
85672
|
-
instance.interceptors.response.eject(id);
|
|
85673
|
-
return [2 /*return*/, txid];
|
|
85674
|
-
case 3:
|
|
85675
|
-
error_1 = _b.sent();
|
|
85676
|
-
// clean up interceptor from axios instance
|
|
85677
|
-
instance.interceptors.response.eject(id);
|
|
85678
|
-
return [2 /*return*/, Promise.reject(error_1)];
|
|
85679
|
-
case 4: return [2 /*return*/];
|
|
85680
|
-
}
|
|
85681
|
-
});
|
|
85501
|
+
const broadcastTx = ({ txHex, haskoinUrl }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85502
|
+
const instance = axios$1.create();
|
|
85503
|
+
const MAX = 5;
|
|
85504
|
+
let counter = 0;
|
|
85505
|
+
const onFullfilled = (res) => res;
|
|
85506
|
+
const onRejected = (error) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85507
|
+
var _a;
|
|
85508
|
+
const config = error.config;
|
|
85509
|
+
if (counter < MAX && ((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 500) {
|
|
85510
|
+
counter++;
|
|
85511
|
+
yield delay(200 * counter);
|
|
85512
|
+
return instance.request(config);
|
|
85513
|
+
}
|
|
85514
|
+
return Promise.reject(error);
|
|
85682
85515
|
});
|
|
85683
|
-
|
|
85516
|
+
// All logic for re-sending same tx is handled by Axios' response interceptor
|
|
85517
|
+
// https://github.com/axios/axios#interceptors
|
|
85518
|
+
const id = instance.interceptors.response.use(onFullfilled, onRejected);
|
|
85519
|
+
const url = `${haskoinUrl}/transactions`;
|
|
85520
|
+
try {
|
|
85521
|
+
const { data: { txid }, } = yield instance.post(url, txHex);
|
|
85522
|
+
// clean up interceptor from axios instance
|
|
85523
|
+
instance.interceptors.response.eject(id);
|
|
85524
|
+
return txid;
|
|
85525
|
+
}
|
|
85526
|
+
catch (error) {
|
|
85527
|
+
// clean up interceptor from axios instance
|
|
85528
|
+
instance.interceptors.response.eject(id);
|
|
85529
|
+
return Promise.reject(error);
|
|
85530
|
+
}
|
|
85531
|
+
});
|
|
85684
85532
|
|
|
85685
|
-
|
|
85686
|
-
|
|
85687
|
-
|
|
85688
|
-
|
|
85689
|
-
|
|
85690
|
-
|
|
85533
|
+
const TX_EMPTY_SIZE$1 = 4 + 1 + 1 + 4; //10
|
|
85534
|
+
const TX_INPUT_BASE$1 = 32 + 4 + 1 + 4; // 41
|
|
85535
|
+
const TX_INPUT_PUBKEYHASH$1 = 107;
|
|
85536
|
+
const TX_OUTPUT_BASE$1 = 8 + 1; //9
|
|
85537
|
+
const TX_OUTPUT_PUBKEYHASH$1 = 25;
|
|
85538
|
+
const inputBytes$1 = (input) => {
|
|
85691
85539
|
return TX_INPUT_BASE$1 + (input.witnessUtxo.script ? input.witnessUtxo.script.length : TX_INPUT_PUBKEYHASH$1);
|
|
85692
85540
|
};
|
|
85693
85541
|
/**
|
|
@@ -85696,8 +85544,8 @@ var inputBytes$1 = function (input) {
|
|
|
85696
85544
|
* @param {string} memo The memo to be compiled.
|
|
85697
85545
|
* @returns {Buffer} The compiled memo.
|
|
85698
85546
|
*/
|
|
85699
|
-
|
|
85700
|
-
|
|
85547
|
+
const compileMemo = (memo) => {
|
|
85548
|
+
const data = Buffer.from(memo, 'utf8'); // converts MEMO to buffer
|
|
85701
85549
|
return src_7$1.compile([src_10.OP_RETURN, data]); // Compile OP_RETURN script
|
|
85702
85550
|
};
|
|
85703
85551
|
/**
|
|
@@ -85708,10 +85556,9 @@ var compileMemo = function (memo) {
|
|
|
85708
85556
|
* @param {Buffer} data The compiled memo (Optional).
|
|
85709
85557
|
* @returns {number} The fee amount.
|
|
85710
85558
|
*/
|
|
85711
|
-
|
|
85712
|
-
|
|
85713
|
-
|
|
85714
|
-
inputs.reduce(function (a, x) { return a + inputBytes$1(x); }, 0) +
|
|
85559
|
+
const getFee = (inputs, feeRate, data = null) => {
|
|
85560
|
+
let sum = TX_EMPTY_SIZE$1 +
|
|
85561
|
+
inputs.reduce((a, x) => a + inputBytes$1(x), 0) +
|
|
85715
85562
|
inputs.length + // +1 byte for each input signature
|
|
85716
85563
|
TX_OUTPUT_BASE$1 +
|
|
85717
85564
|
TX_OUTPUT_PUBKEYHASH$1 +
|
|
@@ -85720,7 +85567,7 @@ var getFee = function (inputs, feeRate, data) {
|
|
|
85720
85567
|
if (data) {
|
|
85721
85568
|
sum += TX_OUTPUT_BASE$1 + data.length;
|
|
85722
85569
|
}
|
|
85723
|
-
|
|
85570
|
+
const fee = sum * feeRate;
|
|
85724
85571
|
return fee > MIN_TX_FEE ? fee : MIN_TX_FEE;
|
|
85725
85572
|
};
|
|
85726
85573
|
/**
|
|
@@ -85729,7 +85576,7 @@ var getFee = function (inputs, feeRate, data) {
|
|
|
85729
85576
|
* @param {Network} network
|
|
85730
85577
|
* @returns {Bitcoin.Network} The BTC network.
|
|
85731
85578
|
*/
|
|
85732
|
-
|
|
85579
|
+
const btcNetwork = (network) => {
|
|
85733
85580
|
switch (network) {
|
|
85734
85581
|
case Network.Mainnet:
|
|
85735
85582
|
case Network.Stagenet:
|
|
@@ -85746,44 +85593,25 @@ var btcNetwork = function (network) {
|
|
|
85746
85593
|
* @param {Address} address
|
|
85747
85594
|
* @returns {Balance[]} The balances of the given address.
|
|
85748
85595
|
*/
|
|
85749
|
-
|
|
85750
|
-
|
|
85751
|
-
|
|
85752
|
-
|
|
85753
|
-
|
|
85754
|
-
|
|
85755
|
-
|
|
85756
|
-
|
|
85757
|
-
|
|
85758
|
-
|
|
85759
|
-
|
|
85760
|
-
|
|
85761
|
-
|
|
85762
|
-
|
|
85763
|
-
|
|
85764
|
-
|
|
85765
|
-
|
|
85766
|
-
|
|
85767
|
-
|
|
85768
|
-
return [4 /*yield*/, getBalance$1({ haskoinUrl: haskoinUrl, address: params.address, confirmedOnly: confirmedOnly })];
|
|
85769
|
-
case 2: return [2 /*return*/, [
|
|
85770
|
-
(_c.amount = _e.sent(),
|
|
85771
|
-
_c)
|
|
85772
|
-
]];
|
|
85773
|
-
case 3:
|
|
85774
|
-
_d = {
|
|
85775
|
-
asset: AssetBTC
|
|
85776
|
-
};
|
|
85777
|
-
return [4 /*yield*/, getBalance(__assign(__assign({}, params), { confirmedOnly: confirmedOnly }))];
|
|
85778
|
-
case 4: return [2 /*return*/, [
|
|
85779
|
-
(_d.amount = _e.sent(),
|
|
85780
|
-
_d)
|
|
85781
|
-
]];
|
|
85782
|
-
case 5: return [2 /*return*/];
|
|
85783
|
-
}
|
|
85784
|
-
});
|
|
85785
|
-
});
|
|
85786
|
-
};
|
|
85596
|
+
const getBalance$2 = ({ params, haskoinUrl, confirmedOnly, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85597
|
+
switch (params.network) {
|
|
85598
|
+
case Network.Mainnet:
|
|
85599
|
+
case Network.Stagenet:
|
|
85600
|
+
return [
|
|
85601
|
+
{
|
|
85602
|
+
asset: AssetBTC,
|
|
85603
|
+
amount: yield getBalance$1({ haskoinUrl, address: params.address, confirmedOnly }),
|
|
85604
|
+
},
|
|
85605
|
+
];
|
|
85606
|
+
case Network.Testnet:
|
|
85607
|
+
return [
|
|
85608
|
+
{
|
|
85609
|
+
asset: AssetBTC,
|
|
85610
|
+
amount: yield getBalance(Object.assign(Object.assign({}, params), { confirmedOnly })),
|
|
85611
|
+
},
|
|
85612
|
+
];
|
|
85613
|
+
}
|
|
85614
|
+
});
|
|
85787
85615
|
/**
|
|
85788
85616
|
* Validate the BTC address.
|
|
85789
85617
|
*
|
|
@@ -85791,7 +85619,7 @@ var getBalance$2 = function (_a) {
|
|
|
85791
85619
|
* @param {Network} network
|
|
85792
85620
|
* @returns {boolean} `true` or `false`.
|
|
85793
85621
|
*/
|
|
85794
|
-
|
|
85622
|
+
const validateAddress = (address, network) => {
|
|
85795
85623
|
try {
|
|
85796
85624
|
src_2$2.toOutputScript(address, btcNetwork(network));
|
|
85797
85625
|
return true;
|
|
@@ -85801,36 +85629,23 @@ var validateAddress = function (address, network) {
|
|
|
85801
85629
|
}
|
|
85802
85630
|
};
|
|
85803
85631
|
// Stores list of txHex in memory to avoid requesting same data
|
|
85804
|
-
|
|
85632
|
+
const txHexMap = {};
|
|
85805
85633
|
/**
|
|
85806
85634
|
* Helper to get `hex` of `Tx`
|
|
85807
85635
|
*
|
|
85808
85636
|
* It will try to get it from cache before requesting it from Sochain
|
|
85809
85637
|
*/
|
|
85810
|
-
|
|
85811
|
-
|
|
85812
|
-
|
|
85813
|
-
|
|
85814
|
-
return
|
|
85815
|
-
|
|
85816
|
-
|
|
85817
|
-
|
|
85818
|
-
|
|
85819
|
-
|
|
85820
|
-
|
|
85821
|
-
];
|
|
85822
|
-
return [4 /*yield*/, getTx({ hash: txHash, sochainUrl: sochainUrl, network: network })
|
|
85823
|
-
// cache it
|
|
85824
|
-
];
|
|
85825
|
-
case 1:
|
|
85826
|
-
tx_hex = (_b.sent()).tx_hex;
|
|
85827
|
-
// cache it
|
|
85828
|
-
txHexMap[txHash] = tx_hex;
|
|
85829
|
-
return [2 /*return*/, tx_hex];
|
|
85830
|
-
}
|
|
85831
|
-
});
|
|
85832
|
-
});
|
|
85833
|
-
};
|
|
85638
|
+
const getTxHex = ({ txHash, sochainUrl, network, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85639
|
+
// try to get hex from cache
|
|
85640
|
+
const txHex = txHexMap[txHash];
|
|
85641
|
+
if (!!txHex)
|
|
85642
|
+
return txHex;
|
|
85643
|
+
// or get it from Sochain
|
|
85644
|
+
const { tx_hex } = yield getTx({ hash: txHash, sochainUrl, network });
|
|
85645
|
+
// cache it
|
|
85646
|
+
txHexMap[txHash] = tx_hex;
|
|
85647
|
+
return tx_hex;
|
|
85648
|
+
});
|
|
85834
85649
|
/**
|
|
85835
85650
|
* Scan UTXOs from sochain.
|
|
85836
85651
|
*
|
|
@@ -85839,202 +85654,122 @@ var getTxHex = function (_a) {
|
|
|
85839
85654
|
* @param {Address} address
|
|
85840
85655
|
* @returns {UTXO[]} The UTXOs of the given address.
|
|
85841
85656
|
*/
|
|
85842
|
-
|
|
85843
|
-
|
|
85844
|
-
|
|
85845
|
-
|
|
85846
|
-
|
|
85847
|
-
|
|
85848
|
-
|
|
85849
|
-
|
|
85850
|
-
|
|
85851
|
-
|
|
85852
|
-
|
|
85853
|
-
|
|
85854
|
-
|
|
85855
|
-
|
|
85856
|
-
|
|
85857
|
-
|
|
85858
|
-
|
|
85859
|
-
|
|
85860
|
-
|
|
85861
|
-
|
|
85862
|
-
|
|
85863
|
-
}
|
|
85864
|
-
|
|
85865
|
-
|
|
85866
|
-
|
|
85867
|
-
|
|
85868
|
-
|
|
85869
|
-
|
|
85870
|
-
|
|
85871
|
-
|
|
85872
|
-
|
|
85873
|
-
|
|
85874
|
-
|
|
85875
|
-
|
|
85876
|
-
|
|
85877
|
-
|
|
85878
|
-
|
|
85879
|
-
|
|
85880
|
-
|
|
85881
|
-
|
|
85882
|
-
|
|
85883
|
-
|
|
85884
|
-
|
|
85885
|
-
|
|
85886
|
-
|
|
85887
|
-
|
|
85888
|
-
|
|
85889
|
-
};
|
|
85890
|
-
if (!withTxHex) return [3 /*break*/, 2];
|
|
85891
|
-
return [4 /*yield*/, getTxHex({ txHash: utxo.txid, sochainUrl: sochainUrl, network: network })];
|
|
85892
|
-
case 1:
|
|
85893
|
-
_a = _c.sent();
|
|
85894
|
-
return [3 /*break*/, 3];
|
|
85895
|
-
case 2:
|
|
85896
|
-
_a = undefined;
|
|
85897
|
-
_c.label = 3;
|
|
85898
|
-
case 3: return [2 /*return*/, (_b.txHex = _a,
|
|
85899
|
-
_b)];
|
|
85900
|
-
}
|
|
85901
|
-
});
|
|
85902
|
-
}); }))];
|
|
85903
|
-
case 6: return [2 /*return*/, _g.sent()];
|
|
85904
|
-
case 7:
|
|
85905
|
-
if (!confirmedOnly) return [3 /*break*/, 9];
|
|
85906
|
-
return [4 /*yield*/, getConfirmedUnspentTxs$1({ address: address, haskoinUrl: haskoinUrl, sochainUrl: sochainUrl, network: network })];
|
|
85907
|
-
case 8:
|
|
85908
|
-
_f = _g.sent();
|
|
85909
|
-
return [3 /*break*/, 11];
|
|
85910
|
-
case 9: return [4 /*yield*/, getUnspentTxs$1({ address: address, haskoinUrl: haskoinUrl })];
|
|
85911
|
-
case 10:
|
|
85912
|
-
_f = _g.sent();
|
|
85913
|
-
_g.label = 11;
|
|
85914
|
-
case 11:
|
|
85915
|
-
utxos = _f;
|
|
85916
|
-
return [4 /*yield*/, Promise.all(utxos.map(function (utxo) { return __awaiter(void 0, void 0, void 0, function () {
|
|
85917
|
-
var _a;
|
|
85918
|
-
var _b;
|
|
85919
|
-
return __generator(this, function (_c) {
|
|
85920
|
-
switch (_c.label) {
|
|
85921
|
-
case 0:
|
|
85922
|
-
_b = {
|
|
85923
|
-
hash: utxo.txid,
|
|
85924
|
-
index: utxo.index,
|
|
85925
|
-
value: baseAmount(utxo.value, BTC_DECIMAL).amount().toNumber(),
|
|
85926
|
-
witnessUtxo: {
|
|
85927
|
-
value: baseAmount(utxo.value, BTC_DECIMAL).amount().toNumber(),
|
|
85928
|
-
script: Buffer.from(utxo.pkscript, 'hex'),
|
|
85929
|
-
}
|
|
85930
|
-
};
|
|
85931
|
-
if (!withTxHex) return [3 /*break*/, 2];
|
|
85932
|
-
return [4 /*yield*/, getTxHex({ txHash: utxo.txid, sochainUrl: sochainUrl, network: network })];
|
|
85933
|
-
case 1:
|
|
85934
|
-
_a = _c.sent();
|
|
85935
|
-
return [3 /*break*/, 3];
|
|
85936
|
-
case 2:
|
|
85937
|
-
_a = undefined;
|
|
85938
|
-
_c.label = 3;
|
|
85939
|
-
case 3: return [2 /*return*/, (_b.txHex = _a,
|
|
85940
|
-
_b)];
|
|
85941
|
-
}
|
|
85942
|
-
});
|
|
85943
|
-
}); }))];
|
|
85944
|
-
case 12: return [2 /*return*/, _g.sent()];
|
|
85945
|
-
case 13: return [2 /*return*/];
|
|
85946
|
-
}
|
|
85947
|
-
});
|
|
85948
|
-
});
|
|
85949
|
-
};
|
|
85657
|
+
const scanUTXOs = ({ sochainUrl, haskoinUrl, network, address, confirmedOnly = true, // default: scan only confirmed UTXOs
|
|
85658
|
+
withTxHex = false, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85659
|
+
switch (network) {
|
|
85660
|
+
case Network.Testnet: {
|
|
85661
|
+
const addressParam = {
|
|
85662
|
+
sochainUrl,
|
|
85663
|
+
network,
|
|
85664
|
+
address,
|
|
85665
|
+
};
|
|
85666
|
+
// Get UTXOs from Sochain
|
|
85667
|
+
const utxos = confirmedOnly
|
|
85668
|
+
? yield getConfirmedUnspentTxs(addressParam)
|
|
85669
|
+
: yield getUnspentTxs(addressParam);
|
|
85670
|
+
return yield Promise.all(utxos.map((utxo) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85671
|
+
return ({
|
|
85672
|
+
hash: utxo.txid,
|
|
85673
|
+
index: utxo.output_no,
|
|
85674
|
+
value: assetToBase(assetAmount(utxo.value, BTC_DECIMAL)).amount().toNumber(),
|
|
85675
|
+
witnessUtxo: {
|
|
85676
|
+
value: assetToBase(assetAmount(utxo.value, BTC_DECIMAL)).amount().toNumber(),
|
|
85677
|
+
script: Buffer.from(utxo.script_hex, 'hex'),
|
|
85678
|
+
},
|
|
85679
|
+
txHex: withTxHex ? yield getTxHex({ txHash: utxo.txid, sochainUrl, network }) : undefined,
|
|
85680
|
+
});
|
|
85681
|
+
})));
|
|
85682
|
+
}
|
|
85683
|
+
case Network.Mainnet:
|
|
85684
|
+
case Network.Stagenet: {
|
|
85685
|
+
// Get UTXOs from Haskoin
|
|
85686
|
+
const utxos = confirmedOnly
|
|
85687
|
+
? yield getConfirmedUnspentTxs$1({ address, haskoinUrl, sochainUrl, network })
|
|
85688
|
+
: yield getUnspentTxs$1({ address, haskoinUrl });
|
|
85689
|
+
return yield Promise.all(utxos.map((utxo) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85690
|
+
return ({
|
|
85691
|
+
hash: utxo.txid,
|
|
85692
|
+
index: utxo.index,
|
|
85693
|
+
value: baseAmount(utxo.value, BTC_DECIMAL).amount().toNumber(),
|
|
85694
|
+
witnessUtxo: {
|
|
85695
|
+
value: baseAmount(utxo.value, BTC_DECIMAL).amount().toNumber(),
|
|
85696
|
+
script: Buffer.from(utxo.pkscript, 'hex'),
|
|
85697
|
+
},
|
|
85698
|
+
txHex: withTxHex ? yield getTxHex({ txHash: utxo.txid, sochainUrl, network }) : undefined,
|
|
85699
|
+
});
|
|
85700
|
+
})));
|
|
85701
|
+
}
|
|
85702
|
+
}
|
|
85703
|
+
});
|
|
85950
85704
|
/**
|
|
85951
85705
|
* Build transcation.
|
|
85952
85706
|
*
|
|
85953
85707
|
* @param {BuildParams} params The transaction build options.
|
|
85954
85708
|
* @returns {Transaction}
|
|
85955
85709
|
*/
|
|
85956
|
-
|
|
85957
|
-
|
|
85958
|
-
|
|
85959
|
-
|
|
85960
|
-
|
|
85961
|
-
|
|
85962
|
-
|
|
85963
|
-
|
|
85964
|
-
|
|
85965
|
-
|
|
85966
|
-
|
|
85967
|
-
|
|
85968
|
-
|
|
85969
|
-
|
|
85970
|
-
|
|
85971
|
-
|
|
85972
|
-
|
|
85973
|
-
|
|
85974
|
-
|
|
85975
|
-
|
|
85976
|
-
|
|
85977
|
-
|
|
85978
|
-
|
|
85979
|
-
|
|
85980
|
-
|
|
85981
|
-
|
|
85982
|
-
|
|
85983
|
-
|
|
85984
|
-
|
|
85985
|
-
|
|
85986
|
-
|
|
85987
|
-
|
|
85988
|
-
|
|
85989
|
-
|
|
85990
|
-
|
|
85991
|
-
|
|
85992
|
-
|
|
85993
|
-
|
|
85994
|
-
|
|
85995
|
-
|
|
85996
|
-
|
|
85997
|
-
|
|
85998
|
-
|
|
85999
|
-
|
|
86000
|
-
|
|
86001
|
-
|
|
86002
|
-
|
|
86003
|
-
|
|
86004
|
-
|
|
86005
|
-
if (!output.script) {
|
|
86006
|
-
psbt.addOutput(output);
|
|
86007
|
-
}
|
|
86008
|
-
else {
|
|
86009
|
-
//we need to add the compiled memo this way to
|
|
86010
|
-
//avoid dust error tx when accumulating memo output with 0 value
|
|
86011
|
-
if (compiledMemo) {
|
|
86012
|
-
psbt.addOutput({ script: compiledMemo, value: 0 });
|
|
86013
|
-
}
|
|
86014
|
-
}
|
|
86015
|
-
});
|
|
86016
|
-
return [2 /*return*/, { psbt: psbt, utxos: utxos, inputs: inputs }];
|
|
85710
|
+
const buildTx = ({ amount, recipient, memo, feeRate, sender, network, sochainUrl, haskoinUrl, spendPendingUTXO = false, // default: prevent spending uncomfirmed UTXOs
|
|
85711
|
+
withTxHex = false, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85712
|
+
// search only confirmed UTXOs if pending UTXO is not allowed
|
|
85713
|
+
const confirmedOnly = !spendPendingUTXO;
|
|
85714
|
+
const utxos = yield scanUTXOs({ sochainUrl, haskoinUrl, network, address: sender, confirmedOnly, withTxHex });
|
|
85715
|
+
if (memo && memo.length > 80) {
|
|
85716
|
+
throw new Error('memo too long, must not be longer than 80 chars.');
|
|
85717
|
+
}
|
|
85718
|
+
if (utxos.length === 0)
|
|
85719
|
+
throw new Error('No confirmed UTXOs. Please wait until your balance has been confirmed on-chain.');
|
|
85720
|
+
if (!validateAddress(recipient, network))
|
|
85721
|
+
throw new Error('Invalid address');
|
|
85722
|
+
const feeRateWhole = Math.ceil(feeRate);
|
|
85723
|
+
const compiledMemo = memo ? compileMemo(memo) : null;
|
|
85724
|
+
const targetOutputs = [];
|
|
85725
|
+
//1. add output amount and recipient to targets
|
|
85726
|
+
targetOutputs.push({
|
|
85727
|
+
address: recipient,
|
|
85728
|
+
value: amount.amount().toNumber(),
|
|
85729
|
+
});
|
|
85730
|
+
//2. add output memo to targets (optional)
|
|
85731
|
+
if (compiledMemo) {
|
|
85732
|
+
targetOutputs.push({ script: compiledMemo, value: 0 });
|
|
85733
|
+
}
|
|
85734
|
+
const { inputs, outputs } = accumulative(utxos, targetOutputs, feeRateWhole);
|
|
85735
|
+
// .inputs and .outputs will be undefined if no solution was found
|
|
85736
|
+
if (!inputs || !outputs)
|
|
85737
|
+
throw new Error('Insufficient Balance for transaction');
|
|
85738
|
+
const psbt = new src_9$1({ network: btcNetwork(network) }); // Network-specific
|
|
85739
|
+
// psbt add input from accumulative inputs
|
|
85740
|
+
inputs.forEach((utxo) => psbt.addInput({
|
|
85741
|
+
hash: utxo.hash,
|
|
85742
|
+
index: utxo.index,
|
|
85743
|
+
witnessUtxo: utxo.witnessUtxo,
|
|
85744
|
+
}));
|
|
85745
|
+
// psbt add outputs from accumulative outputs
|
|
85746
|
+
outputs.forEach((output) => {
|
|
85747
|
+
if (!output.address) {
|
|
85748
|
+
//an empty address means this is the change ddress
|
|
85749
|
+
output.address = sender;
|
|
85750
|
+
}
|
|
85751
|
+
if (!output.script) {
|
|
85752
|
+
psbt.addOutput(output);
|
|
85753
|
+
}
|
|
85754
|
+
else {
|
|
85755
|
+
//we need to add the compiled memo this way to
|
|
85756
|
+
//avoid dust error tx when accumulating memo output with 0 value
|
|
85757
|
+
if (compiledMemo) {
|
|
85758
|
+
psbt.addOutput({ script: compiledMemo, value: 0 });
|
|
86017
85759
|
}
|
|
86018
|
-
}
|
|
85760
|
+
}
|
|
86019
85761
|
});
|
|
86020
|
-
};
|
|
85762
|
+
return { psbt, utxos, inputs };
|
|
85763
|
+
});
|
|
86021
85764
|
/**
|
|
86022
85765
|
* Broadcast the transaction.
|
|
86023
85766
|
*
|
|
86024
85767
|
* @param {BroadcastTxParams} params The transaction broadcast options.
|
|
86025
85768
|
* @returns {TxHash} The transaction hash.
|
|
86026
85769
|
*/
|
|
86027
|
-
|
|
86028
|
-
|
|
86029
|
-
|
|
86030
|
-
return __generator(this, function (_b) {
|
|
86031
|
-
switch (_b.label) {
|
|
86032
|
-
case 0: return [4 /*yield*/, broadcastTx({ haskoinUrl: haskoinUrl, txHex: txHex })];
|
|
86033
|
-
case 1: return [2 /*return*/, _b.sent()];
|
|
86034
|
-
}
|
|
86035
|
-
});
|
|
86036
|
-
});
|
|
86037
|
-
};
|
|
85770
|
+
const broadcastTx$1 = ({ haskoinUrl, txHex }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85771
|
+
return yield broadcastTx({ haskoinUrl, txHex });
|
|
85772
|
+
});
|
|
86038
85773
|
/**
|
|
86039
85774
|
* Calculate fees based on fee rate and memo.
|
|
86040
85775
|
*
|
|
@@ -86042,9 +85777,9 @@ var broadcastTx$1 = function (_a) {
|
|
|
86042
85777
|
* @param {string} memo
|
|
86043
85778
|
* @returns {BaseAmount} The calculated fees based on fee rate and the memo.
|
|
86044
85779
|
*/
|
|
86045
|
-
|
|
86046
|
-
|
|
86047
|
-
|
|
85780
|
+
const calcFee = (feeRate, memo) => {
|
|
85781
|
+
const compiledMemo = memo ? compileMemo(memo) : null;
|
|
85782
|
+
const fee = getFee([], feeRate, compiledMemo);
|
|
86048
85783
|
return baseAmount(fee);
|
|
86049
85784
|
};
|
|
86050
85785
|
/**
|
|
@@ -86052,12 +85787,11 @@ var calcFee = function (feeRate, memo) {
|
|
|
86052
85787
|
*
|
|
86053
85788
|
* @returns {FeesWithRates} The default fees and rates.
|
|
86054
85789
|
*/
|
|
86055
|
-
|
|
86056
|
-
|
|
86057
|
-
var rates = __assign(__assign({}, standardFeeRates(20)), (_a = {}, _a[FeeOption.Fastest] = 50, _a));
|
|
85790
|
+
const getDefaultFeesWithRates = () => {
|
|
85791
|
+
const rates = Object.assign(Object.assign({}, standardFeeRates(20)), { [FeeOption.Fastest]: 50 });
|
|
86058
85792
|
return {
|
|
86059
85793
|
fees: calcFees(rates, calcFee),
|
|
86060
|
-
rates
|
|
85794
|
+
rates,
|
|
86061
85795
|
};
|
|
86062
85796
|
};
|
|
86063
85797
|
/**
|
|
@@ -86065,8 +85799,8 @@ var getDefaultFeesWithRates = function () {
|
|
|
86065
85799
|
*
|
|
86066
85800
|
* @returns {Fees} The default fees.
|
|
86067
85801
|
*/
|
|
86068
|
-
|
|
86069
|
-
|
|
85802
|
+
const getDefaultFees = () => {
|
|
85803
|
+
const { fees } = getDefaultFeesWithRates();
|
|
86070
85804
|
return fees;
|
|
86071
85805
|
};
|
|
86072
85806
|
/**
|
|
@@ -86076,7 +85810,7 @@ var getDefaultFees = function () {
|
|
|
86076
85810
|
* @returns {string} The address prefix based on the network.
|
|
86077
85811
|
*
|
|
86078
85812
|
**/
|
|
86079
|
-
|
|
85813
|
+
const getPrefix = (network) => {
|
|
86080
85814
|
switch (network) {
|
|
86081
85815
|
case Network.Mainnet:
|
|
86082
85816
|
case Network.Stagenet:
|
|
@@ -86089,30 +85823,29 @@ var getPrefix = function (network) {
|
|
|
86089
85823
|
/**
|
|
86090
85824
|
* Custom Bitcoin client
|
|
86091
85825
|
*/
|
|
86092
|
-
|
|
86093
|
-
__extends(Client, _super);
|
|
85826
|
+
class Client extends UTXOClient {
|
|
86094
85827
|
/**
|
|
86095
85828
|
* Constructor
|
|
86096
85829
|
* Client is initialised with network type
|
|
86097
85830
|
*
|
|
86098
85831
|
* @param {BitcoinClientParams} params
|
|
86099
85832
|
*/
|
|
86100
|
-
|
|
86101
|
-
|
|
86102
|
-
|
|
86103
|
-
|
|
86104
|
-
|
|
86105
|
-
|
|
86106
|
-
|
|
86107
|
-
|
|
86108
|
-
|
|
86109
|
-
|
|
86110
|
-
|
|
86111
|
-
|
|
86112
|
-
|
|
86113
|
-
|
|
86114
|
-
|
|
86115
|
-
|
|
85833
|
+
constructor({ network = Network.Testnet, feeBounds = {
|
|
85834
|
+
lower: LOWER_FEE_BOUND,
|
|
85835
|
+
upper: UPPER_FEE_BOUND,
|
|
85836
|
+
}, sochainUrl = 'https://sochain.com/api/v2', haskoinUrl = {
|
|
85837
|
+
[Network.Testnet]: 'https://api.haskoin.com/btctest',
|
|
85838
|
+
[Network.Mainnet]: 'https://api.haskoin.com/btc',
|
|
85839
|
+
[Network.Stagenet]: 'https://api.haskoin.com/btc',
|
|
85840
|
+
}, rootDerivationPaths = {
|
|
85841
|
+
[Network.Mainnet]: `84'/0'/0'/0/`,
|
|
85842
|
+
[Network.Testnet]: `84'/1'/0'/0/`,
|
|
85843
|
+
[Network.Stagenet]: `84'/0'/0'/0/`,
|
|
85844
|
+
}, phrase = '', }) {
|
|
85845
|
+
super(Chain.Bitcoin, { network, rootDerivationPaths, phrase, feeBounds });
|
|
85846
|
+
this.sochainUrl = '';
|
|
85847
|
+
this.setSochainUrl(sochainUrl);
|
|
85848
|
+
this.haskoinUrl = haskoinUrl;
|
|
86116
85849
|
}
|
|
86117
85850
|
/**
|
|
86118
85851
|
* Set/Update the sochain url.
|
|
@@ -86120,15 +85853,15 @@ var Client = /** @class */ (function (_super) {
|
|
|
86120
85853
|
* @param {string} url The new sochain url.
|
|
86121
85854
|
* @returns {void}
|
|
86122
85855
|
*/
|
|
86123
|
-
|
|
85856
|
+
setSochainUrl(url) {
|
|
86124
85857
|
this.sochainUrl = url;
|
|
86125
|
-
}
|
|
85858
|
+
}
|
|
86126
85859
|
/**
|
|
86127
85860
|
* Get the explorer url.
|
|
86128
85861
|
*
|
|
86129
85862
|
* @returns {string} The explorer url based on the network.
|
|
86130
85863
|
*/
|
|
86131
|
-
|
|
85864
|
+
getExplorerUrl() {
|
|
86132
85865
|
switch (this.network) {
|
|
86133
85866
|
case Network.Mainnet:
|
|
86134
85867
|
case Network.Stagenet:
|
|
@@ -86136,25 +85869,25 @@ var Client = /** @class */ (function (_super) {
|
|
|
86136
85869
|
case Network.Testnet:
|
|
86137
85870
|
return 'https://blockstream.info/testnet';
|
|
86138
85871
|
}
|
|
86139
|
-
}
|
|
85872
|
+
}
|
|
86140
85873
|
/**
|
|
86141
85874
|
* Get the explorer url for the given address.
|
|
86142
85875
|
*
|
|
86143
85876
|
* @param {Address} address
|
|
86144
85877
|
* @returns {string} The explorer url for the given address based on the network.
|
|
86145
85878
|
*/
|
|
86146
|
-
|
|
86147
|
-
return this.getExplorerUrl()
|
|
86148
|
-
}
|
|
85879
|
+
getExplorerAddressUrl(address) {
|
|
85880
|
+
return `${this.getExplorerUrl()}/address/${address}`;
|
|
85881
|
+
}
|
|
86149
85882
|
/**
|
|
86150
85883
|
* Get the explorer url for the given transaction id.
|
|
86151
85884
|
*
|
|
86152
85885
|
* @param {string} txID The transaction id
|
|
86153
85886
|
* @returns {string} The explorer url for the given transaction id based on the network.
|
|
86154
85887
|
*/
|
|
86155
|
-
|
|
86156
|
-
return this.getExplorerUrl()
|
|
86157
|
-
}
|
|
85888
|
+
getExplorerTxUrl(txID) {
|
|
85889
|
+
return `${this.getExplorerUrl()}/tx/${txID}`;
|
|
85890
|
+
}
|
|
86158
85891
|
/**
|
|
86159
85892
|
* Get the current address.
|
|
86160
85893
|
*
|
|
@@ -86166,25 +85899,24 @@ var Client = /** @class */ (function (_super) {
|
|
|
86166
85899
|
* @throws {"Phrase must be provided"} Thrown if phrase has not been set before.
|
|
86167
85900
|
* @throws {"Address not defined"} Thrown if failed creating account from phrase.
|
|
86168
85901
|
*/
|
|
86169
|
-
|
|
86170
|
-
if (index === void 0) { index = 0; }
|
|
85902
|
+
getAddress(index = 0) {
|
|
86171
85903
|
if (index < 0) {
|
|
86172
85904
|
throw new Error('index must be greater than zero');
|
|
86173
85905
|
}
|
|
86174
85906
|
if (this.phrase) {
|
|
86175
|
-
|
|
86176
|
-
|
|
86177
|
-
|
|
85907
|
+
const btcNetwork$1 = btcNetwork(this.network);
|
|
85908
|
+
const btcKeys = this.getBtcKeys(this.phrase, index);
|
|
85909
|
+
const { address } = src_6$1.p2wpkh({
|
|
86178
85910
|
pubkey: btcKeys.publicKey,
|
|
86179
85911
|
network: btcNetwork$1,
|
|
86180
|
-
})
|
|
85912
|
+
});
|
|
86181
85913
|
if (!address) {
|
|
86182
85914
|
throw new Error('Address not defined');
|
|
86183
85915
|
}
|
|
86184
85916
|
return address;
|
|
86185
85917
|
}
|
|
86186
85918
|
throw new Error('Phrase must be provided');
|
|
86187
|
-
}
|
|
85919
|
+
}
|
|
86188
85920
|
/**
|
|
86189
85921
|
* @private
|
|
86190
85922
|
* Get private key.
|
|
@@ -86196,25 +85928,24 @@ var Client = /** @class */ (function (_super) {
|
|
|
86196
85928
|
*
|
|
86197
85929
|
* @throws {"Could not get private key from phrase"} Throws an error if failed creating BTC keys from the given phrase
|
|
86198
85930
|
* */
|
|
86199
|
-
|
|
86200
|
-
|
|
86201
|
-
|
|
86202
|
-
|
|
86203
|
-
var master = src_1$2.fromSeed(seed, btcNetwork$1).derivePath(this.getFullDerivationPath(index));
|
|
85931
|
+
getBtcKeys(phrase, index = 0) {
|
|
85932
|
+
const btcNetwork$1 = btcNetwork(this.network);
|
|
85933
|
+
const seed = getSeed(phrase);
|
|
85934
|
+
const master = src_1$2.fromSeed(seed, btcNetwork$1).derivePath(this.getFullDerivationPath(index));
|
|
86204
85935
|
if (!master.privateKey) {
|
|
86205
85936
|
throw new Error('Could not get private key from phrase');
|
|
86206
85937
|
}
|
|
86207
85938
|
return src_4$2.fromPrivateKey(master.privateKey, { network: btcNetwork$1 });
|
|
86208
|
-
}
|
|
85939
|
+
}
|
|
86209
85940
|
/**
|
|
86210
85941
|
* Validate the given address.
|
|
86211
85942
|
*
|
|
86212
85943
|
* @param {Address} address
|
|
86213
85944
|
* @returns {boolean} `true` or `false`
|
|
86214
85945
|
*/
|
|
86215
|
-
|
|
85946
|
+
validateAddress(address) {
|
|
86216
85947
|
return validateAddress(address, this.network);
|
|
86217
|
-
}
|
|
85948
|
+
}
|
|
86218
85949
|
/**
|
|
86219
85950
|
* Gets BTC balances of a given address.
|
|
86220
85951
|
*
|
|
@@ -86226,21 +85957,19 @@ var Client = /** @class */ (function (_super) {
|
|
|
86226
85957
|
*/
|
|
86227
85958
|
// TODO (@xchain-team|@veado) Change params to be an object to be extendable more easily
|
|
86228
85959
|
// see changes for `xchain-bitcoin` https://github.com/xchainjs/xchainjs-lib/pull/490
|
|
86229
|
-
|
|
86230
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
86231
|
-
return
|
|
86232
|
-
|
|
86233
|
-
|
|
86234
|
-
|
|
86235
|
-
|
|
86236
|
-
|
|
86237
|
-
|
|
86238
|
-
|
|
86239
|
-
confirmedOnly: !!confirmedOnly,
|
|
86240
|
-
})];
|
|
85960
|
+
getBalance(address, _assets /* not used */, confirmedOnly) {
|
|
85961
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
85962
|
+
return getBalance$2({
|
|
85963
|
+
params: {
|
|
85964
|
+
sochainUrl: this.sochainUrl,
|
|
85965
|
+
network: this.network,
|
|
85966
|
+
address: address,
|
|
85967
|
+
},
|
|
85968
|
+
haskoinUrl: this.haskoinUrl[this.network],
|
|
85969
|
+
confirmedOnly: !!confirmedOnly,
|
|
86241
85970
|
});
|
|
86242
85971
|
});
|
|
86243
|
-
}
|
|
85972
|
+
}
|
|
86244
85973
|
/**
|
|
86245
85974
|
* Get transaction history of a given address with pagination options.
|
|
86246
85975
|
* By default it will return the transaction history of the current wallet.
|
|
@@ -86248,155 +85977,142 @@ var Client = /** @class */ (function (_super) {
|
|
|
86248
85977
|
* @param {TxHistoryParams} params The options to get transaction history. (optional)
|
|
86249
85978
|
* @returns {TxsPage} The transaction history.
|
|
86250
85979
|
*/
|
|
86251
|
-
|
|
85980
|
+
getTransactions(params) {
|
|
86252
85981
|
var _a;
|
|
86253
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
86254
|
-
|
|
86255
|
-
|
|
86256
|
-
|
|
86257
|
-
|
|
86258
|
-
|
|
86259
|
-
|
|
86260
|
-
|
|
86261
|
-
address: (params === null || params === void 0 ? void 0 : params.address) + '',
|
|
86262
|
-
sochainUrl: this.sochainUrl,
|
|
86263
|
-
network: this.network,
|
|
86264
|
-
})];
|
|
86265
|
-
case 1:
|
|
86266
|
-
response = _b.sent();
|
|
86267
|
-
total = response.txs.length;
|
|
86268
|
-
transactions = [];
|
|
86269
|
-
txs = response.txs.filter(function (_, index) { return offset <= index && index < offset + limit; });
|
|
86270
|
-
_i = 0, txs_1 = txs;
|
|
86271
|
-
_b.label = 2;
|
|
86272
|
-
case 2:
|
|
86273
|
-
if (!(_i < txs_1.length)) return [3 /*break*/, 5];
|
|
86274
|
-
txItem = txs_1[_i];
|
|
86275
|
-
return [4 /*yield*/, getTx({
|
|
86276
|
-
sochainUrl: this.sochainUrl,
|
|
86277
|
-
network: this.network,
|
|
86278
|
-
hash: txItem.txid,
|
|
86279
|
-
})];
|
|
86280
|
-
case 3:
|
|
86281
|
-
rawTx = _b.sent();
|
|
86282
|
-
tx = {
|
|
86283
|
-
asset: AssetBTC,
|
|
86284
|
-
from: rawTx.inputs.map(function (i) { return ({
|
|
86285
|
-
from: i.address,
|
|
86286
|
-
amount: assetToBase(assetAmount(i.value, BTC_DECIMAL)),
|
|
86287
|
-
}); }),
|
|
86288
|
-
to: rawTx.outputs
|
|
86289
|
-
.filter(function (i) { return i.type !== 'nulldata'; })
|
|
86290
|
-
.map(function (i) { return ({ to: i.address, amount: assetToBase(assetAmount(i.value, BTC_DECIMAL)) }); }),
|
|
86291
|
-
date: new Date(rawTx.time * 1000),
|
|
86292
|
-
type: TxType.Transfer,
|
|
86293
|
-
hash: rawTx.txid,
|
|
86294
|
-
};
|
|
86295
|
-
transactions.push(tx);
|
|
86296
|
-
_b.label = 4;
|
|
86297
|
-
case 4:
|
|
86298
|
-
_i++;
|
|
86299
|
-
return [3 /*break*/, 2];
|
|
86300
|
-
case 5:
|
|
86301
|
-
result = {
|
|
86302
|
-
total: total,
|
|
86303
|
-
txs: transactions,
|
|
86304
|
-
};
|
|
86305
|
-
return [2 /*return*/, result];
|
|
86306
|
-
}
|
|
85982
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
85983
|
+
// Sochain API doesn't have pagination parameter
|
|
85984
|
+
const offset = (_a = params === null || params === void 0 ? void 0 : params.offset) !== null && _a !== void 0 ? _a : 0;
|
|
85985
|
+
const limit = (params === null || params === void 0 ? void 0 : params.limit) || 10;
|
|
85986
|
+
const response = yield getAddress({
|
|
85987
|
+
address: (params === null || params === void 0 ? void 0 : params.address) + '',
|
|
85988
|
+
sochainUrl: this.sochainUrl,
|
|
85989
|
+
network: this.network,
|
|
86307
85990
|
});
|
|
85991
|
+
const total = response.txs.length;
|
|
85992
|
+
const transactions = [];
|
|
85993
|
+
const txs = response.txs.filter((_, index) => offset <= index && index < offset + limit);
|
|
85994
|
+
for (const txItem of txs) {
|
|
85995
|
+
const rawTx = yield getTx({
|
|
85996
|
+
sochainUrl: this.sochainUrl,
|
|
85997
|
+
network: this.network,
|
|
85998
|
+
hash: txItem.txid,
|
|
85999
|
+
});
|
|
86000
|
+
const tx = {
|
|
86001
|
+
asset: AssetBTC,
|
|
86002
|
+
from: rawTx.inputs.map((i) => ({
|
|
86003
|
+
from: i.address,
|
|
86004
|
+
amount: assetToBase(assetAmount(i.value, BTC_DECIMAL)),
|
|
86005
|
+
})),
|
|
86006
|
+
to: rawTx.outputs
|
|
86007
|
+
.filter((i) => i.type !== 'nulldata')
|
|
86008
|
+
.map((i) => ({ to: i.address, amount: assetToBase(assetAmount(i.value, BTC_DECIMAL)) })),
|
|
86009
|
+
date: new Date(rawTx.time * 1000),
|
|
86010
|
+
type: TxType.Transfer,
|
|
86011
|
+
hash: rawTx.txid,
|
|
86012
|
+
};
|
|
86013
|
+
transactions.push(tx);
|
|
86014
|
+
}
|
|
86015
|
+
const result = {
|
|
86016
|
+
total,
|
|
86017
|
+
txs: transactions,
|
|
86018
|
+
};
|
|
86019
|
+
return result;
|
|
86308
86020
|
});
|
|
86309
|
-
}
|
|
86021
|
+
}
|
|
86310
86022
|
/**
|
|
86311
86023
|
* Get the transaction details of a given transaction id.
|
|
86312
86024
|
*
|
|
86313
86025
|
* @param {string} txId The transaction id.
|
|
86314
86026
|
* @returns {Tx} The transaction details of the given transaction id.
|
|
86315
86027
|
*/
|
|
86316
|
-
|
|
86317
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
86318
|
-
|
|
86319
|
-
|
|
86320
|
-
|
|
86321
|
-
|
|
86322
|
-
sochainUrl: this.sochainUrl,
|
|
86323
|
-
network: this.network,
|
|
86324
|
-
hash: txId,
|
|
86325
|
-
})];
|
|
86326
|
-
case 1:
|
|
86327
|
-
rawTx = _a.sent();
|
|
86328
|
-
return [2 /*return*/, {
|
|
86329
|
-
asset: AssetBTC,
|
|
86330
|
-
from: rawTx.inputs.map(function (i) { return ({
|
|
86331
|
-
from: i.address,
|
|
86332
|
-
amount: assetToBase(assetAmount(i.value, BTC_DECIMAL)),
|
|
86333
|
-
}); }),
|
|
86334
|
-
to: rawTx.outputs.map(function (i) { return ({ to: i.address, amount: assetToBase(assetAmount(i.value, BTC_DECIMAL)) }); }),
|
|
86335
|
-
date: new Date(rawTx.time * 1000),
|
|
86336
|
-
type: TxType.Transfer,
|
|
86337
|
-
hash: rawTx.txid,
|
|
86338
|
-
}];
|
|
86339
|
-
}
|
|
86028
|
+
getTransactionData(txId) {
|
|
86029
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
86030
|
+
const rawTx = yield getTx({
|
|
86031
|
+
sochainUrl: this.sochainUrl,
|
|
86032
|
+
network: this.network,
|
|
86033
|
+
hash: txId,
|
|
86340
86034
|
});
|
|
86035
|
+
return {
|
|
86036
|
+
asset: AssetBTC,
|
|
86037
|
+
from: rawTx.inputs.map((i) => ({
|
|
86038
|
+
from: i.address,
|
|
86039
|
+
amount: assetToBase(assetAmount(i.value, BTC_DECIMAL)),
|
|
86040
|
+
})),
|
|
86041
|
+
to: rawTx.outputs.map((i) => ({ to: i.address, amount: assetToBase(assetAmount(i.value, BTC_DECIMAL)) })),
|
|
86042
|
+
date: new Date(rawTx.time * 1000),
|
|
86043
|
+
type: TxType.Transfer,
|
|
86044
|
+
hash: rawTx.txid,
|
|
86045
|
+
};
|
|
86341
86046
|
});
|
|
86342
|
-
}
|
|
86343
|
-
|
|
86344
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
86345
|
-
return
|
|
86346
|
-
switch (_a.label) {
|
|
86347
|
-
case 0: return [4 /*yield*/, getSuggestedTxFee()];
|
|
86348
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
86349
|
-
}
|
|
86350
|
-
});
|
|
86047
|
+
}
|
|
86048
|
+
getSuggestedFeeRate() {
|
|
86049
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
86050
|
+
return yield getSuggestedTxFee();
|
|
86351
86051
|
});
|
|
86352
|
-
}
|
|
86353
|
-
|
|
86354
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
86355
|
-
return
|
|
86356
|
-
return [2 /*return*/, calcFee(feeRate, memo)];
|
|
86357
|
-
});
|
|
86052
|
+
}
|
|
86053
|
+
calcFee(feeRate, memo) {
|
|
86054
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
86055
|
+
return calcFee(feeRate, memo);
|
|
86358
86056
|
});
|
|
86359
|
-
}
|
|
86057
|
+
}
|
|
86360
86058
|
/**
|
|
86361
86059
|
* Transfer BTC.
|
|
86362
86060
|
*
|
|
86363
86061
|
* @param {TxParams&FeeRate} params The transfer options.
|
|
86364
86062
|
* @returns {TxHash} The transaction hash.
|
|
86063
|
+
*
|
|
86064
|
+
* @throws {"memo too long"} Thrown if memo longer than 80 chars.
|
|
86365
86065
|
*/
|
|
86366
|
-
|
|
86367
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
86368
|
-
|
|
86369
|
-
|
|
86370
|
-
|
|
86371
|
-
|
|
86372
|
-
|
|
86373
|
-
|
|
86374
|
-
|
|
86375
|
-
|
|
86376
|
-
|
|
86377
|
-
|
|
86378
|
-
|
|
86379
|
-
|
|
86380
|
-
|
|
86381
|
-
|
|
86382
|
-
|
|
86383
|
-
|
|
86384
|
-
|
|
86385
|
-
|
|
86386
|
-
|
|
86387
|
-
|
|
86388
|
-
|
|
86389
|
-
|
|
86390
|
-
|
|
86391
|
-
|
|
86392
|
-
|
|
86393
|
-
|
|
86394
|
-
|
|
86066
|
+
transfer(params) {
|
|
86067
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
86068
|
+
const fromAddressIndex = (params === null || params === void 0 ? void 0 : params.walletIndex) || 0;
|
|
86069
|
+
// set the default fee rate to `fast`
|
|
86070
|
+
const feeRate = params.feeRate || (yield this.getFeeRates())[FeeOption.Fast];
|
|
86071
|
+
checkFeeBounds(this.feeBounds, feeRate);
|
|
86072
|
+
/**
|
|
86073
|
+
* do not spend pending UTXOs when adding a memo
|
|
86074
|
+
* https://github.com/xchainjs/xchainjs-lib/issues/330
|
|
86075
|
+
*/
|
|
86076
|
+
const spendPendingUTXO = !params.memo;
|
|
86077
|
+
const haskoinUrl = this.haskoinUrl[this.network];
|
|
86078
|
+
const { psbt } = yield buildTx(Object.assign(Object.assign({}, params), { feeRate, sender: this.getAddress(fromAddressIndex), sochainUrl: this.sochainUrl, haskoinUrl, network: this.network, spendPendingUTXO }));
|
|
86079
|
+
const btcKeys = this.getBtcKeys(this.phrase, fromAddressIndex);
|
|
86080
|
+
psbt.signAllInputs(btcKeys); // Sign all inputs
|
|
86081
|
+
psbt.finalizeAllInputs(); // Finalise inputs
|
|
86082
|
+
const txHex = psbt.extractTransaction().toHex(); // TX extracted and formatted to hex
|
|
86083
|
+
return yield broadcastTx$1({ txHex, haskoinUrl });
|
|
86084
|
+
});
|
|
86085
|
+
}
|
|
86086
|
+
/**
|
|
86087
|
+
* Transaction to THORChain inbound address.
|
|
86088
|
+
*
|
|
86089
|
+
* @param {DepositParams} params The transaction options.
|
|
86090
|
+
* @returns {TxHash} The transaction hash.
|
|
86091
|
+
*
|
|
86092
|
+
* @throws {"halted chain"} Thrown if chain is halted.
|
|
86093
|
+
* @throws {"halted trading"} Thrown if trading is halted.
|
|
86094
|
+
* @throws {"memo too long"} Thrown if memo longer than 80 chars.
|
|
86095
|
+
*/
|
|
86096
|
+
deposit({ walletIndex = 0, asset = AssetBTC, amount, memo }) {
|
|
86097
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
86098
|
+
const inboundDetails = yield getInboundDetails(asset.chain, this.network);
|
|
86099
|
+
if (inboundDetails.haltedChain) {
|
|
86100
|
+
throw new Error(`Halted chain for ${assetToString(asset)}`);
|
|
86101
|
+
}
|
|
86102
|
+
if (inboundDetails.haltedTrading) {
|
|
86103
|
+
throw new Error(`Halted trading for ${assetToString(asset)}`);
|
|
86104
|
+
}
|
|
86105
|
+
const txHash = yield this.transfer({
|
|
86106
|
+
walletIndex,
|
|
86107
|
+
asset,
|
|
86108
|
+
amount,
|
|
86109
|
+
recipient: inboundDetails.vault,
|
|
86110
|
+
memo,
|
|
86395
86111
|
});
|
|
86112
|
+
return txHash;
|
|
86396
86113
|
});
|
|
86397
|
-
}
|
|
86398
|
-
|
|
86399
|
-
}(UTXOClient));
|
|
86114
|
+
}
|
|
86115
|
+
}
|
|
86400
86116
|
|
|
86401
|
-
export { BTC_DECIMAL, Client, MIN_TX_FEE, broadcastTx$1 as broadcastTx, buildTx, calcFee, getBalance$2 as getBalance, getDefaultFees, getDefaultFeesWithRates, getPrefix, scanUTXOs, validateAddress };
|
|
86117
|
+
export { BTC_DECIMAL, Client, LOWER_FEE_BOUND, MIN_TX_FEE, UPPER_FEE_BOUND, broadcastTx$1 as broadcastTx, buildTx, calcFee, getBalance$2 as getBalance, getDefaultFees, getDefaultFeesWithRates, getPrefix, scanUTXOs, validateAddress };
|
|
86402
86118
|
//# sourceMappingURL=index.esm.js.map
|