Skip to content

Commit a2cc7ec

Browse files
committed
Receipt handling for local relayer
1 parent 6a44e06 commit a2cc7ec

2 files changed

Lines changed: 38 additions & 8 deletions

File tree

packages/wallet/core/src/relayer/local.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { Constants, Payload } from '@0xsequence/wallet-primitives'
2-
import { AbiFunction, Address, Bytes, Hex } from 'ox'
2+
import { AbiFunction, Address, Bytes, Hex, TransactionReceipt } from 'ox'
33
import { FeeOption, FeeQuote, OperationStatus, Relayer } from './relayer.js'
44

5+
type GenericProviderTransactionReceipt = 'success' | 'failed' | 'unknown'
6+
57
export interface GenericProvider {
68
sendTransaction(args: { to: string; data: string }): Promise<string>
9+
getTransactionReceipt(txHash: string): Promise<GenericProviderTransactionReceipt>
710
}
811

912
export class LocalRelayer implements Relayer {
@@ -39,6 +42,18 @@ export class LocalRelayer implements Relayer {
3942
})
4043
return tx
4144
},
45+
getTransactionReceipt: async (txHash) => {
46+
const rpcReceipt = await eth.request({ method: 'eth_getTransactionReceipt', params: [txHash] })
47+
if (rpcReceipt) {
48+
const receipt = TransactionReceipt.fromRpc(rpcReceipt)
49+
if (receipt?.status === 'success') {
50+
return 'success'
51+
} else if (receipt?.status === 'reverted') {
52+
return 'failed'
53+
}
54+
}
55+
return 'unknown'
56+
},
4257
})
4358
}
4459

@@ -65,17 +80,23 @@ export class LocalRelayer implements Relayer {
6580
}
6681

6782
async relay(to: Address.Address, data: Hex.Hex, chainId: bigint, _?: FeeQuote): Promise<{ opHash: Hex.Hex }> {
68-
const hash = Payload.hash(to, chainId, this.decodeCalls(data))
69-
70-
await this.provider.sendTransaction({
83+
const txHash = await this.provider.sendTransaction({
7184
to,
7285
data,
7386
})
87+
Hex.assert(txHash)
7488

75-
return { opHash: Hex.fromBytes(hash) }
89+
return { opHash: txHash }
7690
}
7791

78-
status(opHash: Hex.Hex, chainId: bigint): Promise<OperationStatus> {
79-
throw new Error('Method not implemented.')
92+
async status(opHash: Hex.Hex, chainId: bigint): Promise<OperationStatus> {
93+
const receipt = await this.provider.getTransactionReceipt(opHash)
94+
if (receipt === 'unknown') {
95+
// Could be pending but we don't know
96+
return { status: 'unknown' }
97+
}
98+
return receipt === 'success'
99+
? { status: 'confirmed', transactionHash: opHash }
100+
: { status: 'failed', reason: 'failed' }
80101
}
81102
}

packages/wallet/core/src/relayer/pk-relayer.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Payload } from '@0xsequence/wallet-primitives'
2-
import { Address, Hex, Provider, Secp256k1, TransactionEnvelopeEip1559 } from 'ox'
2+
import { Address, Hex, Provider, Secp256k1, TransactionEnvelopeEip1559, TransactionReceipt } from 'ox'
33
import { LocalRelayer } from './local.js'
44
import { FeeOption, FeeQuote, OperationStatus, Relayer } from './relayer.js'
55

@@ -66,6 +66,15 @@ export class PkRelayer implements Relayer {
6666
})
6767
return tx
6868
},
69+
getTransactionReceipt: async (txHash: string) => {
70+
Hex.assert(txHash)
71+
const rpcReceipt = await this.provider.request({ method: 'eth_getTransactionReceipt', params: [txHash] })
72+
if (!rpcReceipt) {
73+
return 'unknown'
74+
}
75+
const receipt = TransactionReceipt.fromRpc(rpcReceipt)
76+
return receipt.status === 'success' ? 'success' : 'failed'
77+
},
6978
})
7079
}
7180

0 commit comments

Comments
 (0)