Skip to content

Commit

Permalink
Return txHash if available
Browse files Browse the repository at this point in the history
  • Loading branch information
katspaugh committed Oct 6, 2023
1 parent a4775d1 commit 213738a
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/components/tx-flow/flows/SafeAppsTx/ReviewSafeAppsTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ const ReviewSafeAppsTx = ({
createSafeTx().then(setSafeTx).catch(setSafeTxError)
}, [txList, setSafeTx, setSafeTxError, params])

const handleSubmit = async () => {
const handleSubmit = async (txId: string) => {
if (!safeTx || !onboard) return
trackSafeAppTxCount(Number(appId))

try {
await dispatchSafeAppsTx(safeTx, requestId, onboard, safe.chainId)
await dispatchSafeAppsTx(safeTx, requestId, onboard, safe.chainId, txId)
} catch (error) {
setSafeTxError(asError(error))
}
Expand Down
8 changes: 5 additions & 3 deletions src/components/tx/SignOrExecuteForm/ExecuteForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ const ExecuteForm = ({

const txOptions = getTxOptions(advancedParams, currentChain)

let executedTxId: string
try {
const executedTxId = await executeTx(txOptions, safeTx, txId, origin, willRelay, tx)
setTxFlow(<SuccessScreen txId={executedTxId} />, undefined, false)
executedTxId = await executeTx(txOptions, safeTx, txId, origin, willRelay, tx)
} catch (_err) {
const err = asError(_err)
trackError(Errors._804, err)
Expand All @@ -99,7 +99,9 @@ const ExecuteForm = ({
return
}

onSubmit()
// On success
setTxFlow(<SuccessScreen txId={executedTxId} />, undefined, false)
onSubmit(executedTxId)
}

const cannotPropose = !isOwner && !onlyExecute
Expand Down
6 changes: 4 additions & 2 deletions src/components/tx/SignOrExecuteForm/SignForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ const SignForm = ({
setIsSubmittable(false)
setSubmitError(undefined)

let resultTxId: string
try {
await (isAddingToBatch ? addToBatch(safeTx, origin) : signTx(safeTx, txId, origin, tx))
resultTxId = await (isAddingToBatch ? addToBatch(safeTx, origin) : signTx(safeTx, txId, origin, tx))
} catch (_err) {
const err = asError(_err)
trackError(Errors._805, err)
Expand All @@ -66,8 +67,9 @@ const SignForm = ({
return
}

// On success
setTxFlow(undefined)
onSubmit()
onSubmit(resultTxId)
}

const onBatchClick = (e: SyntheticEvent) => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/tx/SignOrExecuteForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { isDelegateCall } from '@/services/tx/tx-sender/sdk'

export type SignOrExecuteProps = {
txId?: string
onSubmit: () => void
onSubmit: (txId: string) => void
children?: ReactNode
isExecutable?: boolean
isRejection?: boolean
Expand Down
31 changes: 21 additions & 10 deletions src/services/safe-wallet-provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export type AppInfo = {
export type WalletSDK = {
signMessage: (message: string, appInfo: AppInfo) => Promise<{ signature?: string }>
signTypedMessage: (typedData: unknown, appInfo: AppInfo) => Promise<{ signature?: string }>
send: (params: { txs: unknown[]; params: { safeTxGas: number } }, appInfo: AppInfo) => Promise<{ safeTxHash: string }>
send: (
params: { txs: unknown[]; params: { safeTxGas: number } },
appInfo: AppInfo,
) => Promise<{ safeTxHash: string; txHash?: string }>
getBySafeTxHash: (safeTxHash: string) => Promise<{ txHash?: string }>
switchChain: (chainId: string, appInfo: AppInfo) => Promise<null>
proxy: (method: string, params: unknown[]) => Promise<unknown>
Expand Down Expand Up @@ -50,25 +53,28 @@ export class SafeWalletProvider {
this.sdk = sdk
}

private async makeRequest(id: number, request: RpcRequest, appInfo: AppInfo): Promise<unknown> {
private async makeRequest(request: RpcRequest, appInfo: AppInfo): Promise<unknown> {
const { method, params = [] } = request

switch (method) {
case 'wallet_switchEthereumChain':
case 'wallet_switchEthereumChain': {
const [{ chainId }] = params as [{ chainId: string }]
try {
await this.sdk.switchChain(chainId, appInfo)
} catch (e) {
throw new RpcError(RpcErrorCode.UNSUPPORTED_CHAIN, 'Unsupported chain')
}
return null
}

case 'eth_accounts':
case 'eth_accounts': {
return [this.safe.safeAddress]
}

case 'net_version':
case 'eth_chainId':
case 'eth_chainId': {
return `0x${this.safe.chainId.toString(16)}`
}

case 'personal_sign': {
const [message, address] = params as [string, string]
Expand Down Expand Up @@ -110,7 +116,7 @@ export class SafeWalletProvider {
return signature || '0x'
}

case 'eth_sendTransaction':
case 'eth_sendTransaction': {
const tx = {
value: '0',
data: '0x',
Expand All @@ -124,14 +130,16 @@ export class SafeWalletProvider {
tx.gas = parseInt(tx.gas, 16)
}

const { safeTxHash } = await this.sdk.send(
const { safeTxHash, txHash } = await this.sdk.send(
{
txs: [tx],
params: { safeTxGas: Number(tx.gas) },
},
appInfo,
)

if (txHash) return txHash

// Store fake transaction
this.submittedTxs.set(safeTxHash, {
from: this.safe.safeAddress,
Expand All @@ -148,8 +156,9 @@ export class SafeWalletProvider {
})

return safeTxHash
}

case 'eth_getTransactionByHash':
case 'eth_getTransactionByHash': {
let txHash = params[0] as string
try {
const resp = await this.sdk.getBySafeTxHash(txHash)
Expand All @@ -161,6 +170,7 @@ export class SafeWalletProvider {
return this.submittedTxs.get(txHash)
}
return await this.sdk.proxy(method, [txHash])
}

case 'eth_getTransactionReceipt': {
let txHash = params[0] as string
Expand All @@ -171,8 +181,9 @@ export class SafeWalletProvider {
return this.sdk.proxy(method, params)
}

default:
default: {
return await this.sdk.proxy(method, params)
}
}
}

Expand All @@ -199,7 +210,7 @@ export class SafeWalletProvider {
return {
jsonrpc: '2.0',
id,
result: await this.makeRequest(id, request, appInfo),
result: await this.makeRequest(request, appInfo),
}
} catch (e) {
return {
Expand Down
18 changes: 14 additions & 4 deletions src/services/safe-wallet-provider/useSafeWalletProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext, useMemo } from 'react'
import { useContext, useEffect, useMemo, useRef } from 'react'
import { BigNumber } from 'ethers'
import { useRouter } from 'next/router'

Expand All @@ -22,6 +22,15 @@ export const _useTxFlowApi = (chainId: string, safeAddress: string): WalletSDK |
const web3ReadOnly = useWeb3ReadOnly()
const router = useRouter()
const { configs } = useChains()
const pendingTxs = useRef<Record<string, string>>({})

useEffect(() => {
const unsubscribe = txSubscribe(TxEvent.PROCESSING, async ({ txId, txHash }) => {
if (!txId) return
pendingTxs.current[txId] = txHash
})
return unsubscribe
}, [])

return useMemo<WalletSDK | undefined>(() => {
if (!chainId || !safeAddress) return
Expand Down Expand Up @@ -50,7 +59,7 @@ export const _useTxFlowApi = (chainId: string, safeAddress: string): WalletSDK |
},

async send(params: { txs: any[]; params: { safeTxGas: number } }, appInfo) {
const id = Math.random().toString(36).slice(2) // TODO: use JsonRpc id
const id = Math.random().toString(36).slice(2)

const transactions = params.txs.map(({ to, value, data }) => {
return {
Expand All @@ -77,9 +86,10 @@ export const _useTxFlowApi = (chainId: string, safeAddress: string): WalletSDK |
)

return new Promise((resolve) => {
const unsubscribe = txSubscribe(TxEvent.SAFE_APPS_REQUEST, async ({ safeAppRequestId, safeTxHash }) => {
const unsubscribe = txSubscribe(TxEvent.SAFE_APPS_REQUEST, async ({ safeAppRequestId, safeTxHash, txId }) => {
if (safeAppRequestId === id) {
resolve({ safeTxHash })
const txHash = pendingTxs.current[txId]
resolve({ safeTxHash, txHash })
unsubscribe()
}
})
Expand Down
3 changes: 2 additions & 1 deletion src/services/tx/tx-sender/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,11 @@ export const dispatchSafeAppsTx = async (
safeAppRequestId: RequestId,
onboard: OnboardAPI,
chainId: SafeInfo['chainId'],
txId: string,
) => {
const sdk = await getSafeSDKWithSigner(onboard, chainId)
const safeTxHash = await sdk.getTransactionHash(safeTx)
txDispatch(TxEvent.SAFE_APPS_REQUEST, { safeAppRequestId, safeTxHash })
txDispatch(TxEvent.SAFE_APPS_REQUEST, { safeAppRequestId, safeTxHash, txId })
}

export const dispatchTxRelay = async (
Expand Down
2 changes: 1 addition & 1 deletion src/services/tx/txEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ interface TxEvents {
[TxEvent.RELAYING]: Id & { taskId: string }
[TxEvent.FAILED]: Id & HumanDescription & { error: Error }
[TxEvent.SUCCESS]: Id & HumanDescription
[TxEvent.SAFE_APPS_REQUEST]: { safeAppRequestId: RequestId; safeTxHash: string }
[TxEvent.SAFE_APPS_REQUEST]: { txId: string; safeAppRequestId: RequestId; safeTxHash: string }
[TxEvent.BATCH_ADD]: Id
}

Expand Down

0 comments on commit 213738a

Please sign in to comment.