@xchainjs/xchain-doge 0.5.6 → 0.5.8

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/lib/client.d.ts CHANGED
@@ -3,6 +3,7 @@ import { Address } from '@xchainjs/xchain-util';
3
3
  export declare type DogecoinClientParams = XChainClientParams & {
4
4
  sochainUrl?: string;
5
5
  blockcypherUrl?: string;
6
+ sochainApiKey: string;
6
7
  };
7
8
  /**
8
9
  * Custom Dogecoin client
@@ -10,6 +11,7 @@ export declare type DogecoinClientParams = XChainClientParams & {
10
11
  declare class Client extends UTXOClient {
11
12
  private sochainUrl;
12
13
  private blockcypherUrl;
14
+ private sochainApiKey;
13
15
  /**
14
16
  * Constructor
15
17
  * Client is initialised with network type
@@ -17,7 +19,7 @@ declare class Client extends UTXOClient {
17
19
  *
18
20
  * @param {DogecoinClientParams} params
19
21
  */
20
- constructor({ network, feeBounds, sochainUrl, blockcypherUrl, phrase, rootDerivationPaths, }: DogecoinClientParams);
22
+ constructor({ network, feeBounds, sochainApiKey, sochainUrl, blockcypherUrl, phrase, rootDerivationPaths, }: DogecoinClientParams);
21
23
  /**
22
24
  * Set/Update the sochain url.
23
25
  *
@@ -90,6 +92,14 @@ declare class Client extends UTXOClient {
90
92
  * @returns {Balance[]} The Doge balance of the address.
91
93
  */
92
94
  getBalance(address: Address): Promise<Balance[]>;
95
+ /**
96
+ * helper function tto limit adding to an array
97
+ *
98
+ * @param arr array to be added to
99
+ * @param toAdd elements to add
100
+ * @param limit do not add more than this limit
101
+ */
102
+ private addArrayUpToLimit;
93
103
  /**
94
104
  * Get transaction history of a given address with pagination options.
95
105
  * By default it will return the transaction history of the current wallet.
package/lib/const.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Asset, Chain } from '@xchainjs/xchain-util';
1
+ import { Asset } from '@xchainjs/xchain-util';
2
2
  /**
3
3
  * Minimum transaction fee
4
4
  * 100000 satoshi/kB (similar to current `minrelaytxfee`)
@@ -12,7 +12,7 @@ export declare const UPPER_FEE_BOUND = 20000000;
12
12
  * Chain identifier for Dogecoin
13
13
  *
14
14
  */
15
- export declare const DOGEChain: Chain;
15
+ export declare const DOGEChain: "DOGE";
16
16
  /**
17
17
  * Base "chain" asset on dogecoin
18
18
  *
package/lib/index.esm.js CHANGED
@@ -84951,7 +84951,6 @@ const AssetDOGE = { chain: DOGEChain, symbol: 'DOGE', ticker: 'DOGE', synth: fal
84951
84951
  const toSochainNetwork = (network) => {
84952
84952
  switch (network) {
84953
84953
  case Network.Mainnet:
84954
- return 'DOGE';
84955
84954
  case Network.Stagenet:
84956
84955
  return 'DOGE';
84957
84956
  case Network.Testnet:
@@ -84959,26 +84958,26 @@ const toSochainNetwork = (network) => {
84959
84958
  }
84960
84959
  };
84961
84960
  const getSendTxUrl$1 = ({ sochainUrl, network }) => {
84962
- return `${sochainUrl}/send_tx/${toSochainNetwork(network)}`;
84961
+ return `${sochainUrl}/broadcast_transaction/${toSochainNetwork(network)}`;
84963
84962
  };
84964
84963
  /**
84965
- * Get address information.
84964
+ * Get transaction by hash.
84966
84965
  *
84967
- * @see https://sochain.com/api#get-display-data-address
84966
+ * @see https://sochain.com/api#get-tx
84968
84967
  *
84969
84968
  * @param {string} sochainUrl The sochain node url.
84970
- * @param {string} network
84971
- * @param {string} address
84972
- * @returns {DogeAddressDTO}
84969
+ * @param {string} network network id
84970
+ * @param {string} hash The transaction hash.
84971
+ * @returns {Transactions}
84973
84972
  */
