Skip to content

Commit

Permalink
main: Add immature and unconfirmed to balance cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechampine committed Dec 11, 2023
1 parent b08e34e commit 7667515
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
5 changes: 3 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ type TxpoolTransactionsResponse struct {

// WalletBalanceResponse is the response type for /wallets/:name/balance.
type WalletBalanceResponse struct {
Siacoins types.Currency `json:"siacoins"`
Siafunds uint64 `json:"siafunds"`
Siacoins types.Currency `json:"siacoins"`
ImmatureSiacoins types.Currency `json:"immatureSiacoins"`
Siafunds uint64 `json:"siafunds"`
}

// WalletOutputsResponse is the response type for /wallets/:name/outputs.
Expand Down
14 changes: 10 additions & 4 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,23 @@ func (s *server) walletsBalanceHandler(jc jape.Context) {
if jc.Check("couldn't load outputs", err) != nil {
return
}
var sc types.Currency
height := s.cm.TipState().Index.Height
var sc, immature types.Currency
var sf uint64
for _, sco := range scos {
sc = sc.Add(sco.SiacoinOutput.Value)
if height >= sco.MaturityHeight {
sc = sc.Add(sco.SiacoinOutput.Value)
} else {
immature = immature.Add(sco.SiacoinOutput.Value)
}
}
for _, sfo := range sfos {
sf += sfo.SiafundOutput.Value
}
jc.Encode(WalletBalanceResponse{
Siacoins: sc,
Siafunds: sf,
Siacoins: sc,
ImmatureSiacoins: immature,
Siafunds: sf,
})
}

Expand Down
14 changes: 13 additions & 1 deletion cmd/walletd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,19 @@ func main() {
c := initTestnetClient(apiAddr, network, seed)
b, err := c.Wallet("primary").Balance()
check("Couldn't get balance:", err)
fmt.Println(b.Siacoins)
out := fmt.Sprint(b.Siacoins)
if !b.ImmatureSiacoins.IsZero() {
out += fmt.Sprintf(" + %v immature", b.ImmatureSiacoins)
}
poolGained, poolLost := testnetTxpoolBalance(c, seed)
if !poolGained.IsZero() || !poolLost.IsZero() {
if poolGained.Cmp(poolLost) >= 0 {
out += fmt.Sprintf(" + %v unconfirmed", poolGained.Sub(poolLost))
} else {
out += fmt.Sprintf(" - %v unconfirmed", poolLost.Sub(poolGained))
}
}
fmt.Println(out)

case sendCmd:
if len(cmd.Args()) != 2 {
Expand Down
29 changes: 29 additions & 0 deletions cmd/walletd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,35 @@ func printTestnetEvents(c *api.Client, seed wallet.Seed) {
}
}

func testnetTxpoolBalance(c *api.Client, seed wallet.Seed) (gained, lost types.Currency) {
ourAddr := types.StandardUnlockHash(seed.PublicKey(0))
txns, v2txns, err := c.TxpoolTransactions()
check("Couldn't get txpool transactions:", err)
for _, txn := range txns {
if len(txn.SiacoinInputs) == 0 || len(txn.SiacoinOutputs) == 0 {
continue
}
sco := txn.SiacoinOutputs[0]
if txn.SiacoinInputs[0].UnlockConditions.UnlockHash() == ourAddr {
lost = lost.Add(sco.Value)
} else if sco.Address == ourAddr {
gained = gained.Add(sco.Value)
}
}
for _, txn := range v2txns {
if len(txn.SiacoinInputs) == 0 || len(txn.SiacoinOutputs) == 0 {
continue
}
sco := txn.SiacoinOutputs[0]
if txn.SiacoinInputs[0].Parent.SiacoinOutput.Address == ourAddr {
lost = lost.Add(sco.Value)
} else if sco.Address == ourAddr {
gained = gained.Add(sco.Value)
}
}
return
}

func printTestnetTxpool(c *api.Client, seed wallet.Seed) {
ourAddr := types.StandardUnlockHash(seed.PublicKey(0))
txns, v2txns, err := c.TxpoolTransactions()
Expand Down

0 comments on commit 7667515

Please sign in to comment.