Skip to content

Commit

Permalink
- add firstLevel and lastLevel info to tokens and NFTs from tzkt to a…
Browse files Browse the repository at this point in the history
…llow ordering with custom grouping logic

- update tests
  • Loading branch information
simonmcl committed Jul 30, 2024
1 parent 2935cd7 commit ce9be84
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Sources/KukaiCoreSwift/Clients/TzKTClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ public class TzKTClient {
continue
} else if balance.token.metadata != nil {
// Else create a Token object and put into array, if we have valid metadata (e.g. able to tell how many decimals it has)
tokens.append(Token(from: balance.token, andTokenAmount: balance.tokenAmount))
tokens.append(Token(from: balance, andTokenAmount: balance.tokenAmount))
}
}

Expand Down
6 changes: 6 additions & 0 deletions Sources/KukaiCoreSwift/Models/BakingBad/TzKTBalance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public struct TzKTBalance: Codable {
/// Details about the Token
public let token: TzKTBalanceToken

/// The block level where the token was first seen
public let firstLevel: Decimal

/// The block level where the token was last seen
public let lastLevel: Decimal

/// Helper to convert the RPC token balance to a `TokenAmount` object
public var tokenAmount: TokenAmount {
return TokenAmount(fromRpcAmount: balance, decimalPlaces: token.metadata?.decimalsInt ?? 0) ?? .zero()
Expand Down
8 changes: 8 additions & 0 deletions Sources/KukaiCoreSwift/Models/NFT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public struct NFT: Codable, Hashable {
return favouriteSortIndex != nil
}

/// The block level where the token was first seen
public var firstlevel: Decimal

/// The block level where the token was last seen
public var lastLevel: Decimal


/**
Create a more developer friednly `NFT` from a generic `TzKTBalance` object
Expand All @@ -83,6 +89,8 @@ public struct NFT: Codable, Hashable {
displayURI = URL(string: tzkt.token.metadata?.displayUri ?? "")
thumbnailURI = URL(string: tzkt.token.metadata?.thumbnailUri ?? "")
metadata = tzkt.token.metadata
firstlevel = tzkt.firstLevel
lastLevel = tzkt.lastLevel
}

/// Confomring to Equatable
Expand Down
32 changes: 22 additions & 10 deletions Sources/KukaiCoreSwift/Models/Token.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ public class Token: Codable, CustomStringConvertible {
/// Recording if the position the index the user chose for the favourite token to appear
public var favouriteSortIndex: Int? = nil

/// The block level where the token was first seen
public var firstlevel: Decimal

/// The block level where the token was last seen
public var lastLevel: Decimal

/// The individual NFT's owned of this token type
public var nfts: [NFT]?

Expand Down Expand Up @@ -126,6 +132,8 @@ public class Token: Codable, CustomStringConvertible {
self.tokenId = tokenId
self.nfts = nfts
self.mintingTool = mintingTool
self.firstlevel = 0
self.lastLevel = 0

// TODO: make failable init
if let faVersion = faVersion, faVersion == .fa2 && tokenId == nil {
Expand All @@ -136,23 +144,25 @@ public class Token: Codable, CustomStringConvertible {
/**
Init a `Token` from an object returned by the TzKT API
*/
public init(from: TzKTBalanceToken, andTokenAmount: TokenAmount, stakedTokenAmount: TokenAmount? = nil, unstakedTokenAmount: TokenAmount? = nil) {
let decimalsString = from.metadata?.decimals ?? "0"
public init(from: TzKTBalance, andTokenAmount: TokenAmount, stakedTokenAmount: TokenAmount? = nil, unstakedTokenAmount: TokenAmount? = nil) {
let decimalsString = from.token.metadata?.decimals ?? "0"
let decimalsInt = Int(decimalsString) ?? 0
let isNFT = (from.metadata?.artifactUri != nil && decimalsInt == 0 && from.standard == .fa2)
let isNFT = (from.token.metadata?.artifactUri != nil && decimalsInt == 0 && from.token.standard == .fa2)

self.name = from.contract.alias
self.symbol = isNFT ? from.contract.alias ?? "" : from.displaySymbol
self.name = from.token.contract.alias
self.symbol = isNFT ? from.token.contract.alias ?? "" : from.token.displaySymbol
self.tokenType = isNFT ? .nonfungible : .fungible
self.faVersion = from.standard
self.faVersion = from.token.standard
self.balance = andTokenAmount
self.stakedBalance = stakedTokenAmount ?? .zeroBalance(decimalPlaces: andTokenAmount.decimalPlaces)
self.unstakedBalance = unstakedTokenAmount ?? .zeroBalance(decimalPlaces: andTokenAmount.decimalPlaces)
self.thumbnailURL = from.metadata?.thumbnailURL ?? TzKTClient.avatarURL(forToken: from.contract.address)
self.tokenContractAddress = from.contract.address
self.tokenId = Decimal(string: from.tokenId) ?? 0
self.thumbnailURL = from.token.metadata?.thumbnailURL ?? TzKTClient.avatarURL(forToken: from.token.contract.address)
self.tokenContractAddress = from.token.contract.address
self.tokenId = Decimal(string: from.token.tokenId) ?? 0
self.nfts = []
self.mintingTool = from.metadata?.mintingTool
self.mintingTool = from.token.metadata?.mintingTool
self.firstlevel = from.firstLevel
self.lastLevel = from.lastLevel

// TODO: make failable init
if let faVersion = faVersion, faVersion == .fa2 && tokenId == nil {
Expand Down Expand Up @@ -183,6 +193,8 @@ public class Token: Codable, CustomStringConvertible {
self.tokenId = Decimal(string: from.token.tokenId) ?? 0
self.nfts = []
self.mintingTool = from.mintingTool
self.firstlevel = from.level
self.lastLevel = from.level

// TODO: make failable init
if let faVersion = faVersion, faVersion == .fa2 && tokenId == nil {
Expand Down
2 changes: 1 addition & 1 deletion Tests/KukaiCoreSwiftTests/MockConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ public struct MockConstants {
public static let token10Decimals = Token(name: "Token 10 decimals", symbol: "TK10", tokenType: .fungible, faVersion: .fa2, balance: TokenAmount.zero(), thumbnailURL: nil, tokenContractAddress: "KT1G1cCRNBgQ48mVDjopHjEmTN5Sbtar8nn9", tokenId: 0, nfts: nil, mintingTool: nil)
public static let nftMetadata = TzKTBalanceMetadata(name: "NFT Name", symbol: "NFT", decimals: "0", formats: nil, displayUri: MediaProxySerivceTests.ipfsURIWithoutExtension, artifactUri: nil, thumbnailUri: MediaProxySerivceTests.ipfsURIWithExtension, description: "A sample description", mintingTool: nil, tags: nil, minter: nil, shouldPreferSymbol: nil, attributes: nil, ttl: nil)
public static let tokenWithNFTs = Token(name: "Token with NFTS", symbol: "T-NFT", tokenType: .nonfungible, faVersion: .fa2, balance: TokenAmount.zero(), thumbnailURL: nil, tokenContractAddress: "KT1G1cCRNBgQ48mVDjopHjEmTN5Sbtabc123", tokenId: 0, nfts: [
NFT(fromTzKTBalance: TzKTBalance(balance: "1", token: TzKTBalanceToken(contract: TzKTAddress(alias: nil, address: "KT1G1cCRNBgQ48mVDjopHjEmTN5Sbtabc123"), tokenId: "4", standard: .fa2, totalSupply: "1", metadata: MockConstants.nftMetadata)))
NFT(fromTzKTBalance: TzKTBalance(balance: "1", token: TzKTBalanceToken(contract: TzKTAddress(alias: nil, address: "KT1G1cCRNBgQ48mVDjopHjEmTN5Sbtabc123"), tokenId: "4", standard: .fa2, totalSupply: "1", metadata: MockConstants.nftMetadata), firstLevel: 0, lastLevel: 0))
], mintingTool: nil)


Expand Down
2 changes: 1 addition & 1 deletion Tests/KukaiCoreSwiftTests/Models/TokenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TokenTests: XCTestCase {
func testToken() {
let token = Token(name: "test1", symbol: "T", tokenType: .fungible, faVersion: .fa1_2, balance: TokenAmount(fromNormalisedAmount: 3, decimalPlaces: 4), thumbnailURL: URL(string: "ipfs://abcdefgh1234"), tokenContractAddress: "KT1abc", tokenId: nil, nfts: nil, mintingTool: nil)

let tzktBalance = TzKTBalance(balance: "1", token: TzKTBalanceToken(contract: TzKTAddress(alias: "Test Alias", address: "KT1abc"), tokenId: "0", standard: .fa2, totalSupply: "1", metadata: nil))
let tzktBalance = TzKTBalance(balance: "1", token: TzKTBalanceToken(contract: TzKTAddress(alias: "Test Alias", address: "KT1abc"), tokenId: "0", standard: .fa2, totalSupply: "1", metadata: nil), firstLevel: 0, lastLevel: 0)
let nft = NFT(fromTzKTBalance: tzktBalance)
let token2 = Token(name: "test2", symbol: "F", tokenType: .nonfungible, faVersion: .fa2, balance: TokenAmount.zero(), thumbnailURL: URL(string: "ipfs://abcdefgh1234"), tokenContractAddress: "KT1abc", tokenId: 0, nfts: [nft], mintingTool: nil)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ final class TzKTTransactionTests: XCTestCase {
transaction.processAdditionalData(withCurrentWalletAddress: "tz1abc")

if let json = try? JSONEncoder().encode(transaction), let jsonString = String(data: json, encoding: .utf8) {
XCTAssert(jsonString.count == 944, jsonString.count.description)
XCTAssert(jsonString.count == 973, jsonString.count.description)
} else {
XCTFail()
}
Expand Down

0 comments on commit ce9be84

Please sign in to comment.