@arkade-os/sdk 0.3.11 → 0.3.12

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 CHANGED
@@ -164,9 +164,8 @@ const boardingUtxos = await wallet.getBoardingUtxos()
164
164
  ```typescript
165
165
  // Send bitcoin via Ark
166
166
  const txid = await wallet.sendBitcoin({
167
- address: 'tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx',
168
- amount: 50000, // in satoshis
169
- feeRate: 1 // optional, in sats/vbyte
167
+ address: 'ark1qq4...', // ark address
168
+ amount: 50000, // in satoshis
170
169
  })
171
170
  ```
172
171
 
@@ -16,7 +16,7 @@ const txKey = {
16
16
  * @param {Set<string>} commitmentsToIgnore - A set of commitment IDs that should be excluded from processing.
17
17
  * @return {ExtendedArkTransaction[]} A sorted array of extended Ark transactions, representing the transaction history.
18
18
  */
19
- function buildTransactionHistory(vtxos, allBoardingTxs, commitmentsToIgnore) {
19
+ async function buildTransactionHistory(vtxos, allBoardingTxs, commitmentsToIgnore, getTxCreatedAt) {
20
20
  const fromOldestVtxo = [...vtxos].sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime());
21
21
  const sent = [];
22
22
  let received = [];
@@ -59,21 +59,23 @@ function buildTransactionHistory(vtxos, allBoardingTxs, commitmentsToIgnore) {
59
59
  if (vtxo.arkTxId &&
60
60
  !sent.some((s) => s.key.arkTxid === vtxo.arkTxId)) {
61
61
  const changes = fromOldestVtxo.filter((_) => _.txid === vtxo.arkTxId);
62
+ // We want to find all the other VTXOs spent by the same transaction to
63
+ // calculate the full amount of the change.
64
+ const allSpent = fromOldestVtxo.filter((v) => v.arkTxId === vtxo.arkTxId);
65
+ const spentAmount = allSpent.reduce((acc, v) => acc + v.value, 0);
62
66
  let txAmount = 0;
63
67
  let txTime = 0;
64
68
  if (changes.length > 0) {
65
69
  const changeAmount = changes.reduce((acc, v) => acc + v.value, 0);
66
- // We want to find all the other VTXOs spent by the same transaction to
67
- // calculate the full amount of the change.
68
- const allSpent = fromOldestVtxo.filter((v) => v.arkTxId === vtxo.arkTxId);
69
- const spentAmount = allSpent.reduce((acc, v) => acc + v.value, 0);
70
70
  txAmount = spentAmount - changeAmount;
71
71
  txTime = changes[0].createdAt.getTime();
72
72
  }
73
73
  else {
74
- txAmount = vtxo.value;
74
+ txAmount = spentAmount;
75
75
  // TODO: fetch the vtxo with /v1/indexer/vtxos?outpoints=<vtxo.arkTxid:0> to know when the tx was made
76
- txTime = vtxo.createdAt.getTime() + 1;
76
+ txTime = getTxCreatedAt
77
+ ? await getTxCreatedAt(vtxo.arkTxId)
78
+ : vtxo.createdAt.getTime() + 1;
77
79
  }
78
80
  sent.push({
79
81
  key: { ...txKey, arkTxid: vtxo.arkTxId },
@@ -268,7 +268,10 @@ class ReadonlyWallet {
268
268
  scripts: [base_1.hex.encode(this.offchainTapscript.pkScript)],
269
269
  });
270
270
  const { boardingTxs, commitmentsToIgnore } = await this.getBoardingTxs();
271
- return (0, transactionHistory_1.buildTransactionHistory)(response.vtxos, boardingTxs, commitmentsToIgnore);
271
+ const getTxCreatedAt = (txid) => this.indexerProvider
272
+ .getVtxos({ outpoints: [{ txid, vout: 0 }] })
273
+ .then((res) => res.vtxos[0]?.createdAt.getTime() || 0);
274
+ return (0, transactionHistory_1.buildTransactionHistory)(response.vtxos, boardingTxs, commitmentsToIgnore, getTxCreatedAt);
272
275
  }
273
276
  async getBoardingTxs() {
274
277
  const utxos = [];
@@ -13,7 +13,7 @@ const txKey = {
13
13
  * @param {Set<string>} commitmentsToIgnore - A set of commitment IDs that should be excluded from processing.
14
14
  * @return {ExtendedArkTransaction[]} A sorted array of extended Ark transactions, representing the transaction history.
15
15
  */
16
- export function buildTransactionHistory(vtxos, allBoardingTxs, commitmentsToIgnore) {
16
+ export async function buildTransactionHistory(vtxos, allBoardingTxs, commitmentsToIgnore, getTxCreatedAt) {
17
17
  const fromOldestVtxo = [...vtxos].sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime());
18
18
  const sent = [];
19
19
  let received = [];
@@ -56,21 +56,23 @@ export function buildTransactionHistory(vtxos, allBoardingTxs, commitmentsToIgno
56
56
  if (vtxo.arkTxId &&
57
57
  !sent.some((s) => s.key.arkTxid === vtxo.arkTxId)) {
58
58
  const changes = fromOldestVtxo.filter((_) => _.txid === vtxo.arkTxId);
59
+ // We want to find all the other VTXOs spent by the same transaction to
60
+ // calculate the full amount of the change.
61
+ const allSpent = fromOldestVtxo.filter((v) => v.arkTxId === vtxo.arkTxId);
62
+ const spentAmount = allSpent.reduce((acc, v) => acc + v.value, 0);
59
63
  let txAmount = 0;
60
64
  let txTime = 0;
61
65
  if (changes.length > 0) {
62
66
  const changeAmount = changes.reduce((acc, v) => acc + v.value, 0);
63
- // We want to find all the other VTXOs spent by the same transaction to
64
- // calculate the full amount of the change.
65
- const allSpent = fromOldestVtxo.filter((v) => v.arkTxId === vtxo.arkTxId);
66
- const spentAmount = allSpent.reduce((acc, v) => acc + v.value, 0);
67
67
  txAmount = spentAmount - changeAmount;
68
68
  txTime = changes[0].createdAt.getTime();
69
69
  }
70
70
  else {
71
- txAmount = vtxo.value;
71
+ txAmount = spentAmount;
72
72
  // TODO: fetch the vtxo with /v1/indexer/vtxos?outpoints=<vtxo.arkTxid:0> to know when the tx was made
73
- txTime = vtxo.createdAt.getTime() + 1;
73
+ txTime = getTxCreatedAt
74
+ ? await getTxCreatedAt(vtxo.arkTxId)
75
+ : vtxo.createdAt.getTime() + 1;
74
76
  }
75
77
  sent.push({
76
78
  key: { ...txKey, arkTxid: vtxo.arkTxId },
@@ -230,7 +230,10 @@ export class ReadonlyWallet {
230
230
  scripts: [hex.encode(this.offchainTapscript.pkScript)],
231
231
  });
232
232
  const { boardingTxs, commitmentsToIgnore } = await this.getBoardingTxs();
233
- return buildTransactionHistory(response.vtxos, boardingTxs, commitmentsToIgnore);
233
+ const getTxCreatedAt = (txid) => this.indexerProvider
234
+ .getVtxos({ outpoints: [{ txid, vout: 0 }] })
235
+ .then((res) => res.vtxos[0]?.createdAt.getTime() || 0);
236
+ return buildTransactionHistory(response.vtxos, boardingTxs, commitmentsToIgnore, getTxCreatedAt);
234
237
  }
235
238
  async getBoardingTxs() {
236
239
  const utxos = [];
@@ -11,5 +11,5 @@ type ExtendedArkTransaction = ArkTransaction & {
11
11
  * @param {Set<string>} commitmentsToIgnore - A set of commitment IDs that should be excluded from processing.
12
12
  * @return {ExtendedArkTransaction[]} A sorted array of extended Ark transactions, representing the transaction history.
13
13
  */
14
- export declare function buildTransactionHistory(vtxos: VirtualCoin[], allBoardingTxs: ArkTransaction[], commitmentsToIgnore: Set<string>): ExtendedArkTransaction[];
14
+ export declare function buildTransactionHistory(vtxos: VirtualCoin[], allBoardingTxs: ArkTransaction[], commitmentsToIgnore: Set<string>, getTxCreatedAt?: (txid: string) => Promise<number>): Promise<ExtendedArkTransaction[]>;
15
15
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkade-os/sdk",
3
- "version": "0.3.11",
3
+ "version": "0.3.12",
4
4
  "description": "Bitcoin wallet SDK with Taproot and Ark integration",
5
5
  "type": "module",
6
6
  "main": "./dist/cjs/index.js",