Skip to content

Commit 9fed50b

Browse files
authored
Feat/papi support (#440)
* zombienet doesnt require connected parachains * prescript errors halt runs * feat: ✨ basic papi support * fix * lint * lint * ci-fix * papi dev * docs: 📝 Add PAPI docs * cs fix * chopsticks fix
1 parent 0856edd commit 9fed50b

File tree

31 files changed

+1103
-49
lines changed

31 files changed

+1103
-49
lines changed

.changeset/olive-lions-enjoy.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"@moonwall/cli": minor
3+
"@moonwall/tests": minor
4+
"@moonwall/types": minor
5+
"@moonwall/util": minor
6+
---
7+
8+
November Update
9+
- [[#435](https://github.com/Moonsong-Labs/moonwall/issues/435)] Zombienets dont require parachains anymore
10+
- [[#408](https://github.com/Moonsong-Labs/moonwall/issues/408)] preScript failures halt test runs
11+
- [[#405](https://github.com/Moonsong-Labs/moonwall/issues/405)] Add PAPI Support

.github/workflows/main.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ jobs:
114114
strategy:
115115
fail-fast: false
116116
matrix:
117-
suite: ["dev_test", "dev_multi", "dev_seq", "dev_smoke"]
117+
suite: ["dev_test", "dev_multi", "dev_seq", "dev_smoke", "papi_dev"]
118118
shard: [1, 2, 3, 4]
119119
steps:
120120
- uses: actions/checkout@v4
@@ -142,6 +142,7 @@ jobs:
142142
- name: Run ${{matrix.suite}} dev test
143143
run: |
144144
cd test
145+
bun papi generate
145146
bun moonwall test ${{matrix.suite}} --ts ${{ matrix.shard }}/4
146147
147148
test_chopsticks:
@@ -188,7 +189,7 @@ jobs:
188189
strategy:
189190
fail-fast: false
190191
matrix:
191-
suite: ["eth_test", "viem_test"]
192+
suite: ["eth_test", "viem_test", "papi_readonly"]
192193
steps:
193194
- uses: actions/checkout@v4
194195
- uses: pnpm/action-setup@v4
@@ -208,6 +209,7 @@ jobs:
208209
- name: Run standard ${{matrix.suite}}
209210
run: |
210211
cd test
212+
bun papi add dot -n polkadot
211213
bun moonwall test ${{matrix.suite}}
212214
# disabled until web3.js fix their stuff
213215
# - name: Run standard web3 run

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,3 @@ artifacts
2323

2424
.idea
2525
db.sqlite*
26-
polkadot*

docs/guide/intro/providers.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ At time of writing, Moonwall supports the following providers:
1717

1818
PolkadotJs is a suite of tools, including a JavaScript library, for interacting with the Polkadot network and other Substrate-based blockchains. To learn more about PolkadotJs, you can check out the [PolkadotJs docs](https://polkadot.js.org/docs/api/){target=_blank}
1919

20+
### Polkadot-API (PAPI)
21+
22+
[Polkadot-API](https://github.com/polkadot-api/polkadot-api){target=_blank} is a modern, lightweight, and fast alternative to the PolkadotJs library for interacting with Substrate-based blockchains. It is designed to be more performant and have a smaller bundle size compared to PolkadotJs, while maintaining compatibility with the Polkadot ecosystem. For more information about Polkadot-API, check out the [Polkadot-API docs](https://papi.how/){target=_blank}.
2023

2124
### Ethers
2225

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"lint": "pnpm -r --filter='./packages/**' run lint",
1313
"lint:fix": "pnpm -r --filter='./packages/**' run lint:fix",
1414
"changeset": "changeset",
15-
"changeset:release": "changeset publish",
15+
"changeset:release": "sleep 1 && pnpm changeset publish",
1616
"changeset:version": "changeset version",
1717
"clean-all": "rm -rf node_modules && pnpm -r --filter='./packages/**' run clean && pnpm store prune",
1818
"pristine-build": "pnpm clean-all && pnpm i && pnpm build && pnpm generate-types",

packages/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
"inquirer-press-to-continue": "*",
8888
"jsonc-parser": "3.3.1",
8989
"minimatch": "9.0.5",
90+
"polkadot-api": "^1.7.4",
9091
"semver": "*",
9192
"tiny-invariant": "*",
9293
"viem": "*",

packages/cli/src/internal/providerFactories.ts

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { createWalletClient, http, publicActions } from "viem";
77
import { privateKeyToAccount } from "viem/accounts";
88
import { Web3 } from "web3";
99
import { WebSocketProvider } from "web3-providers-ws";
10+
import { createClient, type PolkadotClient } from "polkadot-api";
11+
import { getWsProvider, WsEvent } from "polkadot-api/ws-provider/web";
1012
import Debug from "debug";
1113
const debug = Debug("global:providers");
1214

@@ -31,6 +33,8 @@ export class ProviderFactory {
3133
return this.createEthers();
3234
case "viem":
3335
return this.createViem();
36+
case "papi":
37+
return this.createPapi();
3438
default:
3539
return this.createDefault();
3640
}
@@ -105,6 +109,33 @@ export class ProviderFactory {
105109
};
106110
}
107111

112+
private createPapi(): MoonwallProvider {
113+
debug(`🟢 Papi provider ${this.providerConfig.name} details prepared`);
114+
return {
115+
name: this.providerConfig.name,
116+
type: this.providerConfig.type,
117+
connect: () => {
118+
const provider = getWsProvider(this.url, (status) => {
119+
switch (status.type) {
120+
case WsEvent.CONNECTING:
121+
console.log("Connecting... 🔌");
122+
break;
123+
case WsEvent.CONNECTED:
124+
console.log("Connected! ⚡");
125+
break;
126+
case WsEvent.ERROR:
127+
console.log("Errored... 😢");
128+
break;
129+
case WsEvent.CLOSE:
130+
console.log("Closed 🚪");
131+
break;
132+
}
133+
});
134+
return createClient(provider);
135+
},
136+
};
137+
}
138+
108139
private createDefault(): MoonwallProvider {
109140
debug(`🟢 Default provider ${this.providerConfig.name} details prepared`);
110141
return {
@@ -211,15 +242,21 @@ export interface ProviderInterface {
211242
name: string;
212243
api: any;
213244
type: ProviderType;
214-
greet: () => void | Promise<void> | { rtVersion: number; rtName: string };
245+
greet: () => Promise<void> | Promise<{ rtVersion: number; rtName: string }>;
215246
disconnect: () => void | Promise<void> | any;
216247
}
217248

218249
export class ProviderInterfaceFactory {
219250
constructor(
220251
private name: string,
221252
private type: ProviderType,
222-
private connect: () => Promise<ApiPromise> | Wallet | Web3 | Promise<ViemClient> | null
253+
private connect: () =>
254+
| Promise<ApiPromise>
255+
| Wallet
256+
| Web3
257+
| Promise<ViemClient>
258+
| PolkadotClient
259+
| null
223260
) {}
224261

225262
public async create(): Promise<ProviderInterface> {
@@ -232,6 +269,8 @@ export class ProviderInterfaceFactory {
232269
return this.createEthers();
233270
case "viem":
234271
return this.createViem();
272+
case "papi":
273+
return this.createPapi();
235274
default:
236275
throw new Error("UNKNOWN TYPE");
237276
}
@@ -246,7 +285,7 @@ export class ProviderInterfaceFactory {
246285
name: this.name,
247286
api,
248287
type: this.type,
249-
greet: () => {
288+
greet: async () => {
250289
debug(
251290
`👋 Provider ${this.name} is connected to chain` +
252291
` ${(api.consts.system.version as any).specName.toString()} ` +
@@ -319,10 +358,27 @@ export class ProviderInterfaceFactory {
319358
};
320359
}
321360

361+
private async createPapi(): Promise<ProviderInterface> {
362+
const api = (await this.connect()) as PolkadotClient;
363+
return {
364+
name: this.name,
365+
api,
366+
type: this.type,
367+
greet: async () => {
368+
const unsafeApi = await api.getUnsafeApi();
369+
const { spec_version, spec_name } = await unsafeApi.constants.System.Version();
370+
return { rtVersion: spec_version as number, rtName: spec_name as string };
371+
},
372+
async disconnect() {
373+
api.destroy();
374+
},
375+
};
376+
}
377+
322378
public static async populate(
323379
name: string,
324380
type: ProviderType,
325-
connect: () => Promise<ApiPromise> | Wallet | Web3 | Promise<ViemClient> | null
381+
connect: () => Promise<ApiPromise> | Wallet | Web3 | Promise<ViemClient> | PolkadotClient | null
326382
): Promise<ProviderInterface> {
327383
debug(`🔄 Populating provider: ${name} of type: ${type}`);
328384
try {

packages/cli/src/lib/runnerContext.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { chopsticksHandler } from "./handlers/chopsticksHandler";
2424
import { devHandler } from "./handlers/devHandler";
2525
import { readOnlyHandler } from "./handlers/readOnlyHandler";
2626
import { zombieHandler } from "./handlers/zombieHandler";
27+
import type { PolkadotClient } from "polkadot-api";
2728

2829
const RT_VERSION = Number(process.env.MOON_RTVERSION);
2930
const RT_NAME = process.env.MOON_RTNAME;
@@ -150,6 +151,7 @@ export function describeSuite<T extends FoundationType>({
150151
polkadotJs: (apiName?: string): ApiPromise => getApi("polkadotJs", apiName),
151152
ethers: (apiName?: string): Wallet => getApi("ethers", apiName),
152153
web3: (apiName?: string): Web3 => getApi("web3", apiName),
154+
papi: (apiName?: string): PolkadotClient => getApi("papi", apiName),
153155
};
154156

155157
const foundationHandlers: Record<FoundationType, FoundationHandler<any>> = {

packages/types/config_schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@
307307
"type": "object"
308308
},
309309
"ProviderType": {
310-
"description": "The type of provider. Can be \"polkadotJs\", \"ethers\", \"web3\", \"viem\"",
311-
"enum": ["ethers", "polkadotJs", "viem", "web3"],
310+
"description": "The type of provider. Can be \"polkadotJs\", \"ethers\", \"web3\", \"viem\", \"papi\"",
311+
"enum": ["ethers", "papi", "polkadotJs", "viem", "web3"],
312312
"type": "string"
313313
},
314314
"ReadOnlyLaunchSpec": {

packages/types/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,15 @@
5656
"@polkadot/api": "*",
5757
"@polkadot/api-base": "*",
5858
"@polkadot/keyring": "*",
59-
"@polkadot/util-crypto": "*",
6059
"@polkadot/types": "*",
6160
"@polkadot/util": "*",
61+
"@polkadot/util-crypto": "*",
6262
"@types/node": "*",
6363
"@zombienet/utils": "*",
6464
"bottleneck": "*",
6565
"debug": "*",
6666
"ethers": "*",
67+
"polkadot-api": "^1.7.4",
6768
"viem": "*",
6869
"web3": "*"
6970
},

0 commit comments

Comments
 (0)