Skip to content

Commit

Permalink
📦 Gnosis fix state not updating (#1091)
Browse files Browse the repository at this point in the history
* Gnosis Safe fix state not updating
  • Loading branch information
yivlad committed Feb 21, 2023
1 parent 94092d3 commit d5056ed
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/pretty-olives-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@usedapp/core': patch
---

Gnosis Safe fix state not updating
9 changes: 8 additions & 1 deletion packages/core/src/helpers/gnosisSafeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export const getLatestNonce = async (chainId: number, safeAddress: string): Prom
}

export const waitForSafeTransaction = async (
transactionPromise: Promise<TransactionResponse>,
contract: Contract,
chainId: number,
safeTx: SafeTransaction
Expand All @@ -109,7 +110,13 @@ export const waitForSafeTransaction = async (
}> => {
const safeTxHash = calculateSafeTransactionHash(contract, safeTx, chainId)

return new Promise((resolve) => {
return new Promise((resolve, reject) => {
void transactionPromise.catch((err: any) => {
if (err?.message === 'Transaction was rejected') {
reject(err)
}
})

const onExecutionSuccess = async (txHash: string, _payment: BigNumber, event: Event) => {
if (txHash === safeTxHash) {
contract.removeListener('ExecutionSuccess', onExecutionSuccess)
Expand Down
29 changes: 21 additions & 8 deletions packages/core/src/hooks/usePromiseTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ export function usePromiseTransaction(chainId: number | undefined, options?: Tra
return { transaction, receipt }
}

const handleContractWallet = async ({ safeTransaction }: PromiseTransactionOpts = {}) => {
const handleContractWallet = async (
transactionPromise: Promise<TransactionResponse>,
{ safeTransaction }: PromiseTransactionOpts = {}
) => {
if (!chainId || !library || !account) return
setState({ status: 'CollectingSignaturePool', chainId, transactionName: options?.transactionName })

Expand All @@ -138,7 +141,12 @@ export function usePromiseTransaction(chainId: number | undefined, options?: Tra
nonce: latestNonce ? latestNonce + 1 : await gnosisSafeContract.nonce(),
})

const { transaction, receipt, rejected } = await waitForSafeTransaction(gnosisSafeContract, chainId, safeTx)
const { transaction, receipt, rejected } = await waitForSafeTransaction(
transactionPromise,
gnosisSafeContract,
chainId,
safeTx
)

if (rejected) {
const errorMessage = 'On-chain rejection created'
Expand Down Expand Up @@ -195,12 +203,17 @@ export function usePromiseTransaction(chainId: number | undefined, options?: Tra
chainId: chainId,
})
}
transaction = await transactionPromise
const result = (await isNonContractWallet(library, account))
? await handleNonContractWallet(transaction)
: await handleContractWallet({ safeTransaction })
transaction = result?.transaction
return result?.receipt
const isContractWallet = !(await isNonContractWallet(library, account))
if (isContractWallet) {
const result = await handleContractWallet(transactionPromise, { safeTransaction })
transaction = result?.transaction
return result?.receipt
} else {
transaction = await transactionPromise
const result = await handleNonContractWallet(transaction)
transaction = result?.transaction
return result?.receipt
}
} catch (e: any) {
const parsedErrorCode = parseInt(e.error?.data?.code ?? e.error?.code ?? e.data?.code ?? e.code)
const errorCode = isNaN(parsedErrorCode) ? undefined : parsedErrorCode
Expand Down

2 comments on commit d5056ed

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.