From 0a2d144bd56a9b6d06ca5cc43f6f43436b9e34b4 Mon Sep 17 00:00:00 2001 From: Ankit Singh Date: Wed, 11 Sep 2024 16:47:47 +0530 Subject: [PATCH 01/15] feat: `sendTransaction` hook for aleph zero --- packages/react/src/actions/sendTransaction.ts | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/packages/react/src/actions/sendTransaction.ts b/packages/react/src/actions/sendTransaction.ts index b3303755..bc04e87a 100644 --- a/packages/react/src/actions/sendTransaction.ts +++ b/packages/react/src/actions/sendTransaction.ts @@ -2,6 +2,8 @@ import { VersionedTransaction as SolanaVersionedTransaction } from '@solana/web3 import { sendTransaction as sendEVMTransaction } from '@wagmi/core'; import { Address as EVMAddress } from 'viem'; +import { Signer, SubmittableExtrinsic } from '@polkadot/api/types'; +import { useState } from 'react'; import { ChainData, ChainType, ConnectionOrConfig } from '../types/index.js'; import { WalletInstance } from '../types/wallet.js'; @@ -31,7 +33,7 @@ type TransactionArgs = C extends 'evm' } : C extends 'alephZero' ? { - z: string; + submittableExtrinsic: SubmittableExtrinsic<'promise' | 'rxjs'>; } : never; @@ -93,7 +95,36 @@ export const sendTransactionToChain = async ({ } if (chain.type === 'alephZero') { + const [txHash, setTxHash] = useState(''); + const [blockId, setBlockId] = useState(''); + + const walletConnector = config.connector as WalletInstance<'alephZero'>; + // send transaction to Aleph chain + const { submittableExtrinsic } = args as TransactionArgs<'alephZero'>; + await submittableExtrinsic.signAndSend( + from, + { signer: walletConnector.signer as Signer }, + ({ events, status, txHash, txIndex }) => { + console.log('event event - ', events, status); + // @ts-ignore + events.forEach(({ event }) => { + const { method } = event; + + if (method === 'ExtrinsicSuccess' && status.type === 'InBlock') { + setTxHash(txHash.toString()); + setBlockId(status.asFinalized.toHex()); + } else if (method === 'ExtrinsicFailed') { + throw new Error(`Transaction failed: ${method}`); + } + }); + }, + ); + + return { + txHash: txHash, + block: blockId, + }; } throw new Error('Chain not supported'); From eb1bf681fef8f7fabf2873ecced8048ee4a67cbf Mon Sep 17 00:00:00 2001 From: Ankit Singh Date: Wed, 11 Sep 2024 17:20:14 +0530 Subject: [PATCH 02/15] chore: alephZero usdt token added --- example-next/src/components/Tokens.tsx | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/example-next/src/components/Tokens.tsx b/example-next/src/components/Tokens.tsx index 823e80d7..f7a980af 100644 --- a/example-next/src/components/Tokens.tsx +++ b/example-next/src/components/Tokens.tsx @@ -134,6 +134,13 @@ const tokens: TokenMetadata[] = [ symbol: 'USDC', chainId: 'alephZero', }, + { + address: '5CMdxZDuprVZKnw6tEWjhEtK17Z52PUJo2dj1JLdyKeuUcfH', + decimals: 6, + name: 'USDT', + symbol: 'USDT', + chainId: 'alephZero', + }, ]; export default Tokens; From 1557439ff8e93bdca6b102f133d2792b0c83e851 Mon Sep 17 00:00:00 2001 From: Ankit Singh Date: Wed, 11 Sep 2024 18:27:34 +0530 Subject: [PATCH 03/15] chore: sendTransaction logs and variables fix --- packages/react/src/actions/sendTransaction.ts | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/react/src/actions/sendTransaction.ts b/packages/react/src/actions/sendTransaction.ts index bc04e87a..d0b1c2c9 100644 --- a/packages/react/src/actions/sendTransaction.ts +++ b/packages/react/src/actions/sendTransaction.ts @@ -3,7 +3,6 @@ import { sendTransaction as sendEVMTransaction } from '@wagmi/core'; import { Address as EVMAddress } from 'viem'; import { Signer, SubmittableExtrinsic } from '@polkadot/api/types'; -import { useState } from 'react'; import { ChainData, ChainType, ConnectionOrConfig } from '../types/index.js'; import { WalletInstance } from '../types/wallet.js'; @@ -95,8 +94,8 @@ export const sendTransactionToChain = async ({ } if (chain.type === 'alephZero') { - const [txHash, setTxHash] = useState(''); - const [blockId, setBlockId] = useState(''); + let txnHash: string | undefined = undefined; + let block: string | undefined = undefined; const walletConnector = config.connector as WalletInstance<'alephZero'>; @@ -106,14 +105,12 @@ export const sendTransactionToChain = async ({ from, { signer: walletConnector.signer as Signer }, ({ events, status, txHash, txIndex }) => { - console.log('event event - ', events, status); - // @ts-ignore events.forEach(({ event }) => { const { method } = event; if (method === 'ExtrinsicSuccess' && status.type === 'InBlock') { - setTxHash(txHash.toString()); - setBlockId(status.asFinalized.toHex()); + txnHash = txHash.toString(); + block = status.asFinalized.toHex(); } else if (method === 'ExtrinsicFailed') { throw new Error(`Transaction failed: ${method}`); } @@ -121,9 +118,13 @@ export const sendTransactionToChain = async ({ }, ); + if (txnHash === undefined || block === undefined) { + throw 'Trasaction failed'; + } + return { - txHash: txHash, - block: blockId, + txHash: txnHash, + block: block, }; } From b8301fbc9bd6f6bbae4e107fd0e3fcb1059cdeef Mon Sep 17 00:00:00 2001 From: Ankit Singh Date: Wed, 11 Sep 2024 19:00:09 +0530 Subject: [PATCH 04/15] update: `txHash` -> `transactionParams` to allow more args ro fetch transaction data --- .../react/src/actions/waitForTransaction.ts | 20 ++++++++++++++++--- .../react/src/hooks/useWaitForTransaction.ts | 20 +++++++++---------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/packages/react/src/actions/waitForTransaction.ts b/packages/react/src/actions/waitForTransaction.ts index 863694d2..1d363665 100644 --- a/packages/react/src/actions/waitForTransaction.ts +++ b/packages/react/src/actions/waitForTransaction.ts @@ -21,8 +21,19 @@ export type WatchTransactionOverrides = Default } : any); -export type WatchTransactionParams = { +export type DefaultTransactionParams = { txHash: string; +}; + +export type TransactionParams = C extends 'alephZero' + ? { + blockHash: string; + extrinsicIndex: number; + } + : DefaultTransactionParams; + +export type WatchTransactionParams = { + transactionParams: TransactionParams; chain: ChainData; config: ConnectionOrConfig; overrides: WatchTransactionOverrides | undefined; @@ -32,7 +43,7 @@ const DEFAULT_POLLING_INTERVAL = 2500; // 2.5 seconds /** * Watch transaction - * @param txHash - Transaction hash + * @param transactionParams - Transaction Parameters * @param chain - {@link ChainData} * @param config {@link ConnectionOrConfig} * @returns Transaction Receipt {@link TransactionReceipt} @@ -41,9 +52,10 @@ export const waitForTransaction = async ({ chain, config, overrides, - txHash, + transactionParams, }: WatchTransactionParams): Promise> => { if (chain.type === 'evm') { + const { txHash } = transactionParams as TransactionParams<'evm'>; const evmOverrides = (overrides || {}) as WatchTransactionOverrides<'evm'>; const receipt = await waitForTransactionReceipt(config.wagmiConfig, { hash: txHash as `0x${string}`, @@ -57,6 +69,7 @@ export const waitForTransaction = async ({ if (chain.type === 'tron') { const txInfo = await pollCallback( async () => { + const { txHash } = transactionParams as TransactionParams<'tron'>; const tx = await config.tronWeb.trx.getConfirmedTransaction(txHash); // If transaction is not found, return undefined. This will trigger the next poll @@ -74,6 +87,7 @@ export const waitForTransaction = async ({ } if (chain.type === 'solana') { + const { txHash } = transactionParams as TransactionParams<'solana'>; const _overrides = (overrides || {}) as WatchTransactionOverrides<'solana'>; const receipt = await pollCallback( diff --git a/packages/react/src/hooks/useWaitForTransaction.ts b/packages/react/src/hooks/useWaitForTransaction.ts index b85ef32e..f4aaec02 100644 --- a/packages/react/src/hooks/useWaitForTransaction.ts +++ b/packages/react/src/hooks/useWaitForTransaction.ts @@ -1,12 +1,12 @@ import { useQuery } from '@tanstack/react-query'; -import { WatchTransactionOverrides, waitForTransaction } from '../actions/waitForTransaction.js'; +import { TransactionParams, WatchTransactionOverrides, waitForTransaction } from '../actions/waitForTransaction.js'; import { ChainId, TransactionReceipt } from '../types/index.js'; import { useChain } from './useChain.js'; import { useConnectionOrConfig } from './useConnectionOrConfig.js'; export type UseWaitForTransactionParams = { - /** Transaction hash */ - txHash: string | undefined; + /** Transaction Parameters {@link TransactionParams} */ + transactionParams: TransactionParams; /** Chain ID of transaction */ chainId: ChainId; /** Transaction overrides {@link WatchTransactionOverrides} */ @@ -15,15 +15,15 @@ export type UseWaitForTransactionParams = { /** * Watch transaction Hook - * @param txHash - Transaction hash + * @param transactionParams - Transaction Parameters * @param chainId - Chain ID of transaction */ -export const useWaitForTransaction = ({ txHash, chainId, overrides }: UseWaitForTransactionParams) => { +export const useWaitForTransaction = ({ transactionParams, chainId, overrides }: UseWaitForTransactionParams) => { const connectionOrConfig = useConnectionOrConfig(); const chain = useChain(chainId); return useQuery({ - queryKey: ['watch transaction', txHash, chainId], + queryKey: ['watch transaction', transactionParams, chainId], queryFn: async () => { if (!connectionOrConfig) { throw new Error('Connections or config not found'); @@ -31,18 +31,18 @@ export const useWaitForTransaction = ({ txHash, chainId, overrides }: UseWaitFor if (!chain) { throw new Error('Chain is not supported'); } - if (!txHash) { - throw new Error('Transaction hash is required'); + if (!transactionParams) { + throw new Error('Transaction Parameters are required'); } return await waitForTransaction({ - txHash, + transactionParams, chain, config: connectionOrConfig, overrides, }); }, - enabled: Boolean(connectionOrConfig && chain && txHash), + enabled: Boolean(connectionOrConfig && chain && transactionParams), retry: false, refetchOnWindowFocus: false, }); From b71028237393f7b55178e7da861f19fa59a261c0 Mon Sep 17 00:00:00 2001 From: Ankit Singh Date: Wed, 11 Sep 2024 19:08:39 +0530 Subject: [PATCH 05/15] feat: `waitForTransaction` hook added for aleph zero --- .../react/src/actions/waitForTransaction.ts | 48 +++++++++++++++++++ packages/react/src/types/index.ts | 36 +++++++++++++- 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/packages/react/src/actions/waitForTransaction.ts b/packages/react/src/actions/waitForTransaction.ts index 1d363665..5fb9c72e 100644 --- a/packages/react/src/actions/waitForTransaction.ts +++ b/packages/react/src/actions/waitForTransaction.ts @@ -109,5 +109,53 @@ export const waitForTransaction = async ({ return receipt as TransactionReceipt; } + if (chain.type === 'alephZero') { + const { blockHash, extrinsicIndex } = transactionParams as TransactionParams<'alephZero'>; + const alephZero = config.alephZeroApi; + + const receipt = await pollCallback( + async () => { + const signedBlock = await alephZero.rpc.chain.getBlock(blockHash); + + // Get the specific extrinsic + const extrinsic = signedBlock.block.extrinsics[extrinsicIndex]; + + if (!extrinsic) { + throw new Error('Extrinsic not found'); + } + + // Get the events for this block + const apiAt = await alephZero.at(blockHash); + const allRecords = (await apiAt.query.system.events()).toPrimitive() as any[]; + + const extrinsicEvents = allRecords.filter( + (event) => event.phase.applyExtrinsic && event.phase.applyExtrinsic === extrinsicIndex, + ); + + const transactionData = { + blockHash, + extrinsicIndex, + extrinsic: extrinsic, + extrinsicHash: extrinsic.hash.toHuman(), + method: extrinsic.method.toHuman(), + args: extrinsic.args.map((arg) => arg.toHuman()), + events: extrinsicEvents, + }; + + return transactionData; + }, + { + interval: overrides?.interval || DEFAULT_POLLING_INTERVAL, + timeout: overrides?.timeout, + }, + ); + + if (!receipt) { + throw new Error('Transaction not found'); + } + + return receipt as TransactionReceipt; + } + throw new Error('Chain not supported'); }; diff --git a/packages/react/src/types/index.ts b/packages/react/src/types/index.ts index a02a3306..8e71f02f 100644 --- a/packages/react/src/types/index.ts +++ b/packages/react/src/types/index.ts @@ -109,4 +109,38 @@ export type TransactionReceipt = C extends 'evm ? EVMTxReceipt : C extends 'tron' ? TronWebTypes.TransactionInfo - : unknown; + : C extends 'alephZero' + ? AlephTransactionData + : unknown; + +// custom types + +type ExtrinsicArg = { + toHuman: () => string; +}; + +type Extrinsic = { + hash: { + toHuman: () => string; + }; + method: { + toHuman: () => string; + }; + args: ExtrinsicArg[]; +}; + +type ExtrinsicEvent = { + phase: { applyExtrinsic: number }; + event: { index: string; data: any[] }; + topics: string[]; +}; + +type AlephTransactionData = { + blockHash: string; + extrinsicIndex: number; + extrinsic: Extrinsic; + extrinsicHash: string; + method: string; + args: string[]; + events: ExtrinsicEvent[]; +}; From 57e98cd5d597b5421d410fd76f3edb2d1193c794 Mon Sep 17 00:00:00 2001 From: Ankit Singh Date: Wed, 11 Sep 2024 19:09:46 +0530 Subject: [PATCH 06/15] chore: packages update --- packages/react/package.json | 1 + pnpm-lock.yaml | 40 +++++++++++++++++++++++-------------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/packages/react/package.json b/packages/react/package.json index cb3e5f58..f25427ea 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -49,6 +49,7 @@ "react": "^18.2.0", "react-dom": "^18.2.0", "tronweb": "6.0.0-beta.4", + "polkadot-types": "link:@polkadot/types^12.3.1", "viem": "^2.13.8", "wagmi": "^2.9.11", "zustand": "^4.5.2" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8c639494..8e604b22 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -256,6 +256,9 @@ importers: tronweb: specifier: 6.0.0-beta.4 version: 6.0.0-beta.4(bufferutil@4.0.8)(utf-8-validate@5.0.10) + types^12.3.1: + specifier: link:@polkadot/types^12.3.1 + version: link:@polkadot/types^12.3.1 viem: specifier: ^2.13.8 version: 2.13.8(bufferutil@4.0.8)(typescript@5.4.5)(utf-8-validate@5.0.10)(zod@3.23.8) @@ -3207,6 +3210,7 @@ packages: '@walletconnect/sign-client@2.11.0': resolution: {integrity: sha512-H2ukscibBS+6WrzQWh+WyVBqO5z4F5et12JcwobdwgHnJSlqIoZxqnUYYWNCI5rUR5UKsKWaUyto4AE9N5dw4Q==} + deprecated: Reliability and performance greatly improved - please see https://github.com/WalletConnect/walletconnect-monorepo/releases '@walletconnect/sign-client@2.13.0': resolution: {integrity: sha512-En7KSvNUlQFx20IsYGsFgkNJ2lpvDvRsSFOT5PTdGskwCkUfOpB33SQJ6nCrN19gyoKPNvWg80Cy6MJI0TjNYA==} @@ -9931,6 +9935,12 @@ snapshots: '@polkadot/util-crypto': 12.6.2(@polkadot/util@12.6.2) tslib: 2.6.3 + '@polkadot/keyring@12.6.2(@polkadot/util-crypto@12.6.2(@polkadot/util@13.0.2))(@polkadot/util@12.6.2)': + dependencies: + '@polkadot/util': 12.6.2 + '@polkadot/util-crypto': 12.6.2(@polkadot/util@13.0.2) + tslib: 2.6.3 + '@polkadot/keyring@13.0.2(@polkadot/util-crypto@13.0.2(@polkadot/util@13.0.2))(@polkadot/util@13.0.2)': dependencies: '@polkadot/util': 13.0.2 @@ -10022,7 +10032,7 @@ snapshots: '@polkadot/rpc-provider@10.13.1(bufferutil@4.0.8)(utf-8-validate@5.0.10)': dependencies: - '@polkadot/keyring': 12.6.2(@polkadot/util-crypto@12.6.2(@polkadot/util@12.6.2))(@polkadot/util@12.6.2) + '@polkadot/keyring': 12.6.2(@polkadot/util-crypto@12.6.2(@polkadot/util@13.0.2))(@polkadot/util@12.6.2) '@polkadot/types': 10.13.1 '@polkadot/types-support': 10.13.1 '@polkadot/util': 12.6.2 @@ -10165,7 +10175,7 @@ snapshots: '@polkadot/types@10.13.1': dependencies: - '@polkadot/keyring': 12.6.2(@polkadot/util-crypto@12.6.2(@polkadot/util@12.6.2))(@polkadot/util@12.6.2) + '@polkadot/keyring': 12.6.2(@polkadot/util-crypto@12.6.2(@polkadot/util@13.0.2))(@polkadot/util@12.6.2) '@polkadot/types-augment': 10.13.1 '@polkadot/types-codec': 10.13.1 '@polkadot/types-create': 10.13.1 @@ -10191,10 +10201,10 @@ snapshots: '@noble/hashes': 1.4.0 '@polkadot/networks': 12.6.2 '@polkadot/util': 12.6.2 - '@polkadot/wasm-crypto': 7.3.2(@polkadot/util@12.6.2)(@polkadot/x-randomvalues@12.6.2(@polkadot/util@12.6.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@12.6.2))) + '@polkadot/wasm-crypto': 7.3.2(@polkadot/util@12.6.2)(@polkadot/x-randomvalues@12.6.2(@polkadot/util@12.6.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@13.0.2))) '@polkadot/wasm-util': 7.3.2(@polkadot/util@12.6.2) '@polkadot/x-bigint': 12.6.2 - '@polkadot/x-randomvalues': 12.6.2(@polkadot/util@12.6.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@12.6.2)) + '@polkadot/x-randomvalues': 12.6.2(@polkadot/util@12.6.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@13.0.2)) '@scure/base': 1.1.6 tslib: 2.6.3 @@ -10244,11 +10254,11 @@ snapshots: bn.js: 5.2.1 tslib: 2.6.3 - '@polkadot/wasm-bridge@7.3.2(@polkadot/util@12.6.2)(@polkadot/x-randomvalues@12.6.2(@polkadot/util@12.6.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@12.6.2)))': + '@polkadot/wasm-bridge@7.3.2(@polkadot/util@12.6.2)(@polkadot/x-randomvalues@12.6.2(@polkadot/util@12.6.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@13.0.2)))': dependencies: '@polkadot/util': 12.6.2 '@polkadot/wasm-util': 7.3.2(@polkadot/util@12.6.2) - '@polkadot/x-randomvalues': 12.6.2(@polkadot/util@12.6.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@12.6.2)) + '@polkadot/x-randomvalues': 12.6.2(@polkadot/util@12.6.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@13.0.2)) tslib: 2.6.3 '@polkadot/wasm-bridge@7.3.2(@polkadot/util@13.0.2)(@polkadot/x-randomvalues@12.6.2(@polkadot/util@13.0.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@13.0.2)))': @@ -10275,14 +10285,14 @@ snapshots: '@polkadot/util': 13.0.2 tslib: 2.6.3 - '@polkadot/wasm-crypto-init@7.3.2(@polkadot/util@12.6.2)(@polkadot/x-randomvalues@12.6.2(@polkadot/util@12.6.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@12.6.2)))': + '@polkadot/wasm-crypto-init@7.3.2(@polkadot/util@12.6.2)(@polkadot/x-randomvalues@12.6.2(@polkadot/util@12.6.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@13.0.2)))': dependencies: '@polkadot/util': 12.6.2 - '@polkadot/wasm-bridge': 7.3.2(@polkadot/util@12.6.2)(@polkadot/x-randomvalues@12.6.2(@polkadot/util@12.6.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@12.6.2))) + '@polkadot/wasm-bridge': 7.3.2(@polkadot/util@12.6.2)(@polkadot/x-randomvalues@12.6.2(@polkadot/util@12.6.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@13.0.2))) '@polkadot/wasm-crypto-asmjs': 7.3.2(@polkadot/util@12.6.2) '@polkadot/wasm-crypto-wasm': 7.3.2(@polkadot/util@12.6.2) '@polkadot/wasm-util': 7.3.2(@polkadot/util@12.6.2) - '@polkadot/x-randomvalues': 12.6.2(@polkadot/util@12.6.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@12.6.2)) + '@polkadot/x-randomvalues': 12.6.2(@polkadot/util@12.6.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@13.0.2)) tslib: 2.6.3 '@polkadot/wasm-crypto-init@7.3.2(@polkadot/util@13.0.2)(@polkadot/x-randomvalues@12.6.2(@polkadot/util@13.0.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@13.0.2)))': @@ -10317,15 +10327,15 @@ snapshots: '@polkadot/wasm-util': 7.3.2(@polkadot/util@13.0.2) tslib: 2.6.3 - '@polkadot/wasm-crypto@7.3.2(@polkadot/util@12.6.2)(@polkadot/x-randomvalues@12.6.2(@polkadot/util@12.6.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@12.6.2)))': + '@polkadot/wasm-crypto@7.3.2(@polkadot/util@12.6.2)(@polkadot/x-randomvalues@12.6.2(@polkadot/util@12.6.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@13.0.2)))': dependencies: '@polkadot/util': 12.6.2 - '@polkadot/wasm-bridge': 7.3.2(@polkadot/util@12.6.2)(@polkadot/x-randomvalues@12.6.2(@polkadot/util@12.6.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@12.6.2))) + '@polkadot/wasm-bridge': 7.3.2(@polkadot/util@12.6.2)(@polkadot/x-randomvalues@12.6.2(@polkadot/util@12.6.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@13.0.2))) '@polkadot/wasm-crypto-asmjs': 7.3.2(@polkadot/util@12.6.2) - '@polkadot/wasm-crypto-init': 7.3.2(@polkadot/util@12.6.2)(@polkadot/x-randomvalues@12.6.2(@polkadot/util@12.6.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@12.6.2))) + '@polkadot/wasm-crypto-init': 7.3.2(@polkadot/util@12.6.2)(@polkadot/x-randomvalues@12.6.2(@polkadot/util@12.6.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@13.0.2))) '@polkadot/wasm-crypto-wasm': 7.3.2(@polkadot/util@12.6.2) '@polkadot/wasm-util': 7.3.2(@polkadot/util@12.6.2) - '@polkadot/x-randomvalues': 12.6.2(@polkadot/util@12.6.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@12.6.2)) + '@polkadot/x-randomvalues': 12.6.2(@polkadot/util@12.6.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@13.0.2)) tslib: 2.6.3 '@polkadot/wasm-crypto@7.3.2(@polkadot/util@13.0.2)(@polkadot/x-randomvalues@12.6.2(@polkadot/util@13.0.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@13.0.2)))': @@ -10390,10 +10400,10 @@ snapshots: dependencies: tslib: 2.6.3 - '@polkadot/x-randomvalues@12.6.2(@polkadot/util@12.6.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@12.6.2))': + '@polkadot/x-randomvalues@12.6.2(@polkadot/util@12.6.2)(@polkadot/wasm-util@7.3.2(@polkadot/util@13.0.2))': dependencies: '@polkadot/util': 12.6.2 - '@polkadot/wasm-util': 7.3.2(@polkadot/util@12.6.2) + '@polkadot/wasm-util': 7.3.2(@polkadot/util@13.0.2) '@polkadot/x-global': 12.6.2 tslib: 2.6.3 From b9f800614504d9d9988d7a17f891e3f1936134cc Mon Sep 17 00:00:00 2001 From: Ankit Singh Date: Wed, 11 Sep 2024 19:12:41 +0530 Subject: [PATCH 07/15] readme update --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ef051820..ecc42c20 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ export default function App({ Component, pageProps }) { | EVM | :white_check_mark: | :white_check_mark: | :small_orange_diamond: | :small_orange_diamond: | :small_orange_diamond: | | Solana | :white_check_mark: | :white_check_mark: | :small_orange_diamond: | :small_orange_diamond: | :small_orange_diamond: | | Tron | :white_check_mark: | :white_check_mark: | :small_orange_diamond: | :small_orange_diamond: | :small_orange_diamond: | -| AlephZero | :white_check_mark: | :white_check_mark: | :x: | :x: | :x: | +| AlephZero | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :small_orange_diamond: | | Sui | :small_orange_diamond: | :x: | :x: | :x: | :x: | | Cosmos | :x: | :x: | :x: | :x: | :x: | | Near | :x: | :x: | :x: | :x: | :x: | From 23fde093fa31fa7833b66ea00f906f43e7066517 Mon Sep 17 00:00:00 2001 From: Ankit Singh Date: Thu, 12 Sep 2024 01:14:03 +0530 Subject: [PATCH 08/15] feat: transaction reciept added for alephZero --- .../src/actions/getTransactionReceipt.ts | 48 +++++++++++++++++-- .../react/src/hooks/useTransactionReceipt.ts | 17 +++---- 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/packages/react/src/actions/getTransactionReceipt.ts b/packages/react/src/actions/getTransactionReceipt.ts index 8c867367..d6628fe4 100644 --- a/packages/react/src/actions/getTransactionReceipt.ts +++ b/packages/react/src/actions/getTransactionReceipt.ts @@ -1,5 +1,6 @@ import { getTransactionReceipt as getEVMTransactionReceipt } from '@wagmi/core'; import { ChainData, ChainType, ConnectionOrConfig, TransactionReceipt } from '../types/index.js'; +import { TransactionParams } from './waitForTransaction.js'; export type GetTransactionReceiptOverrides = C extends 'solana' ? { @@ -8,7 +9,7 @@ export type GetTransactionReceiptOverrides = C : any; export type GetTransactionReceiptParams = { - txHash: string; + transactionParams: TransactionParams; chain: ChainData; config: ConnectionOrConfig; overrides: GetTransactionReceiptOverrides | undefined; @@ -16,19 +17,20 @@ export type GetTransactionReceiptParams = { /** * Get transaction receipt - * @param txHash - Transaction hash + * @param transactionParams - Transaction Parameters {@link TransactionParams} * @param chain - {@link ChainData} * @param config {@link ConnectionOrConfig} * @returns Transaction Receipt {@link TransactionReceipt} */ export const getTransactionReceipt = async ({ - txHash, + transactionParams, chain, config, overrides = {} as GetTransactionReceiptOverrides, }: GetTransactionReceiptParams): Promise> => { // evm chain if (chain.type === 'evm') { + const { txHash } = transactionParams as TransactionParams<'evm'>; const receipt = await getEVMTransactionReceipt(config.wagmiConfig, { hash: txHash as `0x${string}`, chainId: chain.id, @@ -37,10 +39,12 @@ export const getTransactionReceipt = async ({ } if (chain.type === 'tron') { + const { txHash } = transactionParams as TransactionParams<'tron'>; return (await config.tronWeb.trx.getTransactionInfo(txHash)) as TransactionReceipt; } if (chain.type === 'solana') { + const { txHash } = transactionParams as TransactionParams<'solana'>; const result = config.solanaConnection.getTransaction(txHash, { maxSupportedTransactionVersion: 0, ...overrides, @@ -51,5 +55,43 @@ export const getTransactionReceipt = async ({ return result as TransactionReceipt; } + if (chain.type === 'alephZero') { + const { blockHash, extrinsicIndex } = transactionParams as TransactionParams<'alephZero'>; + const alephZero = config.alephZeroApi; + + const signedBlock = await alephZero.rpc.chain.getBlock(blockHash); + + // Get the specific extrinsic + const extrinsic = signedBlock.block.extrinsics[extrinsicIndex]; + + if (!extrinsic) { + throw new Error('Extrinsic not found'); + } + + // Get the events for this block + const apiAt = await alephZero.at(blockHash); + const allRecords = (await apiAt.query.system.events()).toPrimitive() as any[]; + + const extrinsicEvents = allRecords.filter( + (event) => event.phase.applyExtrinsic && event.phase.applyExtrinsic === extrinsicIndex, + ); + + const transactionData = { + blockHash, + extrinsicIndex, + extrinsic: extrinsic, + extrinsicHash: extrinsic.hash.toHuman(), + method: extrinsic.method.toHuman(), + args: extrinsic.args.map((arg) => arg.toHuman()), + events: extrinsicEvents, + }; + + if (!transactionData) { + throw new Error('Transaction not found'); + } + + return transactionData as TransactionReceipt; + } + throw new Error('Chain type not supported'); }; diff --git a/packages/react/src/hooks/useTransactionReceipt.ts b/packages/react/src/hooks/useTransactionReceipt.ts index 05b03f10..df48d422 100644 --- a/packages/react/src/hooks/useTransactionReceipt.ts +++ b/packages/react/src/hooks/useTransactionReceipt.ts @@ -1,12 +1,13 @@ import { useQuery } from '@tanstack/react-query'; import { GetTransactionReceiptOverrides, getTransactionReceipt } from '../actions/getTransactionReceipt.js'; +import { TransactionParams } from '../actions/waitForTransaction.js'; import { ChainId } from '../types/index.js'; import { useChain } from './useChain.js'; import { useConnectionOrConfig } from './useConnectionOrConfig.js'; export type UseTransactionReceiptParams = { - /** Transaction hash */ - txHash: string | undefined; + /** Transaction hash {@link TransactionParams} */ + transactionParams: TransactionParams; /** Chain ID of transaction */ chainId: ChainId; /** Transaction overrides {@link GetTransactionReceiptOverrides} */ @@ -15,17 +16,17 @@ export type UseTransactionReceiptParams = { /** * Transaction Receipt Hook - * @param txHash - Transaction hash + * @param transactionParams - Transaction Parameters * @param chainId - Chain ID of transaction * @param overrides - Transaction overrides {@link GetTransactionReceiptOverrides} * @returns Query object */ -export const useTransactionReceipt = ({ txHash, chainId, overrides }: UseTransactionReceiptParams) => { +export const useTransactionReceipt = ({ transactionParams, chainId, overrides }: UseTransactionReceiptParams) => { const connectionOrConfig = useConnectionOrConfig(); const chain = useChain(chainId); return useQuery({ - queryKey: ['transaction receipt', txHash, chainId], + queryKey: ['transaction receipt', transactionParams, chainId], queryFn: async () => { if (!connectionOrConfig) { throw new Error('Connections or config not found'); @@ -33,18 +34,18 @@ export const useTransactionReceipt = ({ txHash, chainId, overrides }: UseTransac if (!chain) { throw new Error('Chain is not supported'); } - if (!txHash) { + if (!transactionParams) { throw new Error('Transaction hash is required'); } return await getTransactionReceipt({ - txHash, + transactionParams, chain, config: connectionOrConfig, overrides, }); }, - enabled: Boolean(connectionOrConfig && chain && txHash), + enabled: Boolean(connectionOrConfig && chain && transactionParams), retry: false, refetchOnWindowFocus: false, }); From e2d39a8f7614ccaeeee61489d5d492b8ac8fa5f8 Mon Sep 17 00:00:00 2001 From: Ankit Singh Date: Thu, 12 Sep 2024 01:17:56 +0530 Subject: [PATCH 09/15] chore: update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ecc42c20..abecca92 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ export default function App({ Component, pageProps }) { | EVM | :white_check_mark: | :white_check_mark: | :small_orange_diamond: | :small_orange_diamond: | :small_orange_diamond: | | Solana | :white_check_mark: | :white_check_mark: | :small_orange_diamond: | :small_orange_diamond: | :small_orange_diamond: | | Tron | :white_check_mark: | :white_check_mark: | :small_orange_diamond: | :small_orange_diamond: | :small_orange_diamond: | -| AlephZero | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :small_orange_diamond: | +| AlephZero | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | | Sui | :small_orange_diamond: | :x: | :x: | :x: | :x: | | Cosmos | :x: | :x: | :x: | :x: | :x: | | Near | :x: | :x: | :x: | :x: | :x: | From 0cf077ddab6997b5492e2d30759f94968613bab6 Mon Sep 17 00:00:00 2001 From: Ankit Singh Date: Thu, 12 Sep 2024 01:20:43 +0530 Subject: [PATCH 10/15] chore: lockfile update --- pnpm-lock.yaml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8e604b22..c810a71a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -247,6 +247,9 @@ importers: immer: specifier: ^10.1.1 version: 10.1.1 + polkadot-types: + specifier: link:@polkadot/types^12.3.1 + version: link:@polkadot/types^12.3.1 react: specifier: ^18.2.0 version: 18.3.1 @@ -256,9 +259,6 @@ importers: tronweb: specifier: 6.0.0-beta.4 version: 6.0.0-beta.4(bufferutil@4.0.8)(utf-8-validate@5.0.10) - types^12.3.1: - specifier: link:@polkadot/types^12.3.1 - version: link:@polkadot/types^12.3.1 viem: specifier: ^2.13.8 version: 2.13.8(bufferutil@4.0.8)(typescript@5.4.5)(utf-8-validate@5.0.10)(zod@3.23.8) @@ -3210,7 +3210,6 @@ packages: '@walletconnect/sign-client@2.11.0': resolution: {integrity: sha512-H2ukscibBS+6WrzQWh+WyVBqO5z4F5et12JcwobdwgHnJSlqIoZxqnUYYWNCI5rUR5UKsKWaUyto4AE9N5dw4Q==} - deprecated: Reliability and performance greatly improved - please see https://github.com/WalletConnect/walletconnect-monorepo/releases '@walletconnect/sign-client@2.13.0': resolution: {integrity: sha512-En7KSvNUlQFx20IsYGsFgkNJ2lpvDvRsSFOT5PTdGskwCkUfOpB33SQJ6nCrN19gyoKPNvWg80Cy6MJI0TjNYA==} From 05d494ed33c68e3daa0112c06134508bef574faa Mon Sep 17 00:00:00 2001 From: Ankit Singh Date: Thu, 12 Sep 2024 01:37:09 +0530 Subject: [PATCH 11/15] fix: build issue with unused var --- packages/react/src/actions/sendTransaction.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/react/src/actions/sendTransaction.ts b/packages/react/src/actions/sendTransaction.ts index d0b1c2c9..9b2a0e0a 100644 --- a/packages/react/src/actions/sendTransaction.ts +++ b/packages/react/src/actions/sendTransaction.ts @@ -96,6 +96,7 @@ export const sendTransactionToChain = async ({ if (chain.type === 'alephZero') { let txnHash: string | undefined = undefined; let block: string | undefined = undefined; + let extrinsicId: number | undefined = undefined; const walletConnector = config.connector as WalletInstance<'alephZero'>; @@ -111,6 +112,7 @@ export const sendTransactionToChain = async ({ if (method === 'ExtrinsicSuccess' && status.type === 'InBlock') { txnHash = txHash.toString(); block = status.asFinalized.toHex(); + extrinsicId = txIndex; } else if (method === 'ExtrinsicFailed') { throw new Error(`Transaction failed: ${method}`); } @@ -125,6 +127,7 @@ export const sendTransactionToChain = async ({ return { txHash: txnHash, block: block, + txIndex: extrinsicId, }; } From 0873dc768331a4ce1fbc70b81d590c2ce8523d31 Mon Sep 17 00:00:00 2001 From: Ankit Singh Date: Thu, 12 Sep 2024 16:50:10 +0530 Subject: [PATCH 12/15] fix: allow `transactionParams` to to undefined --- packages/react/src/hooks/useTransactionReceipt.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/react/src/hooks/useTransactionReceipt.ts b/packages/react/src/hooks/useTransactionReceipt.ts index df48d422..ae9e76de 100644 --- a/packages/react/src/hooks/useTransactionReceipt.ts +++ b/packages/react/src/hooks/useTransactionReceipt.ts @@ -7,7 +7,7 @@ import { useConnectionOrConfig } from './useConnectionOrConfig.js'; export type UseTransactionReceiptParams = { /** Transaction hash {@link TransactionParams} */ - transactionParams: TransactionParams; + transactionParams: TransactionParams | undefined; /** Chain ID of transaction */ chainId: ChainId; /** Transaction overrides {@link GetTransactionReceiptOverrides} */ @@ -35,7 +35,7 @@ export const useTransactionReceipt = ({ transactionParams, chainId, overrides }: throw new Error('Chain is not supported'); } if (!transactionParams) { - throw new Error('Transaction hash is required'); + throw new Error('Transaction Params are required'); } return await getTransactionReceipt({ From a45f4c190b2005f339ef209ada03c1f7512c5159 Mon Sep 17 00:00:00 2001 From: Ankit Singh Date: Thu, 12 Sep 2024 17:07:48 +0530 Subject: [PATCH 13/15] fix: aleph types added to new file --- packages/react/src/types/aleph.ts | 29 +++++++++++++++++++++++++++ packages/react/src/types/index.ts | 33 +------------------------------ 2 files changed, 30 insertions(+), 32 deletions(-) create mode 100644 packages/react/src/types/aleph.ts diff --git a/packages/react/src/types/aleph.ts b/packages/react/src/types/aleph.ts new file mode 100644 index 00000000..825cdb0f --- /dev/null +++ b/packages/react/src/types/aleph.ts @@ -0,0 +1,29 @@ +type ExtrinsicArg = { + toHuman: () => string; +}; + +type Extrinsic = { + hash: { + toHuman: () => string; + }; + method: { + toHuman: () => string; + }; + args: ExtrinsicArg[]; +}; + +type ExtrinsicEvent = { + phase: { applyExtrinsic: number }; + event: { index: string; data: any[] }; + topics: string[]; +}; + +export type AlephTransactionData = { + blockHash: string; + extrinsicIndex: number; + extrinsic: Extrinsic; + extrinsicHash: string; + method: string; + args: string[]; + events: ExtrinsicEvent[]; +}; diff --git a/packages/react/src/types/index.ts b/packages/react/src/types/index.ts index 8e71f02f..ee4a629e 100644 --- a/packages/react/src/types/index.ts +++ b/packages/react/src/types/index.ts @@ -5,6 +5,7 @@ import { Types as TronWebTypes, type TronWeb } from 'tronweb'; import { Chain as ViemChain } from 'viem'; import { Config as WagmiConfig } from 'wagmi'; import { CHAIN_ID } from '../constants/index.js'; +import { AlephTransactionData } from './aleph.js'; import { ChainConnectors } from './wallet.js'; export const CHAIN_TYPES = [ @@ -112,35 +113,3 @@ export type TransactionReceipt = C extends 'evm : C extends 'alephZero' ? AlephTransactionData : unknown; - -// custom types - -type ExtrinsicArg = { - toHuman: () => string; -}; - -type Extrinsic = { - hash: { - toHuman: () => string; - }; - method: { - toHuman: () => string; - }; - args: ExtrinsicArg[]; -}; - -type ExtrinsicEvent = { - phase: { applyExtrinsic: number }; - event: { index: string; data: any[] }; - topics: string[]; -}; - -type AlephTransactionData = { - blockHash: string; - extrinsicIndex: number; - extrinsic: Extrinsic; - extrinsicHash: string; - method: string; - args: string[]; - events: ExtrinsicEvent[]; -}; From bf6f05824fb076e07aa1dee3be84fa2c6f869fb6 Mon Sep 17 00:00:00 2001 From: Ankit Singh Date: Thu, 12 Sep 2024 17:08:51 +0530 Subject: [PATCH 14/15] fix: `polkadot-types` removed, not using anymore --- packages/react/package.json | 1 - pnpm-lock.yaml | 13 ++----------- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/packages/react/package.json b/packages/react/package.json index f25427ea..cb3e5f58 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -49,7 +49,6 @@ "react": "^18.2.0", "react-dom": "^18.2.0", "tronweb": "6.0.0-beta.4", - "polkadot-types": "link:@polkadot/types^12.3.1", "viem": "^2.13.8", "wagmi": "^2.9.11", "zustand": "^4.5.2" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c810a71a..6723b2da 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -247,9 +247,6 @@ importers: immer: specifier: ^10.1.1 version: 10.1.1 - polkadot-types: - specifier: link:@polkadot/types^12.3.1 - version: link:@polkadot/types^12.3.1 react: specifier: ^18.2.0 version: 18.3.1 @@ -9934,12 +9931,6 @@ snapshots: '@polkadot/util-crypto': 12.6.2(@polkadot/util@12.6.2) tslib: 2.6.3 - '@polkadot/keyring@12.6.2(@polkadot/util-crypto@12.6.2(@polkadot/util@13.0.2))(@polkadot/util@12.6.2)': - dependencies: - '@polkadot/util': 12.6.2 - '@polkadot/util-crypto': 12.6.2(@polkadot/util@13.0.2) - tslib: 2.6.3 - '@polkadot/keyring@13.0.2(@polkadot/util-crypto@13.0.2(@polkadot/util@13.0.2))(@polkadot/util@13.0.2)': dependencies: '@polkadot/util': 13.0.2 @@ -10031,7 +10022,7 @@ snapshots: '@polkadot/rpc-provider@10.13.1(bufferutil@4.0.8)(utf-8-validate@5.0.10)': dependencies: - '@polkadot/keyring': 12.6.2(@polkadot/util-crypto@12.6.2(@polkadot/util@13.0.2))(@polkadot/util@12.6.2) + '@polkadot/keyring': 12.6.2(@polkadot/util-crypto@12.6.2(@polkadot/util@12.6.2))(@polkadot/util@12.6.2) '@polkadot/types': 10.13.1 '@polkadot/types-support': 10.13.1 '@polkadot/util': 12.6.2 @@ -10174,7 +10165,7 @@ snapshots: '@polkadot/types@10.13.1': dependencies: - '@polkadot/keyring': 12.6.2(@polkadot/util-crypto@12.6.2(@polkadot/util@13.0.2))(@polkadot/util@12.6.2) + '@polkadot/keyring': 12.6.2(@polkadot/util-crypto@12.6.2(@polkadot/util@12.6.2))(@polkadot/util@12.6.2) '@polkadot/types-augment': 10.13.1 '@polkadot/types-codec': 10.13.1 '@polkadot/types-create': 10.13.1 From c2ef646a8b8723fc85adc824f1116d12fcf2417d Mon Sep 17 00:00:00 2001 From: Ankit Singh Date: Thu, 12 Sep 2024 17:11:15 +0530 Subject: [PATCH 15/15] typo --- packages/react/src/hooks/useTransactionReceipt.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react/src/hooks/useTransactionReceipt.ts b/packages/react/src/hooks/useTransactionReceipt.ts index ae9e76de..0dfc7a79 100644 --- a/packages/react/src/hooks/useTransactionReceipt.ts +++ b/packages/react/src/hooks/useTransactionReceipt.ts @@ -6,7 +6,7 @@ import { useChain } from './useChain.js'; import { useConnectionOrConfig } from './useConnectionOrConfig.js'; export type UseTransactionReceiptParams = { - /** Transaction hash {@link TransactionParams} */ + /** Transaction Params {@link TransactionParams} */ transactionParams: TransactionParams | undefined; /** Chain ID of transaction */ chainId: ChainId;