This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
207 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import * as Z from "../deps/zones.ts" | ||
import * as rpc from "../rpc/mod.ts" | ||
import * as U from "../util/mod.ts" | ||
import { entryMetadata, metadata, palletMetadata } from "./metadata.ts" | ||
import { state } from "./rpc_known_methods.ts" | ||
import * as scale from "./scale.ts" | ||
|
||
export function entryReadRaw<Client extends Z.$<rpc.Client>>(client: Client) { | ||
return < | ||
PalletName extends Z.$<string>, | ||
EntryName extends Z.$<string>, | ||
Keys extends unknown[], | ||
Rest extends [blockHash?: Z.$<U.HexHash | undefined>], | ||
>( | ||
palletName: PalletName, | ||
entryName: EntryName, | ||
keys: [...Keys], | ||
...[blockHash]: [...Rest] | ||
) => { | ||
const metadata_ = metadata(client)(blockHash) | ||
const deriveCodec_ = scale.deriveCodec(metadata_) | ||
const palletMetadata_ = palletMetadata(metadata_, palletName) | ||
const entryMetadata_ = entryMetadata(palletMetadata_, entryName) | ||
const $storageKey_ = scale.$storageKey(deriveCodec_, palletMetadata_, entryMetadata_) | ||
const storageKey = scale.scaleEncoded($storageKey_, Z.ls(...keys)).next(U.hex.encode) | ||
return state.getStorage(client)(storageKey, blockHash) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import * as Z from "../deps/zones.ts" | ||
import { entryRead, entryReadRaw, keyPageRead } from "../effects/mod.ts" | ||
import { extrinsic } from "../mod.ts" | ||
import { MultiAddress } from "../primitives/mod.ts" | ||
import * as rpc from "../rpc/mod.ts" | ||
import { multisigAddress, u8a } from "../util/mod.ts" | ||
|
||
interface Timepoint { | ||
height: number | ||
index: number | ||
} | ||
|
||
interface RatifyProps { | ||
sender: MultiAddress | ||
call: unknown | ||
maybeTimepoint?: Timepoint | ||
} | ||
|
||
interface VoteProps { | ||
sender: MultiAddress | ||
callHash: Uint8Array | ||
maybeTimepoint?: Timepoint | ||
} | ||
|
||
export class Multisig<Client extends Z.Effect<rpc.Client>> { | ||
readonly address | ||
constructor( | ||
readonly client: Client, | ||
readonly signatories: Uint8Array[], | ||
readonly threshold: number, | ||
) { | ||
this.address = multisigAddress(signatories, threshold) | ||
} | ||
|
||
ratify<Props extends Z.Rec$<RatifyProps>>({ | ||
sender, | ||
call, | ||
maybeTimepoint, | ||
}: Props) { | ||
const maxWeight = extrinsic(this.client)({ | ||
sender, | ||
call, | ||
}) | ||
.feeEstimate | ||
.access("weight") | ||
return extrinsic(this.client)({ | ||
sender, | ||
call: Z.rec({ | ||
type: "Multisig", | ||
value: Z.rec({ | ||
type: "asMulti", | ||
threshold: this.threshold, | ||
call, | ||
otherSignatories: Z.ls(sender).next(([sender]) => | ||
this.signatories.filter((value) => !u8a.isEqual(value, sender.value!)) | ||
), | ||
storeCall: false, | ||
maxWeight, | ||
maybeTimepoint, | ||
}), | ||
}), | ||
}) | ||
} | ||
|
||
vote<Props extends Z.Rec$<VoteProps>>({ | ||
sender, | ||
callHash, | ||
maybeTimepoint, | ||
}: Props) { | ||
return extrinsic(this.client)({ | ||
sender, | ||
call: Z.rec({ | ||
type: "Multisig", | ||
value: Z.rec({ | ||
type: "approveAsMulti", | ||
threshold: this.threshold, | ||
callHash, | ||
otherSignatories: Z.ls(sender).next(([sender]) => | ||
this.signatories.filter((value) => !u8a.isEqual(value, sender.value!)) | ||
), | ||
storeCall: false, | ||
maxWeight: { | ||
refTime: 0n, | ||
proofSize: 0n, | ||
}, | ||
maybeTimepoint, | ||
}), | ||
}), | ||
}) | ||
} | ||
|
||
proposals(count: number) { | ||
return keyPageRead(this.client)("Multisig", "Multisigs", count, [this.address]) | ||
} | ||
|
||
proposal(callHash: Z.$<Uint8Array>) { | ||
return entryRead(this.client)("Multisig", "Multisigs", [this.address, callHash]) | ||
} | ||
|
||
isProposed(callHash: Z.$<Uint8Array>) { | ||
return entryReadRaw(this.client)("Multisig", "Multisigs", [this.address, callHash]).next( | ||
(entry: any) => entry !== null, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export function isEqual(a: Uint8Array, b: Uint8Array) { | ||
if (a.length !== b.length) { | ||
return false | ||
} | ||
for (let i = 0; i < a.length; i++) { | ||
if (a[i] !== b[i]) { | ||
return false | ||
} | ||
} | ||
return true | ||
} |