Skip to content

Commit

Permalink
add baker data to new staking operations
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmcl committed Jun 18, 2024
1 parent cccb9a2 commit d98a73a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
15 changes: 10 additions & 5 deletions Sources/KukaiCoreSwift/Models/BakingBad/TzKTTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public struct TzKTTransaction: Codable, CustomStringConvertible, Hashable, Ident
public var target: TzKTAddress?
public let prevDelegate: TzKTAddress?
public let newDelegate: TzKTAddress?
public let baker: TzKTAddress?
public var amount: TokenAmount
public let parameter: [String: String]?
public let status: TransactionStatus
Expand Down Expand Up @@ -115,11 +116,11 @@ public struct TzKTTransaction: Codable, CustomStringConvertible, Hashable, Ident
// MARK: - Codable Protocol

public enum CodingKeys: String, CodingKey {
case type, id, level, timestamp, hash, counter, initiater, sender, bakerFee, storageFee, allocationFee, target, prevDelegate, newDelegate, amount, parameter, status, subType, entrypointCalled, primaryToken, hasInternals, tokenTransfersCount, errors, kind
case type, id, level, timestamp, hash, counter, initiater, sender, bakerFee, storageFee, allocationFee, target, prevDelegate, newDelegate, baker, amount, parameter, status, subType, entrypointCalled, primaryToken, hasInternals, tokenTransfersCount, errors, kind
}

