-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
59281e3
commit d24cb5b
Showing
10 changed files
with
461 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package sqlite | ||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
|
||
"go.sia.tech/core/types" | ||
"go.sia.tech/walletd/wallet" | ||
) | ||
|
||
// AddressBalance returns the balance of a single address. | ||
func (s *Store) AddressBalance(address types.Address) (balance wallet.Balance, err error) { | ||
err = s.transaction(func(tx *txn) error { | ||
const query = `SELECT siacoin_balance, immature_siacoin_balance, siafund_balance FROM sia_addresses WHERE sia_address=$1` | ||
return tx.QueryRow(query, encode(address)).Scan(decode(&balance.Siacoins), decode(&balance.ImmatureSiacoins), &balance.Siafunds) | ||
}) | ||
if errors.Is(err, sql.ErrNoRows) { | ||
return wallet.Balance{}, wallet.ErrNotFound | ||
} | ||
return | ||
} | ||
|
||
// AddressEvents returns the events related to a single address. | ||
func (s *Store) AddressEvents(address types.Address, offset, limit int) (events []wallet.Event, err error) { | ||
err = s.transaction(func(tx *txn) error { | ||
const query = `SELECT ev.id, ev.event_id, ev.maturity_height, ev.date_created, ci.height, ci.block_id, ev.event_type, ev.event_data | ||
FROM events ev | ||
INNER JOIN chain_indices ci ON (ev.index_id = ci.id) | ||
INNER JOIN event_addresses ea ON (ev.id = ea.event_id) | ||
INNER JOIN sia_addresses sa ON (ea.address_id = sa.id) | ||
WHERE sa.sia_address = $1 | ||
ORDER BY ev.maturity_height DESC | ||
LIMIT $2 OFFSET $3` | ||
rows, err := tx.Query(query, encode(address), limit, offset) | ||
if err != nil { | ||
return err | ||
} | ||
defer rows.Close() | ||
for rows.Next() { | ||
_, event, err := scanEvent(rows) | ||
if err != nil { | ||
return fmt.Errorf("failed to scan event: %w", err) | ||
} | ||
event.Relevant = []types.Address{address} // only the address is relevant | ||
events = append(events, event) | ||
} | ||
return rows.Err() | ||
}) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.