@n1xyz/nord-ts 0.0.1 → 0.0.4
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/.eslintrc.js +11 -0
- package/README.md +148 -65
- package/dist/bridge/NordUser.d.ts +78 -0
- package/dist/bridge/NordUser.js +196 -0
- package/dist/bridge/client.d.ts +150 -0
- package/dist/bridge/client.js +394 -0
- package/dist/bridge/const.d.ts +23 -0
- package/dist/bridge/const.js +47 -0
- package/dist/bridge/index.d.ts +5 -0
- package/dist/bridge/index.js +23 -0
- package/dist/bridge/types.d.ts +118 -0
- package/dist/bridge/types.js +16 -0
- package/dist/bridge/utils.d.ts +64 -0
- package/dist/bridge/utils.js +131 -0
- package/dist/client.d.ts +70 -0
- package/dist/client.js +129 -0
- package/dist/const.d.ts +2 -5
- package/dist/const.js +18 -22
- package/dist/constants/endpoints.d.ts +65 -0
- package/dist/constants/endpoints.js +68 -0
- package/dist/gen/common.d.ts +6 -1
- package/dist/gen/common.js +19 -9
- package/dist/gen/nord.d.ts +75 -17
- package/dist/gen/nord.js +987 -423
- package/dist/idl/bridge.d.ts +2 -0
- package/dist/idl/bridge.js +703 -0
- package/dist/index.d.ts +8 -5
- package/dist/index.js +18 -2
- package/dist/models/account.d.ts +58 -0
- package/dist/models/account.js +6 -0
- package/dist/models/index.d.ts +8 -0
- package/dist/models/index.js +28 -0
- package/dist/models/market.d.ts +137 -0
- package/dist/models/market.js +6 -0
- package/dist/models/order.d.ts +211 -0
- package/dist/models/order.js +6 -0
- package/dist/models/token.d.ts +50 -0
- package/dist/models/token.js +6 -0
- package/dist/nord/Nord.d.ts +222 -49
- package/dist/nord/Nord.js +290 -278
- package/dist/nord/NordError.d.ts +23 -0
- package/dist/nord/NordError.js +48 -0
- package/dist/nord/NordImpl.d.ts +6 -2
- package/dist/nord/NordImpl.js +21 -1
- package/dist/nord/NordUser.d.ts +208 -42
- package/dist/nord/NordUser.js +389 -157
- package/dist/nord/Subscriber.d.ts +37 -0
- package/dist/nord/Subscriber.js +29 -0
- package/dist/nord/api/actions.d.ts +101 -0
- package/dist/nord/api/actions.js +250 -0
- package/dist/nord/api/core.d.ts +49 -0
- package/dist/nord/api/core.js +121 -0
- package/dist/nord/api/index.d.ts +1 -0
- package/dist/nord/api/index.js +17 -0
- package/dist/nord/api/market.d.ts +36 -0
- package/dist/nord/api/market.js +98 -0
- package/dist/nord/api/metrics.d.ts +67 -0
- package/dist/nord/api/metrics.js +132 -0
- package/dist/nord/api/orderFunctions.d.ts +168 -0
- package/dist/nord/api/orderFunctions.js +133 -0
- package/dist/nord/api/queries.d.ts +81 -0
- package/dist/nord/api/queries.js +187 -0
- package/dist/nord/client/Nord.d.ts +335 -0
- package/dist/nord/client/Nord.js +532 -0
- package/dist/nord/client/NordUser.d.ts +320 -0
- package/dist/nord/client/NordUser.js +701 -0
- package/dist/nord/core.d.ts +48 -0
- package/dist/nord/core.js +97 -0
- package/dist/nord/index.d.ts +9 -2
- package/dist/nord/index.js +30 -6
- package/dist/nord/market.d.ts +36 -0
- package/dist/nord/market.js +90 -0
- package/dist/nord/metrics.d.ts +67 -0
- package/dist/nord/metrics.js +124 -0
- package/dist/nord/models/Subscriber.d.ts +37 -0
- package/dist/nord/models/Subscriber.js +29 -0
- package/dist/nord/queries.d.ts +81 -0
- package/dist/nord/queries.js +181 -0
- package/dist/nord/types.d.ts +88 -0
- package/dist/nord/types.js +2 -0
- package/dist/nord/utils/NordError.d.ts +35 -0
- package/dist/nord/utils/NordError.js +46 -0
- package/dist/nord/websocket.d.ts +49 -0
- package/dist/nord/websocket.js +107 -0
- package/dist/operations/account.d.ts +58 -0
- package/dist/operations/account.js +112 -0
- package/dist/operations/market.d.ts +65 -0
- package/dist/operations/market.js +131 -0
- package/dist/operations/orders.d.ts +57 -0
- package/dist/operations/orders.js +129 -0
- package/dist/solana/NordUser.d.ts +78 -0
- package/dist/solana/NordUser.js +196 -0
- package/dist/solana/client.d.ts +139 -0
- package/dist/solana/client.js +360 -0
- package/dist/solana/const.d.ts +23 -0
- package/dist/solana/const.js +47 -0
- package/dist/solana/index.d.ts +5 -0
- package/dist/solana/index.js +23 -0
- package/dist/solana/types.d.ts +118 -0
- package/dist/solana/types.js +16 -0
- package/dist/solana/utils.d.ts +64 -0
- package/dist/solana/utils.js +131 -0
- package/dist/types/api.d.ts +152 -0
- package/dist/types/api.js +6 -0
- package/dist/types/config.d.ts +34 -0
- package/dist/types/config.js +6 -0
- package/dist/types.d.ts +144 -87
- package/dist/types.js +13 -2
- package/dist/utils/errors.d.ts +96 -0
- package/dist/utils/errors.js +132 -0
- package/dist/utils/http.d.ts +35 -0
- package/dist/utils/http.js +105 -0
- package/dist/utils.d.ts +14 -5
- package/dist/utils.js +26 -7
- package/dist/websocket/NordWebSocketClient.d.ts +71 -0
- package/dist/websocket/NordWebSocketClient.js +343 -0
- package/dist/websocket/client.d.ts +93 -0
- package/dist/websocket/client.js +222 -0
- package/dist/websocket/events.d.ts +19 -0
- package/dist/websocket/events.js +2 -0
- package/dist/websocket/index.d.ts +2 -0
- package/dist/websocket/index.js +5 -0
- package/dist/websocket.d.ts +55 -0
- package/dist/websocket.js +211 -0
- package/docs/assets/navigation.js +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/classes/Nord.html +2 -15
- package/docs/classes/NordUser.html +4 -4
- package/docs/enums/FillMode.html +2 -2
- package/docs/enums/KeyType.html +2 -2
- package/docs/enums/PeakTpsPeriodUnit.html +2 -2
- package/docs/enums/Side.html +2 -2
- package/docs/functions/assert.html +1 -1
- package/docs/functions/bigIntToProtoU128.html +1 -1
- package/docs/functions/checkPubKeyLength.html +1 -1
- package/docs/functions/checkedFetch.html +1 -1
- package/docs/functions/decodeLengthDelimited.html +1 -1
- package/docs/functions/encodeLengthDelimited.html +1 -1
- package/docs/functions/fillModeToProtoFillMode.html +1 -1
- package/docs/functions/findMarket.html +1 -1
- package/docs/functions/findToken.html +1 -1
- package/docs/functions/makeWalletSignFn.html +1 -1
- package/docs/functions/optExpect.html +1 -1
- package/docs/functions/optMap.html +1 -1
- package/docs/functions/optUnwrap.html +1 -1
- package/docs/functions/panic.html +1 -1
- package/docs/functions/signAction.html +1 -1
- package/docs/functions/toScaledU128.html +1 -1
- package/docs/functions/toScaledU64.html +1 -1
- package/docs/interfaces/Account.html +2 -2
- package/docs/interfaces/ActionInfo.html +2 -2
- package/docs/interfaces/ActionQuery.html +2 -2
- package/docs/interfaces/ActionResponse.html +2 -2
- package/docs/interfaces/ActionsExtendedInfo.html +2 -2
- package/docs/interfaces/ActionsQuery.html +2 -2
- package/docs/interfaces/ActionsResponse.html +2 -2
- package/docs/interfaces/AggregateMetrics.html +2 -2
- package/docs/interfaces/BlockQuery.html +2 -2
- package/docs/interfaces/BlockResponse.html +2 -2
- package/docs/interfaces/BlockSummary.html +2 -2
- package/docs/interfaces/BlockSummaryResponse.html +2 -2
- package/docs/interfaces/DeltaEvent.html +2 -2
- package/docs/interfaces/ERC20TokenInfo.html +2 -2
- package/docs/interfaces/Info.html +2 -2
- package/docs/interfaces/Market.html +2 -2
- package/docs/interfaces/MarketStats.html +2 -2
- package/docs/interfaces/MarketsStatsResponse.html +2 -2
- package/docs/interfaces/NordConfig.html +2 -2
- package/docs/interfaces/Order.html +2 -2
- package/docs/interfaces/OrderInfo.html +2 -2
- package/docs/interfaces/PerpMarketStats.html +2 -2
- package/docs/interfaces/RollmanActionExtendedInfo.html +2 -2
- package/docs/interfaces/RollmanActionInfo.html +2 -2
- package/docs/interfaces/RollmanActionResponse.html +2 -2
- package/docs/interfaces/RollmanActionsResponse.html +2 -2
- package/docs/interfaces/RollmanBlockResponse.html +2 -2
- package/docs/interfaces/SubscriberConfig.html +2 -2
- package/docs/interfaces/Token.html +2 -2
- package/docs/interfaces/Trade.html +2 -2
- package/docs/interfaces/Trades.html +2 -2
- package/docs/modules.html +0 -7
- package/docs/types/BigIntValue.html +1 -1
- package/docs/variables/DEBUG_KEYS.html +1 -1
- package/docs/variables/DEFAULT_FUNDING_AMOUNTS.html +1 -1
- package/docs/variables/DEV_CONTRACT_ADDRESS.html +1 -1
- package/docs/variables/DEV_TOKEN_INFOS.html +1 -1
- package/docs/variables/DEV_URL.html +1 -1
- package/docs/variables/ERC20_ABI.html +1 -1
- package/docs/variables/EVM_DEV_URL.html +1 -1
- package/docs/variables/FAUCET_PRIVATE_ADDRESS.html +1 -1
- package/docs/variables/MAX_BUFFER_LEN.html +1 -1
- package/docs/variables/NORD_GETTERS_FACET_ABI.html +1 -1
- package/docs/variables/NORD_RAMP_FACET_ABI.html +1 -1
- package/docs/variables/SESSION_TTL.html +1 -1
- package/docs/variables/WEBSERVER_DEV_URL.html +1 -1
- package/docs/variables/ZERO_DECIMAL.html +1 -1
- package/package.json +10 -12
- package/src/bridge/client.ts +487 -0
- package/src/bridge/const.ts +53 -0
- package/src/bridge/index.ts +7 -0
- package/src/bridge/types.ts +127 -0
- package/src/bridge/utils.ts +140 -0
- package/src/const.ts +20 -25
- package/src/gen/common.ts +27 -10
- package/src/gen/nord.ts +1044 -483
- package/src/idl/bridge.ts +702 -0
- package/src/index.ts +24 -5
- package/src/nord/{actions.ts → api/actions.ts} +33 -37
- package/src/nord/api/core.ts +130 -0
- package/src/nord/api/market.ts +125 -0
- package/src/nord/api/metrics.ts +154 -0
- package/src/nord/api/queries.ts +236 -0
- package/src/nord/client/Nord.ts +652 -0
- package/src/nord/client/NordUser.ts +1105 -0
- package/src/nord/index.ts +16 -2
- package/src/nord/models/Subscriber.ts +57 -0
- package/src/nord/utils/NordError.ts +72 -0
- package/src/types.ts +170 -99
- package/src/utils.ts +40 -19
- package/src/websocket/NordWebSocketClient.ts +432 -0
- package/src/websocket/events.ts +31 -0
- package/src/websocket/index.ts +2 -0
- package/tests/utils.spec.ts +24 -24
- package/docs/classes/Subscriber.html +0 -6
- package/docs/functions/createWebSocketSubscription.html +0 -12
- package/docs/interfaces/OrderbookOrder.html +0 -6
- package/docs/interfaces/OrderbookResponse.html +0 -10
- package/docs/interfaces/TradeInfo.html +0 -20
- package/docs/interfaces/TradesQueryParams.html +0 -10
- package/docs/interfaces/TradesResponse.html +0 -12
- package/src/abis/ERC20_ABI.ts +0 -310
- package/src/abis/NORD_GETTERS_FACET_ABI.ts +0 -192
- package/src/abis/NORD_RAMP_FACET_ABI.ts +0 -141
- package/src/abis/index.ts +0 -3
- package/src/nord/Nord.ts +0 -504
- package/src/nord/NordImpl.ts +0 -8
- package/src/nord/NordUser.ts +0 -469
package/src/nord/NordUser.ts
DELETED
|
@@ -1,469 +0,0 @@
|
|
|
1
|
-
import { BrowserProvider, ethers, SigningKey, MaxUint256 } from "ethers";
|
|
2
|
-
import secp256k1 from "secp256k1";
|
|
3
|
-
import { DEFAULT_FUNDING_AMOUNTS, FAUCET_PRIVATE_ADDRESS } from "../const";
|
|
4
|
-
import {
|
|
5
|
-
assert,
|
|
6
|
-
BigIntValue,
|
|
7
|
-
checkedFetch,
|
|
8
|
-
findMarket,
|
|
9
|
-
findToken,
|
|
10
|
-
optExpect,
|
|
11
|
-
} from "../utils";
|
|
12
|
-
import { ERC20_ABI, NORD_RAMP_FACET_ABI } from "../abis";
|
|
13
|
-
import {
|
|
14
|
-
cancelOrder,
|
|
15
|
-
createSession,
|
|
16
|
-
placeOrder,
|
|
17
|
-
revokeSession,
|
|
18
|
-
transfer,
|
|
19
|
-
withdraw,
|
|
20
|
-
} from "./actions";
|
|
21
|
-
import { FillMode, Order, Side } from "../types";
|
|
22
|
-
import { Nord } from "./Nord";
|
|
23
|
-
import Decimal from "decimal.js";
|
|
24
|
-
|
|
25
|
-
export class NordUser {
|
|
26
|
-
nord: Nord;
|
|
27
|
-
address: string;
|
|
28
|
-
walletSignFn: (message: Uint8Array | string) => Promise<string>;
|
|
29
|
-
sessionSignFn: (message: Uint8Array) => Promise<Uint8Array>;
|
|
30
|
-
balances: {
|
|
31
|
-
[key: string]: { accountId: number; balance: number; symbol: string }[];
|
|
32
|
-
} = {};
|
|
33
|
-
orders: { [key: string]: Order[] } = {};
|
|
34
|
-
accountIds?: number[];
|
|
35
|
-
sessionId?: bigint;
|
|
36
|
-
|
|
37
|
-
publicKey: Uint8Array | undefined;
|
|
38
|
-
lastTs = 0;
|
|
39
|
-
lastNonce = 0;
|
|
40
|
-
|
|
41
|
-
constructor(params: {
|
|
42
|
-
nord: Nord;
|
|
43
|
-
address: string;
|
|
44
|
-
walletSignFn: (message: Uint8Array | string) => Promise<string>;
|
|
45
|
-
sessionSignFn: (message: Uint8Array) => Promise<Uint8Array>;
|
|
46
|
-
}) {
|
|
47
|
-
this.nord = params.nord;
|
|
48
|
-
this.address = params.address;
|
|
49
|
-
this.walletSignFn = params.walletSignFn;
|
|
50
|
-
this.sessionSignFn = params.sessionSignFn;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
clone(): NordUser {
|
|
54
|
-
const newUser = new NordUser({
|
|
55
|
-
nord: this.nord,
|
|
56
|
-
address: this.address,
|
|
57
|
-
walletSignFn: this.walletSignFn,
|
|
58
|
-
sessionSignFn: this.sessionSignFn,
|
|
59
|
-
});
|
|
60
|
-
newUser.publicKey = this.publicKey;
|
|
61
|
-
newUser.lastTs = this.lastTs;
|
|
62
|
-
newUser.lastNonce = this.lastNonce;
|
|
63
|
-
newUser.accountIds = this.accountIds;
|
|
64
|
-
newUser.sessionId = this.sessionId;
|
|
65
|
-
return newUser;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Generates a nonce based on the current timestamp.
|
|
70
|
-
* @returns Generated nonce as a number.
|
|
71
|
-
*/
|
|
72
|
-
getNonce(): number {
|
|
73
|
-
const ts = Date.now() / 1000;
|
|
74
|
-
if (ts === this.lastTs) {
|
|
75
|
-
this.lastNonce += 1;
|
|
76
|
-
} else {
|
|
77
|
-
this.lastTs = ts;
|
|
78
|
-
this.lastNonce = 0;
|
|
79
|
-
}
|
|
80
|
-
return this.lastNonce;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
async updateAccountId() {
|
|
84
|
-
const hexPubkey = ethers
|
|
85
|
-
.hexlify(optExpect(this.publicKey, "No user public key"))
|
|
86
|
-
.slice(2);
|
|
87
|
-
const accountIds_ = await (
|
|
88
|
-
await checkedFetch(
|
|
89
|
-
`${this.nord.webServerUrl}/user_account_ids?pubkey=${hexPubkey}`,
|
|
90
|
-
)
|
|
91
|
-
).json();
|
|
92
|
-
assert(Array.isArray(accountIds_), "Unexpected response");
|
|
93
|
-
const accountIds = accountIds_ as Array<number>;
|
|
94
|
-
this.accountIds = accountIds;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
async fetchInfo() {
|
|
98
|
-
interface FetchOrder {
|
|
99
|
-
orderId: number;
|
|
100
|
-
size: number;
|
|
101
|
-
price: number;
|
|
102
|
-
marketId: number;
|
|
103
|
-
side: "ask" | "bid";
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
interface Balance {
|
|
107
|
-
tokenId: number;
|
|
108
|
-
token: string;
|
|
109
|
-
amount: number;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
interface Account {
|
|
113
|
-
orders: FetchOrder[];
|
|
114
|
-
balances: Balance[];
|
|
115
|
-
accountId: number;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (this.accountIds !== undefined) {
|
|
119
|
-
// todo:implement class
|
|
120
|
-
const accountsData = await Promise.all(
|
|
121
|
-
this.accountIds.map(async (accountId) => {
|
|
122
|
-
const response = await checkedFetch(
|
|
123
|
-
`${this.nord.webServerUrl}/account?account_id=${accountId}`,
|
|
124
|
-
);
|
|
125
|
-
return {
|
|
126
|
-
accountId,
|
|
127
|
-
...((await response.json()) as Promise<Account>),
|
|
128
|
-
};
|
|
129
|
-
}),
|
|
130
|
-
);
|
|
131
|
-
|
|
132
|
-
for (const accountData of accountsData) {
|
|
133
|
-
this.balances[accountData.accountId] = [];
|
|
134
|
-
for (const balance of accountData.balances) {
|
|
135
|
-
this.balances[accountData.accountId].push({
|
|
136
|
-
accountId: accountData.accountId,
|
|
137
|
-
balance: balance.amount,
|
|
138
|
-
symbol: balance.token,
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
this.orders[accountData.accountId] = accountData.orders.map(
|
|
143
|
-
(order: {
|
|
144
|
-
orderId: number;
|
|
145
|
-
side: string;
|
|
146
|
-
size: number;
|
|
147
|
-
price: number;
|
|
148
|
-
marketId: number;
|
|
149
|
-
}) => {
|
|
150
|
-
return {
|
|
151
|
-
orderId: order.orderId,
|
|
152
|
-
isLong: order.side === "bid",
|
|
153
|
-
size: order.size,
|
|
154
|
-
price: order.price,
|
|
155
|
-
marketId: order.marketId,
|
|
156
|
-
};
|
|
157
|
-
},
|
|
158
|
-
);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
async setPublicKey() {
|
|
164
|
-
const message = "Layer N - Nord";
|
|
165
|
-
const msgHash = ethers.hashMessage(message);
|
|
166
|
-
const msgHashBytes = ethers.getBytes(msgHash);
|
|
167
|
-
const signature = await this.walletSignFn(message);
|
|
168
|
-
const recoveredPubKey = SigningKey.recoverPublicKey(
|
|
169
|
-
msgHashBytes,
|
|
170
|
-
signature,
|
|
171
|
-
);
|
|
172
|
-
const publicKeyBuffer = Buffer.from(recoveredPubKey.slice(2), "hex");
|
|
173
|
-
this.publicKey = secp256k1.publicKeyConvert(publicKeyBuffer, true);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
async fundEthWallet() {
|
|
177
|
-
const provider = new ethers.JsonRpcProvider(this.nord.evmUrl);
|
|
178
|
-
const wallet = new ethers.Wallet(FAUCET_PRIVATE_ADDRESS, provider);
|
|
179
|
-
assert(DEFAULT_FUNDING_AMOUNTS["ETH"] != null);
|
|
180
|
-
const ethTx = await wallet.sendTransaction({
|
|
181
|
-
to: this.address,
|
|
182
|
-
value: ethers.parseEther(DEFAULT_FUNDING_AMOUNTS["ETH"][0]),
|
|
183
|
-
});
|
|
184
|
-
await ethTx.wait();
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
async fundErc20Wallet() {
|
|
188
|
-
const provider = new ethers.JsonRpcProvider(this.nord.evmUrl);
|
|
189
|
-
const wallet = new ethers.Wallet(FAUCET_PRIVATE_ADDRESS, provider);
|
|
190
|
-
assert(DEFAULT_FUNDING_AMOUNTS["ETH"] != null);
|
|
191
|
-
for (const tokenInfo of this.nord.tokenInfos) {
|
|
192
|
-
const erc20Contract = new ethers.Contract(
|
|
193
|
-
tokenInfo.address,
|
|
194
|
-
ERC20_ABI,
|
|
195
|
-
wallet,
|
|
196
|
-
);
|
|
197
|
-
if (DEFAULT_FUNDING_AMOUNTS[tokenInfo.address]) {
|
|
198
|
-
const defaultFundingAmount = DEFAULT_FUNDING_AMOUNTS[tokenInfo.address];
|
|
199
|
-
const tokenTx = await erc20Contract.transfer(
|
|
200
|
-
this.address,
|
|
201
|
-
ethers.parseUnits(defaultFundingAmount[0], defaultFundingAmount[1]),
|
|
202
|
-
{
|
|
203
|
-
maxFeePerGas: ethers.parseUnits("30", "gwei"),
|
|
204
|
-
maxPriorityFeePerGas: ethers.parseUnits("0.001", "gwei"),
|
|
205
|
-
},
|
|
206
|
-
);
|
|
207
|
-
await tokenTx.wait();
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
async refreshSession(sessionPk: Uint8Array): Promise<void> {
|
|
213
|
-
this.sessionId = await createSession(
|
|
214
|
-
this.nord.webServerUrl,
|
|
215
|
-
this.walletSignFn,
|
|
216
|
-
await this.nord.impl.getTimestamp(),
|
|
217
|
-
this.getNonce(),
|
|
218
|
-
{
|
|
219
|
-
userPubkey: optExpect(this.publicKey, "No user's public key"),
|
|
220
|
-
sessionPubkey: sessionPk,
|
|
221
|
-
},
|
|
222
|
-
);
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* Revokes session previously created by user
|
|
226
|
-
*
|
|
227
|
-
* @param sessionId - session identifier
|
|
228
|
-
*/
|
|
229
|
-
async revokeSession(sessionId: BigIntValue): Promise<void> {
|
|
230
|
-
return revokeSession(
|
|
231
|
-
this.nord.webServerUrl,
|
|
232
|
-
this.walletSignFn,
|
|
233
|
-
await this.nord.impl.getTimestamp(),
|
|
234
|
-
this.getNonce(),
|
|
235
|
-
{
|
|
236
|
-
sessionId,
|
|
237
|
-
},
|
|
238
|
-
);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
async deposit(
|
|
242
|
-
provider: BrowserProvider,
|
|
243
|
-
amount: number,
|
|
244
|
-
tokenId: number,
|
|
245
|
-
): Promise<void> {
|
|
246
|
-
const erc20 = this.nord.tokenInfos[tokenId];
|
|
247
|
-
const erc20Contract = new ethers.Contract(
|
|
248
|
-
erc20.address,
|
|
249
|
-
ERC20_ABI,
|
|
250
|
-
await provider.getSigner(),
|
|
251
|
-
);
|
|
252
|
-
|
|
253
|
-
const approveTx = await erc20Contract.approve(
|
|
254
|
-
this.nord.contractAddress,
|
|
255
|
-
MaxUint256,
|
|
256
|
-
{
|
|
257
|
-
maxFeePerGas: ethers.parseUnits("30", "gwei"),
|
|
258
|
-
maxPriorityFeePerGas: ethers.parseUnits("0.001", "gwei"),
|
|
259
|
-
},
|
|
260
|
-
);
|
|
261
|
-
await approveTx.wait();
|
|
262
|
-
const nordContract = new ethers.Contract(
|
|
263
|
-
this.nord.contractAddress,
|
|
264
|
-
NORD_RAMP_FACET_ABI,
|
|
265
|
-
await provider.getSigner(),
|
|
266
|
-
);
|
|
267
|
-
const depositTx = await nordContract.depositUnchecked(
|
|
268
|
-
this.publicKey,
|
|
269
|
-
BigInt(0),
|
|
270
|
-
ethers.parseUnits(amount.toString(), erc20.precision),
|
|
271
|
-
{
|
|
272
|
-
maxFeePerGas: ethers.parseUnits("30", "gwei"),
|
|
273
|
-
maxPriorityFeePerGas: ethers.parseUnits("0.001", "gwei"),
|
|
274
|
-
},
|
|
275
|
-
);
|
|
276
|
-
await depositTx.wait();
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
async depositApproveTx(
|
|
280
|
-
provider: BrowserProvider,
|
|
281
|
-
amount: number,
|
|
282
|
-
tokenId: number,
|
|
283
|
-
): Promise<void> {
|
|
284
|
-
const erc20 = this.nord.tokenInfos[tokenId];
|
|
285
|
-
const erc20Contract = new ethers.Contract(
|
|
286
|
-
erc20.address,
|
|
287
|
-
ERC20_ABI,
|
|
288
|
-
await provider.getSigner(),
|
|
289
|
-
);
|
|
290
|
-
|
|
291
|
-
const approveTx = await erc20Contract.approve(
|
|
292
|
-
this.nord.contractAddress,
|
|
293
|
-
MaxUint256,
|
|
294
|
-
{
|
|
295
|
-
maxFeePerGas: ethers.parseUnits("30", "gwei"),
|
|
296
|
-
maxPriorityFeePerGas: ethers.parseUnits("0.001", "gwei"),
|
|
297
|
-
},
|
|
298
|
-
);
|
|
299
|
-
return approveTx.hash;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
async depositOnlyTx(
|
|
303
|
-
provider: BrowserProvider,
|
|
304
|
-
amount: number,
|
|
305
|
-
tokenId: number,
|
|
306
|
-
): Promise<void> {
|
|
307
|
-
const erc20 = this.nord.tokenInfos[tokenId];
|
|
308
|
-
const nordContract = new ethers.Contract(
|
|
309
|
-
this.nord.contractAddress,
|
|
310
|
-
NORD_RAMP_FACET_ABI,
|
|
311
|
-
await provider.getSigner(),
|
|
312
|
-
);
|
|
313
|
-
const depositTx = await nordContract.depositUnchecked(
|
|
314
|
-
this.publicKey,
|
|
315
|
-
BigInt(0),
|
|
316
|
-
ethers.parseUnits(amount.toString(), erc20.precision),
|
|
317
|
-
{
|
|
318
|
-
maxFeePerGas: ethers.parseUnits("30", "gwei"),
|
|
319
|
-
maxPriorityFeePerGas: ethers.parseUnits("0.001", "gwei"),
|
|
320
|
-
},
|
|
321
|
-
);
|
|
322
|
-
return depositTx.hash;
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
async depositEth(
|
|
326
|
-
provider: BrowserProvider,
|
|
327
|
-
amount: number,
|
|
328
|
-
tokenId: number,
|
|
329
|
-
): Promise<void> {
|
|
330
|
-
if (tokenId || tokenId == 0) {
|
|
331
|
-
const nordContract = new ethers.Contract(
|
|
332
|
-
this.nord.contractAddress,
|
|
333
|
-
NORD_RAMP_FACET_ABI,
|
|
334
|
-
await provider.getSigner(),
|
|
335
|
-
);
|
|
336
|
-
const depositTx = await nordContract.depositUnchecked(
|
|
337
|
-
this.publicKey,
|
|
338
|
-
BigInt(1),
|
|
339
|
-
ethers.parseUnits(amount.toString(), 18),
|
|
340
|
-
{
|
|
341
|
-
maxFeePerGas: ethers.parseUnits("30", "gwei"),
|
|
342
|
-
maxPriorityFeePerGas: ethers.parseUnits("0.001", "gwei"),
|
|
343
|
-
},
|
|
344
|
-
);
|
|
345
|
-
await depositTx.wait();
|
|
346
|
-
} else {
|
|
347
|
-
// todo:implement eth deposits
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
async withdraw(tokenId: number, amount: number): Promise<void> {
|
|
352
|
-
withdraw(
|
|
353
|
-
this.nord.webServerUrl,
|
|
354
|
-
this.sessionSignFn,
|
|
355
|
-
await this.nord.impl.getTimestamp(),
|
|
356
|
-
this.getNonce(),
|
|
357
|
-
{
|
|
358
|
-
sizeDecimals: findToken(this.nord.tokens, tokenId).decimals,
|
|
359
|
-
sessionId: optExpect(this.sessionId, "No session"),
|
|
360
|
-
tokenId: tokenId,
|
|
361
|
-
amount,
|
|
362
|
-
},
|
|
363
|
-
);
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
async placeOrder(params: {
|
|
367
|
-
marketId: number;
|
|
368
|
-
side: Side;
|
|
369
|
-
fillMode: FillMode;
|
|
370
|
-
isReduceOnly: boolean;
|
|
371
|
-
size?: Decimal.Value;
|
|
372
|
-
price?: Decimal.Value;
|
|
373
|
-
quoteSize?: Decimal.Value;
|
|
374
|
-
accountId?: number;
|
|
375
|
-
}): Promise<bigint | undefined> {
|
|
376
|
-
const market = findMarket(this.nord.markets, params.marketId);
|
|
377
|
-
|
|
378
|
-
return placeOrder(
|
|
379
|
-
this.nord.webServerUrl,
|
|
380
|
-
this.sessionSignFn,
|
|
381
|
-
await this.nord.impl.getTimestamp(),
|
|
382
|
-
this.getNonce(),
|
|
383
|
-
{
|
|
384
|
-
sessionId: optExpect(this.sessionId, "No session"),
|
|
385
|
-
senderId: params.accountId,
|
|
386
|
-
sizeDecimals: market.sizeDecimals,
|
|
387
|
-
priceDecimals: market.priceDecimals,
|
|
388
|
-
marketId: params.marketId,
|
|
389
|
-
side: params.side,
|
|
390
|
-
fillMode: params.fillMode,
|
|
391
|
-
isReduceOnly: params.isReduceOnly,
|
|
392
|
-
size: params.size,
|
|
393
|
-
price: params.price,
|
|
394
|
-
quoteSize: params.quoteSize,
|
|
395
|
-
},
|
|
396
|
-
);
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
async cancelOrder(orderId: BigIntValue, accountId: number): Promise<bigint> {
|
|
400
|
-
return cancelOrder(
|
|
401
|
-
this.nord.webServerUrl,
|
|
402
|
-
this.sessionSignFn,
|
|
403
|
-
await this.nord.impl.getTimestamp(),
|
|
404
|
-
this.getNonce(),
|
|
405
|
-
{
|
|
406
|
-
sessionId: optExpect(this.sessionId, "No session"),
|
|
407
|
-
senderId: accountId,
|
|
408
|
-
orderId,
|
|
409
|
-
},
|
|
410
|
-
);
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
async transferToAccount(params: {
|
|
414
|
-
to: NordUser;
|
|
415
|
-
tokenId: number;
|
|
416
|
-
amount: Decimal.Value;
|
|
417
|
-
fromAccountId: number;
|
|
418
|
-
toAccountId: number;
|
|
419
|
-
}): Promise<void> {
|
|
420
|
-
const token = findToken(this.nord.tokens, params.tokenId);
|
|
421
|
-
|
|
422
|
-
await transfer(
|
|
423
|
-
this.nord.webServerUrl,
|
|
424
|
-
this.sessionSignFn,
|
|
425
|
-
await this.nord.impl.getTimestamp(),
|
|
426
|
-
this.getNonce(),
|
|
427
|
-
{
|
|
428
|
-
sessionId: optExpect(this.sessionId, "No session"),
|
|
429
|
-
fromAccountId: optExpect(params.fromAccountId, "No source account"),
|
|
430
|
-
toAccountId: optExpect(params.toAccountId, "No target account"),
|
|
431
|
-
tokenId: params.tokenId,
|
|
432
|
-
tokenDecimals: token.decimals,
|
|
433
|
-
amount: params.amount,
|
|
434
|
-
},
|
|
435
|
-
);
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
async createAccount(params: {
|
|
439
|
-
tokenId: number;
|
|
440
|
-
amount: Decimal.Value;
|
|
441
|
-
}): Promise<NordUser> {
|
|
442
|
-
const token = findToken(this.nord.tokens, params.tokenId);
|
|
443
|
-
|
|
444
|
-
const maybeToAccountId = await transfer(
|
|
445
|
-
this.nord.webServerUrl,
|
|
446
|
-
this.sessionSignFn,
|
|
447
|
-
await this.nord.impl.getTimestamp(),
|
|
448
|
-
this.getNonce(),
|
|
449
|
-
{
|
|
450
|
-
sessionId: optExpect(this.sessionId, "No session"),
|
|
451
|
-
fromAccountId: optExpect(this.accountIds?.[0], "No source account"),
|
|
452
|
-
toAccountId: undefined,
|
|
453
|
-
tokenId: params.tokenId,
|
|
454
|
-
tokenDecimals: token.decimals,
|
|
455
|
-
amount: params.amount,
|
|
456
|
-
},
|
|
457
|
-
);
|
|
458
|
-
|
|
459
|
-
const toAccountId = optExpect(
|
|
460
|
-
maybeToAccountId,
|
|
461
|
-
"New account should have been created",
|
|
462
|
-
);
|
|
463
|
-
const newUser = this.clone();
|
|
464
|
-
newUser.accountIds?.push(toAccountId);
|
|
465
|
-
await newUser.fetchInfo();
|
|
466
|
-
|
|
467
|
-
return newUser;
|
|
468
|
-
}
|
|
469
|
-
}
|