Skip to content

Commit

Permalink
Merge pull request #2 from polkadot-api/vo-referenda-sdk
Browse files Browse the repository at this point in the history
feat: make enactment optional, add `getTrack` to referendum.
  • Loading branch information
voliva authored Jan 15, 2025
2 parents b585d7e + 86e44b8 commit 22e6386
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/sdk-governance/src/bounties/bounties-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export function createBountiesSdk(typedApi: BountiesSdkTypedApi): BountiesSdk {
}

return {
watchBounties,
watch: watchBounties(),
getBounties,
getBounty,
getProposedBounty,
Expand Down
3 changes: 2 additions & 1 deletion packages/sdk-governance/src/bounties/child-bounties-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
ChildBounty,
GenericChildBounty,
} from "./child-sdk-types"
import { keyedMemo } from "@/util/memo"

export function createChildBountiesSdk(
typedApi: ChildBountiesSdkTypedApi,
Expand Down Expand Up @@ -180,7 +181,7 @@ export function createChildBountiesSdk(
}

return {
watchChildBounties,
watch: keyedMemo(watchChildBounties, new Map()),
getChildBounty,
}
}
2 changes: 1 addition & 1 deletion packages/sdk-governance/src/bounties/child-sdk-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export type ChildBounty =
| PendingPayoutChildBounty

export interface ChildBountiesSdk {
watchChildBounties(parentId: number): {
watch(parentId: number): {
bounties$: Observable<Map<number, ChildBounty>>
bountyIds$: Observable<number[]>
getBountyById$: (key: number) => Observable<ChildBounty>
Expand Down
5 changes: 3 additions & 2 deletions packages/sdk-governance/src/bounties/find-referenda.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OngoingReferendum } from "@/referenda/sdk-types"
import { weakMemo } from "@/util/memo"
import { keyedMemo } from "@/util/memo"
import { MultiAddress } from "./descriptors"

const spenderOrigins = [
Expand All @@ -11,7 +11,7 @@ const spenderOrigins = [
"BigTipper",
]

const getDecodedSpenderReferenda = weakMemo(
const getDecodedSpenderReferenda = keyedMemo(
async (ongoingReferenda: OngoingReferendum[]) => {
const spenderReferenda = ongoingReferenda.filter(
(ref) =>
Expand All @@ -35,6 +35,7 @@ const getDecodedSpenderReferenda = weakMemo(
)
return response.filter((v) => !!v)
},
new WeakMap(),
)

export async function findApprovingReferenda(
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-governance/src/bounties/sdk-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export type Bounty =
| PendingPayoutBounty

export interface BountiesSdk {
watchBounties(): {
watch: {
bounties$: Observable<Map<number, Bounty>>
bountyIds$: Observable<number[]>
getBountyById$: (key: number) => Observable<Bounty>
Expand Down
29 changes: 18 additions & 11 deletions packages/sdk-governance/src/referenda/referenda-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ export function createReferendaSdk(

return confirmationStart + track.confirm_period
},
async getTrack() {
const track = await getTrack(referendum.track)
if (!track) {
// Should never happen
throw new Error("Track not found")
}
return track
},
}
}

Expand Down Expand Up @@ -156,12 +164,18 @@ export function createReferendaSdk(

const createReferenda: ReferendaSdk["createReferenda"] = (
origin,
enactment,
proposal,
options,
) => {
// The pallet already calculates uses the earliest_allowed in case it's too small
const enactment_moment = options?.enactment ?? {
type: "After",
value: 0,
}

if (proposal.asBytes().length <= MAX_INLINE_SIZE) {
return typedApi.tx.Referenda.submit({
enactment_moment: enactment,
enactment_moment,
proposal: {
type: "Inline",
value: proposal,
Expand All @@ -180,7 +194,7 @@ export function createReferendaSdk(
bytes: proposal,
}).decodedCall,
typedApi.tx.Referenda.submit({
enactment_moment: enactment,
enactment_moment,
proposal: {
type: "Lookup",
value: {
Expand All @@ -200,14 +214,7 @@ export function createReferendaSdk(
) => {
const spenderTrack = getSpenderTrack(value)

return createReferenda(
spenderTrack.origin,
{
type: "After",
value: 0,
},
callData,
)
return createReferenda(spenderTrack.origin, callData)
}

const getSubmittedReferendum = (txEvent: TxEvent) =>
Expand Down
5 changes: 4 additions & 1 deletion packages/sdk-governance/src/referenda/sdk-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type OngoingReferendum = Omit<RawOngoingReferendum, "proposal"> & {
getDetails: (apiKey: string) => Promise<ReferendumDetails>
getConfirmationStart: () => Promise<number | null>
getConfirmationEnd: () => Promise<number | null>
getTrack: () => Promise<ReferendaTrack>
}

export interface ReferendaSdkConfig {
Expand Down Expand Up @@ -68,8 +69,10 @@ export interface ReferendaSdk {

createReferenda(
origin: PolkadotRuntimeOriginCaller,
enactment: TraitsScheduleDispatchTime,
proposal: Binary,
options?: Partial<{
enactment: TraitsScheduleDispatchTime
}>,
): Transaction<any, string, string, unknown>
createSpenderReferenda(
callData: Binary,
Expand Down
10 changes: 6 additions & 4 deletions packages/sdk-governance/src/util/memo.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
export const weakMemo = <Arg extends [object], R>(fn: (...arg: Arg) => R) => {
const cache = new WeakMap<Arg[0], R>()
return (...arg: Arg) => {
export const keyedMemo =
<Arg extends [any], R>(
fn: (...arg: Arg) => R,
cache: Map<Arg[0], R> | WeakMap<Arg[0], R>,
) =>
(...arg: Arg) => {
if (cache.has(arg[0])) return cache.get(arg[0])!
const result = fn(...arg)
cache.set(arg[0], result)
return result
}
}

export const memo = <Arg extends Array<unknown>, R>(fn: (...arg: Arg) => R) => {
let cachedKey: Arg | null = null
Expand Down

0 comments on commit 22e6386

Please sign in to comment.