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: make enactment optional, add getTrack to referendum. #2

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Changes from all 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
2 changes: 1 addition & 1 deletion packages/sdk-governance/src/bounties/bounties-sdk.ts
Original file line number Diff line number Diff line change
@@ -247,7 +247,7 @@ export function createBountiesSdk(typedApi: BountiesSdkTypedApi): BountiesSdk {
}

return {
watchBounties,
watch: watchBounties(),
getBounties,
getBounty,
getProposedBounty,
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
@@ -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,
}
}
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
@@ -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>
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 = [
@@ -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(
2 changes: 1 addition & 1 deletion packages/sdk-governance/src/bounties/sdk-types.ts
Original file line number Diff line number Diff line change
@@ -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>
29 changes: 18 additions & 11 deletions packages/sdk-governance/src/referenda/referenda-sdk.ts
Original file line number Diff line number Diff line change
@@ -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) =>
5 changes: 4 additions & 1 deletion packages/sdk-governance/src/referenda/sdk-types.ts
Original file line number Diff line number Diff line change
@@ -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 {
@@ -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,
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