@lifi/perps-sdk-provider-ondo 2.1.1 → 3.0.0
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/dist/cjs/OndoProvider.d.ts.map +1 -1
- package/dist/cjs/OndoProvider.js +33 -3
- package/dist/cjs/OndoProvider.js.map +1 -1
- package/dist/cjs/accountConfig.d.ts.map +1 -1
- package/dist/cjs/accountConfig.js +6 -0
- package/dist/cjs/accountConfig.js.map +1 -1
- package/dist/cjs/auth/signActions.d.ts.map +1 -1
- package/dist/cjs/auth/signActions.js +14 -0
- package/dist/cjs/auth/signActions.js.map +1 -1
- package/dist/cjs/utils/depositAddress.d.ts +25 -0
- package/dist/cjs/utils/depositAddress.d.ts.map +1 -0
- package/dist/cjs/utils/depositAddress.js +84 -0
- package/dist/cjs/utils/depositAddress.js.map +1 -0
- package/dist/cjs/utils/index.d.ts +1 -0
- package/dist/cjs/utils/index.d.ts.map +1 -1
- package/dist/cjs/utils/index.js +6 -1
- package/dist/cjs/utils/index.js.map +1 -1
- package/dist/esm/OndoProvider.d.ts.map +1 -1
- package/dist/esm/OndoProvider.js +38 -5
- package/dist/esm/OndoProvider.js.map +1 -1
- package/dist/esm/accountConfig.d.ts.map +1 -1
- package/dist/esm/accountConfig.js +8 -1
- package/dist/esm/accountConfig.js.map +1 -1
- package/dist/esm/auth/signActions.d.ts.map +1 -1
- package/dist/esm/auth/signActions.js +16 -0
- package/dist/esm/auth/signActions.js.map +1 -1
- package/dist/esm/utils/depositAddress.d.ts +39 -0
- package/dist/esm/utils/depositAddress.d.ts.map +1 -0
- package/dist/esm/utils/depositAddress.js +90 -0
- package/dist/esm/utils/depositAddress.js.map +1 -0
- package/dist/esm/utils/index.d.ts +1 -0
- package/dist/esm/utils/index.d.ts.map +1 -1
- package/dist/esm/utils/index.js +1 -0
- package/dist/esm/utils/index.js.map +1 -1
- package/dist/types/OndoProvider.d.ts.map +1 -1
- package/dist/types/accountConfig.d.ts.map +1 -1
- package/dist/types/auth/signActions.d.ts.map +1 -1
- package/dist/types/utils/depositAddress.d.ts +39 -0
- package/dist/types/utils/depositAddress.d.ts.map +1 -0
- package/dist/types/utils/index.d.ts +1 -0
- package/dist/types/utils/index.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/OndoProvider.ts +71 -23
- package/src/accountConfig.ts +10 -1
- package/src/auth/signActions.ts +28 -0
- package/src/utils/depositAddress.ts +128 -0
- package/src/utils/index.ts +8 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { type Address, isAddress } from 'viem'
|
|
2
|
+
import type { OndoApiClient } from './apiClient.js'
|
|
3
|
+
import { OndoApiError } from './apiClient.js'
|
|
4
|
+
|
|
5
|
+
/** The only deposit policy supported by the Ondo provider. */
|
|
6
|
+
export const ONDO_DEPOSIT_POLICY = {
|
|
7
|
+
network: 'ethereum',
|
|
8
|
+
symbol: 'USDC',
|
|
9
|
+
depositDestination: { wallet: 'margin' },
|
|
10
|
+
} as const
|
|
11
|
+
|
|
12
|
+
/** @internal */
|
|
13
|
+
export type OndoDepositPolicyMarker = {
|
|
14
|
+
network: 'ethereum'
|
|
15
|
+
symbol: 'USDC'
|
|
16
|
+
depositDestination: { wallet: 'margin' }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** @internal */
|
|
20
|
+
export interface OndoDepositAddressRecord {
|
|
21
|
+
address: string
|
|
22
|
+
coin: string
|
|
23
|
+
network: string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
27
|
+
typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
28
|
+
|
|
29
|
+
const malformed = (message: string): OndoApiError =>
|
|
30
|
+
new OndoApiError(`Ondo deposit-address response is malformed: ${message}`)
|
|
31
|
+
|
|
32
|
+
const entriesFromResult = (result: unknown): unknown[] => {
|
|
33
|
+
if (Array.isArray(result)) {
|
|
34
|
+
return result
|
|
35
|
+
}
|
|
36
|
+
if (isRecord(result)) {
|
|
37
|
+
for (const key of ['addresses', 'deposit_addresses', 'depositAddresses']) {
|
|
38
|
+
if (Array.isArray(result[key])) {
|
|
39
|
+
return result[key]
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
throw malformed('expected an address array')
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Extract the canonical Ethereum USDC address from Ondo's list response.
|
|
48
|
+
* An empty list is a valid, unsatisfied setup state; malformed records are
|
|
49
|
+
* errors so an API/schema failure cannot be mistaken for an absent address.
|
|
50
|
+
*/
|
|
51
|
+
export const parseOndoDepositAddress = (result: unknown): Address | null => {
|
|
52
|
+
const entries = entriesFromResult(result)
|
|
53
|
+
for (const entry of entries) {
|
|
54
|
+
if (!isRecord(entry)) {
|
|
55
|
+
throw malformed('address entries must be objects')
|
|
56
|
+
}
|
|
57
|
+
const coin = entry.coin ?? entry.symbol
|
|
58
|
+
const network = entry.network
|
|
59
|
+
if (typeof coin !== 'string' || typeof network !== 'string') {
|
|
60
|
+
throw malformed('address entries require coin and network')
|
|
61
|
+
}
|
|
62
|
+
if (coin.toUpperCase() !== 'USDC' || network.toLowerCase() !== 'ethereum') {
|
|
63
|
+
continue
|
|
64
|
+
}
|
|
65
|
+
if (typeof entry.address !== 'string' || !isAddress(entry.address)) {
|
|
66
|
+
throw malformed('the Ethereum USDC entry has an invalid address')
|
|
67
|
+
}
|
|
68
|
+
return entry.address
|
|
69
|
+
}
|
|
70
|
+
return null
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Query Ondo's authenticated Ethereum USDC deposit-address list. */
|
|
74
|
+
export const listOndoDepositAddress = async (
|
|
75
|
+
client: OndoApiClient,
|
|
76
|
+
authToken: string
|
|
77
|
+
): Promise<Address | null> =>
|
|
78
|
+
parseOndoDepositAddress(
|
|
79
|
+
await client.post<unknown>(
|
|
80
|
+
'/v1/wallet/deposit_address/list',
|
|
81
|
+
{ coins: ['USDC'], network: 'ethereum' },
|
|
82
|
+
{ authToken }
|
|
83
|
+
)
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
const policyCandidate = (marker: object): Record<string, unknown> => {
|
|
87
|
+
if (!isRecord(marker)) {
|
|
88
|
+
throw malformed('session marker must be an object')
|
|
89
|
+
}
|
|
90
|
+
const candidate = marker.policy
|
|
91
|
+
if (candidate === undefined) {
|
|
92
|
+
return marker
|
|
93
|
+
}
|
|
94
|
+
if (!isRecord(candidate)) {
|
|
95
|
+
throw malformed('session policy must be an object')
|
|
96
|
+
}
|
|
97
|
+
return candidate
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Validate the backend's minimal client-only marker and hydrate the venue
|
|
102
|
+
* request with the authenticated account ID. The marker is validated against
|
|
103
|
+
* the one fixed Ondo policy before any venue write is attempted.
|
|
104
|
+
*/
|
|
105
|
+
export const buildOndoProvisionPayload = (
|
|
106
|
+
marker: object,
|
|
107
|
+
accountID: string
|
|
108
|
+
): Record<string, unknown> => {
|
|
109
|
+
if (typeof accountID !== 'string' || accountID.length === 0) {
|
|
110
|
+
throw malformed('accountID is missing from /v1/account')
|
|
111
|
+
}
|
|
112
|
+
const candidate = policyCandidate(marker)
|
|
113
|
+
const network = candidate.network
|
|
114
|
+
const symbol = candidate.symbol
|
|
115
|
+
const destination =
|
|
116
|
+
candidate.depositDestination ?? candidate.deposit_destination
|
|
117
|
+
if (network !== 'ethereum' || symbol !== 'USDC' || !isRecord(destination)) {
|
|
118
|
+
throw malformed('unsupported deposit policy')
|
|
119
|
+
}
|
|
120
|
+
if (destination.wallet !== 'margin') {
|
|
121
|
+
throw malformed('unsupported deposit wallet')
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
network: 'ethereum',
|
|
125
|
+
symbol: 'USDC',
|
|
126
|
+
deposit_destination: { id: accountID, wallet: 'margin' },
|
|
127
|
+
}
|
|
128
|
+
}
|
package/src/utils/index.ts
CHANGED
|
@@ -16,6 +16,14 @@ export {
|
|
|
16
16
|
type OndoRequestOptions,
|
|
17
17
|
OndoSessionExpiredError,
|
|
18
18
|
} from './apiClient.js'
|
|
19
|
+
export {
|
|
20
|
+
buildOndoProvisionPayload,
|
|
21
|
+
listOndoDepositAddress,
|
|
22
|
+
ONDO_DEPOSIT_POLICY,
|
|
23
|
+
type OndoDepositAddressRecord,
|
|
24
|
+
type OndoDepositPolicyMarker,
|
|
25
|
+
parseOndoDepositAddress,
|
|
26
|
+
} from './depositAddress.js'
|
|
19
27
|
export { estimateLiquidationPrice } from './liquidation.js'
|
|
20
28
|
export { mapFundingActivity, mapLiquidationActivity } from './mapActivity.js'
|
|
21
29
|
export { mapFill } from './mapFill.js'
|