Skip to content

Commit

Permalink
add unit tests for baking rewards with no previous payout to test ear…
Browse files Browse the repository at this point in the history
…ly users, catching the previous issue
  • Loading branch information
simonmcl committed Jun 20, 2024
1 parent 7342af3 commit 1e3c41c
Show file tree
Hide file tree
Showing 4 changed files with 495 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Tests/KukaiCoreSwiftTests/Clients/TzKTClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,34 @@ class TzKTClientTests: XCTestCase {
wait(for: [expectation], timeout: 120)
}

func testEstimateRewardsNoPrevious() {
let expectation = XCTestExpectation(description: "tzkt-testEstimateRewardsNoPrevious")
let delegate = TzKTAccountDelegate(alias: "The Shire", address: "tz1ZgkTFmiwddPXGbs4yc6NWdH4gELW7wsnv", active: true)

MockConstants.shared.tzktClient.estimateLastAndNextReward(forAddress: "tz1iv8r8UUCEZK5gqpLPnMPzP4VRJBJUdGgr", delegate: delegate) { result in
switch result {
case .success(let rewards):
XCTAssert(rewards.previousReward == nil, rewards.previousReward?.amount.description ?? "")

XCTAssert(rewards.estimatedPreviousReward == nil, rewards.estimatedPreviousReward?.amount.normalisedRepresentation ?? "")

XCTAssert(rewards.estimatedNextReward?.amount.normalisedRepresentation == "0.000368", rewards.estimatedNextReward?.amount.normalisedRepresentation ?? "")
XCTAssert(rewards.estimatedNextReward?.fee.description == "0.042", rewards.estimatedNextReward?.fee.description ?? "")
XCTAssert(rewards.estimatedNextReward?.cycle.description == "743", rewards.estimatedNextReward?.cycle.description ?? "")
XCTAssert(rewards.estimatedNextReward?.bakerAlias == "The Shire", rewards.estimatedNextReward?.bakerAlias ?? "")

XCTAssert(rewards.moreThan1CycleBetweenPreiousAndNext() == false)

case .failure(let error):
XCTFail("Error: \(error)")
}

expectation.fulfill()
}

wait(for: [expectation], timeout: 120)
}