84974
- const getAddress = ({ sochainUrl, network, address, }) => __awaiter(void 0, void 0, void 0, function* () {
84975
- const url = `${sochainUrl}/address/${toSochainNetwork(network)}/${address}`;
84976
- const response = yield axios.get(url);
84977
- const addressResponse = response.data;
84978
- return addressResponse.data;
84973
+ const getTx = ({ apiKey, sochainUrl, network, hash }) => __awaiter(void 0, void 0, void 0, function* () {
84974
+ const url = `${sochainUrl}/transaction/${toSochainNetwork(network)}/${hash}`;
84975
+ const response = yield axios.get(url, { headers: { 'API-KEY': apiKey } });
84976
+ const tx = response.data;
84977
+ return tx.data;
84979
84978
  });
84980
84979
  /**
84981
- * Get transaction by hash.
84980
+ * Get transactions
84982
84981
  *
84983
84982
  * @see https://sochain.com/api#get-tx
84984
84983
  *
@@ -84987,11 +84986,11 @@ const getAddress = ({ sochainUrl, network, address, }) => __awaiter(void 0, void
84987
84986
  * @param {string} hash The transaction hash.
84988
84987
  * @returns {Transactions}
84989
84988
  */
84990
- const getTx = ({ sochainUrl, network, hash }) => __awaiter(void 0, void 0, void 0, function* () {
84991
- const url = `${sochainUrl}/get_tx/${toSochainNetwork(network)}/${hash}`;
84992
- const response = yield axios.get(url);
84993
- const tx = response.data;
84994
- return tx.data;
84989
+ const getTxs = ({ apiKey, address, sochainUrl, network, page, }) => __awaiter(void 0, void 0, void 0, function* () {
84990
+ const url = `${sochainUrl}/transactions/${toSochainNetwork(network)}/${address}/${page}`; //TODO support paging
84991
+ const response = yield axios.get(url, { headers: { 'API-KEY': apiKey } });
84992
+ const txs = response.data;
84993
+ return txs.data;
84995
84994
  });
84996
84995
  /**
84997
84996
  * Get address balance.
@@ -85003,12 +85002,12 @@ const getTx = ({ sochainUrl, network, hash }) => __awaiter(void 0, void 0, void
85003
85002
  * @param {string} address
85004
85003
  * @returns {number}
85005
85004
  */
85006
- const getBalance = ({ sochainUrl, network, address, }) => __awaiter(void 0, void 0, void 0, function* () {
85007
- const url = `${sochainUrl}/get_address_balance/${toSochainNetwork(network)}/${address}`;
85008
- const response = yield axios.get(url);
85005
+ const getBalance = ({ apiKey, sochainUrl, network, address }) => __awaiter(void 0, void 0, void 0, function* () {
85006
+ const url = `${sochainUrl}/balance/${toSochainNetwork(network)}/${address}`;
85007
+ const response = yield axios.get(url, { headers: { 'API-KEY': apiKey } });
85009
85008
  const balanceResponse = response.data;
85010
- const confirmed = assetAmount(balanceResponse.data.confirmed_balance, DOGE_DECIMAL);
85011
- const unconfirmed = assetAmount(balanceResponse.data.unconfirmed_balance, DOGE_DECIMAL);
85009
+ const confirmed = assetAmount(balanceResponse.data.confirmed, DOGE_DECIMAL);
85010
+ const unconfirmed = assetAmount(balanceResponse.data.unconfirmed, DOGE_DECIMAL);
85012
85011
  const netAmt = confirmed.amount().plus(unconfirmed.amount());
85013
85012
  const result = assetToBase(assetAmount(netAmt, DOGE_DECIMAL));
85014
85013
  return result;
@@ -85023,24 +85022,19 @@ const getBalance = ({ sochainUrl, network, address, }) => __awaiter(void 0, void
85023
85022
  * @param {string} address
85024
85023
  * @returns {DogeAddressUTXO[]}
85025
85024
  */
85026
- const getUnspentTxs = ({ sochainUrl, network, address, startingFromTxId, }) => __awaiter(void 0, void 0, void 0, function* () {
85027
- let resp = null;
85028
- if (startingFromTxId) {
85029
- resp = yield axios.get(`${sochainUrl}/get_tx_unspent/${toSochainNetwork(network)}/${address}/${startingFromTxId}`);
85030
- }
85031
- else {
85032
- resp = yield axios.get(`${sochainUrl}/get_tx_unspent/${toSochainNetwork(network)}/${address}`);
85033
- }
85025
+ const getUnspentTxs = ({ apiKey, sochainUrl, network, address, page, }) => __awaiter(void 0, void 0, void 0, function* () {
85026
+ const url = [sochainUrl, 'unspent_outputs', toSochainNetwork(network), address, page].filter((v) => !!v).join('/');
85027
+ const resp = yield axios.get(url, { headers: { 'API-KEY': apiKey } });
85034
85028
  const response = resp.data;
85035
- const txs = response.data.txs;
85036
- if (txs.length === 100) {
85029
+ const txs = response.data.outputs;
85030
+ if (txs.length === 10) {
85037
85031
  //fetch the next batch
85038
- const lastTxId = txs[99].txid;
85039
85032
  const nextBatch = yield getUnspentTxs({
85033
+ apiKey,
85040
85034
  sochainUrl,
85041
85035
  network,
85042
85036
  address,
85043
- startingFromTxId: lastTxId,
85037
+ page: page + 1,
85044
85038
  });
85045
85039
  return txs.concat(nextBatch);
85046
85040
  }
@@ -86504,43 +86498,52 @@ const validateAddress = (address, network) => {
86504
86498
  }
86505
86499
  };
86506
86500
  // Stores list of txHex in memory to avoid requesting same data
86507
- const txHexMap = {};
86508
- /**
86509
- * Helper to get `hex` of `Tx`
86510
- *
86511
- * It will try to get it from cache before requesting it from Sochain
86512
- */
86513
- const getTxHex = ({ txHash, sochainUrl, network, }) => __awaiter(void 0, void 0, void 0, function* () {
86514
- // try to get hex from cache
86515
- const txHex = txHexMap[txHash];
86516
- if (!!txHex)
86517
- return txHex;
86518
- // or get it from Sochain
86519
- const { tx_hex } = yield getTx({ hash: txHash, sochainUrl, network });
86520
- // cache it
86521
- txHexMap[txHash] = tx_hex;
86522
- return tx_hex;
86523
- });
86501
+ // const txHexMap: Record<TxHash, string> = {}
86502
+ // /**
86503
+ // * Helper to get `hex` of `Tx`
86504
+ // *
86505
+ // * It will try to get it from cache before requesting it from Sochain
86506
+ // */
86507
+ // const getTxHex = async ({
86508
+ // apiKey,
86509
+ // txHash,
86510
+ // sochainUrl,
86511
+ // network,
86512
+ // }: {
86513
+ // apiKey: string
86514
+ // sochainUrl: string
86515
+ // txHash: TxHash
86516
+ // network: Network
86517
+ // }): Promise<string> => {
86518
+ // // try to get hex from cache
86519
+ // const txHex = txHexMap[txHash]
86520
+ // if (!!txHex) return txHex
86521
+ // // or get it from Sochain
86522
+ // const { tx_hex } = await sochain.getTx({ apiKey, hash: txHash, sochainUrl, network })
86523
+ // // cache it
86524
+ // txHexMap[txHash] = tx_hex
86525
+ // return tx_hex
86526
+ // }
86524
86527
  /**
86525
86528
  * Scan UTXOs from sochain.
86526
86529
  *
86527
86530
  * @param params
86528
86531
  * @returns {UTXO[]} The UTXOs of the given address.
86529
86532
  */
86530
- const scanUTXOs = ({ sochainUrl, network, address, withTxHex, }) => __awaiter(void 0, void 0, void 0, function* () {
86533
+ const scanUTXOs = ({ apiKey, sochainUrl, network, address, }) => __awaiter(void 0, void 0, void 0, function* () {
86531
86534
  const utxos = yield getUnspentTxs({
86535
+ apiKey,
86532
86536
  sochainUrl,
86533
86537
  network,
86534
86538
  address,
86539
+ page: 1,
86535
86540
  });
86536
- return yield Promise.all(utxos.map((utxo) => __awaiter(void 0, void 0, void 0, function* () {
86537
- return ({
86538
- hash: utxo.txid,
86539
- index: utxo.output_no,
86540
- value: assetToBase(assetAmount(utxo.value, DOGE_DECIMAL)).amount().toNumber(),
86541
- txHex: withTxHex ? yield getTxHex({ txHash: utxo.txid, sochainUrl, network }) : undefined,
86542
- });
86543
- })));
86541
+ return utxos.map((utxo) => ({
86542
+ hash: utxo.hash,
86543
+ index: utxo.index,
86544
+ value: assetToBase(assetAmount(utxo.value, DOGE_DECIMAL)).amount().toNumber(),
86545
+ txHex: utxo.tx_hex,
86546
+ }));
86544
86547
  });
86545
86548
  /**
86546
86549
  * Build transcation.
@@ -86548,10 +86551,10 @@ const scanUTXOs = ({ sochainUrl, network, address, withTxHex, }) => __awaiter(vo
86548
86551
  * @param {BuildParams} params The transaction build options.
86549
86552
  * @returns {Transaction}
86550
86553
  */
86551
- const buildTx = ({ amount, recipient, memo, feeRate, sender, network, sochainUrl, withTxHex = false, }) => __awaiter(void 0, void 0, void 0, function* () {
86554
+ const buildTx = ({ apiKey, amount, recipient, memo, feeRate, sender, network, sochainUrl, }) => __awaiter(void 0, void 0, void 0, function* () {
86552
86555
  if (!validateAddress(recipient, network))
86553
86556
  throw new Error('Invalid address');
86554
- const utxos = yield scanUTXOs({ sochainUrl, network, address: sender, withTxHex });
86557
+ const utxos = yield scanUTXOs({ apiKey, sochainUrl, network, address: sender });
86555
86558
  if (utxos.length === 0)
86556
86559
  throw new Error('No utxos to send');
86557
86560
  const feeRateWhole = Number(feeRate.toFixed(0));
@@ -86576,12 +86579,12 @@ const buildTx = ({ amount, recipient, memo, feeRate, sender, network, sochainUrl
86576
86579
  // https://thornode.ninerealms.com/thorchain/inbound_addresses?height=7526662 (09-27-2022)
86577
86580
  // For now we increase it by 10x
86578
86581
  psbt.setMaximumFeeRate(7500000);
86579
- const params = { sochainUrl, network, address: sender };
86582
+ // const params = { sochainUrl, network, address: sender }
86580
86583
  for (const utxo of inputs) {
86581
86584
  psbt.addInput({
86582
86585
  hash: utxo.hash,
86583
86586
  index: utxo.index,
86584
- nonWitnessUtxo: Buffer.from((yield getTx(Object.assign({ hash: utxo.hash }, params))).tx_hex, 'hex'),
86587
+ nonWitnessUtxo: Buffer.from(utxo.txHex, 'hex'),
86585
86588
  });
86586
86589
  }
86587
86590
  // Outputs
@@ -86681,7 +86684,7 @@ class Client extends UTXOClient {
86681
86684
  constructor({ network = Network.Mainnet, feeBounds = {
86682
86685
  lower: LOWER_FEE_BOUND,
86683
86686
  upper: UPPER_FEE_BOUND,
86684
- }, sochainUrl = 'https://sochain.com/api/v2', blockcypherUrl = 'https://api.blockcypher.com/v1', phrase, rootDerivationPaths = {
86687
+ }, sochainApiKey, sochainUrl = 'https://sochain.com/api/v3', blockcypherUrl = 'https://api.blockcypher.com/v1', phrase, rootDerivationPaths = {
86685
86688
  [Network.Mainnet]: `m/44'/3'/0'/0/`,
86686
86689
  [Network.Stagenet]: `m/44'/3'/0'/0/`,
86687
86690
  [Network.Testnet]: `m/44'/1'/0'/0/`,
@@ -86691,6 +86694,7 @@ class Client extends UTXOClient {
86691
86694
  this.blockcypherUrl = '';
86692
86695
  this.setSochainUrl(sochainUrl);
86693
86696
  this.setBlockcypherUrl(blockcypherUrl);
86697
+ this.sochainApiKey = sochainApiKey;
86694
86698
  }
86695
86699
  /**
86696
86700
  * Set/Update the sochain url.
@@ -86718,7 +86722,6 @@ class Client extends UTXOClient {
86718
86722
  getExplorerUrl() {
86719
86723
  switch (this.network) {
86720
86724
  case Network.Mainnet:
86721
- return 'https://blockchair.com/dogecoin';
86722
86725
  case Network.Stagenet:
86723
86726
  return 'https://blockchair.com/dogecoin';
86724
86727
  case Network.Testnet:
@@ -86817,12 +86820,28 @@ class Client extends UTXOClient {
86817
86820
  getBalance(address) {
86818
86821
  return __awaiter(this, void 0, void 0, function* () {
86819
86822
  return getBalance$1({
86823
+ apiKey: this.sochainApiKey,
86820
86824
  sochainUrl: this.sochainUrl,
86821
86825
  network: this.network,
86822
86826
  address,
86823
86827
  });
86824
86828
  });
86825
86829
  }
86830
+ /**
86831
+ * helper function tto limit adding to an array
86832
+ *
86833
+ * @param arr array to be added to
86834
+ * @param toAdd elements to add
86835
+ * @param limit do not add more than this limit
86836
+ */
86837
+ addArrayUpToLimit(arr, toAdd, limit) {
86838
+ for (let index = 0; index < toAdd.length; index++) {
86839
+ const element = toAdd[index];
86840
+ if (arr.length < limit) {
86841
+ arr.push(element);
86842
+ }
86843
+ }
86844
+ }
86826
86845
  /**
86827
86846
  * Get transaction history of a given address with pagination options.
86828
86847
  * By default it will return the transaction history of the current wallet.
@@ -86833,39 +86852,44 @@ class Client extends UTXOClient {
86833
86852
  getTransactions(params) {
86834
86853
  var _a;
86835
86854
  return __awaiter(this, void 0, void 0, function* () {
86836
- // Sochain API doesn't have pagination parameter
86837
86855
  const offset = (_a = params === null || params === void 0 ? void 0 : params.offset) !== null && _a !== void 0 ? _a : 0;
86838
86856
  const limit = (params === null || params === void 0 ? void 0 : params.limit) || 10;
86839
- const response = yield getAddress({
86840
- sochainUrl: this.sochainUrl,
86841
- network: this.network,
86842
- address: `${params === null || params === void 0 ? void 0 : params.address}`,
86843
- });
86844
- const total = response.txs.length;
86845
- const transactions = [];
86846
- const txs = response.txs.filter((_, index) => offset <= index && index < offset + limit);
86847
- for (const txItem of txs) {
86848
- const rawTx = yield getTx({
86849
- sochainUrl: this.sochainUrl,
86850
- network: this.network,
86851
- hash: txItem.txid,
86852
- });
86853
- const tx = {
86854
- asset: AssetDOGE,
86855
- from: rawTx.inputs.map((i) => ({
86856
- from: i.address,
86857
- amount: assetToBase(assetAmount(i.value, DOGE_DECIMAL)),
86858
- })),
86859
- to: rawTx.outputs
86860
- // ignore tx with type 'nulldata'
86861
- .filter((i) => i.type !== 'nulldata')
86862
- .map((i) => ({ to: i.address, amount: assetToBase(assetAmount(i.value, DOGE_DECIMAL)) })),
86863
- date: new Date(rawTx.time * 1000),
86864
- type: TxType.Transfer,
86865
- hash: rawTx.txid,
86866
- };
86867
- transactions.push(tx);
86857
+ if (offset < 0 || limit < 0)
86858
+ throw Error('ofset and limit must be equal or greater than 0');
86859
+ const firstPage = Math.floor(offset / 10) + 1;
86860
+ const lastPage = limit > 10 ? firstPage + Math.floor(limit / 10) : firstPage;
86861
+ const offsetOnFirstPage = offset % 10;
86862
+ const txHashesToFetch = [];
86863
+ let page = firstPage;
86864
+ try {
86865
+ while (page <= lastPage) {
86866
+ const response = yield getTxs({
86867
+ apiKey: this.sochainApiKey,
86868
+ sochainUrl: this.sochainUrl,
86869
+ network: this.network,
86870
+ address: `${params === null || params === void 0 ? void 0 : params.address}`,
86871
+ page,
86872
+ });
86873
+ if (response.transactions.length === 0)
86874
+ break;
86875
+ if (page === firstPage && response.transactions.length > offsetOnFirstPage) {
86876
+ //start from offset
86877
+ const txsToGet = response.transactions.slice(offsetOnFirstPage);
86878
+ this.addArrayUpToLimit(txHashesToFetch, txsToGet.map((i) => i.hash), limit);
86879
+ }
86880
+ else {
86881
+ this.addArrayUpToLimit(txHashesToFetch, response.transactions.map((i) => i.hash), limit);
86882
+ }
86883
+ page++;
86884
+ }
86885
+ }
86886
+ catch (error) {
86887
+ console.error(error);
86888
+ //an errors means no more results
86868
86889
  }
86890
+ // console.log(JSON.stringify({ txHashesToFetch }, null, 2))
86891
+ const total = txHashesToFetch.length;
86892
+ const transactions = yield Promise.all(txHashesToFetch.map((hash) => this.getTransactionData(hash)));
86869
86893
  const result = {
86870
86894
  total,
86871
86895
  txs: transactions,
@@ -86882,6 +86906,7 @@ class Client extends UTXOClient {
86882
86906
  getTransactionData(txId) {
86883
86907
  return __awaiter(this, void 0, void 0, function* () {
86884
86908
  const rawTx = yield getTx({
86909
+ apiKey: this.sochainApiKey,
86885
86910
  sochainUrl: this.sochainUrl,
86886
86911
  network: this.network,
86887
86912
  hash: txId,
@@ -86895,7 +86920,7 @@ class Client extends UTXOClient {
86895
86920
  to: rawTx.outputs.map((i) => ({ to: i.address, amount: assetToBase(assetAmount(i.value, DOGE_DECIMAL)) })),
86896
86921
  date: new Date(rawTx.time * 1000),
86897
86922
  type: TxType.Transfer,
86898
- hash: rawTx.txid,
86923
+ hash: rawTx.hash,
86899
86924
  };
86900
86925
  });
86901
86926
  }
@@ -86921,6 +86946,7 @@ class Client extends UTXOClient {
86921
86946
  const feeRate = params.feeRate || (yield this.getSuggestedFeeRate());
86922
86947
  checkFeeBounds(this.feeBounds, feeRate);
86923
86948
  const { psbt } = yield buildTx({
86949
+ apiKey: this.sochainApiKey,
86924
86950
  amount: params.amount,
86925
86951
  recipient: params.recipient,
86926
86952
  memo: params.memo,
@@ -86928,7 +86954,6 @@ class Client extends UTXOClient {
86928
86954
  sender: this.getAddress(fromAddressIndex),
86929
86955
  sochainUrl: this.sochainUrl,
86930
86956
  network: this.network,
86931
- withTxHex: false,
86932
86957
  });
86933
86958
  const dogeKeys = this.getDogeKeys(this.phrase, fromAddressIndex);
86934
86959
  psbt.signAllInputs(dogeKeys); // Sign all inputs