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

feat(governance): Add conviction voting sdk #6

Merged
merged 8 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
4 changes: 4 additions & 0 deletions packages/sdk-governance/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ export type * from "./bounties/sdk-types"
export { createChildBountiesSdk } from "./bounties/child-bounties-sdk"
export type * from "./bounties/child-descriptors"
export type * from "./bounties/child-sdk-types"

export { createConvictionVotingSdk } from "./voting/voting-sdk"
export type * from "./voting/descriptors"
export type * from "./voting/sdk-types"
106 changes: 106 additions & 0 deletions packages/sdk-governance/src/voting/descriptors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { MultiAddress } from "@/bounties/descriptors"
import { SdkDefinition } from "@polkadot-api/common-sdk-utils"
import {
ApisTypedef,
Enum,
PalletsTypedef,
PlainDescriptor,
SS58String,
StorageDescriptor,
TxDescriptor,
TypedApi,
} from "polkadot-api"

export type ConvictionVotingVoteAccountVote = Enum<{
Standard: {
vote: number
balance: bigint
}
Split: {
aye: bigint
nay: bigint
}
SplitAbstain: {
aye: bigint
nay: bigint
abstain: bigint
}
}>

export type VotingConviction = Enum<{
None: undefined
Locked1x: undefined
Locked2x: undefined
Locked3x: undefined
Locked4x: undefined
Locked5x: undefined
Locked6x: undefined
}>

export type ConvictionVotingVoteVoting = Enum<{
Casting: {
votes: Array<[number, ConvictionVotingVoteAccountVote]>
delegations: {
votes: bigint
capital: bigint
}
prior: [number, bigint]
}
Delegating: {
balance: bigint
target: SS58String
conviction: VotingConviction
delegations: {
votes: bigint
capital: bigint
}
prior: [number, bigint]
}
}>

type VotingSdkPallets = PalletsTypedef<
{
ConvictionVoting: {
VotingFor: StorageDescriptor<
[SS58String, number],
ConvictionVotingVoteVoting,
false,
never
>
}
},
{
ConvictionVoting: {
vote: TxDescriptor<{
poll_index: number
vote: ConvictionVotingVoteAccountVote
}>
delegate: TxDescriptor<{
class: number
to: MultiAddress
conviction: VotingConviction
balance: bigint
}>
undelegate: TxDescriptor<{
class: number
}>
unlock: TxDescriptor<{
class: number
target: MultiAddress
}>
remove_vote: TxDescriptor<{
class: number | undefined
index: number
}>
}
},
{},
{},
{
ConvictionVoting: {
VoteLockingPeriod: PlainDescriptor<number>
}
}
>
type VotingSdkDefinition = SdkDefinition<VotingSdkPallets, ApisTypedef<{}>>
export type VotingSdkTypedApi = TypedApi<VotingSdkDefinition>
128 changes: 128 additions & 0 deletions packages/sdk-governance/src/voting/sdk-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { ReferendumInfo } from "@/referenda/descriptors"
import { SS58String, Transaction } from "polkadot-api"
import { Observable } from "rxjs"
import { VotingConviction } from "./descriptors"

export type PollOutcome = {
ended: number
side: "aye" | "nay"
} | null
export const getReferendumOutcome = (referendum: ReferendumInfo) => {
if (referendum.type === "Approved" || referendum.type === "Rejected") {
return {
ended: referendum.value[0],
side: referendum.type === "Approved" ? "aye" : "nay",
}
}
return null
}

/**
* Types:
* - free: The funds locked by this vote will become free once the vote is removed.
* - locked: The funds locked by this vote will become locked once the vote is removed.
* - extends: Removing this vote will extend the duration of a pre-existing lock.
* - extended: Removing this vote before its end will have it locked for the duration of the pre-existing lock.
*/
export type VoteLock =
| {
type: "free"
}
| {
type: "locked" | "extends" | "extended"
end: number
}

export interface StandardVote {
type: "standard"
poll: number
direction: "aye" | "nay" | "abstain"
balance: bigint
conviction: VotingConviction

getLock(outcome: PollOutcome): VoteLock
remove(): Transaction<any, string, string, unknown>
}
export interface SplitVote {
type: "split"
poll: number
balance: bigint

aye: bigint
nay: bigint
abstain: bigint

getLock(outcome: PollOutcome): VoteLock
remove(): Transaction<any, string, string, unknown>
}
export type Vote = StandardVote | SplitVote

interface TrackDetails {
track: number
delegationPower: DelegationPower
lock: {
block: number
balance: bigint
} | null
unlock(): Transaction<any, string, string, unknown>
}
export type UnlockSchedule = Array<{
block: number
balance: bigint
unlocks: Array<
| {
type: "poll"
id: number
}
| {
type: "lock"
}
>
}>

export interface TrackCasting extends TrackDetails {
type: "casting"
votes: Vote[]
getUnlockSchedule(pollOutcomes: Record<number, PollOutcome>): UnlockSchedule
}
export interface TrackDelegating extends TrackDetails {
type: "delegation"
target: SS58String
balance: bigint
conviction: VotingConviction
lockDuration: number
remove(): Transaction<any, string, string, unknown>
}
export type VotingTrack = TrackCasting | TrackDelegating

export interface DelegationPower {
track: number
votes: bigint
capital: bigint
}

export interface ConvictionVotingSdk {
getVotingTracks(account: SS58String): Promise<Array<VotingTrack>>
getVotingTrack(account: SS58String, track: number): Promise<VotingTrack>
votingTracks$(account: SS58String): Observable<Array<VotingTrack>>
votingTrack$(account: SS58String, track: number): Observable<VotingTrack>

vote(
vote: "aye" | "nay",
poll: number,
value: bigint,
conviction?: VotingConviction,
): Transaction<any, string, string, unknown>
voteAbstain(
poll: number,
value: bigint,
): Transaction<any, string, string, unknown>
voteSplit(
poll: number,
vote: Partial<{
aye: bigint
nay: bigint
abstain: bigint
}>,
): Transaction<any, string, string, unknown>
}
Loading
Loading