func testBakers() {
let expectation = XCTestExpectation(description: "tzkt-bakers")

Expand Down
14 changes: 14 additions & 0 deletions Tests/KukaiCoreSwiftTests/MockConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public struct MockConstants {
var tzktDelegatorRewardsURL = tzktURL.appendingPathComponent("v1/rewards/delegators/tz1Ue76bLW7boAcJEZf2kSGcamdBKVi4Kpss")
tzktDelegatorRewardsURL.appendQueryItem(name: "limit", value: 25)

var tzktDelegatorRewardsNoPreviousURL = tzktURL.appendingPathComponent("v1/rewards/delegators/tz1iv8r8UUCEZK5gqpLPnMPzP4VRJBJUdGgr")
tzktDelegatorRewardsNoPreviousURL.appendQueryItem(name: "limit", value: 25)

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

Expand All @@ -99,6 +102,9 @@ public struct MockConstants {
var bakingBadConfigURL3 = bakingBadURL.appendingPathComponent("v2/bakers/tz1S5WxdZR5f9NzsPXhr7L9L1vrEb5spZFur")
bakingBadConfigURL3.appendQueryItem(name: "configs", value: "true")

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

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

Expand Down Expand Up @@ -126,6 +132,11 @@ public struct MockConstants {
tzktLastBakerRewardURL3.appendQueryItem(name: "type", value: "transaction")
tzktLastBakerRewardURL3.appendQueryItem(name: "sender.in", value: "tz1ZgkTFmiwddPXGbs4yc6NWdH4gELW7wsnv,tz1S5WxdZR5f9NzsPXhr7L9L1vrEb5spZFur,tz1gnuBF9TbBcgHPV2mUE96tBrW7PxqRmx1h")

var tzktLastBakerRewardURL4 = tzktURL.appendingPathComponent("v1/accounts/tz1iv8r8UUCEZK5gqpLPnMPzP4VRJBJUdGgr/operations")
tzktLastBakerRewardURL4.appendQueryItem(name: "limit", value: 1)
tzktLastBakerRewardURL4.appendQueryItem(name: "type", value: "transaction")
tzktLastBakerRewardURL4.appendQueryItem(name: "sender.in", value: "tz1ZgkTFmiwddPXGbs4yc6NWdH4gELW7wsnv,tz1ShireJgwr8ag5dETMY4RNqkXeu1YgyDYC")

var tzktDelegatesURL = tzktURL.appendingPathComponent("v1/delegates")
tzktDelegatesURL.appendQueryItem(name: "select.values", value: "address,alias,balance,stakingBalance")
tzktDelegatesURL.appendQueryItem(name: "active", value: "true")
Expand Down Expand Up @@ -179,16 +190,19 @@ public struct MockConstants {
tzktBalancePageURL: (MockConstants.jsonStub(fromFilename: "tzkt_balance-page"), MockConstants.http200),
tzktCyclesURL: (MockConstants.jsonStub(fromFilename: "tzkt_cycles"), MockConstants.http200),
tzktDelegatorRewardsURL: (MockConstants.jsonStub(fromFilename: "tzkt_delegator-rewards"), MockConstants.http200),
tzktDelegatorRewardsNoPreviousURL: (MockConstants.jsonStub(fromFilename: "tzkt_delegator-rewards-no-previous"), 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),
bakingBadConfigURL4: (MockConstants.jsonStub(fromFilename: "tzkt_baker-config-tz1aRoaRhSpRYvFdyvgWLL6TGyRoGF51wDjM"), 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),
tzktLastBakerRewardURL4: (MockConstants.jsonStub(fromFilename: "tzkt_last-baker-payment_updated"), MockConstants.http200),
tzktDelegatesURL: (MockConstants.jsonStub(fromFilename: "tzkt_ghostnet-bakers"), MockConstants.http200),

// Media proxy
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
{
"address": "tz1ZgkTFmiwddPXGbs4yc6NWdH4gELW7wsnv",
"name": "The Shire",
"logo": "https://services.tzkt.io/v1/avatars/tz1ZgkTFmiwddPXGbs4yc6NWdH4gELW7wsnv",
"balance": 5326177.599918,
"stakingBalance": 44473420.298689,
"stakingCapacity": 53261775.99918,
"maxStakingBalance": 53261775.99918,
"freeSpace": 8788355.700490996,
"fee": 0.15,
"minDelegation": 0,
"payoutDelay": 1,
"payoutPeriod": 1,
"openForDelegation": true,
"estimatedRoi": 0.053113,
"serviceType": "multiasset",
"serviceHealth": "active",
"payoutTiming": "stable",
"payoutAccuracy": "precise",
"audit": "601aabbe5e0a85c68fb18d13",
"insuranceCoverage": 0,
"config": {
"address": "tz1ZgkTFmiwddPXGbs4yc6NWdH4gELW7wsnv",
"fee": [
{
"cycle": 559,
"value": 0.15
},
{
"cycle": 473,
"value": 0.109
},
{
"cycle": 468,
"value": 0.05
},
{
"cycle": 440,
"value": 0.109
},
{
"cycle": 437,
"value": 0.12
},
{
"cycle": 407,
"value": 0.15
},
{
"cycle": 265,
"value": 0.08
},
{
"cycle": 0,
"value": 0.05
}
],
"minDelegation": [
{
"cycle": 0,
"value": 0
}
],
"payoutFee": [
{
"cycle": 0,
"value": false
}
],
"payoutDelay": [
{
"cycle": 467,
"value": 1
},
{
"cycle": 466,
"value": 2
},
{
"cycle": 465,
"value": 3
},
{
"cycle": 464,
"value": 4
},
{
"cycle": 463,
"value": 5
},
{
"cycle": 0,
"value": 6
}
],
"payoutPeriod": [
{
"cycle": 0,
"value": 1
}
],
"minPayout": [
{
"cycle": 0,
"value": 0.001
}
],
"rewardStruct": [
{
"cycle": 0,
"value": 981
}
],
"payoutRatio": [
{
"cycle": 0,
"value": 666
}
],
"maxStakingThreshold": [
{
"cycle": 0,
"value": 1
}
],
"openForDelegation": [
{
"cycle": 0,
"value": true
}
],
"allocationFee": [
{
"cycle": 0,
"value": false
}
]
}
}
Loading

0 comments on commit 1e3c41c

Please sign in to comment.