Skip to content

Commit

Permalink
feat(txwrapper-substrate): Add nominationPools calls (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
TarikGul authored Oct 16, 2023
1 parent 44594f5 commit 856262a
Show file tree
Hide file tree
Showing 46 changed files with 1,677 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/txwrapper-core/src/types/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export interface OptionsWithMeta extends Options {
*/
export type Args = Record<string, AnyJson>;

/**
* Empty args.
*/
export type EmptyArgs = Record<string, never>;

/**
* Format used in txwrapper to represent a method.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/txwrapper-substrate/src/methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * as assets from './assets';
export * as balances from './balances';
export * as democracy from './democracy';
export * as multisig from './multisig';
export * as nominationPools from './nominationPools';
export * as proxy from './proxy';
export * as session from './session';
export * as staking from './staking';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
itHasCorrectBaseTxInfo,
KUSAMA_TEST_OPTIONS,
TEST_BASE_TX_INFO,
} from '@substrate/txwrapper-dev';

import { TEST_METHOD_ARGS } from '../../test-helpers';
import { bondExtra } from './bondExtra';

describe('nominationPools::bondExtra', () => {
it('should work', () => {
const unsigned = bondExtra(
TEST_METHOD_ARGS.nominationPools.bondExtra,
TEST_BASE_TX_INFO,
KUSAMA_TEST_OPTIONS
);

itHasCorrectBaseTxInfo(unsigned);

expect(unsigned.method).toBe('0x29010040420f00000000000000000000000000');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {
Args,
BaseTxInfo,
defineMethod,
OptionsWithMeta,
UnsignedTransaction,
} from '@substrate/txwrapper-core';

export interface NominationPoolsBondExtra extends Args {
extra: {
/**
* Take from the free balance.
*/
FreeBalance: string | null;
/**
* Take the entire amount from the accumulated rewards.
*/
Rewards: boolean | null;
};
}

