Skip to content

Commit

Permalink
export methods to use chopsticks as lib (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
ermalkaleci authored Jan 24, 2023
1 parent 2f800b9 commit 2764b99
Show file tree
Hide file tree
Showing 14 changed files with 254 additions and 247 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ dist
!.yarn/patches
!.yarn/plugins
!.yarn/sdks
!.yarn/versions
.yarn/versions

.idea

Expand Down
2 changes: 1 addition & 1 deletion chopsticks.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env node
require('./dist/index.js')
require('./dist/cli.js')
10 changes: 5 additions & 5 deletions e2e/__snapshots__/dev.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -504,11 +504,11 @@ exports[`dev rpc > dryRun 1`] = `
}
`;

exports[`dev rpc > setStorages 1`] = `"5F98oWfz2r5rcRVnP9VCndg33DAAsky3iuoBSpaPUbgN9AJn"`;
exports[`dev rpc > setStorage 1`] = `"5F98oWfz2r5rcRVnP9VCndg33DAAsky3iuoBSpaPUbgN9AJn"`;

exports[`dev rpc > setStorages 2`] = `"5FA9nQDVg267DEd8m1ZypXLBnvN7SFxYwV7ndqSYGiN9TTpu"`;
exports[`dev rpc > setStorage 2`] = `"5FA9nQDVg267DEd8m1ZypXLBnvN7SFxYwV7ndqSYGiN9TTpu"`;

exports[`dev rpc > setStorages 3`] = `
exports[`dev rpc > setStorage 3`] = `
{
"consumers": 0,
"data": {
Expand All @@ -523,7 +523,7 @@ exports[`dev rpc > setStorages 3`] = `
}
`;

exports[`dev rpc > setStorages 4`] = `
exports[`dev rpc > setStorage 4`] = `
{
"consumers": 0,
"data": {
Expand All @@ -538,7 +538,7 @@ exports[`dev rpc > setStorages 4`] = `
}
`;

exports[`dev rpc > setStorages 5`] = `
exports[`dev rpc > setStorage 5`] = `
{
"consumers": 0,
"data": {
Expand Down
14 changes: 7 additions & 7 deletions e2e/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { api, dev, env, expectJson, setupApi, testingPairs, ws } from './helper'
setupApi(env.mandala)

describe('dev rpc', () => {
it('setStorages', async () => {
it('setStorage', async () => {
const { alice, test1 } = testingPairs()

await expectJson(api.query.sudo.key()).toMatchSnapshot()

await dev.setStorages([[api.query.sudo.key.key(), u8aToHex(alice.addressRaw)]])
await dev.setStorage([[api.query.sudo.key.key(), u8aToHex(alice.addressRaw)]])

await expectJson(api.query.sudo.key()).toMatchSnapshot()

Expand All @@ -20,11 +20,11 @@ describe('dev rpc', () => {

await expectJson(api.query.system.account(test1.address)).toMatchSnapshot()

await dev.setStorages([[api.query.system.account.key(test1.address), null]], hash)
await dev.setStorage([[api.query.system.account.key(test1.address), null]], hash)

await expectJson(api.query.system.account(test1.address)).toMatchSnapshot()

await dev.setStorages({
await dev.setStorage({
System: {
Account: [[[test1.address], { data: { free: 100000 }, nonce: 1 }]],
},
Expand All @@ -33,15 +33,15 @@ describe('dev rpc', () => {
await expectJson(api.query.system.account(test1.address)).toMatchSnapshot()
})

it('setStorages handle errors', async () => {
it('setStorage handle errors', async () => {
await expect(
dev.setStorages({
dev.setStorage({
SSystem: { Account: [] },
})
).rejects.toThrowError('1: Error: Cannot find pallet SSystem')

await expect(
dev.setStorages({
dev.setStorage({
System: { AAccount: [] },
})
).rejects.toThrowError('1: Error: Cannot find storage AAccount in pallet System')
Expand Down
4 changes: 2 additions & 2 deletions e2e/dry-run-extrinsic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('dry-run-extrinsic', () => {
const properties = await chain.api.chainProperties
const { alice, bob } = testingPairs(properties.ss58Format)

await dev.setStorages({
await dev.setStorage({
System: {
Account: [[[alice.address], { data: { free: 1000 * 1e12 } }]],
},
Expand All @@ -26,7 +26,7 @@ describe('dry-run-extrinsic', () => {
it('dry run extrinsic with fake signature', async () => {
const ALICE = '5FA9nQDVg267DEd8m1ZypXLBnvN7SFxYwV7ndqSYGiN9TTpu'

await dev.setStorages({ Sudo: { Key: ALICE } })
await dev.setStorage({ Sudo: { Key: ALICE } })

// sudo.sudo(system.fillBlock(10000000))
const call = '0xff00000080969800'
Expand Down
2 changes: 1 addition & 1 deletion e2e/genesis-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('genesis provider works', () => {
const properties = await chain.api.chainProperties
const { test1, test2 } = testingPairs(properties.ss58Format)

await dev.setStorages({
await dev.setStorage({
System: {
Account: [[[test1.address], { data: { free: 1000 * 1e12 } }]],
},
Expand Down
4 changes: 2 additions & 2 deletions e2e/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ export const dev = {
newBlock: (param?: { count?: number; to?: number }): Promise<string> => {
return ws.send('dev_newBlock', [param])
},
setStorages: (values: StorageValues, blockHash?: string) => {
return ws.send('dev_setStorages', [values, blockHash])
setStorage: (values: StorageValues, blockHash?: string) => {
return ws.send('dev_setStorage', [values, blockHash])
},
timeTravel: (date: string | number) => {
return ws.send<number>('dev_timeTravel', [date])
Expand Down
2 changes: 1 addition & 1 deletion e2e/upgrade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ setupApi({
describe('upgrade', () => {
const { alice, bob } = testingPairs()
it('setCode works', async () => {
await dev.setStorages({
await dev.setStorage({
Sudo: {
Key: alice.address,
},
Expand Down
17 changes: 8 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@acala-network/chopsticks",
"version": "0.3.5",
"version": "0.3.6",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"author": "Bryan Chen <[email protected]>",
Expand All @@ -15,18 +15,18 @@
"lint": "tsc --noEmit --project tsconfig.json && eslint . --ext .js,.ts && prettier --check .",
"fix": "eslint . --ext .js,.ts --fix && prettier -w .",
"prepare": "husky install",
"start": "ts-node --transpile-only src/index.ts",
"start": "ts-node --transpile-only src/cli.ts",
"build": "rm -rf dist && tsc -p tsconfig.prod.json",
"build-wasm": "wasm-pack build executor --target nodejs --scope acala-network",
"build-wasm-logging": "yarn build-wasm --features=logging",
"check": "cd executor && cargo check --locked",
"test": "vitest --silent",
"test:dev": "LOG_LEVEL=trace vitest --inspect",
"dev": "LOG_LEVEL=trace ts-node-dev --transpile-only --inspect --notify=false src/index.ts -- dev --config=configs/dev.yml",
"dev:karura": "ts-node-dev --transpile-only --inspect --notify=false src/index.ts -- dev --config=configs/karura.yml",
"dev:acala": "ts-node-dev --transpile-only --inspect --notify=false src/index.ts -- dev --config=configs/acala.yml",
"dev:moonriver": "ts-node-dev --transpile-only --inspect --notify=false src/index.ts -- dev --config=configs/moonriver.yml",
"dev:moonbeam": "ts-node-dev --transpile-only --inspect --notify=false src/index.ts -- dev --config=configs/moonbeam.yml"
"dev": "LOG_LEVEL=trace ts-node-dev --transpile-only --inspect --notify=false src/cli.ts -- dev --config=configs/dev.yml",
"dev:karura": "ts-node-dev --transpile-only --inspect --notify=false src/cli.ts -- dev --config=configs/karura.yml",
"dev:acala": "ts-node-dev --transpile-only --inspect --notify=false src/cli.ts -- dev --config=configs/acala.yml",
"dev:moonriver": "ts-node-dev --transpile-only --inspect --notify=false src/cli.ts -- dev --config=configs/moonriver.yml",
"dev:moonbeam": "ts-node-dev --transpile-only --inspect --notify=false src/cli.ts -- dev --config=configs/moonbeam.yml"
},
"dependencies": {
"@acala-network/chopsticks-executor": "workspace:*",
Expand Down Expand Up @@ -74,8 +74,7 @@
"files": [
"dist",
"bin",
"template",
"chopsticks.mjs"
"template"
],
"engines": {
"node": ">=v14"
Expand Down
16 changes: 10 additions & 6 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ExtDef } from '@polkadot/types/extrinsic/signedExtensions/types'
import { HexString } from '@polkadot/util/types'
import { ProviderInterface } from '@polkadot/rpc-provider/types'
import { WsProvider } from '@polkadot/rpc-provider'

type ChainProperties = {
ss58Format?: number
Expand Down Expand Up @@ -45,12 +44,17 @@ export class Api {
}

get isReady() {
if (this.#provider instanceof WsProvider) {
return this.#provider.isReady
}

if (!this.#ready) {
this.#ready = this.#provider.connect()
if (this.#provider['isReady']) {
this.#ready = this.#provider['isReady']
} else {
this.#ready = new Promise((resolve): void => {
this.#provider.on('connected', (): void => {
resolve()
})
this.#provider.connect()
})
}
}

return this.#ready
Expand Down
Loading

0 comments on commit 2764b99

Please sign in to comment.