Skip to content

Commit

Permalink
update rewards estimator to deal with changes in the TzKT api
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmcl committed Jun 5, 2024
1 parent 20c37e6 commit 27ee93d
Show file tree
Hide file tree
Showing 11 changed files with 2,315 additions and 940 deletions.
11 changes: 10 additions & 1 deletion Sources/KukaiCoreSwift/Clients/TzKTClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class TzKTClient {
public static let tokenBalanceQuerySize = 10000
}

static let numberOfFutureCyclesReturned = 5
static var numberOfFutureCyclesReturned = 2

private let networkService: NetworkService
private let config: TezosNodeClientConfig
Expand Down Expand Up @@ -550,6 +550,15 @@ public class TzKTClient {
return
}

for (index, reward) in res.enumerated() {
if reward.futureBlocks == 0 {
// Number of future cycles is 1 less than the index of the first reward where `futureBlocks` is equal to zero
// The first non zero is the current, in-progress block
TzKTClient.numberOfFutureCyclesReturned = (index-1)
break
}
}

currentRewards = res
dispatchGroup.leave()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public struct TzKTDelegatorReward: Codable {
public let extraBlockRewards: Decimal
public let missedExtraBlockRewards: Decimal

public let futureBlocks: Decimal
public let futureBlockRewards: Decimal
public let futureEndorsementRewards: Decimal

Expand Down
26 changes: 13 additions & 13 deletions Tests/KukaiCoreSwiftTests/Clients/TzKTClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -343,22 +343,22 @@ class TzKTClientTests: XCTestCase {
MockConstants.shared.tzktClient.estimateLastAndNextReward(forAddress: MockConstants.defaultHdWallet.address, delegate: delegate) { result in
switch result {
case .success(let rewards):
XCTAssert(rewards.previousReward?.amount.description == "0.207106", rewards.previousReward?.amount.description ?? "")
XCTAssert(rewards.previousReward?.fee.description == "0.05", rewards.previousReward?.fee.description ?? "")
XCTAssert(rewards.previousReward?.cycle.description == "515", rewards.previousReward?.cycle.description ?? "")
XCTAssert(rewards.previousReward?.bakerAlias == "Bake Nug", rewards.previousReward?.bakerAlias ?? "")
XCTAssert(rewards.previousReward?.amount.description == "0.5598", rewards.previousReward?.amount.description ?? "")
XCTAssert(rewards.previousReward?.fee.description == "0.2", rewards.previousReward?.fee.description ?? "")
XCTAssert(rewards.previousReward?.cycle.description == "742", rewards.previousReward?.cycle.description ?? "")
XCTAssert(rewards.previousReward?.bakerAlias == "Baking Benjamins", rewards.previousReward?.bakerAlias ?? "")

XCTAssert(rewards.estimatedPreviousReward?.amount.normalisedRepresentation == "0.197861", rewards.estimatedPreviousReward?.amount.normalisedRepresentation ?? "")
XCTAssert(rewards.estimatedPreviousReward?.fee.description == "0.05", rewards.estimatedPreviousReward?.fee.description ?? "")
XCTAssert(rewards.estimatedPreviousReward?.cycle.description == "516", rewards.estimatedPreviousReward?.cycle.description ?? "")
XCTAssert(rewards.estimatedPreviousReward?.bakerAlias == "Bake Nug", rewards.estimatedPreviousReward?.bakerAlias ?? "")
XCTAssert(rewards.estimatedPreviousReward?.amount.normalisedRepresentation == "0.5598", rewards.estimatedPreviousReward?.amount.normalisedRepresentation ?? "")
XCTAssert(rewards.estimatedPreviousReward?.fee.description == "0.2", rewards.estimatedPreviousReward?.fee.description ?? "")
XCTAssert(rewards.estimatedPreviousReward?.cycle.description == "742", rewards.estimatedPreviousReward?.cycle.description ?? "")
XCTAssert(rewards.estimatedPreviousReward?.bakerAlias == "Baking Benjamins", rewards.estimatedPreviousReward?.bakerAlias ?? "")

XCTAssert(rewards.estimatedNextReward?.amount.normalisedRepresentation == "0.034051", rewards.estimatedNextReward?.amount.normalisedRepresentation ?? "")
XCTAssert(rewards.estimatedNextReward?.fee.description == "0.05", rewards.estimatedNextReward?.fee.description ?? "")
XCTAssert(rewards.estimatedNextReward?.cycle.description == "517", rewards.estimatedNextReward?.cycle.description ?? "")
XCTAssert(rewards.estimatedNextReward?.bakerAlias == "Bake Nug", rewards.estimatedNextReward?.bakerAlias ?? "")
XCTAssert(rewards.estimatedNextReward?.amount.normalisedRepresentation == "0.722042", rewards.estimatedNextReward?.amount.normalisedRepresentation ?? "")
XCTAssert(rewards.estimatedNextReward?.fee.description == "0.2", rewards.estimatedNextReward?.fee.description ?? "")
XCTAssert(rewards.estimatedNextReward?.cycle.description == "743", rewards.estimatedNextReward?.cycle.description ?? "")
XCTAssert(rewards.estimatedNextReward?.bakerAlias == "Baking Benjamins", rewards.estimatedNextReward?.bakerAlias ?? "")

XCTAssert(rewards.moreThan1CycleBetweenPreiousAndNext() == true)
XCTAssert(rewards.moreThan1CycleBetweenPreiousAndNext() == false)

case .failure(let error):
XCTFail("Error: \(error)")
Expand Down
14 changes: 11 additions & 3 deletions Tests/KukaiCoreSwiftTests/MockConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public struct MockConstants {
var bakingBadConfigURL2 = bakingBadURL.appendingPathComponent("v2/bakers/tz1ZgkTFmiwddPXGbs4yc6NWdH4gELW7wsnv")
bakingBadConfigURL2.appendQueryItem(name: "configs", value: "true")

var bakingBadConfigURL3 = bakingBadURL.appendingPathComponent("v2/bakers/tz1S5WxdZR5f9NzsPXhr7L9L1vrEb5spZFur")
bakingBadConfigURL3.appendQueryItem(name: "configs", value: "true")

var tzktsuggestURL1 = tzktURL.appendingPathComponent("v1/suggest/accounts/Bake Nug Payouts")
tzktsuggestURL1.appendQueryItem(name: "limit", value: 1)

Expand All @@ -105,20 +108,23 @@ public struct MockConstants {
var tzktsuggestURL3 = tzktURL.appendingPathComponent("v1/suggest/accounts/The Payouts")
tzktsuggestURL3.appendQueryItem(name: "limit", value: 1)

var tzktsuggestURL4 = tzktURL.appendingPathComponent("v1/suggest/accounts/Baking Benjamins Payouts")
tzktsuggestURL4.appendQueryItem(name: "limit", value: 1)

var tzktLastBakerRewardURL = tzktURL.appendingPathComponent("v1/accounts/tz1Ue76bLW7boAcJEZf2kSGcamdBKVi4Kpss/operations")
tzktLastBakerRewardURL.appendQueryItem(name: "limit", value: 1)
tzktLastBakerRewardURL.appendQueryItem(name: "type", value: "transaction")
tzktLastBakerRewardURL.appendQueryItem(name: "sender.in", value: "tz1ZgkTFmiwddPXGbs4yc6NWdH4gELW7wsnv,tz1fwnfJNgiDACshK9avfRfFbMaXrs3ghoJa,tz1XWEi47p85eUwRZk2Xsp9VobSwUFq7GYKi,tz1ShireJgwr8ag5dETMY4RNqkXeu1YgyDYC")
tzktLastBakerRewardURL.appendQueryItem(name: "sender.in", value: "tz1ZgkTFmiwddPXGbs4yc6NWdH4gELW7wsnv,tz1S5WxdZR5f9NzsPXhr7L9L1vrEb5spZFur,tz1ShireJgwr8ag5dETMY4RNqkXeu1YgyDYC,tz1gnuBF9TbBcgHPV2mUE96tBrW7PxqRmx1h")

var tzktLastBakerRewardURL2 = tzktURL.appendingPathComponent("v1/accounts/tz1Ue76bLW7boAcJEZf2kSGcamdBKVi4Kpss/operations")
tzktLastBakerRewardURL2.appendQueryItem(name: "limit", value: 1)
tzktLastBakerRewardURL2.appendQueryItem(name: "type", value: "transaction")
tzktLastBakerRewardURL2.appendQueryItem(name: "sender.in", value: "tz1ZgkTFmiwddPXGbs4yc6NWdH4gELW7wsnv,tz1fwnfJNgiDACshK9avfRfFbMaXrs3ghoJa,tz1ShireJgwr8ag5dETMY4RNqkXeu1YgyDYC,tz1XWEi47p85eUwRZk2Xsp9VobSwUFq7GYKi")
tzktLastBakerRewardURL2.appendQueryItem(name: "sender.in", value: "tz1ZgkTFmiwddPXGbs4yc6NWdH4gELW7wsnv,tz1S5WxdZR5f9NzsPXhr7L9L1vrEb5spZFur,tz1gnuBF9TbBcgHPV2mUE96tBrW7PxqRmx1h,tz1ShireJgwr8ag5dETMY4RNqkXeu1YgyDYC")

var tzktLastBakerRewardURL3 = tzktURL.appendingPathComponent("v1/accounts/tz1Ue76bLW7boAcJEZf2kSGcamdBKVi4Kpss/operations")
tzktLastBakerRewardURL3.appendQueryItem(name: "limit", value: 1)
tzktLastBakerRewardURL3.appendQueryItem(name: "type", value: "transaction")
tzktLastBakerRewardURL3.appendQueryItem(name: "sender.in", value: "tz1ZgkTFmiwddPXGbs4yc6NWdH4gELW7wsnv,tz1fwnfJNgiDACshK9avfRfFbMaXrs3ghoJa,tz1XWEi47p85eUwRZk2Xsp9VobSwUFq7GYKi")
tzktLastBakerRewardURL3.appendQueryItem(name: "sender.in", value: "tz1ZgkTFmiwddPXGbs4yc6NWdH4gELW7wsnv,tz1fwnfJNgiDACshK9avfRfFbMaXrs3ghoJa,tz1gnuBF9TbBcgHPV2mUE96tBrW7PxqRmx1h")

var tzktDelegatesURL = tzktURL.appendingPathComponent("v1/delegates")
tzktDelegatesURL.appendQueryItem(name: "select.values", value: "address,alias,balance,stakingBalance")
Expand Down Expand Up @@ -175,9 +181,11 @@ public struct MockConstants {
tzktDelegatorRewardsURL: (MockConstants.jsonStub(fromFilename: "tzkt_delegator-rewards"), MockConstants.http200),
bakingBadConfigURL1: (MockConstants.jsonStub(fromFilename: "tzkt_baker-config-tz1fwnfJNgiDACshK9avfRfFbMaXrs3ghoJa"), MockConstants.http200),
bakingBadConfigURL2: (MockConstants.jsonStub(fromFilename: "tzkt_baker-config-tz1ZgkTFmiwddPXGbs4yc6NWdH4gELW7wsnv"), MockConstants.http200),
bakingBadConfigURL3: (MockConstants.jsonStub(fromFilename: "tzkt_baker-config-tz1S5WxdZR5f9NzsPXhr7L9L1vrEb5spZFur"), MockConstants.http200),
tzktsuggestURL1: (MockConstants.jsonStub(fromFilename: "tzkt_suggest-bake-nug"), MockConstants.http200),
tzktsuggestURL2: (MockConstants.jsonStub(fromFilename: "tzkt_suggest-the-shire"), MockConstants.http200),
tzktsuggestURL3: (MockConstants.jsonStub(fromFilename: "tzkt_suggest-the-shire_updated"), MockConstants.http200),
tzktsuggestURL4: (MockConstants.jsonStub(fromFilename: "tzkt_suggest-baking-benjamins"), MockConstants.http200),
tzktLastBakerRewardURL: (MockConstants.jsonStub(fromFilename: "tzkt_last-baker-payment"), MockConstants.http200),
tzktLastBakerRewardURL2: (MockConstants.jsonStub(fromFilename: "tzkt_last-baker-payment"), MockConstants.http200),
tzktLastBakerRewardURL3: (MockConstants.jsonStub(fromFilename: "tzkt_last-baker-payment_updated"), MockConstants.http200),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
{
"address": "tz1S5WxdZR5f9NzsPXhr7L9L1vrEb5spZFur",
"name": "Baking Benjamins",
"logo": "https://services.tzkt.io/v1/avatars/tz1S5WxdZR5f9NzsPXhr7L9L1vrEb5spZFur",
"balance": 63510.843938,
"stakingBalance": 626388.733175,
"stakingCapacity": 635108.43938,
"maxStakingBalance": 635108.43938,
"freeSpace": 8719.706205000053,
"fee": 0.2,
"minDelegation": 0.01,
"payoutDelay": 1,
"payoutPeriod": 1,
"openForDelegation": true,
"estimatedRoi": 0.049882,
"serviceType": "tezos_only",
"serviceHealth": "active",
"payoutTiming": "stable",
"payoutAccuracy": "inaccurate",
"audit": "6064b1d379aafa90137ab034",
"insuranceCoverage": 0,
"config": {
"address": "tz1S5WxdZR5f9NzsPXhr7L9L1vrEb5spZFur",
"fee": [
{
"cycle": 438,
"value": 0.2
},
{
"cycle": 334,
"value": 0.1
},
{
"cycle": 246,
"value": 0.06
},
{
"cycle": 0,
"value": 0.08
}
],
"minDelegation": [
{
"cycle": 569,
"value": 0.01
},
{
"cycle": 278,
"value": 1
},
{
"cycle": 244,
"value": 50
},
{
"cycle": 0,
"value": 100
}
],
"payoutFee": [
{
"cycle": 0,
"value": false
}
],
"payoutDelay": [
{
"cycle": 245,
"value": 1
},
{
"cycle": 244,
"value": 2
},
{
"cycle": 243,
"value": 3
},
{
"cycle": 242,
"value": 4
},
{
"cycle": 241,
"value": 5
},
{
"cycle": 0,
"value": 6
}
],
"payoutPeriod": [
{
"cycle": 0,
"value": 1
}
],
"minPayout": [
{
"cycle": 0,
"value": 0
}
],
"rewardStruct": [
{
"cycle": 0,
"value": 21
}
],
"payoutRatio": [
{
"cycle": 0,
"value": 666
}
],
"maxStakingThreshold": [
{
"cycle": 0,
"value": 1
}
],
"openForDelegation": [
{
"cycle": 0,
"value": true
}
],
"allocationFee": [
{
"cycle": 0,
"value": true
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
"address": "tz1ZgkTFmiwddPXGbs4yc6NWdH4gELW7wsnv",
"name": "The Shire",
"logo": "https://services.tzkt.io/v1/avatars/tz1ZgkTFmiwddPXGbs4yc6NWdH4gELW7wsnv",
"balance": 13765.509375,
"stakingBalance": 113473.999574,
"stakingCapacity": 133000,
"maxStakingBalance": 133000,
"freeSpace": 19526.000426,
"balance": 40340.153319,
"stakingBalance": 405649.076449,
"stakingCapacity": 400000,
"maxStakingBalance": 400000,
"freeSpace": -5649.076448999986,
"fee": 0.042,
"minDelegation": 0,
"payoutDelay": 1,
"payoutPeriod": 1,
"openForDelegation": true,
"estimatedRoi": 0.058841,
"estimatedRoi": 0.058902,
"serviceType": "tezos_only",
"serviceHealth": "active",
"payoutTiming": "stable",
"payoutAccuracy": "precise",
"audit": "62947959d0970a602a90d424",
"audit": "6565bfaaa6632298f27ed779",
"insuranceCoverage": 0,
"config": {
"address": "tz1ZgkTFmiwddPXGbs4yc6NWdH4gELW7wsnv",
Expand All @@ -34,6 +34,14 @@
}
],
"payoutFee": [
{
"cycle": 617,
"value": true
},
{
"cycle": 529,
"value": false
},
{
"cycle": 0,
"value": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
"address": "tz1fwnfJNgiDACshK9avfRfFbMaXrs3ghoJa",
"name": "Bake Nug",
"logo": "https://services.tzkt.io/v1/avatars/tz1fwnfJNgiDACshK9avfRfFbMaXrs3ghoJa",
"balance": 295299.99798,
"stakingBalance": 2930551.45779,
"stakingCapacity": 2952999.9798,
"maxStakingBalance": 2952999.9798,
"freeSpace": 22448.522009999957,
"fee": 0.05,
"balance": 187878.327672,
"stakingBalance": 1663793.545668,
"stakingCapacity": 1878783.27672,
"maxStakingBalance": 1878783.27672,
"freeSpace": 214989.73105200008,
"fee": 0.1498,
"minDelegation": 5,
"payoutDelay": 6,
"payoutPeriod": 1,
"openForDelegation": true,
"estimatedRoi": 0.058402,
"estimatedRoi": 0.053012,
"serviceType": "tezos_only",
"serviceHealth": "active",
"payoutTiming": "stable",
Expand All @@ -22,6 +22,14 @@
"config": {
"address": "tz1fwnfJNgiDACshK9avfRfFbMaXrs3ghoJa",
"fee": [
{
"cycle": 690,
"value": 0.1498
},
{
"cycle": 551,
"value": 0.0795
},
{
"cycle": 392,
"value": 0.05
Expand Down
Loading

0 comments on commit 27ee93d

Please sign in to comment.