Skip to content

Commit d036b38

Browse files
sinzii0xLuccanhussein11
authored
feat: add dedot (#602)
* add getting started with dedot * updates * fix grammars * Update develop/toolkit/api-libraries/dedot.md Co-authored-by: 0xLucca <[email protected]> * Update dedot.md * Update develop/toolkit/api-libraries/dedot.md Co-authored-by: 0xLucca <[email protected]> * Update develop/toolkit/api-libraries/dedot.md Co-authored-by: 0xLucca <[email protected]> * Update develop/toolkit/api-libraries/dedot.md Co-authored-by: 0xLucca <[email protected]> * Update dedot.md * Update develop/toolkit/api-libraries/dedot.md Co-authored-by: 0xLucca <[email protected]> * Update develop/toolkit/api-libraries/dedot.md Co-authored-by: 0xLucca <[email protected]> * Update develop/toolkit/api-libraries/dedot.md Co-authored-by: 0xLucca <[email protected]> * Update develop/toolkit/api-libraries/dedot.md Co-authored-by: 0xLucca <[email protected]> * Update develop/toolkit/api-libraries/dedot.md Co-authored-by: 0xLucca <[email protected]> * Update develop/toolkit/api-libraries/dedot.md Co-authored-by: 0xLucca <[email protected]> * Update develop/toolkit/api-libraries/dedot.md Co-authored-by: Nicolás Hussein <[email protected]> * Apply suggestions from code review Co-authored-by: 0xLucca <[email protected]> * move code to snippets * adding desc for each example * wording tweaks --------- Co-authored-by: 0xLucca <[email protected]> Co-authored-by: Nicolás Hussein <[email protected]>
1 parent 2636f5e commit d036b38

15 files changed

+490
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const metadata = await client.call.metadata.metadataAtVersion(15);
2+
console.log('Metadata V15', metadata)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { DedotClient, SmoldotProvider } from 'dedot';
2+
import type { PolkadotApi } from '@dedot/chaintypes';
3+
import * as smoldot from 'smoldot';
4+
5+
// import `polkadot` chain spec to connect to Polkadot
6+
import { polkadot } from '@substrate/connect-known-chains'
7+
8+
// Start smoldot instance & initialize a chain
9+
const client = smoldot.start();
10+
const chain = await client.addChain({ chainSpec: polkadot });
11+
12+
// Initialize providers & clients
13+
const provider = new SmoldotProvider(chain);
14+
const client = await DedotClient.new<PolkadotApi>(provider);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { DedotClient, WsProvider } from 'dedot';
2+
import type { PolkadotApi } from '@dedot/chaintypes';
3+
4+
// Initialize providers & clients
5+
const provider = new WsProvider('wss://rpc.polkadot.io');
6+
const client = await DedotClient.new<PolkadotApi>(provider);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { LegacyClient, WsProvider } from 'dedot';
2+
import type { PolkadotApi } from '@dedot/chaintypes';
3+
4+
const provider = new WsProvider('wss://rpc.polkadot.io');
5+
const client = await LegacyClient.new<PolkadotApi>(provider);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { DedotClient, WsProvider } from 'dedot';
2+
import type { PolkadotApi, KusamaApi } from '@dedot/chaintypes';
3+
4+
const polkadotClient = await DedotClient.new<PolkadotApi>(new WsProvider('wss://rpc.polkadot.io'));
5+
const kusamaClient = await DedotClient.new<KusamaApi>(new WsProvider('wss://kusama-rpc.polkadot.io'));
6+
const genericClient = await DedotClient.new(new WsProvider('ws://localhost:9944'));
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const ss58Prefix = client.consts.system.ss58Prefix;
2+
console.log('Polkadot ss58Prefix:', ss58Prefix);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const injected = await window.injectedWeb3['polkadot-js'].enable('My dApp');
2+
const account = (await injected.accounts.get())[0];
3+
const signer = injected.signer;
4+
const unsub = await client.tx.balances
5+
.transferKeepAlive(<destAddress>, 2_000_000_000_000n)
6+
.signAndSend(account.address, { signer }, async ({ status }) => {
7+
console.log('Transaction status', status.type);
8+
if (status.type === 'BestChainBlockIncluded') {
9+
console.log(`Transaction is included in best block`);
10+
}
11+
if (status.type === 'Finalized') {
12+
console.log(`Transaction completed at block hash ${status.value.blockHash}`);
13+
await unsub();
14+
}
15+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { cryptoWaitReady } from '@polkadot/util-crypto';
2+
import { Keyring } from '@polkadot/keyring';
3+
// Setup keyring
4+
await cryptoWaitReady();
5+
const keyring = new Keyring({ type: 'sr25519' });
6+
const alice = keyring.addFromUri('//Alice');
7+
// Send transaction
8+
const unsub = await client.tx.balances
9+
.transferKeepAlive(<destAddress>, 2_000_000_000_000n)
10+
.signAndSend(alice, async ({ status }) => {
11+
console.log('Transaction status', status.type);
12+
if (status.type === 'BestChainBlockIncluded') {
13+
console.log(`Transaction is included in best block`);
14+
}
15+
if (status.type === 'Finalized') {
16+
console.log(`Transaction completed at block hash ${status.value.blockHash}`);
17+
await unsub();
18+
}
19+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const balance = await client.query.system.account(<address>);
2+
console.log('Balance:', balance.data.free);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const unsub = await client.query.system.number((blockNumber) => {
2+
console.log(`Current block number: ${blockNumber}`);
3+
});

0 commit comments

Comments
 (0)