secant-ios-wallet/secant/Models/Balance.swift

109 lines
2.3 KiB
Swift
Raw Normal View History

//
// Balance.swift
// secant
//
// Created by Francisco Gindre on 8/9/21.
//
import Foundation
2021-09-14 04:15:29 -07:00
/**
2021-09-14 04:15:29 -07:00
Funds Expressed in Zatoshis
*/
protocol Funds {
/**
2021-09-14 04:15:29 -07:00
Confirmed, spendable funds
*/
var confirmed: Int64 { get }
2021-09-14 04:15:29 -07:00
/**
2021-09-14 04:15:29 -07:00
Unconfirmed, not yet spendable funds.
*/
var unconfirmed: Int64 { get }
2021-09-14 04:15:29 -07:00
/**
2021-09-14 04:15:29 -07:00
Represents a null value of Funds with Zero confirmed and Zero unconfirmed funds.
*/
static var noFunds: Funds { get }
}
/**
2022-02-18 13:39:04 -08:00
Wallet Balance that condenses transparent, Sapling and Orchard funds
2021-09-14 04:15:29 -07:00
*/
protocol WalletBalance {
/**
2021-09-14 04:15:29 -07:00
Transparent funds. This is the sum of the UTXOs of the user found at a given time
*/
2022-02-18 13:39:04 -08:00
var transparent: Funds { get }
2021-09-14 04:15:29 -07:00
/**
2021-09-14 04:15:29 -07:00
Funds on the Sapling shielded pool for a given user.
*/
var sapling: Funds { get }
2021-09-14 04:15:29 -07:00
/**
2021-09-14 04:15:29 -07:00
Funds on the Orchard shielded pool for a given user.
*/
var orchard: Funds { get }
2021-09-14 04:15:29 -07:00
/**
2021-09-14 04:15:29 -07:00
The sum of all confirmed funds, transparent, sapling and orchard funds (calculated)
*/
var totalAvailableBalance: Int64 { get }
2021-09-14 04:15:29 -07:00
/**
2021-09-14 04:15:29 -07:00
The sum of all unconfirmed funds: transparent, sapling, and orchard funds (calculated
*/
var totalUnconfirmedBalance: Int64 { get }
2021-09-14 04:15:29 -07:00
/**
2021-09-14 04:15:29 -07:00
The sum of all funds confirmed and unconfirmed of all pools (transparent, sapling and orchard).
*/
var totalBalance: Int64 { get }
2021-09-14 04:15:29 -07:00
/**
2021-09-14 04:15:29 -07:00
Represents a the value of Zero funds.
*/
static var nullBalance: WalletBalance { get }
}
2021-09-14 04:15:29 -07:00
extension WalletBalance {
static var nullBalance: WalletBalance {
Balance(
2022-02-18 13:39:04 -08:00
transparent: ZcashFunds.noFunds,
2021-09-14 04:15:29 -07:00
sapling: ZcashFunds.noFunds,
orchard: ZcashFunds.noFunds
)
}
var totalAvailableBalance: Int64 {
2022-02-18 13:39:04 -08:00
transparent.confirmed + sapling.confirmed + orchard.confirmed
2021-09-14 04:15:29 -07:00
}
var totalUnconfirmedBalance: Int64 {
2022-02-18 13:39:04 -08:00
transparent.unconfirmed + sapling.unconfirmed + orchard.unconfirmed
2021-09-14 04:15:29 -07:00
}
var totalBalance: Int64 {
totalAvailableBalance + totalUnconfirmedBalance
}
}
/**
2021-09-14 04:15:29 -07:00
Concrete Wallet Balance.
*/
struct Balance: WalletBalance {
2022-02-18 13:39:04 -08:00
var transparent: Funds
var sapling: Funds
var orchard: Funds
}
struct ZcashFunds: Funds {
static var noFunds: Funds {
ZcashFunds(confirmed: 0, unconfirmed: 0)
}
2021-09-14 04:15:29 -07:00
var confirmed: Int64
var unconfirmed: Int64
}