Skip to content

Commit 0d1e1c8

Browse files
committed
add createdAt to onboard UTXO
1 parent 70f5539 commit 0d1e1c8

File tree

4 files changed

+208
-190
lines changed

4 files changed

+208
-190
lines changed

pkg/client-sdk/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func TestVtxosToTxs(t *testing.T) {
135135
if err != nil {
136136
t.Fatalf("failed to load fixture: %s", err)
137137
}
138-
got, err := vtxosToTxsCovenantless(30, args.spendable, args.spent)
138+
got, err := vtxosToTxsCovenantless(30, args.spendable, args.spent, nil)
139139
require.NoError(t, err)
140140
require.Len(t, got, len(tt.want))
141141

pkg/client-sdk/covenant_client.go

Lines changed: 105 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -521,101 +521,9 @@ func (a *covenantArkClient) GetTransactionHistory(ctx context.Context) ([]Transa
521521
return nil, err
522522
}
523523

524-
claimableUtxos, _ := a.getClaimableBoardingUtxos(ctx)
524+
boardingTxs := a.getBoardingTxs(ctx)
525525

526-
return vtxosToTxsCovenant(config.RoundLifetime, spendableVtxos, spentVtxos, claimableUtxos)
527-
}
528-
529-
func vtxosToTxsCovenant(roundLifetime int64, spendable, spent []client.Vtxo, claimableUtxos []explorer.Utxo) ([]Transaction, error) {
530-
transactions := make([]Transaction, 0)
531-
532-
for _, v := range append(spendable, spent...) {
533-
// get vtxo amount
534-
amount := int(v.Amount)
535-
if v.Pending {
536-
// find other spent vtxos that spent this one
537-
relatedVtxos := findVtxosBySpentBy(spent, v.Txid)
538-
for _, r := range relatedVtxos {
539-
if r.Amount < math.MaxInt64 {
540-
rAmount := int(r.Amount)
541-
amount -= rAmount
542-
}
543-
}
544-
} else {
545-
// an onboarding tx has pending false and no pending true related txs
546-
relatedVtxos := findVtxosBySpentBy(spent, v.RoundTxid)
547-
if len(relatedVtxos) > 0 { // not an onboard tx, ignore
548-
continue
549-
}
550-
} // what kind of tx was this? send or receive?
551-
txType := TxReceived
552-
if amount < 0 {
553-
txType = TxSent
554-
}
555-
// check if is a pending tx
556-
pending := false
557-
claimed := true
558-
if len(v.RoundTxid) == 0 && len(v.SpentBy) == 0 {
559-
pending = true
560-
claimed = false
561-
}
562-
redeemTxid := ""
563-
if len(v.RedeemTx) > 0 {
564-
txid, err := getRedeemTxidCovenant(v.RedeemTx)
565-
if err != nil {
566-
return nil, err
567-
}
568-
redeemTxid = txid
569-
}
570-
571-
// add transaction
572-
transactions = append(transactions, Transaction{
573-
RoundTxid: v.RoundTxid,
574-
RedeemTxid: redeemTxid,
575-
Amount: uint64(math.Abs(float64(amount))),
576-
Type: txType,
577-
Pending: pending,
578-
Claimed: claimed,
579-
CreatedAt: getCreatedAtFromExpiry(roundLifetime, *v.ExpiresAt),
580-
})
581-
}
582-
583-
for _, u := range claimableUtxos {
584-
transactions = append(transactions, Transaction{
585-
Amount: u.Amount,
586-
BoardingTxid: u.Txid,
587-
Type: TxReceived,
588-
Pending: true,
589-
Claimed: false,
590-
CreatedAt: u.CreatedAt,
591-
})
592-
}
593-
594-
// Sort the slice by age
595-
sort.Slice(transactions, func(i, j int) bool {
596-
txi := transactions[i]
597-
txj := transactions[j]
598-
if txi.CreatedAt.Equal(txj.CreatedAt) {
599-
return txi.Type > txj.Type
600-
}
601-
return txi.CreatedAt.After(txj.CreatedAt)
602-
})
603-
604-
return transactions, nil
605-
}
606-
607-
func getRedeemTxidCovenant(redeemTx string) (string, error) {
608-
redeemPtx, err := psetv2.NewPsetFromBase64(redeemTx)
609-
if err != nil {
610-
return "", fmt.Errorf("failed to parse redeem tx: %s", err)
611-
}
612-
613-
tx, err := redeemPtx.UnsignedTx()
614-
if err != nil {
615-
return "", fmt.Errorf("failed to get txid from redeem tx: %s", err)
616-
}
617-
618-
return tx.TxHash().String(), nil
526+
return vtxosToTxsCovenant(config.RoundLifetime, spendableVtxos, spentVtxos, boardingTxs)
619527
}
620528

