-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
145 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/next-common/hooks/useWalletConnectBuildPayload.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { useSignerAccount } from "next-common/components/popupWithSigner/context"; | ||
import { useContextApi } from "next-common/context/api"; | ||
import { useSubAccount } from "./account/useSubAccount"; | ||
import { useCallback } from "react"; | ||
|
||
/** | ||
* @file https://github.com/polkadot-cloud/polkadot-developer-console/blob/main/packages/app/src/hooks/useBuildPayload/index.tsx | ||
* @link https://docs.reown.com/advanced/multichain/polkadot/dapp-integration-guide#example-namespace-and-sign-client-connect-call | ||
*/ | ||
export function useWalletConnectBuildPayload() { | ||
const api = useContextApi(); | ||
const signerAccount = useSignerAccount(); | ||
const from = signerAccount?.address; | ||
|
||
const { data } = useSubAccount(from); | ||
|
||
const nonceRaw = data?.account?.nonce?.toNumber() || 0; | ||
|
||
return useCallback( | ||
async (tx) => { | ||
const lastHeader = await api.rpc.chain.getHeader(); | ||
const blockNumber = api.registry.createType( | ||
"BlockNumber", | ||
lastHeader.number.toNumber(), | ||
); | ||
const method = api.createType("Call", tx); | ||
const era = api.registry.createType("ExtrinsicEra", { | ||
current: lastHeader.number.toNumber(), | ||
period: 64, | ||
}); | ||
|
||
const nonce = api.registry.createType("Compact<Index>", nonceRaw); | ||
|
||
const payload = { | ||
specVersion: api.runtimeVersion.specVersion.toHex(), | ||
transactionVersion: api.runtimeVersion.transactionVersion.toHex(), | ||
runtimeVersion: api.runtimeVersion, | ||
version: tx.version, | ||
address: from, | ||
blockHash: lastHeader.hash.toHex(), | ||
blockNumber: blockNumber.toHex(), | ||
era: era.toHex(), | ||
genesisHash: api.genesisHash.toHex(), | ||
method: method.toHex(), | ||
nonce: nonce.toHex(), | ||
signedExtensions: api.registry.signedExtensions, | ||
tip: api.registry.createType("Compact<Balance>", 0).toHex(), | ||
}; | ||
|
||
return payload; | ||
}, | ||
[api, nonceRaw, from], | ||
); | ||
} |
42 changes: 42 additions & 0 deletions
42
packages/next-common/utils/sendTransaction/sendWalletConnectTx.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { noop } from "lodash-es"; | ||
import { createSendTxEventHandler } from "./sendSubstrateTx"; | ||
|
||
export async function sendWalletConnectTx({ | ||
api, | ||
tx, | ||
signerAddress, | ||
buildPayload = noop, | ||
signWcTx = noop, | ||
onStarted = noop, | ||
onInBlock = noop, | ||
onSubmitted = noop, | ||
onFinalized = noop, | ||
onError = noop, | ||
}) { | ||
onStarted(); | ||
|
||
try { | ||
const payload = await buildPayload(tx); | ||
const { signature } = await signWcTx(payload); | ||
const rawPayload = api.registry.createType("ExtrinsicPayload", payload, { | ||
version: payload.version, | ||
}); | ||
|
||
tx.addSignature(signerAddress, signature, rawPayload); | ||
|
||
const unsub = await tx.send( | ||
createSendTxEventHandler({ | ||
onFinalized, | ||
onInBlock, | ||
onError, | ||
unsub: () => unsub(), | ||
}), | ||
); | ||
|
||
onSubmitted(); | ||
} catch (error) { | ||
onError(error); | ||
} | ||
|
||
return true; | ||
} |