Skip to content

Commit

Permalink
update TzKTAccount and Account to deal with new balances for AI
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmcl committed Jun 7, 2024
1 parent 0e807df commit c3b5c97
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Sources/KukaiCoreSwift/Clients/TzKTClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ public class TzKTClient {
private func getAllBalances(forAddress address: String, numberOfPages: Int, completion: @escaping ((Result<Account, KukaiError>) -> Void)) {
let dispatchGroup = DispatchGroup()

var tzktAccount = TzKTAccount(balance: 0, type: "user", address: "", publicKey: "", revealed: false, delegate: TzKTAccountDelegate(alias: nil, address: "", active: false), delegationLevel: 0, activeTokensCount: 0, tokenBalancesCount: 0)
var tzktAccount = TzKTAccount(balance: 0, stakedBalance: 0, unstakedBalance: 0, type: "user", address: "", publicKey: "", revealed: false, delegate: TzKTAccountDelegate(alias: nil, address: "", active: false), delegationLevel: 0, activeTokensCount: 0, tokenBalancesCount: 0)
var tokenBalances: [TzKTBalance] = []
//var liquidityTokens: [DipDupPositionData] = []
var errorFound: KukaiError? = nil
Expand Down Expand Up @@ -851,7 +851,7 @@ public class TzKTClient {

} else {
groupedData = self?.groupBalances(tokenBalances, filteringOutLiquidityTokens: []) ?? (tokens: [], nftGroups: [], recentNFTs: [])
let account = Account(walletAddress: address, xtzBalance: tzktAccount.xtzBalance, tokens: groupedData.tokens, nfts: groupedData.nftGroups, recentNFTs: groupedData.recentNFTs, liquidityTokens: [], delegate: tzktAccount.delegate, delegationLevel: tzktAccount.delegationLevel)
let account = Account(walletAddress: address, xtzBalance: tzktAccount.xtzBalance, xtzStakedBalance: tzktAccount.xtzUnstakedBalance, xtzUnstakedBalance: tzktAccount.xtzUnstakedBalance, tokens: groupedData.tokens, nfts: groupedData.nftGroups, recentNFTs: groupedData.recentNFTs, liquidityTokens: [], delegate: tzktAccount.delegate, delegationLevel: tzktAccount.delegationLevel)

completion(Result.success(account))
}
Expand Down
18 changes: 17 additions & 1 deletion Sources/KukaiCoreSwift/Models/Account.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public struct Account: Codable, Hashable {
/// The XTZ balance of the wallet
public let xtzBalance: XTZAmount

/// The staked (locked) XTZ balance of the wallet
public let xtzStakedBalance: XTZAmount

/// The unstaked (pending unlock) XTZ balance of the wallet
public let xtzUnstakedBalance: XTZAmount

/// All the wallets FA1.2, FA2 funginble tokens
public let tokens: [Token]

Expand All @@ -36,11 +42,19 @@ public struct Account: Codable, Hashable {
/// The block level that the delegate was set
public let delegationLevel: Decimal?

/// The total available (or spendable) balance of the account
public var availableBalance: XTZAmount {
get {
return (xtzBalance - xtzStakedBalance) - xtzUnstakedBalance
}
}

/// Basic init to default properties to zero / empty, so that optionals can be avoided on a key model throughout an app
public init(walletAddress: String) {
self.walletAddress = walletAddress
self.xtzBalance = .zero()
self.xtzStakedBalance = .zero()
self.xtzUnstakedBalance = .zero()
self.tokens = []
self.nfts = []
self.recentNFTs = []
Expand All @@ -50,9 +64,11 @@ public struct Account: Codable, Hashable {
}

/// Full init
public init(walletAddress: String, xtzBalance: XTZAmount, tokens: [Token], nfts: [Token], recentNFTs: [NFT], liquidityTokens: [DipDupPositionData], delegate: TzKTAccountDelegate?, delegationLevel: Decimal?) {
public init(walletAddress: String, xtzBalance: XTZAmount, xtzStakedBalance: XTZAmount, xtzUnstakedBalance: XTZAmount, tokens: [Token], nfts: [Token], recentNFTs: [NFT], liquidityTokens: [DipDupPositionData], delegate: TzKTAccountDelegate?, delegationLevel: Decimal?) {
self.walletAddress = walletAddress
self.xtzBalance = xtzBalance
self.xtzStakedBalance = xtzStakedBalance
self.xtzUnstakedBalance = xtzUnstakedBalance
self.tokens = tokens
self.nfts = nfts
self.recentNFTs = recentNFTs
Expand Down
26 changes: 25 additions & 1 deletion Sources/KukaiCoreSwift/Models/BakingBad/TzKTAccount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ public struct TzKTAccount: Codable, Equatable {
/// The address XTZ balance in RPC format
public let balance: Decimal?

/// The address staked (locked) XTZ balance in RPC format
public let stakedBalance: Decimal?

/// The address unstaked (pending unlock) XTZ balance in RPC format
public let unstakedBalance: Decimal?

/// type of account e.g. "user" or "empty"
public let type: String

Expand Down Expand Up @@ -40,9 +46,27 @@ public struct TzKTAccount: Codable, Equatable {
return XTZAmount(fromRpcAmount: balance ?? 0) ?? .zero()
}

/// Helper method to convert the RPC balance into an XTZAmount
public var xtzStakedBalance: XTZAmount {
return XTZAmount(fromRpcAmount: stakedBalance ?? 0) ?? .zero()
}

/// Helper method to convert the RPC balance into an XTZAmount
public var xtzUnstakedBalance: XTZAmount {
return XTZAmount(fromRpcAmount: unstakedBalance ?? 0) ?? .zero()
}

/// Helper method to to return the available or spendable balance
public var xtzAvailableBalance: XTZAmount {
return ((xtzBalance - xtzStakedBalance) - xtzUnstakedBalance)
}


/// Generic init
public init(balance: Decimal?, type: String, address: String, publicKey: String, revealed: Bool, delegate: TzKTAccountDelegate?, delegationLevel: Decimal?, activeTokensCount: Decimal?, tokenBalancesCount: Decimal?) {
public init(balance: Decimal?, stakedBalance: Decimal?, unstakedBalance: Decimal?, type: String, address: String, publicKey: String, revealed: Bool, delegate: TzKTAccountDelegate?, delegationLevel: Decimal?, activeTokensCount: Decimal?, tokenBalancesCount: Decimal?) {
self.balance = balance
self.stakedBalance = stakedBalance
self.unstakedBalance = unstakedBalance
self.type = type
self.address = address
self.publicKey = publicKey
Expand Down
3 changes: 3 additions & 0 deletions Tests/KukaiCoreSwiftTests/Clients/TzKTClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ class TzKTClientTests: XCTestCase {

// Tokens
XCTAssert(account.xtzBalance.normalisedRepresentation == "1.843617", account.xtzBalance.normalisedRepresentation)
XCTAssert(account.xtzStakedBalance.normalisedRepresentation == "0", account.xtzStakedBalance.normalisedRepresentation)
XCTAssert(account.xtzUnstakedBalance.normalisedRepresentation == "0", account.xtzUnstakedBalance.normalisedRepresentation)
XCTAssert(account.availableBalance.normalisedRepresentation == "1.843617", account.availableBalance.normalisedRepresentation)
XCTAssert(account.tokens.count == 21, "\(account.tokens.count)")
XCTAssert(account.tokens[0].symbol == "wBUSD", account.tokens[0].symbol)
XCTAssert(account.tokens[0].name == "Wrapped Tokens Contract", account.tokens[0].name ?? "")
Expand Down

0 comments on commit c3b5c97

Please sign in to comment.