621529
func (a *covenantArkClient) getClaimableBoardingUtxos(ctx context.Context) ([]explorer.Utxo, error) {
@@ -1480,3 +1388,106 @@ func (a *covenantArkClient) selfTransferAllPendingPayments(
14801388

14811389
return roundTxid, nil
14821390
}
1391+
1392+
func (a *covenantArkClient) getBoardingTxs(ctx context.Context) []Transaction {
1393+
utxos, err := a.getClaimableBoardingUtxos(ctx)
1394+
if err != nil {
1395+
return nil
1396+
}
1397+
1398+
txs := make([]Transaction, 0, len(utxos))
1399+
for _, u := range utxos {
1400+
txs = append(txs, Transaction{
1401+
BoardingTxid: u.Txid,
1402+
Amount: u.Amount,
1403+
Type: TxReceived,
1404+
Pending: true,
1405+
Claimed: false,
1406+
CreatedAt: u.CreatedAt,
1407+
})
1408+
}
1409+
return txs
1410+
}
1411+
1412+
func vtxosToTxsCovenant(
1413+
roundLifetime int64, spendable, spent []client.Vtxo, boardingTxs []Transaction,
1414+
) ([]Transaction, error) {
1415+
transactions := append([]Transaction{}, boardingTxs...)
1416+
1417+
for _, v := range append(spendable, spent...) {
1418+
// get vtxo amount
1419+
amount := int(v.Amount)
1420+
if v.Pending {
1421+
// find other spent vtxos that spent this one
1422+
relatedVtxos := findVtxosBySpentBy(spent, v.Txid)
1423+
for _, r := range relatedVtxos {
1424+
if r.Amount < math.MaxInt64 {
1425+
rAmount := int(r.Amount)
1426+
amount -= rAmount
1427+
}
1428+
}
1429+
} else {
1430+
// an onboarding tx has pending false and no pending true related txs
1431+
relatedVtxos := findVtxosBySpentBy(spent, v.RoundTxid)
1432+
if len(relatedVtxos) > 0 { // not an onboard tx, ignore
1433+
continue
1434+
}
1435+
} // what kind of tx was this? send or receive?
1436+
txType := TxReceived
1437+
if amount < 0 {
1438+
txType = TxSent
1439+
}
1440+
// check if is a pending tx
1441+
pending := false
1442+
claimed := true
1443+
if len(v.RoundTxid) == 0 && len(v.SpentBy) == 0 {
1444+
pending = true
1445+
claimed = false
1446+
}
1447+
redeemTxid := ""
1448+
if len(v.RedeemTx) > 0 {
1449+
txid, err := getRedeemTxidCovenant(v.RedeemTx)
1450+
if err != nil {
1451+
return nil, err
1452+
}
1453+
redeemTxid = txid
1454+
}
1455+
1456+
// add transaction
1457+
transactions = append(transactions, Transaction{
1458+
RoundTxid: v.RoundTxid,
1459+
RedeemTxid: redeemTxid,
1460+
Amount: uint64(math.Abs(float64(amount))),
1461+
Type: txType,
1462+
Pending: pending,
1463+
Claimed: claimed,
1464+
CreatedAt: getCreatedAtFromExpiry(roundLifetime, *v.ExpiresAt),
1465+
})
1466+
}
1467+
1468+
// Sort the slice by age
1469+
sort.Slice(transactions, func(i, j int) bool {
1470+
txi := transactions[i]
1471+
txj := transactions[j]
1472+
if txi.CreatedAt.Equal(txj.CreatedAt) {
1473+
return txi.Type > txj.Type
1474+
}
1475+
return txi.CreatedAt.After(txj.CreatedAt)
1476+
})
1477+
1478+
return transactions, nil
1479+
}
1480+
1481+
func getRedeemTxidCovenant(redeemTx string) (string, error) {
1482+
redeemPtx, err := psetv2.NewPsetFromBase64(redeemTx)
1483+
if err != nil {
1484+
return "", fmt.Errorf("failed to parse redeem tx: %s", err)
1485+
}
1486+
1487+
tx, err := redeemPtx.UnsignedTx()
1488+
if err != nil {
1489+
return "", fmt.Errorf("failed to get txid from redeem tx: %s", err)
1490+
}
1491+
1492+
return tx.TxHash().String(), nil
1493+
}

0 commit comments

Comments
 (0)