Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quiet-candies-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@relayprotocol/relay-sdk': patch
---

Add onTransactionReceived to execute() for settled request metadata
218 changes: 218 additions & 0 deletions packages/sdk/src/actions/execute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { MAINNET_RELAY_API } from '../constants'
import { executeBridge } from '../../tests/data/executeBridge'
import type { AdaptedWallet, Execute } from '../types'
import { evmDeadAddress } from '../constants/address'
import { axios } from '../utils'

let client: RelayClient | undefined
let wallet: AdaptedWallet = {
Expand Down Expand Up @@ -174,4 +175,221 @@ describe('Should test the execute action.', () => {
})
).toThrow('Recipient should never be burn address')
})

it('Should emit settled metadata through onProgress and onTransactionReceived', async () => {
client = createClient({
baseApiUrl: MAINNET_RELAY_API,
pollingInterval: 1,
maxPollingAttemptsBeforeTimeout: 3
})

const onProgress = vi.fn()
const onTransactionReceived = vi.fn()
const settledAmount = '1002000000000000'

executeStepsSpy.mockImplementation(
(
chainId: any,
request: any,
wallet: any,
progress: any,
clonedQuote: Execute,
options?: any
) => {
progress({
steps: clonedQuote.steps,
fees: clonedQuote.fees,
breakdown: clonedQuote.breakdown,
details: clonedQuote.details
})
return Promise.resolve(clonedQuote)
}
)

const axiosRequestSpy = vi
.spyOn(axios, 'request')
.mockResolvedValueOnce({
data: {
requests: [
{
id: '0xabc',
data: {
metadata: {}
}
}
]
}
} as any)
.mockResolvedValueOnce({
data: {
requests: [
{
id: '0xabc',
data: {
metadata: {}
}
}
]
}
} as any)
.mockResolvedValueOnce({
data: {
requests: [
{
id: '0xabc',
data: {
metadata: {
currencyOut: {
...quote.details?.currencyOut,
amount: settledAmount,
amountFormatted: '0.001002'
}
}
}
}
]
}
} as any)

await client?.actions?.execute({
wallet,
quote,
onProgress,
onTransactionReceived
})

await vi.waitFor(() => {
expect(onTransactionReceived).toHaveBeenCalledTimes(1)
})

expect(onTransactionReceived).toHaveBeenCalledWith(
expect.objectContaining({
id: '0xabc'
})
)
expect(axiosRequestSpy).toHaveBeenCalledTimes(3)
expect(onProgress).toHaveBeenCalled()
expect(
onProgress.mock.calls.at(-1)?.[0]?.details?.currencyOut?.amount
).toBe(settledAmount)

axiosRequestSpy.mockRestore()
})

it('Should not emit onTransactionReceived when settled metadata is unavailable', async () => {
client = createClient({
baseApiUrl: MAINNET_RELAY_API,
pollingInterval: 1,
maxPollingAttemptsBeforeTimeout: 1
})

const onTransactionReceived = vi.fn()
const axiosRequestSpy = vi.spyOn(axios, 'request').mockResolvedValue({
data: {
requests: [
{
id: '0xabc',
data: {
metadata: {}
}
}
]
}
} as any)

await client?.actions?.execute({
wallet,
quote,
onTransactionReceived
})

await vi.waitFor(() => {
expect(axiosRequestSpy).toHaveBeenCalledTimes(1)
})
expect(onTransactionReceived).not.toHaveBeenCalled()

axiosRequestSpy.mockRestore()
})

it('Should not emit settled metadata after aborting execution', async () => {
client = createClient({
baseApiUrl: MAINNET_RELAY_API,
pollingInterval: 1,
maxPollingAttemptsBeforeTimeout: 1
})

const onProgress = vi.fn()
const onTransactionReceived = vi.fn()
const settledAmount = '1002000000000000'
let resolveRequest: (value: any) => void = () => {}

executeStepsSpy.mockImplementation(
(
chainId: any,
request: any,
wallet: any,
progress: any,
clonedQuote: Execute,
options?: any
) => {
progress({
steps: clonedQuote.steps,
fees: clonedQuote.fees,
breakdown: clonedQuote.breakdown,
details: clonedQuote.details
})
return Promise.resolve(clonedQuote)
}
)

const axiosRequestSpy = vi.spyOn(axios, 'request').mockImplementation(
(config: any) =>
new Promise((resolve) => {
expect(config.signal).toBeDefined()
resolveRequest = resolve
}) as any
)

const execution = client?.actions?.execute({
wallet,
quote,
onProgress,
onTransactionReceived
})

await execution
execution?.abortController.abort()

resolveRequest({
data: {
requests: [
{
id: '0xabc',
data: {
metadata: {
currencyOut: {
...quote.details?.currencyOut,
amount: settledAmount,
amountFormatted: '0.001002'
}
}
}
}
]
}
})

await vi.waitFor(() => {
expect(axiosRequestSpy).toHaveBeenCalledTimes(1)
})

await new Promise((resolve) => setTimeout(resolve, 0))

expect(onTransactionReceived).not.toHaveBeenCalled()
expect(
onProgress.mock.calls.at(-1)?.[0]?.details?.currencyOut?.amount
).not.toBe(settledAmount)

axiosRequestSpy.mockRestore()
})
})
Loading
Loading