diff --git a/packages/sdk-governance/src/bounties/bounties-sdk.ts b/packages/sdk-governance/src/bounties/bounties-sdk.ts index c297a9d..b66bb70 100644 --- a/packages/sdk-governance/src/bounties/bounties-sdk.ts +++ b/packages/sdk-governance/src/bounties/bounties-sdk.ts @@ -247,7 +247,7 @@ export function createBountiesSdk(typedApi: BountiesSdkTypedApi): BountiesSdk { } return { - watchBounties, + watch: watchBounties(), getBounties, getBounty, getProposedBounty, diff --git a/packages/sdk-governance/src/bounties/child-bounties-sdk.ts b/packages/sdk-governance/src/bounties/child-bounties-sdk.ts index f0154d6..d2a6b9a 100644 --- a/packages/sdk-governance/src/bounties/child-bounties-sdk.ts +++ b/packages/sdk-governance/src/bounties/child-bounties-sdk.ts @@ -16,6 +16,7 @@ import { ChildBounty, GenericChildBounty, } from "./child-sdk-types" +import { keyedMemo } from "@/util/memo" export function createChildBountiesSdk( typedApi: ChildBountiesSdkTypedApi, @@ -180,7 +181,7 @@ export function createChildBountiesSdk( } return { - watchChildBounties, + watch: keyedMemo(watchChildBounties, new Map()), getChildBounty, } } diff --git a/packages/sdk-governance/src/bounties/child-sdk-types.ts b/packages/sdk-governance/src/bounties/child-sdk-types.ts index e9d3a6e..f6a2e55 100644 --- a/packages/sdk-governance/src/bounties/child-sdk-types.ts +++ b/packages/sdk-governance/src/bounties/child-sdk-types.ts @@ -58,7 +58,7 @@ export type ChildBounty = | PendingPayoutChildBounty export interface ChildBountiesSdk { - watchChildBounties(parentId: number): { + watch(parentId: number): { bounties$: Observable> bountyIds$: Observable getBountyById$: (key: number) => Observable diff --git a/packages/sdk-governance/src/bounties/find-referenda.ts b/packages/sdk-governance/src/bounties/find-referenda.ts index 231cb15..cccc1e6 100644 --- a/packages/sdk-governance/src/bounties/find-referenda.ts +++ b/packages/sdk-governance/src/bounties/find-referenda.ts @@ -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 = [ @@ -11,7 +11,7 @@ const spenderOrigins = [ "BigTipper", ] -const getDecodedSpenderReferenda = weakMemo( +const getDecodedSpenderReferenda = keyedMemo( async (ongoingReferenda: OngoingReferendum[]) => { const spenderReferenda = ongoingReferenda.filter( (ref) => @@ -35,6 +35,7 @@ const getDecodedSpenderReferenda = weakMemo( ) return response.filter((v) => !!v) }, + new WeakMap(), ) export async function findApprovingReferenda( diff --git a/packages/sdk-governance/src/bounties/sdk-types.ts b/packages/sdk-governance/src/bounties/sdk-types.ts index eaa81c1..f897e0f 100644 --- a/packages/sdk-governance/src/bounties/sdk-types.ts +++ b/packages/sdk-governance/src/bounties/sdk-types.ts @@ -94,7 +94,7 @@ export type Bounty = | PendingPayoutBounty export interface BountiesSdk { - watchBounties(): { + watch: { bounties$: Observable> bountyIds$: Observable getBountyById$: (key: number) => Observable diff --git a/packages/sdk-governance/src/referenda/referenda-sdk.ts b/packages/sdk-governance/src/referenda/referenda-sdk.ts index 45e46e3..3ffc096 100644 --- a/packages/sdk-governance/src/referenda/referenda-sdk.ts +++ b/packages/sdk-governance/src/referenda/referenda-sdk.ts @@ -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 + }, } } @@ -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, @@ -180,7 +194,7 @@ export function createReferendaSdk( bytes: proposal, }).decodedCall, typedApi.tx.Referenda.submit({ - enactment_moment: enactment, + enactment_moment, proposal: { type: "Lookup", value: { @@ -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) => diff --git a/packages/sdk-governance/src/referenda/sdk-types.ts b/packages/sdk-governance/src/referenda/sdk-types.ts index e98dbd7..0da4453 100644 --- a/packages/sdk-governance/src/referenda/sdk-types.ts +++ b/packages/sdk-governance/src/referenda/sdk-types.ts @@ -31,6 +31,7 @@ export type OngoingReferendum = Omit & { getDetails: (apiKey: string) => Promise getConfirmationStart: () => Promise getConfirmationEnd: () => Promise + getTrack: () => Promise } export interface ReferendaSdkConfig { @@ -68,8 +69,10 @@ export interface ReferendaSdk { createReferenda( origin: PolkadotRuntimeOriginCaller, - enactment: TraitsScheduleDispatchTime, proposal: Binary, + options?: Partial<{ + enactment: TraitsScheduleDispatchTime + }>, ): Transaction createSpenderReferenda( callData: Binary, diff --git a/packages/sdk-governance/src/util/memo.ts b/packages/sdk-governance/src/util/memo.ts index 2332163..3b57fce 100644 --- a/packages/sdk-governance/src/util/memo.ts +++ b/packages/sdk-governance/src/util/memo.ts @@ -1,12 +1,14 @@ -export const weakMemo = (fn: (...arg: Arg) => R) => { - const cache = new WeakMap() - return (...arg: Arg) => { +export const keyedMemo = + ( + fn: (...arg: Arg) => R, + cache: Map | WeakMap, + ) => + (...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 = , R>(fn: (...arg: Arg) => R) => { let cachedKey: Arg | null = null