@pear-protocol/exchanges-sdk 0.14.0 → 0.15.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/client/index.js +2 -0
- package/dist/client/lighter/constants.js +25 -5
- package/dist/client/lighter/exchange-ws.js +8 -7
- package/dist/client/lighter/rest.js +36 -36
- package/dist/client/lighter/ws.js +4 -3
- package/dist/helper/factory.js +3 -1
- package/dist/helper/lighter/asset-decimals.helper.js +1 -1
- package/dist/helper/lighter/auth-token.helper.js +10 -7
- package/dist/helper/lighter/cctp-deposit.helper.js +13 -13
- package/dist/helper/lighter/deposit-settlement.helper.js +4 -4
- package/dist/helper/lighter/funxyz-deposit.helper.js +3 -3
- package/dist/helper/lighter/helper.js +84 -59
- package/dist/helper/lighter/history.helper.js +6 -6
- package/dist/helper/lighter/misc-routes.helper.js +13 -0
- package/dist/helper/lighter/onboarding.helper.js +2 -2
- package/dist/helper/lighter/referral.helper.js +6 -6
- package/dist/helper/lighter/rhc-deposit.helper.js +91 -0
- package/dist/helper/lighter/types.js +12 -2
- package/dist/helper/subaccount.helper.js +10 -10
- package/dist/index.d.ts +166 -9
- package/dist/sdk.js +6 -0
- package/dist/status/factory.js +2 -1
- package/dist/status/lighter/status.js +5 -5
- package/dist/syncer/factory.js +5 -1
- package/dist/syncer/lighter/funding.js +2 -2
- package/dist/syncer/lighter/syncer.js +6 -6
- package/dist/tracker/index.js +3 -0
- package/dist/tracker/lighter/const.js +5 -2
- package/dist/tracker/lighter/mapper.js +12 -13
- package/dist/tracker/lighter/tracker.js +21 -15
- package/package.json +4 -4
|
@@ -1,20 +1,22 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isAddress, parseUnits } from 'viem';
|
|
2
2
|
import { fetchApiKeys, fetchAccount, fetchPublicAccount, fetchAccountsByL1Address } from '../../client/lighter/rest';
|
|
3
|
-
import {
|
|
3
|
+
import { LIGHTER_QUOTE_ASSETS } from '../../tracker/lighter/const';
|
|
4
|
+
import { parseCollateralAmount } from '../subaccount.helper';
|
|
4
5
|
import { resolveAssetDecimals } from './asset-decimals.helper';
|
|
5
6
|
import { listCctpDepositNetworks } from './cctp-deposit.helper';
|
|
6
7
|
import { createDepositAddressForRoute, depositThroughRoute } from './deposit.helper';
|
|
7
8
|
import { writeWithdraw } from './ethereum';
|
|
8
9
|
import { readWithdrawHistory, readDepositHistory } from './history.helper';
|
|
10
|
+
import { selectLighterMiscRoutes } from './misc-routes.helper';
|
|
9
11
|
import { LIGHTER_ACCOUNT_NOT_REGISTERED_CODE, readOnboardingStatus } from './onboarding.helper';
|
|
10
12
|
import { readUserReferrals, applyReferralCode } from './referral.helper';
|
|
11
|
-
import {
|
|
13
|
+
import { depositUsdgOnRobinhoodChain, createRhcDepositAddress } from './rhc-deposit.helper';
|
|
14
|
+
import { MIN_LIGHTER_FAST_WITHDRAW_COLLATERAL } from './types';
|
|
12
15
|
|
|
13
16
|
var __defProp = Object.defineProperty;
|
|
14
17
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
15
18
|
const MIN_USER_API_KEY_INDEX = 4;
|
|
16
19
|
const MAX_API_KEY_INDEX = 254;
|
|
17
|
-
const MIN_FAST_WITHDRAW_USDC_UNITS = parseUsdcAmount(MIN_LIGHTER_FAST_WITHDRAW_USDC);
|
|
18
20
|
function findAvailableApiKeyIndex(apiKeys) {
|
|
19
21
|
const occupied = new Set(apiKeys.map((key) => key.api_key_index));
|
|
20
22
|
for (let index = MIN_USER_API_KEY_INDEX; index <= MAX_API_KEY_INDEX; index += 1) {
|
|
@@ -25,15 +27,72 @@ function findAvailableApiKeyIndex(apiKeys) {
|
|
|
25
27
|
__name(findAvailableApiKeyIndex, "findAvailableApiKeyIndex");
|
|
26
28
|
function createLighterHelper(sdk, demo = false) {
|
|
27
29
|
const helper = {
|
|
30
|
+
...buildSharedLighterHelper(sdk, "lighter", demo),
|
|
31
|
+
/** Lists the source networks Lighter credits through a CCTP intent address right now. */
|
|
32
|
+
async listCctpDepositNetworks() {
|
|
33
|
+
return listCctpDepositNetworks(demo);
|
|
34
|
+
},
|
|
35
|
+
/** Runs the deposit route the caller selected, from validation through settlement. */
|
|
36
|
+
async deposit(params) {
|
|
37
|
+
const result = await depositThroughRoute(sdk, params, demo);
|
|
38
|
+
return result;
|
|
39
|
+
},
|
|
40
|
+
/** Hands out an address any wallet or exchange may fund, for the route the caller selected. */
|
|
41
|
+
async createDepositAddress(params) {
|
|
42
|
+
const result = await createDepositAddressForRoute(sdk, params, demo);
|
|
43
|
+
return result;
|
|
44
|
+
},
|
|
45
|
+
async withdraw(walletClient, params) {
|
|
46
|
+
if (demo) {
|
|
47
|
+
throw new Error("Lighter demo withdrawals are unavailable because the testnet gateway is not verified");
|
|
48
|
+
}
|
|
49
|
+
const l2Decimals = await resolveAssetDecimals(params.assetId, "decimals", params.l2Decimals, demo);
|
|
50
|
+
const txHash = await writeWithdraw(walletClient, {
|
|
51
|
+
accountIndex: params.accountIndex,
|
|
52
|
+
assetIndex: params.assetId,
|
|
53
|
+
routeType: params.routeType,
|
|
54
|
+
baseAmount: parseUnits(params.amount, l2Decimals)
|
|
55
|
+
});
|
|
56
|
+
return {
|
|
57
|
+
txHash
|
|
58
|
+
};
|
|
59
|
+
},
|
|
60
|
+
/** Reads the Fun.xyz bridge deposits Lighter has seen for a wallet. */
|
|
61
|
+
async listUdaDeposits(walletAddress) {
|
|
62
|
+
return sdk.core.misc.lighter.listUdaDeposits(walletAddress);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
return helper;
|
|
66
|
+
}
|
|
67
|
+
__name(createLighterHelper, "createLighterHelper");
|
|
68
|
+
function createLighterRhcHelper(sdk, demo = false) {
|
|
69
|
+
return {
|
|
70
|
+
...buildSharedLighterHelper(sdk, "lighter_rhc", demo),
|
|
71
|
+
/** Hands out the persistent address that credits this owner's Robinhood Chain account. */
|
|
72
|
+
async createDepositAddress(params) {
|
|
73
|
+
return createRhcDepositAddress(params, demo);
|
|
74
|
+
},
|
|
75
|
+
/** Sends USDG from the connected wallet to that address and follows it to registration. */
|
|
76
|
+
async deposit(params) {
|
|
77
|
+
return depositUsdgOnRobinhoodChain(params, demo);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
__name(createLighterRhcHelper, "createLighterRhcHelper");
|
|
82
|
+
function buildSharedLighterHelper(sdk, connector, demo) {
|
|
83
|
+
const misc = selectLighterMiscRoutes(sdk, connector);
|
|
84
|
+
const collateralSymbol = LIGHTER_QUOTE_ASSETS[connector];
|
|
85
|
+
const minFastWithdraw = MIN_LIGHTER_FAST_WITHDRAW_COLLATERAL[connector];
|
|
86
|
+
return {
|
|
28
87
|
/** Reports whether an L1 address still needs its first deposit before it owns a Lighter account. */
|
|
29
88
|
async checkOnboarding(l1Address) {
|
|
30
89
|
if (!isAddress(l1Address)) {
|
|
31
90
|
throw new Error(`Invalid Lighter L1 address: ${l1Address}`);
|
|
32
91
|
}
|
|
33
|
-
return readOnboardingStatus(l1Address, demo);
|
|
92
|
+
return readOnboardingStatus(l1Address, connector, demo);
|
|
34
93
|
},
|
|
35
94
|
async lookupAccount(l1Address) {
|
|
36
|
-
const data = await fetchAccountsByL1Address(l1Address, demo);
|
|
95
|
+
const data = await fetchAccountsByL1Address(l1Address, connector, demo);
|
|
37
96
|
if (data.code === LIGHTER_ACCOUNT_NOT_REGISTERED_CODE) {
|
|
38
97
|
throw new Error("Account not registered on Lighter");
|
|
39
98
|
}
|
|
@@ -49,7 +108,7 @@ function createLighterHelper(sdk, demo = false) {
|
|
|
49
108
|
},
|
|
50
109
|
async checkIntegratorApproval(accountIndex, integratorAccountIndex, requirements, authToken) {
|
|
51
110
|
if (requirements) validateApprovalRequirements(requirements);
|
|
52
|
-
const data = authToken ? await fetchAccount(accountIndex, authToken, demo) : await fetchPublicAccount(accountIndex, demo);
|
|
111
|
+
const data = authToken ? await fetchAccount(accountIndex, authToken, connector, demo) : await fetchPublicAccount(accountIndex, connector, demo);
|
|
53
112
|
const account = data.accounts[0];
|
|
54
113
|
if (!account) return false;
|
|
55
114
|
const minMakerFee = requirements?.minPerpsMakerFee ?? 0;
|
|
@@ -61,9 +120,9 @@ function createLighterHelper(sdk, demo = false) {
|
|
|
61
120
|
if (demo) {
|
|
62
121
|
throw new Error("Lighter demo wallet connection is unavailable because backend signing is mainnet-only");
|
|
63
122
|
}
|
|
64
|
-
const apiKeys = await fetchApiKeys(params.accountIndex, demo);
|
|
123
|
+
const apiKeys = await fetchApiKeys(params.accountIndex, connector, demo);
|
|
65
124
|
const apiKeyIndex = findAvailableApiKeyIndex(apiKeys);
|
|
66
|
-
const { privateKey, messageToSign, txInfo } = await
|
|
125
|
+
const { privateKey, messageToSign, txInfo } = await misc.createApiKey({
|
|
67
126
|
accountIndex: params.accountIndex,
|
|
68
127
|
apiKeyIndex
|
|
69
128
|
});
|
|
@@ -71,7 +130,7 @@ function createLighterHelper(sdk, demo = false) {
|
|
|
71
130
|
account: walletClient.account,
|
|
72
131
|
message: messageToSign
|
|
73
132
|
});
|
|
74
|
-
const { txHash } = await
|
|
133
|
+
const { txHash } = await misc.changePubKey({
|
|
75
134
|
l1Signature,
|
|
76
135
|
txInfo,
|
|
77
136
|
accountIndex: params.accountIndex,
|
|
@@ -87,7 +146,7 @@ function createLighterHelper(sdk, demo = false) {
|
|
|
87
146
|
if (demo) {
|
|
88
147
|
throw new Error("Lighter demo integrator approval is unavailable because backend signing is mainnet-only");
|
|
89
148
|
}
|
|
90
|
-
const { messageToSign, txInfo } = await
|
|
149
|
+
const { messageToSign, txInfo } = await misc.prepareApproveIntegrator({
|
|
91
150
|
accountIndex: params.accountIndex,
|
|
92
151
|
apiKeyIndex: params.apiKeyIndex,
|
|
93
152
|
apiPrivateKey: params.apiPrivateKey
|
|
@@ -96,7 +155,7 @@ function createLighterHelper(sdk, demo = false) {
|
|
|
96
155
|
account: walletClient.account,
|
|
97
156
|
message: messageToSign
|
|
98
157
|
});
|
|
99
|
-
const { txHash } = await
|
|
158
|
+
const { txHash } = await misc.submitApproveIntegrator({
|
|
100
159
|
l1Signature,
|
|
101
160
|
txInfo,
|
|
102
161
|
accountIndex: params.accountIndex,
|
|
@@ -117,7 +176,7 @@ function createLighterHelper(sdk, demo = false) {
|
|
|
117
176
|
if (demo) {
|
|
118
177
|
throw new Error("Lighter demo tier changes are unavailable because backend signing is mainnet-only");
|
|
119
178
|
}
|
|
120
|
-
const result = await
|
|
179
|
+
const result = await misc.changeAccountTier({
|
|
121
180
|
accountIndex: params.accountIndex,
|
|
122
181
|
apiKeyIndex: params.apiKeyIndex,
|
|
123
182
|
apiPrivateKey: params.apiPrivateKey,
|
|
@@ -127,21 +186,7 @@ function createLighterHelper(sdk, demo = false) {
|
|
|
127
186
|
tier: result.tier
|
|
128
187
|
};
|
|
129
188
|
},
|
|
130
|
-
/**
|
|
131
|
-
async listCctpDepositNetworks() {
|
|
132
|
-
return listCctpDepositNetworks(demo);
|
|
133
|
-
},
|
|
134
|
-
/** Runs the deposit route the caller selected, from validation through settlement. */
|
|
135
|
-
async deposit(params) {
|
|
136
|
-
const result = await depositThroughRoute(sdk, params, demo);
|
|
137
|
-
return result;
|
|
138
|
-
},
|
|
139
|
-
/** Hands out an address any wallet or exchange may fund, for the route the caller selected. */
|
|
140
|
-
async createDepositAddress(params) {
|
|
141
|
-
const result = await createDepositAddressForRoute(sdk, params, demo);
|
|
142
|
-
return result;
|
|
143
|
-
},
|
|
144
|
-
/** Performs a wallet-authorized fast USDC withdrawal through Lighter's transfer pool. */
|
|
189
|
+
/** Performs a wallet-authorized fast collateral withdrawal through Lighter's transfer pool. */
|
|
145
190
|
async fastWithdraw(walletClient, params) {
|
|
146
191
|
if (demo) {
|
|
147
192
|
throw new Error("Lighter demo fast withdrawals are unavailable because backend signing is mainnet-only");
|
|
@@ -149,11 +194,11 @@ function createLighterHelper(sdk, demo = false) {
|
|
|
149
194
|
if (!isAddress(params.toAddress)) {
|
|
150
195
|
throw new Error(`Invalid Lighter fast withdrawal address: ${params.toAddress}`);
|
|
151
196
|
}
|
|
152
|
-
const amount =
|
|
153
|
-
if (amount <
|
|
154
|
-
throw new Error(`Lighter fast withdrawal minimum is ${
|
|
197
|
+
const amount = parseCollateralAmount(params.amount, collateralSymbol);
|
|
198
|
+
if (amount < parseCollateralAmount(minFastWithdraw, collateralSymbol)) {
|
|
199
|
+
throw new Error(`Lighter fast withdrawal minimum is ${minFastWithdraw} ${collateralSymbol}`);
|
|
155
200
|
}
|
|
156
|
-
const { messageToSign, txInfo, fee } = await
|
|
201
|
+
const { messageToSign, txInfo, fee } = await misc.prepareFastWithdraw({
|
|
157
202
|
accountIndex: params.accountIndex,
|
|
158
203
|
toAddress: params.toAddress,
|
|
159
204
|
amount,
|
|
@@ -164,7 +209,7 @@ function createLighterHelper(sdk, demo = false) {
|
|
|
164
209
|
account: walletClient.account,
|
|
165
210
|
message: messageToSign
|
|
166
211
|
});
|
|
167
|
-
const { txHash } = await
|
|
212
|
+
const { txHash } = await misc.submitFastWithdraw({
|
|
168
213
|
accountIndex: params.accountIndex,
|
|
169
214
|
toAddress: params.toAddress,
|
|
170
215
|
apiKeyIndex: params.apiKeyIndex,
|
|
@@ -177,45 +222,25 @@ function createLighterHelper(sdk, demo = false) {
|
|
|
177
222
|
fee
|
|
178
223
|
};
|
|
179
224
|
},
|
|
180
|
-
async withdraw(walletClient, params) {
|
|
181
|
-
if (demo) {
|
|
182
|
-
throw new Error("Lighter demo withdrawals are unavailable because the testnet gateway is not verified");
|
|
183
|
-
}
|
|
184
|
-
const l2Decimals = await resolveAssetDecimals(params.assetId, "decimals", params.l2Decimals, demo);
|
|
185
|
-
const txHash = await writeWithdraw(walletClient, {
|
|
186
|
-
accountIndex: params.accountIndex,
|
|
187
|
-
assetIndex: params.assetId,
|
|
188
|
-
routeType: params.routeType,
|
|
189
|
-
baseAmount: parseUnits(params.amount, l2Decimals)
|
|
190
|
-
});
|
|
191
|
-
return {
|
|
192
|
-
txHash
|
|
193
|
-
};
|
|
194
|
-
},
|
|
195
225
|
/** Reads one page of the account's deposit history, newest first. */
|
|
196
226
|
async getDepositHistory(params) {
|
|
197
|
-
return readDepositHistory(sdk, params, demo);
|
|
227
|
+
return readDepositHistory(sdk, connector, params, demo);
|
|
198
228
|
},
|
|
199
229
|
/** Reads one page of the account's withdrawal history, newest first. */
|
|
200
230
|
async getWithdrawHistory(params) {
|
|
201
|
-
return readWithdrawHistory(sdk, params, demo);
|
|
202
|
-
},
|
|
203
|
-
/** Reads the Fun.xyz bridge deposits Lighter has seen for a wallet. */
|
|
204
|
-
async listUdaDeposits(walletAddress) {
|
|
205
|
-
return sdk.core.misc.lighter.listUdaDeposits(walletAddress);
|
|
231
|
+
return readWithdrawHistory(sdk, connector, params, demo);
|
|
206
232
|
},
|
|
207
233
|
/** Applies a referrer's code to the owner. Lighter accepts this at most once per owner. */
|
|
208
234
|
async applyReferralCode(params) {
|
|
209
|
-
return applyReferralCode(sdk, params, demo);
|
|
235
|
+
return applyReferralCode(sdk, connector, params, demo);
|
|
210
236
|
},
|
|
211
237
|
/** Reads one page of the owner's referral record, including the code that referred them. */
|
|
212
238
|
async getUserReferrals(params) {
|
|
213
|
-
return readUserReferrals(sdk, params, demo);
|
|
239
|
+
return readUserReferrals(sdk, connector, params, demo);
|
|
214
240
|
}
|
|
215
241
|
};
|
|
216
|
-
return helper;
|
|
217
242
|
}
|
|
218
|
-
__name(
|
|
243
|
+
__name(buildSharedLighterHelper, "buildSharedLighterHelper");
|
|
219
244
|
function validateApprovalRequirements(requirements) {
|
|
220
245
|
for (const [name, value] of [
|
|
221
246
|
[
|
|
@@ -234,4 +259,4 @@ function validateApprovalRequirements(requirements) {
|
|
|
234
259
|
}
|
|
235
260
|
__name(validateApprovalRequirements, "validateApprovalRequirements");
|
|
236
261
|
|
|
237
|
-
export { MAX_API_KEY_INDEX, MIN_USER_API_KEY_INDEX, createLighterHelper, findAvailableApiKeyIndex };
|
|
262
|
+
export { MAX_API_KEY_INDEX, MIN_USER_API_KEY_INDEX, createLighterHelper, createLighterRhcHelper, findAvailableApiKeyIndex };
|
|
@@ -3,23 +3,23 @@ import { retrieveLighterAuthToken } from './auth-token.helper';
|
|
|
3
3
|
|
|
4
4
|
var __defProp = Object.defineProperty;
|
|
5
5
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
-
async function readDepositHistory(sdk, params, demo) {
|
|
7
|
-
const authToken = await retrieveLighterAuthToken(sdk, params.credentials, demo);
|
|
6
|
+
async function readDepositHistory(sdk, connector, params, demo) {
|
|
7
|
+
const authToken = await retrieveLighterAuthToken(sdk, connector, params.credentials, demo);
|
|
8
8
|
return fetchDepositHistory({
|
|
9
9
|
accountIndex: params.credentials.accountIndex,
|
|
10
10
|
l1Address: params.l1Address,
|
|
11
11
|
authToken,
|
|
12
12
|
cursor: params.cursor
|
|
13
|
-
}, demo);
|
|
13
|
+
}, connector, demo);
|
|
14
14
|
}
|
|
15
15
|
__name(readDepositHistory, "readDepositHistory");
|
|
16
|
-
async function readWithdrawHistory(sdk, params, demo) {
|
|
17
|
-
const authToken = await retrieveLighterAuthToken(sdk, params.credentials, demo);
|
|
16
|
+
async function readWithdrawHistory(sdk, connector, params, demo) {
|
|
17
|
+
const authToken = await retrieveLighterAuthToken(sdk, connector, params.credentials, demo);
|
|
18
18
|
return fetchWithdrawHistory({
|
|
19
19
|
accountIndex: params.credentials.accountIndex,
|
|
20
20
|
authToken,
|
|
21
21
|
cursor: params.cursor
|
|
22
|
-
}, demo);
|
|
22
|
+
}, connector, demo);
|
|
23
23
|
}
|
|
24
24
|
__name(readWithdrawHistory, "readWithdrawHistory");
|
|
25
25
|
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
function selectLighterMiscRoutes(sdk, connector) {
|
|
4
|
+
switch (connector) {
|
|
5
|
+
case "lighter":
|
|
6
|
+
return sdk.core.misc.lighter;
|
|
7
|
+
case "lighter_rhc":
|
|
8
|
+
return sdk.core.misc.lighterRhc;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
__name(selectLighterMiscRoutes, "selectLighterMiscRoutes");
|
|
12
|
+
|
|
13
|
+
export { selectLighterMiscRoutes };
|
|
@@ -3,8 +3,8 @@ import { fetchAccountsByL1Address } from '../../client/lighter/rest';
|
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
4
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
5
5
|
const LIGHTER_ACCOUNT_NOT_REGISTERED_CODE = 21100;
|
|
6
|
-
async function readOnboardingStatus(l1Address, demo) {
|
|
7
|
-
const data = await fetchAccountsByL1Address(l1Address, demo);
|
|
6
|
+
async function readOnboardingStatus(l1Address, connector, demo) {
|
|
7
|
+
const data = await fetchAccountsByL1Address(l1Address, connector, demo);
|
|
8
8
|
if (data.code === LIGHTER_ACCOUNT_NOT_REGISTERED_CODE) {
|
|
9
9
|
return {
|
|
10
10
|
step: "deposit-required"
|
|
@@ -3,8 +3,8 @@ import { retrieveLighterAuthToken } from './auth-token.helper';
|
|
|
3
3
|
|
|
4
4
|
var __defProp = Object.defineProperty;
|
|
5
5
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
-
async function applyReferralCode(sdk, params, demo) {
|
|
7
|
-
const authToken = await retrieveLighterAuthToken(sdk, params.credentials, demo);
|
|
6
|
+
async function applyReferralCode(sdk, connector, params, demo) {
|
|
7
|
+
const authToken = await retrieveLighterAuthToken(sdk, connector, params.credentials, demo);
|
|
8
8
|
await submitReferralCode({
|
|
9
9
|
l1Address: params.l1Address,
|
|
10
10
|
referralCode: params.referralCode,
|
|
@@ -13,11 +13,11 @@ async function applyReferralCode(sdk, params, demo) {
|
|
|
13
13
|
telegram: params.telegram,
|
|
14
14
|
x: params.x,
|
|
15
15
|
signature: params.signature
|
|
16
|
-
}, demo);
|
|
16
|
+
}, connector, demo);
|
|
17
17
|
}
|
|
18
18
|
__name(applyReferralCode, "applyReferralCode");
|
|
19
|
-
async function readUserReferrals(sdk, params, demo) {
|
|
20
|
-
const authToken = await retrieveLighterAuthToken(sdk, params.credentials, demo);
|
|
19
|
+
async function readUserReferrals(sdk, connector, params, demo) {
|
|
20
|
+
const authToken = await retrieveLighterAuthToken(sdk, connector, params.credentials, demo);
|
|
21
21
|
return fetchUserReferrals({
|
|
22
22
|
l1Address: params.l1Address,
|
|
23
23
|
authToken,
|
|
@@ -26,7 +26,7 @@ async function readUserReferrals(sdk, params, demo) {
|
|
|
26
26
|
statsEndTimestamp: params.statsEndTimestamp,
|
|
27
27
|
limit: params.limit,
|
|
28
28
|
eligibleOnly: params.eligibleOnly
|
|
29
|
-
}, demo);
|
|
29
|
+
}, connector, demo);
|
|
30
30
|
}
|
|
31
31
|
__name(readUserReferrals, "readUserReferrals");
|
|
32
32
|
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { parseUnits, erc20Abi } from 'viem';
|
|
2
|
+
import { createDepositIntentAddress, fetchDepositNetworks } from '../../client/lighter/rest';
|
|
3
|
+
import { assertOwnerWallet, emitProgress, watchAccountRegistration } from './deposit-settlement.helper';
|
|
4
|
+
import { LIGHTER_RHC_USDG_DECIMALS, MIN_LIGHTER_RHC_DEPOSIT_USDG, LIGHTER_RHC_USDG_ADDRESS } from './types';
|
|
5
|
+
|
|
6
|
+
var __defProp = Object.defineProperty;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
const USDG_AMOUNT_PATTERN = /^\d+(?:\.\d{1,6})?$/;
|
|
9
|
+
async function createRhcDepositAddress(params, demo) {
|
|
10
|
+
if (demo) {
|
|
11
|
+
throw new Error("Lighter demo deposits are unavailable because Robinhood Chain has no testnet deployment");
|
|
12
|
+
}
|
|
13
|
+
if (!/^0x[0-9a-fA-F]{40}$/.test(params.ownerAddress)) {
|
|
14
|
+
throw new Error(`Invalid Lighter Robinhood Chain owner address: ${params.ownerAddress}`);
|
|
15
|
+
}
|
|
16
|
+
const chainId = await retrieveDepositChainId(demo);
|
|
17
|
+
const intentAddress = await createDepositIntentAddress({
|
|
18
|
+
chainId,
|
|
19
|
+
fromAddress: params.ownerAddress,
|
|
20
|
+
amount: "0",
|
|
21
|
+
external: true
|
|
22
|
+
}, "lighter_rhc", demo);
|
|
23
|
+
return {
|
|
24
|
+
intentAddress,
|
|
25
|
+
chainId
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
__name(createRhcDepositAddress, "createRhcDepositAddress");
|
|
29
|
+
async function retrieveDepositChainId(demo) {
|
|
30
|
+
const networks = await fetchDepositNetworks("lighter_rhc", demo);
|
|
31
|
+
const [only] = networks;
|
|
32
|
+
if (only === void 0) {
|
|
33
|
+
throw new Error("Lighter reported no deposit network for Robinhood Chain");
|
|
34
|
+
}
|
|
35
|
+
if (networks.length > 1) {
|
|
36
|
+
const named = networks.map((network) => `${network.name} (${network.chainId})`).join(", ");
|
|
37
|
+
throw new Error(`Lighter reported more than one deposit network for Robinhood Chain: ${named}`);
|
|
38
|
+
}
|
|
39
|
+
return only.chainId;
|
|
40
|
+
}
|
|
41
|
+
__name(retrieveDepositChainId, "retrieveDepositChainId");
|
|
42
|
+
async function depositUsdgOnRobinhoodChain(params, demo) {
|
|
43
|
+
assertOwnerWallet(params.ownerAddress, params.connectedAddress);
|
|
44
|
+
if (!USDG_AMOUNT_PATTERN.test(params.amount)) {
|
|
45
|
+
throw new Error("USDG amount must be a positive decimal with at most 6 fractional digits");
|
|
46
|
+
}
|
|
47
|
+
const units = parseUnits(params.amount, LIGHTER_RHC_USDG_DECIMALS);
|
|
48
|
+
const minimumUnits = parseUnits(MIN_LIGHTER_RHC_DEPOSIT_USDG, LIGHTER_RHC_USDG_DECIMALS);
|
|
49
|
+
if (units < minimumUnits) {
|
|
50
|
+
throw new Error(`Lighter Robinhood Chain deposits start at ${MIN_LIGHTER_RHC_DEPOSIT_USDG} USDG`);
|
|
51
|
+
}
|
|
52
|
+
const { intentAddress, chainId } = await createRhcDepositAddress({
|
|
53
|
+
ownerAddress: params.ownerAddress
|
|
54
|
+
}, demo);
|
|
55
|
+
const balance = await params.wallet.readTokenBalance(chainId, LIGHTER_RHC_USDG_ADDRESS, params.ownerAddress);
|
|
56
|
+
if (balance < units) {
|
|
57
|
+
throw new Error(`Your wallet holds ${formatUsdgUnits(balance)} USDG.`);
|
|
58
|
+
}
|
|
59
|
+
const walletClient = await params.wallet.retrieveWallet(chainId);
|
|
60
|
+
const txHash = await walletClient.writeContract({
|
|
61
|
+
chain: walletClient.chain,
|
|
62
|
+
address: LIGHTER_RHC_USDG_ADDRESS,
|
|
63
|
+
abi: erc20Abi,
|
|
64
|
+
functionName: "transfer",
|
|
65
|
+
args: [
|
|
66
|
+
intentAddress,
|
|
67
|
+
units
|
|
68
|
+
]
|
|
69
|
+
});
|
|
70
|
+
emitProgress(params.onProgress, params.signal, {
|
|
71
|
+
state: "confirming",
|
|
72
|
+
txHash
|
|
73
|
+
});
|
|
74
|
+
await params.wallet.confirmTransaction(chainId, txHash);
|
|
75
|
+
await watchAccountRegistration(params.ownerAddress, "lighter_rhc", txHash, (progress) => emitProgress(params.onProgress, params.signal, progress), params.signal, demo, params.settlementTimeoutMs);
|
|
76
|
+
return {
|
|
77
|
+
txHash,
|
|
78
|
+
intentAddress,
|
|
79
|
+
chainId
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
__name(depositUsdgOnRobinhoodChain, "depositUsdgOnRobinhoodChain");
|
|
83
|
+
function formatUsdgUnits(units) {
|
|
84
|
+
const value = units.toString().padStart(LIGHTER_RHC_USDG_DECIMALS + 1, "0");
|
|
85
|
+
const whole = value.slice(0, -LIGHTER_RHC_USDG_DECIMALS);
|
|
86
|
+
const fraction = value.slice(-LIGHTER_RHC_USDG_DECIMALS).replace(/0+$/, "");
|
|
87
|
+
return fraction ? `${whole}.${fraction}` : whole;
|
|
88
|
+
}
|
|
89
|
+
__name(formatUsdgUnits, "formatUsdgUnits");
|
|
90
|
+
|
|
91
|
+
export { createRhcDepositAddress, depositUsdgOnRobinhoodChain };
|
|
@@ -11,12 +11,22 @@ const AssetId = {
|
|
|
11
11
|
LDO: 9,
|
|
12
12
|
AZTEC: 10
|
|
13
13
|
};
|
|
14
|
+
const LighterRhcAssetId = {
|
|
15
|
+
USDG: 3
|
|
16
|
+
};
|
|
14
17
|
const RouteType = {
|
|
15
18
|
PERP: 0,
|
|
16
19
|
SPOT: 1
|
|
17
20
|
};
|
|
18
21
|
const MIN_LIGHTER_CCTP_DEPOSIT_USDC = "5";
|
|
19
|
-
const
|
|
22
|
+
const MIN_LIGHTER_FAST_WITHDRAW_COLLATERAL = {
|
|
23
|
+
lighter: "4",
|
|
24
|
+
lighter_rhc: "4"
|
|
25
|
+
};
|
|
26
|
+
const MIN_LIGHTER_RHC_DEPOSIT_USDG = "1";
|
|
27
|
+
const LIGHTER_RHC_CHAIN_ID = 4663;
|
|
28
|
+
const LIGHTER_RHC_USDG_ADDRESS = "0x5fc5360D0400a0Fd4f2af552ADD042D716F1d168";
|
|
29
|
+
const LIGHTER_RHC_USDG_DECIMALS = 6;
|
|
20
30
|
const L2_ASSET_DECIMALS = {
|
|
21
31
|
[AssetId.ETH]: 8,
|
|
22
32
|
[AssetId.LIT]: 8,
|
|
@@ -71,4 +81,4 @@ const FUNXYZ_DEPOSIT_MINIMUMS_USD = {
|
|
|
71
81
|
}
|
|
72
82
|
};
|
|
73
83
|
|
|
74
|
-
export { ASSET_DECIMALS, AssetId, FUNXYZ_DEPOSIT_MINIMUMS_USD, FUNXYZ_ETHEREUM_MAINNET_CHAIN_ID, FUNXYZ_EVM_CHAIN_NAMES, L2_ASSET_DECIMALS, MIN_LIGHTER_CCTP_DEPOSIT_USDC,
|
|
84
|
+
export { ASSET_DECIMALS, AssetId, FUNXYZ_DEPOSIT_MINIMUMS_USD, FUNXYZ_ETHEREUM_MAINNET_CHAIN_ID, FUNXYZ_EVM_CHAIN_NAMES, L2_ASSET_DECIMALS, LIGHTER_RHC_CHAIN_ID, LIGHTER_RHC_USDG_ADDRESS, LIGHTER_RHC_USDG_DECIMALS, LighterRhcAssetId, MIN_LIGHTER_CCTP_DEPOSIT_USDC, MIN_LIGHTER_FAST_WITHDRAW_COLLATERAL, MIN_LIGHTER_RHC_DEPOSIT_USDG, RouteType, getAssetDecimals };
|
|
@@ -2,19 +2,19 @@ import { parseUnits } from 'viem';
|
|
|
2
2
|
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
4
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
if (!
|
|
10
|
-
throw new Error(
|
|
5
|
+
const COLLATERAL_DECIMALS = 6;
|
|
6
|
+
const COLLATERAL_AMOUNT_PATTERN = /^\d+(?:\.\d{1,6})?$/;
|
|
7
|
+
function parseCollateralAmount(amount, assetSymbol) {
|
|
8
|
+
const invalidAmountMessage = `${assetSymbol} amount must be a positive decimal with at most 6 fractional digits`;
|
|
9
|
+
if (!COLLATERAL_AMOUNT_PATTERN.test(amount)) {
|
|
10
|
+
throw new Error(invalidAmountMessage);
|
|
11
11
|
}
|
|
12
|
-
const units = parseUnits(amount,
|
|
12
|
+
const units = parseUnits(amount, COLLATERAL_DECIMALS);
|
|
13
13
|
if (units <= 0n || units > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
14
|
-
throw new Error(
|
|
14
|
+
throw new Error(invalidAmountMessage);
|
|
15
15
|
}
|
|
16
16
|
return Number(units);
|
|
17
17
|
}
|
|
18
|
-
__name(
|
|
18
|
+
__name(parseCollateralAmount, "parseCollateralAmount");
|
|
19
19
|
|
|
20
|
-
export {
|
|
20
|
+
export { parseCollateralAmount };
|