/// Manually init a `TzKTTransaction`
public init(type: TransactionType, id: Decimal, level: Decimal, timestamp: String, hash: String, counter: Decimal, initiater: TzKTAddress?, sender: TzKTAddress, bakerFee: XTZAmount, storageFee: XTZAmount, allocationFee: XTZAmount, target: TzKTAddress?, prevDelegate: TzKTAddress?, newDelegate: TzKTAddress?, amount: TokenAmount, parameter: [String: String]?, status: TransactionStatus, hasInternals: Bool, tokenTransfersCount: Decimal?, errors: [TransactionError]?, kind: String?) {
public init(type: TransactionType, id: Decimal, level: Decimal, timestamp: String, hash: String, counter: Decimal, initiater: TzKTAddress?, sender: TzKTAddress, bakerFee: XTZAmount, storageFee: XTZAmount, allocationFee: XTZAmount, target: TzKTAddress?, prevDelegate: TzKTAddress?, newDelegate: TzKTAddress?, baker: TzKTAddress?, amount: TokenAmount, parameter: [String: String]?, status: TransactionStatus, hasInternals: Bool, tokenTransfersCount: Decimal?, errors: [TransactionError]?, kind: String?) {

self.type = type
self.id = id
Expand All @@ -135,6 +136,7 @@ public struct TzKTTransaction: Codable, CustomStringConvertible, Hashable, Ident
self.target = target
self.prevDelegate = prevDelegate
self.newDelegate = newDelegate
self.baker = baker
self.amount = amount
self.parameter = parameter
self.status = status
Expand Down Expand Up @@ -164,6 +166,7 @@ public struct TzKTTransaction: Codable, CustomStringConvertible, Hashable, Ident
self.target = from.to ?? from.token.contract
self.prevDelegate = nil
self.newDelegate = nil
self.baker = nil
self.amount = from.tokenAmount()
self.parameter = nil
self.status = .applied
Expand Down Expand Up @@ -193,6 +196,7 @@ public struct TzKTTransaction: Codable, CustomStringConvertible, Hashable, Ident
target = try? container.decode(TzKTAddress.self, forKey: .target)
prevDelegate = try? container.decode(TzKTAddress.self, forKey: .prevDelegate)
newDelegate = try? container.decode(TzKTAddress.self, forKey: .newDelegate)
baker = try? container.decodeIfPresent(TzKTAddress.self, forKey: .baker)
parameter = try? container.decodeIfPresent([String: String].self, forKey: .parameter)


Expand Down Expand Up @@ -244,6 +248,7 @@ public struct TzKTTransaction: Codable, CustomStringConvertible, Hashable, Ident
try container.encode(target, forKey: .target)
try container.encode(prevDelegate, forKey: .prevDelegate)
try container.encode(newDelegate, forKey: .newDelegate)
try container.encodeIfPresent(baker, forKey: .baker)
try container.encode(parameter, forKey: .parameter)
try container.encode(bakerFee.rpcRepresentation, forKey: .bakerFee)
try container.encode(storageFee.rpcRepresentation, forKey: .storageFee)
Expand All @@ -261,11 +266,11 @@ public struct TzKTTransaction: Codable, CustomStringConvertible, Hashable, Ident
}

/// Used for creating "Pending" transactions
public static func placeholder(withStatus status: TransactionStatus, id: Decimal, opHash: String, type: TransactionType, counter: Decimal, fromWallet: WalletMetadata, destination: TzKTAddress, xtzAmount: TokenAmount, parameters: [String: String]?, primaryToken: Token?, kind: String?) -> TzKTTransaction {
public static func placeholder(withStatus status: TransactionStatus, id: Decimal, opHash: String, type: TransactionType, counter: Decimal, fromWallet: WalletMetadata, destination: TzKTAddress, xtzAmount: TokenAmount, parameters: [String: String]?, primaryToken: Token?, baker: TzKTAddress?, kind: String?) -> TzKTTransaction {
let timestamp = TzKTTransaction.dateFormatter.string(from: Date())
let sender = TzKTAddress(alias: fromWallet.walletNickname ?? fromWallet.socialUsername ?? fromWallet.address, address: fromWallet.address)

var transaction = TzKTTransaction(type: .transaction, id: id, level: id, timestamp: timestamp, hash: opHash, counter: counter, initiater: nil, sender: sender, bakerFee: .zero(), storageFee: .zero(), allocationFee: .zero(), target: destination, prevDelegate: nil, newDelegate: nil, amount: xtzAmount, parameter: parameters, status: status, hasInternals: false, tokenTransfersCount: nil, errors: nil, kind: kind)
var transaction = TzKTTransaction(type: .transaction, id: id, level: id, timestamp: timestamp, hash: opHash, counter: counter, initiater: nil, sender: sender, bakerFee: .zero(), storageFee: .zero(), allocationFee: .zero(), target: destination, prevDelegate: nil, newDelegate: nil, baker: baker, amount: xtzAmount, parameter: parameters, status: status, hasInternals: false, tokenTransfersCount: nil, errors: nil, kind: kind)
transaction.processAdditionalData(withCurrentWalletAddress: fromWallet.address)

if let pToken = primaryToken {
Expand All @@ -280,7 +285,7 @@ public struct TzKTTransaction: Codable, CustomStringConvertible, Hashable, Ident
let timestamp = TzKTTransaction.dateFormatter.string(from: Date())
let sender = TzKTAddress(alias: fromWallet.walletNickname ?? fromWallet.socialUsername ?? fromWallet.address, address: fromWallet.address)

var transaction = TzKTTransaction(type: .delegation, id: id, level: id, timestamp: timestamp, hash: opHash, counter: counter, initiater: nil, sender: sender, bakerFee: .zero(), storageFee: .zero(), allocationFee: .zero(), target: nil, prevDelegate: nil, newDelegate: newDelegate, amount: .zero(), parameter: nil, status: status, hasInternals: false, tokenTransfersCount: nil, errors: nil, kind: nil)
var transaction = TzKTTransaction(type: .delegation, id: id, level: id, timestamp: timestamp, hash: opHash, counter: counter, initiater: nil, sender: sender, bakerFee: .zero(), storageFee: .zero(), allocationFee: .zero(), target: nil, prevDelegate: nil, newDelegate: newDelegate, baker: nil, amount: .zero(), parameter: nil, status: status, hasInternals: false, tokenTransfersCount: nil, errors: nil, kind: nil)
transaction.processAdditionalData(withCurrentWalletAddress: fromWallet.address)

return transaction
Expand Down
4 changes: 4 additions & 0 deletions Tests/KukaiCoreSwiftTests/Clients/TzKTClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,17 @@ class TzKTClientTests: XCTestCase {
XCTAssert(group.groupType == .unstake, group.groupType.rawValue)
XCTAssert(group.transactions.count == 1, group.transactions.count.description)
XCTAssert(group.transactions.first?.amount.description == "1", group.transactions.first?.amount.description ?? "-")
XCTAssert(group.transactions.first?.baker?.address == "tz1YgDUQV2eXm8pUWNz3S5aWP86iFzNp4jnD", group.transactions.first?.baker?.address ?? "-")
XCTAssert(group.transactions.first?.baker?.alias == "Baking Benjamins", group.transactions.first?.baker?.alias ?? "-")
XCTAssert(group.hash == "ooyVR1r5vt3K4JGoVnH2XLQjwAVpoZaAkfdG1PssCPPovi7m1FL", group.hash)
XCTAssert(group.status == .applied, group.status.rawValue)

case 20:
XCTAssert(group.groupType == .stake, group.groupType.rawValue)
XCTAssert(group.transactions.count == 1, group.transactions.count.description)
XCTAssert(group.transactions.first?.amount.description == "10", group.transactions.first?.amount.description ?? "-")
XCTAssert(group.transactions.first?.baker?.address == "tz1YgDUQV2eXm8pUWNz3S5aWP86iFzNp4jnD", group.transactions.first?.baker?.address ?? "-")
XCTAssert(group.transactions.first?.baker?.alias == "Baking Benjamins", group.transactions.first?.baker?.alias ?? "-")
XCTAssert(group.hash == "opPGcuZ459ZGR11RXaL2rRDtKnHFC9o5JQdyBHj3Qua4BMBkAsi", group.hash)
XCTAssert(group.status == .applied, group.status.rawValue)

Expand Down
12 changes: 6 additions & 6 deletions Tests/KukaiCoreSwiftTests/Models/TzKTTransactionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class TzKTTransactionTests: XCTestCase {
"value": "{\"to\": \"tz1eDdsp1d27kortm5LuoRqD6aKrdxmTiTSQ\", \"from\": \"tz1Z8E2gMHLXFcGGzW68B5e1Vk49FpHQd7xc\", \"value\": \"500000000000000000\"}"
]

var transaction = TzKTTransaction(type: .transaction, id: 123, level: 123, timestamp: "", hash: "", counter: 123, initiater: TzKTAddress(alias: nil, address: "tz1abc"), sender: TzKTAddress(alias: nil, address: "tz1abc"), bakerFee: .zero(), storageFee: .zero(), allocationFee: .zero(), target: TzKTAddress(alias: nil, address: "KT1GG8Zd5rUp1XV8nMPRBY2tSyVn6NR5F4Q1"), prevDelegate: nil, newDelegate: nil, amount: .zero(), parameter: transferTokenParameters, status: .applied, hasInternals: false, tokenTransfersCount: 0, errors: nil, kind: nil)
var transaction = TzKTTransaction(type: .transaction, id: 123, level: 123, timestamp: "", hash: "", counter: 123, initiater: TzKTAddress(alias: nil, address: "tz1abc"), sender: TzKTAddress(alias: nil, address: "tz1abc"), bakerFee: .zero(), storageFee: .zero(), allocationFee: .zero(), target: TzKTAddress(alias: nil, address: "KT1GG8Zd5rUp1XV8nMPRBY2tSyVn6NR5F4Q1"), prevDelegate: nil, newDelegate: nil, baker: nil, amount: .zero(), parameter: transferTokenParameters, status: .applied, hasInternals: false, tokenTransfersCount: 0, errors: nil, kind: nil)
transaction.processAdditionalData(withCurrentWalletAddress: "tz1abc")

let token = transaction.getFaTokenTransferData()
Expand All @@ -42,7 +42,7 @@ final class TzKTTransactionTests: XCTestCase {
"entrypoint": "transfer",
"value": "{\"to\": \"tz1eDdsp1d27kortm5LuoRqD6aKrdxmTiTSQ\", \"from\": \"tz1Z8E2gMHLXFcGGzW68B5e1Vk49FpHQd7xc\", \"value\": \"500000000000000000\"}"
]
let transaction1 = TzKTTransaction(type: .transaction, id: 1, level: 1, timestamp: "", hash: "", counter: 1, initiater: nil, sender: TzKTAddress(alias: nil, address: "tz1abc"), bakerFee: .zero(), storageFee: .zero(), allocationFee: .zero(), target: TzKTAddress(alias: nil, address: "tz1abc"), prevDelegate: nil, newDelegate: nil, amount: .zero(), parameter: dictParams, status: .confirmed, hasInternals: false, tokenTransfersCount: nil, errors: nil, kind: nil)
let transaction1 = TzKTTransaction(type: .transaction, id: 1, level: 1, timestamp: "", hash: "", counter: 1, initiater: nil, sender: TzKTAddress(alias: nil, address: "tz1abc"), bakerFee: .zero(), storageFee: .zero(), allocationFee: .zero(), target: TzKTAddress(alias: nil, address: "tz1abc"), prevDelegate: nil, newDelegate: nil, baker: nil, amount: .zero(), parameter: dictParams, status: .confirmed, hasInternals: false, tokenTransfersCount: nil, errors: nil, kind: nil)
let dict = transaction1.parameterValueAsDict()
let dictValue1 = dict?["to"] as? String
XCTAssert(dictValue1 == "tz1eDdsp1d27kortm5LuoRqD6aKrdxmTiTSQ", dictValue1 ?? "-")
Expand All @@ -53,7 +53,7 @@ final class TzKTTransactionTests: XCTestCase {
"entrypoint": "transfer",
"value": "[\"test\", 123, 14.7]"
]
let transaction2 = TzKTTransaction(type: .transaction, id: 1, level: 1, timestamp: "", hash: "", counter: 1, initiater: nil, sender: TzKTAddress(alias: nil, address: "tz1abc"), bakerFee: .zero(), storageFee: .zero(), allocationFee: .zero(), target: TzKTAddress(alias: nil, address: "tz1abc"), prevDelegate: nil, newDelegate: nil, amount: .zero(), parameter: arrayParams, status: .confirmed, hasInternals: false, tokenTransfersCount: nil, errors: nil, kind: nil)
let transaction2 = TzKTTransaction(type: .transaction, id: 1, level: 1, timestamp: "", hash: "", counter: 1, initiater: nil, sender: TzKTAddress(alias: nil, address: "tz1abc"), bakerFee: .zero(), storageFee: .zero(), allocationFee: .zero(), target: TzKTAddress(alias: nil, address: "tz1abc"), prevDelegate: nil, newDelegate: nil, baker: nil, amount: .zero(), parameter: arrayParams, status: .confirmed, hasInternals: false, tokenTransfersCount: nil, errors: nil, kind: nil)
let array = transaction2.parameterValueAsArray()
let arrayValue1 = array?[0] as? String
let arrayValue2 = array?[1] as? Int
Expand All @@ -67,7 +67,7 @@ final class TzKTTransactionTests: XCTestCase {
"entrypoint": "transfer",
"value": "[{\"key\": \"value\"}, {\"key\": 123}, {\"key\": 14.7}]"
]
let transaction3 = TzKTTransaction(type: .transaction, id: 1, level: 1, timestamp: "", hash: "", counter: 1, initiater: nil, sender: TzKTAddress(alias: nil, address: "tz1abc"), bakerFee: .zero(), storageFee: .zero(), allocationFee: .zero(), target: TzKTAddress(alias: nil, address: "tz1abc"), prevDelegate: nil, newDelegate: nil, amount: .zero(), parameter: arrayOfDictParams, status: .confirmed, hasInternals: false, tokenTransfersCount: nil, errors: nil, kind: nil)
let transaction3 = TzKTTransaction(type: .transaction, id: 1, level: 1, timestamp: "", hash: "", counter: 1, initiater: nil, sender: TzKTAddress(alias: nil, address: "tz1abc"), bakerFee: .zero(), storageFee: .zero(), allocationFee: .zero(), target: TzKTAddress(alias: nil, address: "tz1abc"), prevDelegate: nil, newDelegate: nil, baker: nil, amount: .zero(), parameter: arrayOfDictParams, status: .confirmed, hasInternals: false, tokenTransfersCount: nil, errors: nil, kind: nil)
let arryOfDict = transaction3.parameterValueAsArrayOfDictionary()
let arryOfDictValue1 = (arryOfDict?[0] as? [String: String])?["key"] as? String
let arryOfDictValue2 = (arryOfDict?[1] as? [String: Int])?["key"] as? Int
Expand All @@ -81,7 +81,7 @@ final class TzKTTransactionTests: XCTestCase {
let source = WalletMetadata(address: "tz1abc", hdWalletGroupName: nil, type: .hd, children: [], isChild: false, isWatchOnly: false, bas58EncodedPublicKey: "", backedUp: true)
let placeholder1 = TzKTTransaction.placeholder(withStatus: .unconfirmed, id: 567, opHash: "abc123", type: .transaction, counter: 0, fromWallet: source, newDelegate: TzKTAddress(alias: "Baking Benjamins", address: "tz1YgDUQV2eXm8pUWNz3S5aWP86iFzNp4jnD"))

let placeholder2 = TzKTTransaction.placeholder(withStatus: .unconfirmed, id: 456, opHash: "def456", type: .transaction, counter: 1, fromWallet: source, destination: TzKTAddress(alias: nil, address: "tz1def"), xtzAmount: .init(fromNormalisedAmount: 4.17, decimalPlaces: 6), parameters: nil, primaryToken: nil, kind: nil)
let placeholder2 = TzKTTransaction.placeholder(withStatus: .unconfirmed, id: 456, opHash: "def456", type: .transaction, counter: 1, fromWallet: source, destination: TzKTAddress(alias: nil, address: "tz1def"), xtzAmount: .init(fromNormalisedAmount: 4.17, decimalPlaces: 6), parameters: nil, primaryToken: nil, baker: nil, kind: nil)

XCTAssert(placeholder1.newDelegate?.address == "tz1YgDUQV2eXm8pUWNz3S5aWP86iFzNp4jnD", placeholder1.newDelegate?.address ?? "-")
XCTAssert(placeholder2.amount.description == "4.17", placeholder2.amount.description)
Expand All @@ -93,7 +93,7 @@ final class TzKTTransactionTests: XCTestCase {
"value": "{\"to\": \"tz1eDdsp1d27kortm5LuoRqD6aKrdxmTiTSQ\", \"from\": \"tz1Z8E2gMHLXFcGGzW68B5e1Vk49FpHQd7xc\", \"value\": \"500000000000000000\"}"
]

var transaction = TzKTTransaction(type: .transaction, id: 123, level: 123, timestamp: "", hash: "", counter: 123, initiater: TzKTAddress(alias: nil, address: "tz1abc"), sender: TzKTAddress(alias: nil, address: "tz1abc"), bakerFee: .zero(), storageFee: .zero(), allocationFee: .zero(), target: TzKTAddress(alias: nil, address: "KT1GG8Zd5rUp1XV8nMPRBY2tSyVn6NR5F4Q1"), prevDelegate: nil, newDelegate: nil, amount: .zero(), parameter: transferTokenParameters, status: .applied, hasInternals: false, tokenTransfersCount: 0, errors: nil, kind: nil)
var transaction = TzKTTransaction(type: .transaction, id: 123, level: 123, timestamp: "", hash: "", counter: 123, initiater: TzKTAddress(alias: nil, address: "tz1abc"), sender: TzKTAddress(alias: nil, address: "tz1abc"), bakerFee: .zero(), storageFee: .zero(), allocationFee: .zero(), target: TzKTAddress(alias: nil, address: "KT1GG8Zd5rUp1XV8nMPRBY2tSyVn6NR5F4Q1"), prevDelegate: nil, newDelegate: nil, baker: nil, amount: .zero(), parameter: transferTokenParameters, status: .applied, hasInternals: false, tokenTransfersCount: 0, errors: nil, kind: nil)
transaction.processAdditionalData(withCurrentWalletAddress: "tz1abc")

if let json = try? JSONEncoder().encode(transaction), let jsonString = String(data: json, encoding: .utf8) {
Expand Down

0 comments on commit d98a73a

Please sign in to comment.