/**
* Bond `extra` more funds from `origin` into the pool to which they already belong.
*
* Additional funds can come from either the free balance of the account, or from the
* accumulated rewards, see [`BondExtra`].
*
* Bonding extra funds implies an automatic payout of all pending rewards as well.
* See `bond_extra_other` to bond pending rewards of `other` members.
*
* NOTE: this transaction is implemented with the sole purpose of readability and
* correctness, not optimization. We read/write several storage items multiple times instead
* of just once, in the spirit of reusing code.
*
* @param args - Arguments specific to this method.
* @param info - Information required to construct the transaction.
* @param options - Registry and metadata used for constructing the method.
*/
export function bondExtra(
args: NominationPoolsBondExtra,
info: BaseTxInfo,
options: OptionsWithMeta
): UnsignedTransaction {
return defineMethod(
{
method: {
args,
name: 'bondExtra',
pallet: 'nominationPools',
},
...info,
},
options
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
itHasCorrectBaseTxInfo,
KUSAMA_TEST_OPTIONS,
TEST_BASE_TX_INFO,
} from '@substrate/txwrapper-dev';

import { TEST_METHOD_ARGS } from '../../test-helpers';
import { bondExtraOther } from './bondExtraOther';

describe('nominationPools::bondExtraOther', () => {
it('should work', () => {
const unsigned = bondExtraOther(
TEST_METHOD_ARGS.nominationPools.bondExtraOther,
TEST_BASE_TX_INFO,
KUSAMA_TEST_OPTIONS
);

itHasCorrectBaseTxInfo(unsigned);

expect(unsigned.method).toBe(
'0x290e00d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d0040420f00000000000000000000000000'
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
BaseTxInfo,
defineMethod,
OptionsWithMeta,
UnsignedTransaction,
} from '@substrate/txwrapper-core';

import { NominationPoolsBondExtra } from './bondExtra';

export interface NominationPoolsBondExtraOther
extends NominationPoolsBondExtra {
/**
* AccountId.
*/
member: string;
}

/**
* `origin` bonds funds from `extra` for some pool member `member` into their respective
* pools.
*
* `origin` can bond extra funds from free balance or pending rewards when `origin ==
* other`.
*
* In the case of `origin != other`, `origin` can only bond extra pending rewards of
* `other` members assuming set_claim_permission for the given member is
* `PermissionlessAll` or `PermissionlessCompound`.
*
* @param args - Arguments specific to this method.
* @param info - Information required to construct the transaction.
* @param options - Registry and metadata used for constructing the method.
*/
export function bondExtraOther(
args: NominationPoolsBondExtraOther,
info: BaseTxInfo,
options: OptionsWithMeta
): UnsignedTransaction {
return defineMethod(
{
method: {
args,
name: 'bondExtraOther',
pallet: 'nominationPools',
},
...info,
},
options
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
itHasCorrectBaseTxInfo,
KUSAMA_TEST_OPTIONS,
TEST_BASE_TX_INFO,
} from '@substrate/txwrapper-dev';

import { TEST_METHOD_ARGS } from '../../test-helpers';
import { chill } from './chill';

describe('nominationPools::chill', () => {
it('should work', () => {
const unsigned = chill(
TEST_METHOD_ARGS.nominationPools.chill,
TEST_BASE_TX_INFO,
KUSAMA_TEST_OPTIONS
);

itHasCorrectBaseTxInfo(unsigned);

expect(unsigned.method).toBe('0x290d04000000');
});
});
45 changes: 45 additions & 0 deletions packages/txwrapper-substrate/src/methods/nominationPools/chill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
Args,
BaseTxInfo,
defineMethod,
OptionsWithMeta,
UnsignedTransaction,
} from '@substrate/txwrapper-core';

export interface NominationPoolsChill extends Args {
/**
* A valid PoolId.
*/
poolId: number | string;
}

/**
* Chill on behalf of the pool.
*
* The dispatch origin of this call must be signed by the pool nominator or the pool
* root role, same as [`Pallet::nominate`].
*
* This directly forwards the call to the staking pallet, on behalf of the pool bonded
* account.
*
* @param args - Arguments specific to this method.
* @param info - Information required to construct the transaction.
* @param options - Registry and metadata used for constructing the method.
*/
export function chill(
args: NominationPoolsChill,
info: BaseTxInfo,
options: OptionsWithMeta
): UnsignedTransaction {
return defineMethod(
{
method: {
args,
name: 'chill',
pallet: 'nominationPools',
},
...info,
},
options
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
itHasCorrectBaseTxInfo,
KUSAMA_TEST_OPTIONS,
TEST_BASE_TX_INFO,
} from '@substrate/txwrapper-dev';

import { TEST_METHOD_ARGS } from '../../test-helpers';
import { claimCommission } from './claimCommission';

describe('nominationPools::claimCommission', () => {
it('should work', () => {
const unsigned = claimCommission(
TEST_METHOD_ARGS.nominationPools.claimCommission,
TEST_BASE_TX_INFO,
KUSAMA_TEST_OPTIONS
);

itHasCorrectBaseTxInfo(unsigned);

expect(unsigned.method).toBe('0x291404000000');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
Args,
BaseTxInfo,
defineMethod,
OptionsWithMeta,
UnsignedTransaction,
} from '@substrate/txwrapper-core';

export interface NominationPoolsClaimCommission extends Args {
/**
* A valid poolId.
*/
poolId: number | string;
}

/**
* Claim pending commission.
*
* The dispatch origin of this call must be signed by the `root` role of the pool. Pending
* commission is paid out and added to total claimed commission`. Total pending commission
* is reset to zero. the current.
*
* @param args - Arguments specific to this method.
* @param info - Information required to construct the transaction.
* @param options - Registry and metadata used for constructing the method.
*/
export function claimCommission(
args: NominationPoolsClaimCommission,
info: BaseTxInfo,
options: OptionsWithMeta
): UnsignedTransaction {
return defineMethod(
{
method: {
args,
name: 'claimCommission',
pallet: 'nominationPools',
},
...info,
},
options
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {
itHasCorrectBaseTxInfo,
KUSAMA_TEST_OPTIONS,
TEST_BASE_TX_INFO,
} from '@substrate/txwrapper-dev';

import { claimPayout } from './claimPayout';

describe('nominationPools::claimPayout', () => {
it('should work', () => {
const unsigned = claimPayout({}, TEST_BASE_TX_INFO, KUSAMA_TEST_OPTIONS);

itHasCorrectBaseTxInfo(unsigned);

expect(unsigned.method).toBe('0x2902');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {
BaseTxInfo,
defineMethod,
EmptyArgs,
OptionsWithMeta,
UnsignedTransaction,
} from '@substrate/txwrapper-core';

/**
* A bonded member can use this to claim their payout based on the rewards that the pool
* has accumulated since their last claimed payout (OR since joining if this is their first
* time claiming rewards). The payout will be transferred to the member's account.
*
* The member will earn rewards pro rata based on the members stake vs the sum of the
* members in the pools stake. Rewards do not "expire".
*
* See `claim_payout_other` to caim rewards on bahalf of some `other` pool member.
*
* @param args - Arguments specific to this method.
* @param info - Information required to construct the transaction.
* @param options - Registry and metadata used for constructing the method.
*/
export function claimPayout(
args: EmptyArgs,
info: BaseTxInfo,
options: OptionsWithMeta
): UnsignedTransaction {
return defineMethod(
{
method: {
args,
name: 'claimPayout',
pallet: 'nominationPools',
},
...info,
},
options
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
itHasCorrectBaseTxInfo,
KUSAMA_TEST_OPTIONS,
TEST_BASE_TX_INFO,
} from '@substrate/txwrapper-dev';

import { TEST_METHOD_ARGS } from '../../test-helpers';
import { claimPayoutOther } from './claimPayoutOther';

describe('nominationPools::claimPayoutOther', () => {
it('should work', () => {
const unsigned = claimPayoutOther(
TEST_METHOD_ARGS.nominationPools.claimPayoutOther,
TEST_BASE_TX_INFO,
KUSAMA_TEST_OPTIONS
);

itHasCorrectBaseTxInfo(unsigned);

expect(unsigned.method).toBe(
'0x2910d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d'
);
});
});
Loading

0 comments on commit 856262a

Please sign in to comment.