Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

chore: improve users util #813

Merged
merged 6 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
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
17 changes: 11 additions & 6 deletions crypto/test_pairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,23 @@ function pair(secret: string) {
export function testUser(userId: number) {
return Sr25519.fromSeed(blake2_256.hash(new TextEncoder().encode(`capi-test-user-${userId}`)))
}

export function testUserFactory(url: string) {
return async function users<N extends number>(count: N): Promise<ArrayOfLength<Sr25519, N>> {
return createUsers
function createUsers(): Promise<Record<typeof NAMES[number], Sr25519>>
function createUsers<N extends number>(count: N): Promise<ArrayOfLength<Sr25519, N>>
async function createUsers(count?: number): Promise<Record<string, Sr25519> | Sr25519[]> {
const response = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ count }),
body: JSON.stringify({ count: count ?? NAMES.length }),
})
if (!response.ok) throw new Error(await response.text())
const { index }: { index: number } = await response.json()
const userIds: Sr25519[] = []
for (let i = index; i < index + count; i++) userIds.push(testUser(i))
return userIds as ArrayOfLength<Sr25519, N>
return typeof count === "number"
? Array.from({ length: count }, (_, i) => testUser(index + i))
: Object.fromEntries(NAMES.map((name, i) => [name, testUser(index + i)]))
}
}

// dprint-ignore-next-line
const NAMES = ["alexa", "billy", "carol", "david", "ellie", "felix", "grace", "harry", "india", "jason", "kiera", "laura", "matty", "nadia", "oscar", "piper", "quinn", "ryann", "steff", "tisix", "usher", "vicky", "wendy", "xenia", "yetis", "zelda"] as const
4 changes: 2 additions & 2 deletions examples/balance.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { System, users } from "polkadot_dev/mod.js"
import { createUsers, System } from "polkadot_dev/mod.js"

const [alexa] = await users(1)
const { alexa } = await createUsers()

const result = await System.Account.value(alexa.publicKey).run()

Expand Down
4 changes: 2 additions & 2 deletions examples/batch.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Rune } from "capi"
import { signature } from "capi/patterns/signature/polkadot.ts"
import { Balances, System, users, Utility } from "westend_dev/mod.js"
import { Balances, createUsers, System, Utility } from "westend_dev/mod.js"
import { mapEntries } from "../deps/std/collections/map_entries.ts"

const [alexa, billy, carol, david] = await users(4)
const { alexa, billy, carol, david } = await createUsers()

