Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nomination pools rewards #21

Merged
merged 29 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
86274ed
added base PaidOut support
vovunku Jul 10, 2023
5a607d0
add source(direct/pooled) field to reward
vovunku Jul 10, 2023
9a031b1
add source to AccumulatedReward
vovunku Jul 10, 2023
b057df3
removed source/added new stakingType
vovunku Jul 11, 2023
0091b56
added BondedSlash handler(not tested)
vovunku Jul 11, 2023
f0e335b
pooled slashes implemented(not tested yet)
vovunku Jul 11, 2023
f9c487f
first test for bonded slash added
vovunku Jul 14, 2023
2dfb406
more accounts
vovunku Jul 17, 2023
711cc22
add unbonding test
vovunku Jul 17, 2023
04988f1
fixed member point logic
vovunku Jul 17, 2023
0301848
Merge branch 'feature/nomination-pools-rewards' into feature/nomintai…
vovunku Jul 17, 2023
50d2e23
added all unbonding tests
vovunku Jul 17, 2023
338c501
fixed division by zero if poolPoints === 0
vovunku Jul 17, 2023
83ac959
Merge branch 'feature/nomination-pools-rewards' into feature/nomintai…
vovunku Jul 17, 2023
1b09033
removed copypaste
vovunku Jul 17, 2023
c6faed5
added test for pool rewards
vovunku Jul 17, 2023
5d2e693
added pool handlers to Polkadot, Westend and Aleph-zero
vovunku Jul 17, 2023
b19971f
removed console.log
vovunku Jul 17, 2023
ecc1cf7
renamed test
vovunku Jul 17, 2023
2ff283e
code refactoring
vovunku Jul 18, 2023
adb5e38
Merge branch 'feature/nomination-pools-rewards' into feature/nomintai…
vovunku Jul 18, 2023
72368bd
updated nomination pools path
vovunku Jul 18, 2023
3f34099
merged master for apy
vovunku Jul 18, 2023
f603550
fixed common.ts while merging
vovunku Jul 18, 2023
2837b82
base added
vovunku Jul 20, 2023
8b95164
Merge branch 'feature/nomination-pools-rewards' into feature/nomintai…
vovunku Jul 25, 2023
b457ba9
Revert "base added"
vovunku Jul 25, 2023
eabbd75
Merge branch 'feature/nomination-pools-rewards' into feature/nomintai…
vovunku Jul 25, 2023
8da30b9
Merge pull request #22 from novasamatech/feature/nomintaion_pools_test
vovunku Jul 26, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
first test for bonded slash added
  • Loading branch information
vovunku committed Jul 14, 2023
commit f9c487f4bd4c3311effb71186e50b8246fb83216
4 changes: 4 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
preset: 'ts-jest',
testMatch: ['**/tests/**/*.test.ts'],
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
"@subql/types": "1.7.3-2",
"@zeitgeistpm/type-defs": "^1.0.0",
"@zeroio/type-definitions": "^0.0.14",
"jest": "^29.3.1",
"jest": "^29.5.0",
"moonbeam-types-bundle": "^2.0.8",
"ts-jest": "^29.0.3",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"@types/jest": "^29.5.1",
"typescript": "^4.1.3"
},
"exports": {
Expand Down
74 changes: 74 additions & 0 deletions tests/reward.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { AccumulatedReward, Reward, RewardType } from '../src/types';
import { handleRelaychainPooledStakingBondedSlash } from "../src/mappings/rewards/history/relaychain"
import { SubstrateTestEventBuilder, mockNumber, mockAddress } from "./utils/mockFunctions"


const MOCK_GENESIS = "0xb0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafe"
const DIRECT_STAKING_TYPE = "relaychain"
const POOLED_STAKING_TYPE = "nomination-pool"

const mockBondedPools = {
42: {
unwrap : function() {
return {points: mockNumber(1000)}
}
}
}

// Mock the API object using Jest
let mockAPI = {
queryMulti: jest.fn((data) => {return data}),
query: {
staking: {
payee: {
isStaked: true,
}
},
nominationPools: {
bondedPools: async function(poolId) {
return mockBondedPools[poolId]
},

poolMembers: {
entries: async function() {
return [
[
mockAddress("JHXFqYWQFFr5RkHVzviRiKhY7tutyGcYQb6kUyoScSir862"),
{
points: mockNumber(100)
}
]
]
}
}
}
},
};

describe('handlePoolBondedSlash', () => {
let rewardEvent
let poolId
let slashAmount

beforeAll(() => {
(global as any).api = mockAPI;
poolId = mockNumber(42)
slashAmount = mockNumber(1000)

rewardEvent = new SubstrateTestEventBuilder().buildEventForPoolSlashed(poolId, slashAmount)
});

it('Slash for account calculated correctly', async () => {
jest.spyOn(AccumulatedReward, "get").mockResolvedValue(undefined)
jest.spyOn(AccumulatedReward.prototype, "save").mockImplementation(function (this: AccumulatedReward) {
console.log(this)
return Promise.resolve()
})
jest.spyOn(Reward.prototype, "save").mockImplementation(function (this: Reward) {
expect(this.amount).toBe(BigInt(100))
return Promise.resolve()
})

await handleRelaychainPooledStakingBondedSlash(rewardEvent, MOCK_GENESIS, POOLED_STAKING_TYPE);
});
});
61 changes: 61 additions & 0 deletions tests/utils/mockFunctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { AnyTuple } from '@polkadot/types-codec/types';
import { IEvent } from '@polkadot/types/types';
import { EventRecord } from '@polkadot/types/interfaces';
import { SubstrateBlock, SubstrateEvent, SubstrateExtrinsic } from "@subql/types";

export class SubstrateTestEventBuilder<T extends AnyTuple = AnyTuple> {
private _event: Partial<SubstrateEvent> = {};

withExtrinsic(extrinsic: SubstrateExtrinsic): SubstrateTestEventBuilder<T> {
this._event.extrinsic = extrinsic;
return this;
}

withBlock(timestamp: Date = new Date(), blockNumber: number = 1): SubstrateTestEventBuilder<T> {
this._event.block = {
timestamp: timestamp,
block: {
header: {
number: mockNumber(blockNumber)
}
}
} as SubstrateBlock
return this;
}

withEvent(eventData): SubstrateTestEventBuilder<T> {
this._event.event = {data: eventData, method: 'method', section: 'section'} as unknown as IEvent<AnyTuple, unknown>
this._event.block.events = []
const event = {event: {data: eventData, method: 'method', section: 'section'}} as EventRecord
this._event.block.events.push(event)
return this
}

build(): SubstrateEvent<T> {
return this._event as SubstrateEvent<T>;
}

buildEventForRewards(rewardAccountId, amount): SubstrateEvent<T> {
return this.withBlock().withEvent([rewardAccountId, amount]).build()
}

buildEventForPoolSlashed(poolId, amount): SubstrateEvent<T> {
return this.withBlock().withEvent([poolId, amount]).build()
}
}


export function mockAddress(toStringValue: string): unknown {
return {
toString: jest.fn().mockReturnValue(toStringValue),
toRawType: jest.fn().mockReturnValue('AccountId')
}
}

export function mockNumber(number: number): unknown {
return {
toString: jest.fn().mockReturnValue(number.toString()),
toNumber: jest.fn().mockReturnValue(number),
toBigInt: jest.fn().mockReturnValue(BigInt(number))
}
}