Skip to content

Commit 229d6a2

Browse files
author
openweb3
committed
update types
1 parent d48904e commit 229d6a2

File tree

13 files changed

+590
-58
lines changed

13 files changed

+590
-58
lines changed

README.md

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,48 @@
22

33
A TypeScript SDK for interacting with PolkaVM Query (PVQ)
44

5-
65
## Quick Start
76

87
```typescript
9-
import { ApiPromise } from '@polkadot/api';
10-
import { PvqProgram } from '@open-web3/pvq';
8+
import { ApiPromise } from "@polkadot/api";
9+
import { PvqProgram } from "@open-web3/pvq";
1110

1211
// Connect to a Polkadot node
1312
const api = await ApiPromise.create({
14-
provider: 'ws://localhost:9944'
13+
provider: "ws://localhost:9944",
1514
});
1615

1716
// Create PVQ program instance
1817
const program = new PvqProgram(
1918
api,
2019
guestProgramBytes, // Uint8Array or hex string
21-
programMetadata // Program metadata object
20+
programMetadata // Program metadata object
2221
);
2322

2423
// Execute a query
25-
const result = await program.entrypoint.sumBalance([
26-
21,
27-
['15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5']
28-
], {
29-
gasLimit: 1000000000000000000n
30-
});
24+
const result = await program.entrypoint.sumBalance(
25+
[21, ["15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5"]],
26+
{
27+
gasLimit: 1000000000000000000n,
28+
}
29+
);
3130

32-
console.log('Query result:', result);
31+
console.log("Query result:", result);
3332
```
3433

3534
## Usage Examples
3635

3736
### Basic Program Execution
3837

3938
```typescript
40-
import { ApiPromise, WsProvider } from '@polkadot/api';
41-
import { PvqProgram } from '@open-web3/pvq';
39+
import { ApiPromise, WsProvider } from "@polkadot/api";
40+
import { PvqProgram } from "@open-web3/pvq";
4241

4342
async function executeProgram() {
44-
const provider = new WsProvider('ws://127.0.0.1:8000');
43+
const provider = new WsProvider("ws://127.0.0.1:8000");
4544
const api = await ApiPromise.create({ provider });
4645

47-
const guestProgram = '0x...'; // Your program bytecode
46+
const guestProgram = "0x..."; // Your program bytecode
4847
const metadata = {
4948
// Your program metadata
5049
};
@@ -53,10 +52,13 @@ async function executeProgram() {
5352

5453
try {
5554
// Execute with default gas limit
56-
const result = await program.entrypoint.sumBalance([21, ['15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5']]);
57-
console.log('Success:', result.toJSON());
55+
const result = await program.entrypoint.sumBalance([
56+
21,
57+
["15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5"],
58+
]);
59+
console.log("Success:", result.toJSON());
5860
} catch (error) {
59-
console.error('Execution failed:', error);
61+
console.error("Execution failed:", error);
6062
}
6163

6264
await api.disconnect();
@@ -68,7 +70,7 @@ async function executeProgram() {
6870
```typescript
6971
// Execute with custom gas limit
7072
const result = await program.entrypoint.sumBalance(
71-
[21, ['15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5']],
73+
[21, ["15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5"]],
7274
{ gasLimit: 1000000000000000000n }
7375
);
7476
```
@@ -77,26 +79,37 @@ const result = await program.entrypoint.sumBalance(
7779

7880
```typescript
7981
// Execute using entrypoint identifier
80-
const result = await program.executeQuery('sum_balance', { gasLimit: 1000000n }, [21, ['15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5']]);
82+
const result = await program.executeQuery(
83+
"sum_balance",
84+
{ gasLimit: 1000000n },
85+
[21, ["15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5"]]
86+
);
87+
88+
// Execute with specific return type
89+
const typedResult = await program.executeQuery<u64>(
90+
"sum_balance",
91+
{ gasLimit: 1000000n },
92+
[21, ["15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5"]]
93+
);
8194
```
8295

8396
### Fetching Metadata
8497

8598
```typescript
8699
// Fetch metadata from the chain
87100
const metadata = await program.getMetadata();
88-
console.log('Chain metadata:', metadata);
101+
console.log("Chain metadata:", metadata);
89102
```
90103

91104
### Checking Extensions
92105

93106
```typescript
94107
// Check whether the program's extensions match the extensions on the current chain
95108
const matched = await program.checkExtensions();
96-
console.log('Extensions matched:', matched); // true or throws error if not matched
109+
console.log("Extensions matched:", matched); // true or throws error if not matched
97110

98111
// You can also check the cached result
99-
console.log('Cached result:', program.extensionsMatched); // true/false/undefined
112+
console.log("Cached result:", program.extensionsMatched); // true/false/undefined
100113
```
101114

102115
## API Reference
@@ -117,7 +130,7 @@ new PvqProgram(
117130

118131
#### Methods
119132

120-
- `executeQuery(entrypoint, options, params)` - Execute a query on the program
133+
- `executeQuery<T = Codec>(entrypoint, options, params)` - Execute a query on the program with optional return type specification
121134
- `checkExtensions()` - Check if required extensions are available
122135
- `getMetadata()` - Get runtime metadata
123136

@@ -191,4 +204,4 @@ Your program metadata should follow this structure:
191204
}
192205
]
193206
}
194-
```
207+
```

demo/app/page.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ import { Query } from "@/components/Query";
66
import { SelectProgram } from "@/components/SelectProgram";
77
import { Card, CardContent } from "@/components/ui/card";
88
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
9+
import { useAtom } from "jotai";
910

11+
import { atomWithStorage } from "jotai/utils";
1012
import Link from "next/link";
1113
import { usePathname } from "next/navigation";
12-
import { useState } from "react";
14+
15+
const tabAtom = atomWithStorage("tab", "program");
1316

1417
export default function Home() {
1518
const pathname = usePathname();
16-
const [tab, setTab] = useState("program");
19+
const [tab, setTab] = useAtom(tabAtom);
1720

1821
return (
1922
<div>

0 commit comments

Comments
 (0)