const balances = Rune.rec(
mapEntries({ billy, carol, david }, ([name, { publicKey }]) => {
Expand Down
4 changes: 2 additions & 2 deletions examples/fee_estimate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Balances, users } from "westend_dev/mod.js"
import { Balances, createUsers } from "westend_dev/mod.js"

const [alexa] = await users(1)
const { alexa } = await createUsers()

const result = await Balances
.transfer({
Expand Down
4 changes: 2 additions & 2 deletions examples/identity_cr.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { $ } from "capi"
import { IdentityInfoTranscoders } from "capi/patterns/identity.ts"
import { signature } from "capi/patterns/signature/polkadot.ts"
import { Identity, users } from "polkadot_dev/mod.js"
import { createUsers, Identity } from "polkadot_dev/mod.js"

const [alexa] = await users(1)
const { alexa } = await createUsers()

const transcoders = new IdentityInfoTranscoders({ stars: $.u8 })

Expand Down
4 changes: 2 additions & 2 deletions examples/indices.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { PublicKeyRune } from "capi"
import { chain, Indices, users } from "polkadot_dev/mod.js"
import { chain, createUsers, Indices } from "polkadot_dev/mod.js"
import { signature } from "../patterns/signature/polkadot.ts"

const [alexa] = await users(1)
const { alexa } = await createUsers()

const index = 254

Expand Down
4 changes: 2 additions & 2 deletions examples/multisig_pure_proxy_stash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Rune } from "capi"
import { MultisigRune } from "capi/patterns/multisig/mod.ts"
import { filterPureCreatedEvents } from "capi/patterns/proxy/mod.ts"
import { signature } from "capi/patterns/signature/polkadot.ts"
import { Balances, chain, Proxy, System, users } from "polkadot_dev/mod.js"
import { Balances, chain, createUsers, Proxy, System } from "polkadot_dev/mod.js"
import { MultiAddress } from "polkadot_dev/types/sp_runtime/multiaddress.js"

const [alexa, billy, carol] = await users(3)
const { alexa, billy, carol } = await createUsers()

const multisig = Rune
.constant({
Expand Down
4 changes: 2 additions & 2 deletions examples/multisig_transfer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Rune, ValueRune } from "capi"
import { MultisigRune } from "capi/patterns/multisig/mod.ts"
import { signature } from "capi/patterns/signature/polkadot.ts"
import { Balances, chain, System, users } from "polkadot_dev/mod.js"
import { Balances, chain, createUsers, System } from "polkadot_dev/mod.js"

const [alexa, billy, carol, david] = await users(4)
const { alexa, billy, carol, david } = await createUsers()

const multisig = Rune
.constant({
Expand Down
4 changes: 2 additions & 2 deletions examples/polkadot_js_signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { pjsSender } from "capi/patterns/compat/pjs_sender.ts"
import { signature } from "capi/patterns/signature/polkadot.ts"
import { createPair } from "https://deno.land/x/polkadot@0.2.25/keyring/mod.ts"
import { TypeRegistry } from "https://deno.land/x/polkadot@0.2.25/types/mod.ts"
import { Balances, chain, users } from "polkadot_dev/mod.js"
import { Balances, chain, createUsers } from "polkadot_dev/mod.js"

const [alexa, billy] = await users(2)
const { alexa, billy } = await createUsers()

// Usually injected by an extension, e.g.
// const pjsSigner = (await web3FromSource("polkadot-js")).signer!;
Expand Down
4 changes: 2 additions & 2 deletions examples/transfer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Balances, users } from "westend_dev/mod.js"
import { Balances, createUsers } from "westend_dev/mod.js"
import { signature } from "../patterns/signature/polkadot.ts"

const [alexa, billy] = await users(2)
const { alexa, billy } = await createUsers()

const result = await Balances
.transfer({
Expand Down
4 changes: 2 additions & 2 deletions examples/transfer_sequence.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Rune, Sr25519 } from "capi"
import { Balances, System, users } from "westend_dev/mod.js"
import { Balances, createUsers, System } from "westend_dev/mod.js"
import { signature } from "../patterns/signature/polkadot.ts"

const [alexa, billy, carol] = await users(3)
const { alexa, billy, carol } = await createUsers()

const balances = Rune.rec({
alice: balance(alexa),
Expand Down
4 changes: 2 additions & 2 deletions examples/virtual_multisig.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Rune, Sr25519 } from "capi"
import { VirtualMultisigRune } from "capi/patterns/multisig/mod.ts"
import { signature } from "capi/patterns/signature/polkadot.ts"
import { Balances, chain, System, users, Utility } from "polkadot_dev/mod.js"
import { Balances, chain, createUsers, System, Utility } from "polkadot_dev/mod.js"
import { MultiAddress } from "polkadot_dev/types/sp_runtime/multiaddress.js"
import { parse } from "../deps/std/flags.ts"

const [alexa, billy, carol, david] = await users(4)
const { alexa, billy, carol, david } = await createUsers()

let { state } = parse(Deno.args, { string: ["state"] })
if (!state) {
Expand Down
8 changes: 2 additions & 6 deletions providers/frame/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,11 @@ export async function createCustomChainSpec(
return customChainSpecRawPath
}

export function connectionCodeWithUsers(
code: string,
isTypes: boolean,
url: string,
): string {
export function connectionCodeWithUsers(code: string, isTypes: boolean, url: string): string {
return `
${code}

export const users ${
export const createUsers ${
isTypes
? `: ReturnType<typeof C.testUserFactory>`
: `= C.testUserFactory(${JSON.stringify(url)})`
Expand Down
5 changes: 5 additions & 0 deletions words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ katal
katalchain
keypair
kico
kiera
kint
kintsugi
kton
Expand All @@ -106,6 +107,7 @@ ltex
lxkf
mathchain
matklad
matty
merkle
micnncim
monomorphization
Expand Down Expand Up @@ -169,6 +171,7 @@ rotr
runtimes
rustc
rustup
ryann
sastan
schnorr
schnorrkel
Expand All @@ -184,6 +187,7 @@ stalebot
statemigration
statemine
statemint
steff
struct
subpaths
sufficients
Expand All @@ -196,6 +200,7 @@ teer
tidefi
tifi
timepoint
tisix
tjjfvi
tnkr
todos
Expand Down