Skip to content

Commit 685c7f8

Browse files
committed
Added polkadot/api and rewrite sendTransaction
1 parent 8796a77 commit 685c7f8

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

advanced/dapps/react-dapp-v2/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"@multiversx/sdk-wallet": "4.2.0",
2222
"@noble/curves": "^1.8.1",
2323
"@noble/secp256k1": "^2.2.3",
24+
"@polkadot/api": "^15.10.2",
2425
"@polkadot/util-crypto": "^10.1.2",
2526
"@solana/web3.js": "^1.36.0",
2627
"@walletconnect/core": "^2.19.1",

advanced/dapps/react-dapp-v2/src/contexts/JsonRpcContext.tsx

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as encoding from "@walletconnect/encoding";
44
import { Transaction as EthTransaction } from "@ethereumjs/tx";
55
import { recoverTransaction } from "@celo/wallet-base";
66
import * as bitcoin from "bitcoinjs-lib";
7+
import { ApiPromise, WsProvider } from "@polkadot/api";
78

89
import {
910
formatDirectSignDoc,
@@ -84,6 +85,7 @@ import {
8485
} from "../helpers/bip122";
8586
import { getAddressFromAccount } from "@walletconnect/utils";
8687
import { BIP122_DUST_LIMIT } from "../chains/bip122";
88+
import { PolkadotChainData } from "../chains/polkadot";
8789

8890
/**
8991
* Types
@@ -966,18 +968,28 @@ export function JsonRpcContextProvider({
966968
chainId: string,
967969
address: string
968970
): Promise<IFormattedRpcResponse> => {
971+
// Initialize API
972+
973+
const [namespace, reference] = chainId.split(":");
974+
const targetChainData = chainData[namespace][reference];
975+
const wsProvider = new WsProvider(targetChainData.rpc);
976+
const api = await ApiPromise.create({ provider: wsProvider });
977+
978+
const call = api.tx.balances.transfer(address, 1000000000000); // 1 DOT
979+
980+
const runtime = await api.rpc.state.getRuntimeVersion();
981+
const blockHash = await api.rpc.chain.getBlockHash();
982+
const blockNumber = await api.rpc.chain.getHeader();
983+
969984
const transactionPayload = {
970-
specVersion: "0x00002468",
971-
transactionVersion: "0x0000000e",
985+
specVersion: runtime.specVersion.toHex(),
986+
transactionVersion: runtime.transactionVersion.toHex(),
972987
address: `${address}`,
973-
blockHash:
974-
"0x554d682a74099d05e8b7852d19c93b527b5fae1e9e1969f6e1b82a2f09a14cc9",
975-
blockNumber: "0x00cb539c",
988+
blockHash: blockHash.toHex(),
989+
blockNumber: blockNumber.number.toHex(),
976990
era: "0xc501",
977-
genesisHash:
978-
"0xe143f23803ac50e8f6f8e62695d1ce9e4e1d68aa36c1cd2cfd15340213f3423e",
979-
method:
980-
"0x0001784920616d207369676e696e672074686973207472616e73616374696f6e21",
991+
genesisHash: api.genesisHash.toHex(),
992+
method: call.method.toHex(),
981993
nonce: "0x00000000",
982994
signedExtensions: [
983995
"CheckNonZeroSender",
@@ -990,7 +1002,7 @@ export function JsonRpcContextProvider({
9901002
"ChargeTransactionPayment",
9911003
],
9921004
tip: "0x00000000000000000000000000000000",
993-
version: 4,
1005+
version: api.extrinsicVersion
9941006
};
9951007

9961008
const result = await client!.request<{
@@ -1008,6 +1020,17 @@ export function JsonRpcContextProvider({
10081020
},
10091021
});
10101022

1023+
// result = { signature: '0x...' }
1024+
const extrinsic = api.createType('Extrinsic', call);
1025+
extrinsic.addSignature(address, `0x${result.signature.replaceAll('0x', '')}`, {
1026+
...transactionPayload,
1027+
specVersion: api.runtimeVersion.specVersion,
1028+
transactionVersion: api.runtimeVersion.transactionVersion,
1029+
});
1030+
1031+
const txHash = api.registry.hash(extrinsic.toU8a()).toHex();
1032+
console.log('Transaction hash:', txHash);
1033+
10111034
return {
10121035
method: DEFAULT_POLKADOT_METHODS.POLKADOT_SIGN_TRANSACTION,
10131036
address,

0 commit comments

Comments
 (0)