@lifi/sdk 4.0.0-alpha.1 → 4.0.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -165
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/types/version.d.ts +1 -1
- package/package.json +16 -1
- package/src/version.ts +1 -1
- package/CHANGELOG.md +0 -1272
- package/package.json.tmp +0 -103
- package/src/actions/actions.unit.handlers.ts +0 -78
- package/src/actions/getChains.unit.spec.ts +0 -19
- package/src/actions/getConnections.unit.spec.ts +0 -45
- package/src/actions/getContractCallsQuote.unit.spec.ts +0 -323
- package/src/actions/getGasRecommendation.unit.spec.ts +0 -40
- package/src/actions/getNameServiceAddress.unit.spec.ts +0 -169
- package/src/actions/getQuote.int.spec.ts +0 -18
- package/src/actions/getQuote.unit.spec.ts +0 -154
- package/src/actions/getRelayedTransactionStatus.unit.spec.ts +0 -243
- package/src/actions/getRelayerQuote.unit.spec.ts +0 -220
- package/src/actions/getRoutes.unit.spec.ts +0 -112
- package/src/actions/getStatus.unit.spec.ts +0 -53
- package/src/actions/getStepTransaction.unit.spec.ts +0 -140
- package/src/actions/getToken.unit.spec.ts +0 -45
- package/src/actions/getTokenBalance.unit.spec.ts +0 -61
- package/src/actions/getTokenBalances.unit.spec.ts +0 -68
- package/src/actions/getTokenBalancesByChain.unit.spec.ts +0 -108
- package/src/actions/getTokens.unit.spec.ts +0 -16
- package/src/actions/getTools.unit.spec.ts +0 -20
- package/src/actions/getTransactionHistory.unit.spec.ts +0 -36
- package/src/actions/getWalletBalances.unit.spec.ts +0 -90
- package/src/actions/relayTransaction.unit.spec.ts +0 -229
- package/src/client/createClient.unit.spec.ts +0 -274
- package/src/client/getClientStorage.unit.spec.ts +0 -382
- package/src/core/StatusManager.unit.spec.ts +0 -298
- package/src/core/execution.unit.handlers.ts +0 -32
- package/src/core/execution.unit.mock.ts +0 -252
- package/src/core/execution.unit.spec.ts +0 -86
- package/src/core/stepComparison.unit.spec.ts +0 -89
- package/src/errors/SDKError.unit.spec.ts +0 -160
- package/src/errors/baseError.unit.spec.ts +0 -22
- package/src/errors/httpError.unit.spec.ts +0 -125
- package/src/errors/utils/baseErrorRootCause.unit.spec.ts +0 -89
- package/src/errors/utils/rootCause.unit.spec.ts +0 -36
- package/src/utils/checkPackageUpdates.unit.spec.ts +0 -71
- package/src/utils/convertQuoteToRoute.unit.spec.ts +0 -56
- package/src/utils/fetchTxErrorDetails.unit.spec.ts +0 -42
- package/src/utils/getTransactionMessage.unit.spec.ts +0 -38
- package/src/utils/isRoutesRequest.unit.spec.ts +0 -46
- package/src/utils/isStep.unit.spec.ts +0 -55
- package/src/utils/isToken.unit.spec.ts +0 -49
- package/src/utils/request.unit.spec.ts +0 -159
- package/src/utils/sleep.unit.spec.ts +0 -17
- package/src/utils/waitForResult.unit.spec.ts +0 -75
- package/src/utils/withDedupe.unit.spec.ts +0 -26
- package/tsconfig.json +0 -18
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import { findDefaultToken } from '@lifi/data-types'
|
|
2
|
-
import type { Token } from '@lifi/types'
|
|
3
|
-
import { ChainId, CoinKey } from '@lifi/types'
|
|
4
|
-
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
5
|
-
import { client } from './actions.unit.handlers.js'
|
|
6
|
-
import { getTokenBalancesByChain } from './getTokenBalancesByChain.js'
|
|
7
|
-
|
|
8
|
-
const mockedGetTokenBalancesForChains = vi.spyOn(
|
|
9
|
-
await import('./getTokenBalancesByChain.js'),
|
|
10
|
-
'getTokenBalancesByChain'
|
|
11
|
-
)
|
|
12
|
-
|
|
13
|
-
describe('getTokenBalancesByChain', () => {
|
|
14
|
-
beforeEach(() => {
|
|
15
|
-
vi.clearAllMocks()
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
const SOME_TOKEN = {
|
|
19
|
-
...findDefaultToken(CoinKey.USDC, ChainId.DAI),
|
|
20
|
-
priceUSD: '',
|
|
21
|
-
}
|
|
22
|
-
const SOME_WALLET_ADDRESS = 'some wallet address'
|
|
23
|
-
|
|
24
|
-
describe('user input is invalid', () => {
|
|
25
|
-
it('should throw Error because of missing walletAddress', async () => {
|
|
26
|
-
await expect(
|
|
27
|
-
getTokenBalancesByChain(client, '', {
|
|
28
|
-
[ChainId.DAI]: [SOME_TOKEN],
|
|
29
|
-
})
|
|
30
|
-
).rejects.toThrow('Missing walletAddress.')
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
it('should throw Error because of an invalid token', async () => {
|
|
34
|
-
await expect(
|
|
35
|
-
getTokenBalancesByChain(client, SOME_WALLET_ADDRESS, {
|
|
36
|
-
[ChainId.DAI]: [{ not: 'a token' } as unknown as Token],
|
|
37
|
-
})
|
|
38
|
-
).rejects.toThrow('Invalid tokens passed.')
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
it('should return empty token list as it is', async () => {
|
|
42
|
-
mockedGetTokenBalancesForChains.mockReturnValue(Promise.resolve([]))
|
|
43
|
-
|
|
44
|
-
const result = await getTokenBalancesByChain(
|
|
45
|
-
client,
|
|
46
|
-
SOME_WALLET_ADDRESS,
|
|
47
|
-
{
|
|
48
|
-
[ChainId.DAI]: [],
|
|
49
|
-
}
|
|
50
|
-
)
|
|
51
|
-
|
|
52
|
-
expect(result).toEqual([])
|
|
53
|
-
expect(mockedGetTokenBalancesForChains).toHaveBeenCalledTimes(1)
|
|
54
|
-
})
|
|
55
|
-
})
|
|
56
|
-
|
|
57
|
-
describe('user input is valid', () => {
|
|
58
|
-
it('should call the balance service', async () => {
|
|
59
|
-
const balanceResponse = {
|
|
60
|
-
[ChainId.DAI]: [
|
|
61
|
-
{
|
|
62
|
-
...SOME_TOKEN,
|
|
63
|
-
amount: 123n,
|
|
64
|
-
blockNumber: 1n,
|
|
65
|
-
},
|
|
66
|
-
],
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
mockedGetTokenBalancesForChains.mockReturnValue(
|
|
70
|
-
Promise.resolve(balanceResponse)
|
|
71
|
-
)
|
|
72
|
-
|
|
73
|
-
const result = await getTokenBalancesByChain(
|
|
74
|
-
client,
|
|
75
|
-
SOME_WALLET_ADDRESS,
|
|
76
|
-
{
|
|
77
|
-
[ChainId.DAI]: [SOME_TOKEN],
|
|
78
|
-
}
|
|
79
|
-
)
|
|
80
|
-
|
|
81
|
-
expect(mockedGetTokenBalancesForChains).toHaveBeenCalledTimes(1)
|
|
82
|
-
expect(result).toEqual(balanceResponse)
|
|
83
|
-
})
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
describe('provider is not the same as the chain type', () => {
|
|
87
|
-
it('should return the tokens as is', async () => {
|
|
88
|
-
const balanceResponse = {
|
|
89
|
-
[ChainId.DAI]: [SOME_TOKEN],
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
mockedGetTokenBalancesForChains.mockReturnValue(
|
|
93
|
-
Promise.resolve(balanceResponse)
|
|
94
|
-
)
|
|
95
|
-
|
|
96
|
-
const result = await getTokenBalancesByChain(
|
|
97
|
-
client,
|
|
98
|
-
SOME_WALLET_ADDRESS,
|
|
99
|
-
{
|
|
100
|
-
[ChainId.DAI]: [SOME_TOKEN],
|
|
101
|
-
}
|
|
102
|
-
)
|
|
103
|
-
|
|
104
|
-
expect(mockedGetTokenBalancesForChains).toHaveBeenCalledTimes(1)
|
|
105
|
-
expect(result).toEqual(balanceResponse)
|
|
106
|
-
})
|
|
107
|
-
})
|
|
108
|
-
})
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { ChainId } from '@lifi/types'
|
|
2
|
-
import { describe, expect, it } from 'vitest'
|
|
3
|
-
import { client, setupTestServer } from './actions.unit.handlers.js'
|
|
4
|
-
import { getTokens } from './getTokens.js'
|
|
5
|
-
|
|
6
|
-
describe('getTokens', () => {
|
|
7
|
-
setupTestServer()
|
|
8
|
-
|
|
9
|
-
it('return the tokens', async () => {
|
|
10
|
-
const result = await getTokens(client, {
|
|
11
|
-
chains: [ChainId.ETH, ChainId.POL],
|
|
12
|
-
})
|
|
13
|
-
expect(result).toBeDefined()
|
|
14
|
-
expect(result.tokens[ChainId.ETH]).toBeDefined()
|
|
15
|
-
})
|
|
16
|
-
})
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { ChainId } from '@lifi/types'
|
|
2
|
-
import { describe, expect, it } from 'vitest'
|
|
3
|
-
import { client, setupTestServer } from './actions.unit.handlers.js'
|
|
4
|
-
import { getTools } from './getTools.js'
|
|
5
|
-
|
|
6
|
-
describe('getTools', () => {
|
|
7
|
-
setupTestServer()
|
|
8
|
-
|
|
9
|
-
describe('and the backend succeeds', () => {
|
|
10
|
-
it('returns the tools', async () => {
|
|
11
|
-
const tools = await getTools(client, {
|
|
12
|
-
chains: [ChainId.ETH, ChainId.POL],
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
expect(tools).toBeDefined()
|
|
16
|
-
expect(tools.bridges).toBeDefined()
|
|
17
|
-
expect(tools.exchanges).toBeDefined()
|
|
18
|
-
})
|
|
19
|
-
})
|
|
20
|
-
})
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import type { TransactionAnalyticsRequest } from '@lifi/types'
|
|
2
|
-
import { HttpResponse, http } from 'msw'
|
|
3
|
-
import { describe, expect, it, vi } from 'vitest'
|
|
4
|
-
import * as request from '../utils/request.js'
|
|
5
|
-
import { client, setupTestServer } from './actions.unit.handlers.js'
|
|
6
|
-
import { getTransactionHistory } from './getTransactionHistory.js'
|
|
7
|
-
|
|
8
|
-
const mockedFetch = vi.spyOn(request, 'request')
|
|
9
|
-
|
|
10
|
-
describe('getTransactionHistory', () => {
|
|
11
|
-
const server = setupTestServer()
|
|
12
|
-
|
|
13
|
-
it('returns empty array in response', async () => {
|
|
14
|
-
server.use(
|
|
15
|
-
http.get(`${client.config.apiUrl}/analytics/transfers`, async () =>
|
|
16
|
-
HttpResponse.json({})
|
|
17
|
-
)
|
|
18
|
-
)
|
|
19
|
-
|
|
20
|
-
const walletAnalyticsRequest: TransactionAnalyticsRequest = {
|
|
21
|
-
fromTimestamp: 1696326609361,
|
|
22
|
-
toTimestamp: 1696326609362,
|
|
23
|
-
wallet: '0x5520abcd',
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const generatedURL =
|
|
27
|
-
'https://li.quest/v1/analytics/transfers?integrator=lifi-sdk&wallet=0x5520abcd&fromTimestamp=1696326609361&toTimestamp=1696326609362'
|
|
28
|
-
|
|
29
|
-
await expect(
|
|
30
|
-
getTransactionHistory(client, walletAnalyticsRequest)
|
|
31
|
-
).resolves.toEqual({})
|
|
32
|
-
|
|
33
|
-
expect((mockedFetch.mock.calls[0][1] as URL).href).toEqual(generatedURL)
|
|
34
|
-
expect(mockedFetch).toHaveBeenCalledOnce()
|
|
35
|
-
})
|
|
36
|
-
})
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { findDefaultToken } from '@lifi/data-types'
|
|
2
|
-
import type { WalletTokenExtended } from '@lifi/types'
|
|
3
|
-
import { ChainId, CoinKey } from '@lifi/types'
|
|
4
|
-
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
5
|
-
import { client } from './actions.unit.handlers.js'
|
|
6
|
-
import { getWalletBalances } from './getWalletBalances.js'
|
|
7
|
-
|
|
8
|
-
const mockedGetWalletBalances = vi.spyOn(
|
|
9
|
-
await import('./getWalletBalances.js'),
|
|
10
|
-
'getWalletBalances'
|
|
11
|
-
)
|
|
12
|
-
|
|
13
|
-
describe('getWalletBalances', () => {
|
|
14
|
-
beforeEach(() => {
|
|
15
|
-
vi.clearAllMocks()
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
const SOME_TOKEN = {
|
|
19
|
-
...findDefaultToken(CoinKey.USDC, ChainId.DAI),
|
|
20
|
-
priceUSD: '',
|
|
21
|
-
}
|
|
22
|
-
const SOME_WALLET_ADDRESS = 'some wallet address'
|
|
23
|
-
|
|
24
|
-
describe('user input is invalid', () => {
|
|
25
|
-
it('should throw Error because of missing walletAddress', async () => {
|
|
26
|
-
await expect(getWalletBalances(client, '')).rejects.toThrow(
|
|
27
|
-
'Missing walletAddress.'
|
|
28
|
-
)
|
|
29
|
-
})
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
describe('user input is valid', () => {
|
|
33
|
-
it('should call the balance service without options', async () => {
|
|
34
|
-
const balanceResponse: Record<number, WalletTokenExtended[]> = {
|
|
35
|
-
[ChainId.DAI]: [
|
|
36
|
-
{
|
|
37
|
-
...SOME_TOKEN,
|
|
38
|
-
amount: '123',
|
|
39
|
-
marketCapUSD: 1000000,
|
|
40
|
-
volumeUSD24H: 50000,
|
|
41
|
-
fdvUSD: 2000000,
|
|
42
|
-
},
|
|
43
|
-
],
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
mockedGetWalletBalances.mockReturnValue(Promise.resolve(balanceResponse))
|
|
47
|
-
|
|
48
|
-
const result = await getWalletBalances(client, SOME_WALLET_ADDRESS)
|
|
49
|
-
|
|
50
|
-
expect(mockedGetWalletBalances).toHaveBeenCalledTimes(1)
|
|
51
|
-
expect(mockedGetWalletBalances).toHaveBeenCalledWith(
|
|
52
|
-
client,
|
|
53
|
-
SOME_WALLET_ADDRESS
|
|
54
|
-
)
|
|
55
|
-
expect(result).toEqual(balanceResponse)
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
it('should call the balance service with options', async () => {
|
|
59
|
-
const balanceResponse: Record<number, WalletTokenExtended[]> = {
|
|
60
|
-
[ChainId.DAI]: [
|
|
61
|
-
{
|
|
62
|
-
...SOME_TOKEN,
|
|
63
|
-
amount: '123',
|
|
64
|
-
marketCapUSD: 1000000,
|
|
65
|
-
volumeUSD24H: 50000,
|
|
66
|
-
fdvUSD: 2000000,
|
|
67
|
-
},
|
|
68
|
-
],
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const options = { signal: new AbortController().signal }
|
|
72
|
-
|
|
73
|
-
mockedGetWalletBalances.mockReturnValue(Promise.resolve(balanceResponse))
|
|
74
|
-
|
|
75
|
-
const result = await getWalletBalances(
|
|
76
|
-
client,
|
|
77
|
-
SOME_WALLET_ADDRESS,
|
|
78
|
-
options
|
|
79
|
-
)
|
|
80
|
-
|
|
81
|
-
expect(mockedGetWalletBalances).toHaveBeenCalledTimes(1)
|
|
82
|
-
expect(mockedGetWalletBalances).toHaveBeenCalledWith(
|
|
83
|
-
client,
|
|
84
|
-
SOME_WALLET_ADDRESS,
|
|
85
|
-
options
|
|
86
|
-
)
|
|
87
|
-
expect(result).toEqual(balanceResponse)
|
|
88
|
-
})
|
|
89
|
-
})
|
|
90
|
-
})
|
|
@@ -1,229 +0,0 @@
|
|
|
1
|
-
import type { RelayRequest, RelayResponse, RequestOptions } from '@lifi/types'
|
|
2
|
-
import { HttpResponse, http } from 'msw'
|
|
3
|
-
import { describe, expect, it } from 'vitest'
|
|
4
|
-
import { BaseError } from '../errors/baseError.js'
|
|
5
|
-
import { ErrorName } from '../errors/constants.js'
|
|
6
|
-
import { SDKError } from '../errors/SDKError.js'
|
|
7
|
-
import { client, setupTestServer } from './actions.unit.handlers.js'
|
|
8
|
-
import { relayTransaction } from './relayTransaction.js'
|
|
9
|
-
|
|
10
|
-
describe('relayTransaction', () => {
|
|
11
|
-
const server = setupTestServer()
|
|
12
|
-
|
|
13
|
-
const createMockRelayRequest = (typedData: any[]): RelayRequest => ({
|
|
14
|
-
type: 'lifi',
|
|
15
|
-
id: 'test-step-id',
|
|
16
|
-
includedSteps: [],
|
|
17
|
-
tool: 'test-tool',
|
|
18
|
-
toolDetails: {
|
|
19
|
-
key: 'test-tool',
|
|
20
|
-
name: 'Test Tool',
|
|
21
|
-
logoURI: 'https://example.com/logo.png',
|
|
22
|
-
},
|
|
23
|
-
action: {
|
|
24
|
-
fromChainId: 1,
|
|
25
|
-
toChainId: 1,
|
|
26
|
-
fromToken: {
|
|
27
|
-
address: '0x1234567890123456789012345678901234567890',
|
|
28
|
-
symbol: 'TEST',
|
|
29
|
-
decimals: 18,
|
|
30
|
-
chainId: 1,
|
|
31
|
-
name: 'Test Token',
|
|
32
|
-
priceUSD: '1.00',
|
|
33
|
-
},
|
|
34
|
-
toToken: {
|
|
35
|
-
address: '0x0987654321098765432109876543210987654321',
|
|
36
|
-
symbol: 'TEST2',
|
|
37
|
-
decimals: 18,
|
|
38
|
-
chainId: 1,
|
|
39
|
-
name: 'Test Token 2',
|
|
40
|
-
priceUSD: '1.00',
|
|
41
|
-
},
|
|
42
|
-
fromAmount: '1000000000000000000',
|
|
43
|
-
},
|
|
44
|
-
estimate: {
|
|
45
|
-
fromAmount: '1000000000000000000',
|
|
46
|
-
toAmount: '1000000000000000000',
|
|
47
|
-
toAmountMin: '1000000000000000000',
|
|
48
|
-
approvalAddress: '0x1234567890123456789012345678901234567890',
|
|
49
|
-
tool: 'test-tool',
|
|
50
|
-
executionDuration: 30000,
|
|
51
|
-
},
|
|
52
|
-
transactionRequest: {
|
|
53
|
-
to: '0x1234567890123456789012345678901234567890',
|
|
54
|
-
data: '0x',
|
|
55
|
-
value: '0',
|
|
56
|
-
gasLimit: '100000',
|
|
57
|
-
},
|
|
58
|
-
typedData,
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
const mockRelayRequest: RelayRequest = createMockRelayRequest([
|
|
62
|
-
{
|
|
63
|
-
domain: {
|
|
64
|
-
name: 'Test Token',
|
|
65
|
-
version: '1',
|
|
66
|
-
chainId: 1,
|
|
67
|
-
verifyingContract: '0x1234567890123456789012345678901234567890',
|
|
68
|
-
},
|
|
69
|
-
types: {
|
|
70
|
-
Permit: [
|
|
71
|
-
{ name: 'owner', type: 'address' },
|
|
72
|
-
{ name: 'spender', type: 'address' },
|
|
73
|
-
{ name: 'value', type: 'uint256' },
|
|
74
|
-
{ name: 'nonce', type: 'uint256' },
|
|
75
|
-
{ name: 'deadline', type: 'uint256' },
|
|
76
|
-
],
|
|
77
|
-
},
|
|
78
|
-
primaryType: 'Permit',
|
|
79
|
-
message: {
|
|
80
|
-
owner: '0x1234567890123456789012345678901234567890',
|
|
81
|
-
spender: '0x0987654321098765432109876543210987654321',
|
|
82
|
-
value: '1000000000000000000',
|
|
83
|
-
nonce: 0,
|
|
84
|
-
deadline: 1234567890,
|
|
85
|
-
},
|
|
86
|
-
signature:
|
|
87
|
-
'0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef12',
|
|
88
|
-
},
|
|
89
|
-
])
|
|
90
|
-
|
|
91
|
-
const mockRelayRequestWithPermitWitness: RelayRequest =
|
|
92
|
-
createMockRelayRequest([
|
|
93
|
-
{
|
|
94
|
-
domain: {
|
|
95
|
-
name: 'Test Token',
|
|
96
|
-
version: '1',
|
|
97
|
-
chainId: 1,
|
|
98
|
-
verifyingContract: '0x1234567890123456789012345678901234567890',
|
|
99
|
-
},
|
|
100
|
-
types: {
|
|
101
|
-
PermitWitnessTransferFrom: [
|
|
102
|
-
{ name: 'owner', type: 'address' },
|
|
103
|
-
{ name: 'spender', type: 'address' },
|
|
104
|
-
{ name: 'value', type: 'uint256' },
|
|
105
|
-
{ name: 'nonce', type: 'uint256' },
|
|
106
|
-
{ name: 'deadline', type: 'uint256' },
|
|
107
|
-
],
|
|
108
|
-
},
|
|
109
|
-
primaryType: 'PermitWitnessTransferFrom',
|
|
110
|
-
message: {
|
|
111
|
-
owner: '0x1234567890123456789012345678901234567890',
|
|
112
|
-
spender: '0x0987654321098765432109876543210987654321',
|
|
113
|
-
value: '1000000000000000000',
|
|
114
|
-
nonce: 0,
|
|
115
|
-
deadline: 1234567890,
|
|
116
|
-
},
|
|
117
|
-
signature:
|
|
118
|
-
'0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef12',
|
|
119
|
-
},
|
|
120
|
-
])
|
|
121
|
-
|
|
122
|
-
const mockSuccessResponse: RelayResponse = {
|
|
123
|
-
status: 'ok',
|
|
124
|
-
data: {
|
|
125
|
-
taskId: 'test-task-id-123',
|
|
126
|
-
},
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const mockErrorResponse: RelayResponse = {
|
|
130
|
-
status: 'error',
|
|
131
|
-
data: {
|
|
132
|
-
code: 400,
|
|
133
|
-
message: 'Invalid request parameters',
|
|
134
|
-
},
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
describe('success scenarios', () => {
|
|
138
|
-
it('should relay transaction successfully for advanced relayer', async () => {
|
|
139
|
-
server.use(
|
|
140
|
-
http.post(`${client.config.apiUrl}/advanced/relay`, async () => {
|
|
141
|
-
return HttpResponse.json(mockSuccessResponse)
|
|
142
|
-
})
|
|
143
|
-
)
|
|
144
|
-
|
|
145
|
-
const result = await relayTransaction(client, mockRelayRequest)
|
|
146
|
-
|
|
147
|
-
expect(result).toEqual(mockSuccessResponse.data)
|
|
148
|
-
})
|
|
149
|
-
|
|
150
|
-
it('should relay transaction successfully for gasless relayer', async () => {
|
|
151
|
-
server.use(
|
|
152
|
-
http.post(`${client.config.apiUrl}/relayer/relay`, async () => {
|
|
153
|
-
return HttpResponse.json(mockSuccessResponse)
|
|
154
|
-
})
|
|
155
|
-
)
|
|
156
|
-
|
|
157
|
-
const result = await relayTransaction(
|
|
158
|
-
client,
|
|
159
|
-
mockRelayRequestWithPermitWitness
|
|
160
|
-
)
|
|
161
|
-
|
|
162
|
-
expect(result).toEqual(mockSuccessResponse.data)
|
|
163
|
-
})
|
|
164
|
-
})
|
|
165
|
-
|
|
166
|
-
describe('error scenarios', () => {
|
|
167
|
-
it('should throw BaseError when server returns error status', async () => {
|
|
168
|
-
server.use(
|
|
169
|
-
http.post(`${client.config.apiUrl}/advanced/relay`, async () => {
|
|
170
|
-
return HttpResponse.json(mockErrorResponse)
|
|
171
|
-
})
|
|
172
|
-
)
|
|
173
|
-
|
|
174
|
-
await expect(relayTransaction(client, mockRelayRequest)).rejects.toThrow(
|
|
175
|
-
BaseError
|
|
176
|
-
)
|
|
177
|
-
|
|
178
|
-
try {
|
|
179
|
-
await relayTransaction(client, mockRelayRequest)
|
|
180
|
-
} catch (error) {
|
|
181
|
-
expect(error).toBeInstanceOf(BaseError)
|
|
182
|
-
expect((error as BaseError).name).toBe(ErrorName.ServerError)
|
|
183
|
-
expect((error as BaseError).code).toBe(400)
|
|
184
|
-
expect((error as BaseError).message).toBe('Invalid request parameters')
|
|
185
|
-
}
|
|
186
|
-
})
|
|
187
|
-
|
|
188
|
-
it('should throw SDKError when network request fails', async () => {
|
|
189
|
-
server.use(
|
|
190
|
-
http.post(`${client.config.apiUrl}/advanced/relay`, async () => {
|
|
191
|
-
return HttpResponse.error()
|
|
192
|
-
})
|
|
193
|
-
)
|
|
194
|
-
|
|
195
|
-
await expect(relayTransaction(client, mockRelayRequest)).rejects.toThrow(
|
|
196
|
-
SDKError
|
|
197
|
-
)
|
|
198
|
-
})
|
|
199
|
-
|
|
200
|
-
it('should throw SDKError when request times out', async () => {
|
|
201
|
-
server.use(
|
|
202
|
-
http.post(`${client.config.apiUrl}/advanced/relay`, async () => {
|
|
203
|
-
// Simulate timeout by not responding
|
|
204
|
-
await new Promise(() => {}) // Never resolves
|
|
205
|
-
})
|
|
206
|
-
)
|
|
207
|
-
|
|
208
|
-
const timeoutOptions: RequestOptions = {
|
|
209
|
-
signal: AbortSignal.timeout(100), // 100ms timeout
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
await expect(
|
|
213
|
-
relayTransaction(client, mockRelayRequest, timeoutOptions)
|
|
214
|
-
).rejects.toThrow()
|
|
215
|
-
})
|
|
216
|
-
})
|
|
217
|
-
|
|
218
|
-
describe('validation scenarios', () => {
|
|
219
|
-
it('should throw SDKError when typedData is missing', async () => {
|
|
220
|
-
const invalidRequest = createMockRelayRequest([])
|
|
221
|
-
// Remove typedData to test validation
|
|
222
|
-
delete (invalidRequest as any).typedData
|
|
223
|
-
|
|
224
|
-
await expect(relayTransaction(client, invalidRequest)).rejects.toThrow(
|
|
225
|
-
SDKError
|
|
226
|
-
)
|
|
227
|
-
})
|
|
228
|
-
})
|
|
229
|
-
})
|