datai-sdk 1.0.6 → 1.1.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/API/v1/activePositions/ActivePositionsResult.ts +221 -3
- package/API/v1/activePositions/NftItem.ts +63 -0
- package/API/v1/activePositions/PerpPosition.ts +129 -0
- package/API/v1/activePositions/TokenBalance.ts +32 -1
- package/API/v1/activePositions/activePositions.ts +8 -1
- package/API/v1/index.ts +6 -0
- package/API/v1/proto/activePositions/activePositions/ActivePositionsResultPb.ts +159 -0
- package/API/v1/proto/activePositions/activePositions/NftItemPb.ts +71 -0
- package/API/v1/proto/activePositions/activePositions/PerpPositionPb.ts +142 -0
- package/API/v1/proto/activePositions/activePositions/PerpSide.ts +10 -0
- package/API/v1/proto/activePositions/activePositions/TokenBalancePb.ts +71 -0
- package/API/v1/proto/activePositions/google/protobuf/Timestamp.ts +48 -0
- package/API/v1/proto/watcher/WatcherInputPb.ts +121 -0
- package/API/v1/watcher/WatcherInput.ts +100 -2
- package/API/v1/watcher/watcher.ts +154 -4
- package/package.json +5 -2
- package/@graphprotocol/graph-ts/README.md +0 -13
- package/@graphprotocol/graph-ts/chain/arweave.ts +0 -82
- package/@graphprotocol/graph-ts/chain/cosmos.ts +0 -426
- package/@graphprotocol/graph-ts/chain/ethereum.ts +0 -727
- package/@graphprotocol/graph-ts/chain/near.ts +0 -420
- package/@graphprotocol/graph-ts/chain/starknet.ts +0 -39
- package/@graphprotocol/graph-ts/common/collections.ts +0 -495
- package/@graphprotocol/graph-ts/common/conversion.ts +0 -3
- package/@graphprotocol/graph-ts/common/datasource.ts +0 -41
- package/@graphprotocol/graph-ts/common/eager_offset.ts +0 -42
- package/@graphprotocol/graph-ts/common/json.ts +0 -28
- package/@graphprotocol/graph-ts/common/numbers.ts +0 -407
- package/@graphprotocol/graph-ts/common/value.ts +0 -585
- package/@graphprotocol/graph-ts/global/global.ts +0 -4
- package/@graphprotocol/graph-ts/helper-functions.ts +0 -79
- package/@graphprotocol/graph-ts/index.ts +0 -156
- package/@graphprotocol/graph-ts/package.json +0 -3
- package/@graphprotocol/graph-ts/tsconfig.json +0 -4
- package/@graphprotocol/graph-ts/types/tsconfig.base.json +0 -3
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import { Address, BigInt } from '@graphprotocol/graph-ts'
|
|
1
|
+
import { Address, BigInt, BigDecimal } from '@graphprotocol/graph-ts'
|
|
2
2
|
import { Timestamp } from '../proto/google/protobuf/Timestamp'
|
|
3
|
-
import { ActivePositionsResultPb } from '../proto/activePositions/ActivePositionsResultPb'
|
|
4
|
-
import { TokenBalancePb } from '../proto/activePositions/TokenBalancePb'
|
|
3
|
+
import { ActivePositionsResultPb } from '../proto/activePositions/activePositions/ActivePositionsResultPb'
|
|
4
|
+
import { TokenBalancePb } from '../proto/activePositions/activePositions/TokenBalancePb'
|
|
5
5
|
import { TokenBalance } from './TokenBalance'
|
|
6
|
+
import { NftItem } from './NftItem'
|
|
7
|
+
import { PerpPosition, PerpSide } from './PerpPosition'
|
|
6
8
|
|
|
7
9
|
export class ActivePositionsResult extends ActivePositionsResultPb {
|
|
8
10
|
tokenBalance(tokenBalances: Array<TokenBalancePb>, index: i32): TokenBalance {
|
|
@@ -144,4 +146,220 @@ export class ActivePositionsResult extends ActivePositionsResultPb {
|
|
|
144
146
|
positionToken.balanceBytes = balance
|
|
145
147
|
if (tokenId) positionToken.tokenId = tokenId
|
|
146
148
|
}
|
|
149
|
+
|
|
150
|
+
// ==========================================================================
|
|
151
|
+
// VESTING / CLAIMABLE METHODS
|
|
152
|
+
// ==========================================================================
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Set supply balance with claimable amount (for vesting positions)
|
|
156
|
+
*
|
|
157
|
+
* @param tokenAddress - The vesting token address
|
|
158
|
+
* @param totalBalance - Total vested amount (including locked)
|
|
159
|
+
* @param claimableAmount - Currently claimable/unlocked amount
|
|
160
|
+
* @param tokenId - Optional token ID for ERC721/1155
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* ```typescript
|
|
164
|
+
* output.setSupplyBalanceWithClaimable(
|
|
165
|
+
* vestingToken,
|
|
166
|
+
* BigInt.fromString("1000000000000000000000"), // 1000 total
|
|
167
|
+
* BigInt.fromString("250000000000000000000") // 250 claimable
|
|
168
|
+
* )
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
setSupplyBalanceWithClaimable(
|
|
172
|
+
tokenAddress: Address,
|
|
173
|
+
totalBalance: BigInt,
|
|
174
|
+
claimableAmount: BigInt,
|
|
175
|
+
tokenId: BigInt = BigInt.fromI32(0)
|
|
176
|
+
): void {
|
|
177
|
+
const token = new TokenBalance()
|
|
178
|
+
token.tokenAddress = tokenAddress
|
|
179
|
+
token.balance = totalBalance
|
|
180
|
+
token.tokenId = tokenId
|
|
181
|
+
token.claimableAmount = claimableAmount
|
|
182
|
+
this.supplyTokens.push(token)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Get claimable amount for a supply token
|
|
187
|
+
*
|
|
188
|
+
* @param tokenAddress - Token address to look up
|
|
189
|
+
* @param tokenId - Optional token ID for ERC721/1155
|
|
190
|
+
* @returns Claimable amount or null if not found/not set
|
|
191
|
+
*/
|
|
192
|
+
supplyClaimableAmount(
|
|
193
|
+
tokenAddress: Address,
|
|
194
|
+
tokenId: BigInt = BigInt.fromI32(0)
|
|
195
|
+
): BigInt | null {
|
|
196
|
+
for (let i = 0; i < this.supplyTokens.length; i++) {
|
|
197
|
+
const token = changetype<TokenBalance>(this.supplyTokens[i])
|
|
198
|
+
if (token.tokenAddress.equals(tokenAddress) && token.tokenId.equals(tokenId)) {
|
|
199
|
+
return token.claimableAmount
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return null
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// ==========================================================================
|
|
206
|
+
// NFT STAKED METHODS
|
|
207
|
+
// ==========================================================================
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Set staked NFTs array
|
|
211
|
+
*
|
|
212
|
+
* @param nfts - Array of NftItem objects representing staked NFTs
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* ```typescript
|
|
216
|
+
* const nfts: NftItem[] = []
|
|
217
|
+
* const nft1 = new NftItem()
|
|
218
|
+
* nft1.collectionId = baycCollection
|
|
219
|
+
* nft1.contractId = baycContract
|
|
220
|
+
* nft1.tokenId = BigInt.fromI32(1234)
|
|
221
|
+
* nft1.amount = BigInt.fromI32(1)
|
|
222
|
+
* nfts.push(nft1)
|
|
223
|
+
*
|
|
224
|
+
* output.setSupplyNfts(nfts)
|
|
225
|
+
* ```
|
|
226
|
+
*/
|
|
227
|
+
setSupplyNfts(nfts: Array<NftItem>): void {
|
|
228
|
+
this.supplyNfts = nfts
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Add a single staked NFT (convenience helper)
|
|
233
|
+
*
|
|
234
|
+
* @param collectionId - NFT collection identifier (may differ from contract for wrapped NFTs)
|
|
235
|
+
* @param contractId - Actual NFT contract address
|
|
236
|
+
* @param tokenId - NFT token ID
|
|
237
|
+
* @param amount - Amount (1 for ERC721, variable for ERC1155)
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* ```typescript
|
|
241
|
+
* output.addSupplyNft(
|
|
242
|
+
* baycCollection, // Collection ID
|
|
243
|
+
* baycContract, // Contract (same for direct staking)
|
|
244
|
+
* BigInt.fromI32(1234),
|
|
245
|
+
* BigInt.fromI32(1)
|
|
246
|
+
* )
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
addSupplyNft(
|
|
250
|
+
collectionId: Address,
|
|
251
|
+
contractId: Address,
|
|
252
|
+
tokenId: BigInt,
|
|
253
|
+
amount: BigInt = BigInt.fromI32(1)
|
|
254
|
+
): void {
|
|
255
|
+
const nft = new NftItem()
|
|
256
|
+
nft.collectionId = collectionId
|
|
257
|
+
nft.contractId = contractId
|
|
258
|
+
nft.tokenId = tokenId
|
|
259
|
+
nft.amount = amount
|
|
260
|
+
this.supplyNfts.push(nft)
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Get count of staked NFTs
|
|
265
|
+
*/
|
|
266
|
+
get supplyNftCount(): i32 {
|
|
267
|
+
return this.supplyNfts.length
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Get a staked NFT by index
|
|
272
|
+
*
|
|
273
|
+
* @param index - Zero-based index
|
|
274
|
+
* @returns NftItem or null if index out of bounds
|
|
275
|
+
*/
|
|
276
|
+
getSupplyNft(index: i32): NftItem | null {
|
|
277
|
+
if (index < 0 || index >= this.supplyNfts.length) {
|
|
278
|
+
return null
|
|
279
|
+
}
|
|
280
|
+
return changetype<NftItem>(this.supplyNfts[index])
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// ==========================================================================
|
|
284
|
+
// PERPETUAL POSITION METHODS
|
|
285
|
+
// ==========================================================================
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Set perpetual/leveraged position data
|
|
289
|
+
*
|
|
290
|
+
* @param baseToken - Base asset symbol (e.g., "ETH", "BTC")
|
|
291
|
+
* @param quoteToken - Quote asset symbol (e.g., "USD", "USDC")
|
|
292
|
+
* @param marginTokenAddress - Margin/collateral token address
|
|
293
|
+
* @param marginInitialAmount - Initial margin deposited
|
|
294
|
+
* @param entryPrice - Entry price in quote terms
|
|
295
|
+
* @param markPrice - Current mark price
|
|
296
|
+
* @param baseSize - Position size in base terms
|
|
297
|
+
* @param unrealizedPnl - Unrealized profit/loss
|
|
298
|
+
* @param currentEquity - Current equity (margin + PnL)
|
|
299
|
+
* @param leverage - Current leverage ratio
|
|
300
|
+
* @param side - Position side (LONG or SHORT)
|
|
301
|
+
*
|
|
302
|
+
* @example
|
|
303
|
+
* ```typescript
|
|
304
|
+
* output.setPerpPosition(
|
|
305
|
+
* "ETH",
|
|
306
|
+
* "USD",
|
|
307
|
+
* USDC_ADDRESS,
|
|
308
|
+
* BigInt.fromString("1000000000"), // 1000 USDC margin
|
|
309
|
+
* BigDecimal.fromString("2500.00"), // Entry
|
|
310
|
+
* BigDecimal.fromString("2600.00"), // Mark
|
|
311
|
+
* BigDecimal.fromString("0.4"), // 0.4 ETH
|
|
312
|
+
* BigDecimal.fromString("40.00"), // $40 profit
|
|
313
|
+
* BigDecimal.fromString("1040.00"), // $1040 equity
|
|
314
|
+
* BigDecimal.fromString("1.0"), // 1x leverage
|
|
315
|
+
* PerpSide.PERP_SIDE_LONG
|
|
316
|
+
* )
|
|
317
|
+
* ```
|
|
318
|
+
*/
|
|
319
|
+
setPerpPosition(
|
|
320
|
+
baseToken: string,
|
|
321
|
+
quoteToken: string,
|
|
322
|
+
marginTokenAddress: Address,
|
|
323
|
+
marginInitialAmount: BigInt,
|
|
324
|
+
entryPrice: BigDecimal,
|
|
325
|
+
markPrice: BigDecimal,
|
|
326
|
+
baseSize: BigDecimal,
|
|
327
|
+
unrealizedPnl: BigDecimal,
|
|
328
|
+
currentEquity: BigDecimal,
|
|
329
|
+
leverage: BigDecimal,
|
|
330
|
+
side: PerpSide
|
|
331
|
+
): void {
|
|
332
|
+
const perp = new PerpPosition()
|
|
333
|
+
perp.baseToken = baseToken
|
|
334
|
+
perp.quoteToken = quoteToken
|
|
335
|
+
perp.marginTokenAddress = marginTokenAddress
|
|
336
|
+
perp.marginInitialAmount = marginInitialAmount
|
|
337
|
+
perp.entryPrice = entryPrice
|
|
338
|
+
perp.markPrice = markPrice
|
|
339
|
+
perp.baseSize = baseSize
|
|
340
|
+
perp.unrealizedPnl = unrealizedPnl
|
|
341
|
+
perp.currentEquity = currentEquity
|
|
342
|
+
perp.leverage = leverage
|
|
343
|
+
perp.side = side
|
|
344
|
+
this.perpPosition = perp
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Get perpetual position (typed)
|
|
349
|
+
*
|
|
350
|
+
* @returns PerpPosition or null if no perp position set
|
|
351
|
+
*/
|
|
352
|
+
getPerp(): PerpPosition | null {
|
|
353
|
+
if (this.perpPosition === null) {
|
|
354
|
+
return null
|
|
355
|
+
}
|
|
356
|
+
return changetype<PerpPosition>(this.perpPosition)
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Check if a perpetual position exists
|
|
361
|
+
*/
|
|
362
|
+
get hasPerp(): bool {
|
|
363
|
+
return this.perpPosition !== null
|
|
364
|
+
}
|
|
147
365
|
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Address, BigInt } from '@graphprotocol/graph-ts'
|
|
2
|
+
import { NftItemPb } from '../proto/activePositions/activePositions/NftItemPb'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* NftItem - Wrapper class for NFT staked positions
|
|
6
|
+
*
|
|
7
|
+
* Represents an NFT that is staked in a protocol (e.g., Unagi, Ape Staking)
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const nft = new NftItem()
|
|
12
|
+
* nft.collectionId = Address.fromString("0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D") // BAYC collection
|
|
13
|
+
* nft.contractId = Address.fromString("0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D") // Same for direct staking
|
|
14
|
+
* nft.tokenId = BigInt.fromI32(1234)
|
|
15
|
+
* nft.amount = BigInt.fromI32(1) // Always 1 for ERC721
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export class NftItem extends NftItemPb {
|
|
19
|
+
/**
|
|
20
|
+
* Collection identifier
|
|
21
|
+
* May differ from contractId for wrapped NFTs or when staking through a wrapper contract
|
|
22
|
+
*/
|
|
23
|
+
get collectionId(): Address {
|
|
24
|
+
return changetype<Address>(this.collectionIdBytes)
|
|
25
|
+
}
|
|
26
|
+
set collectionId(value: Address) {
|
|
27
|
+
this.collectionIdBytes = value
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Actual NFT contract address
|
|
32
|
+
* The on-chain contract that the NFT belongs to
|
|
33
|
+
*/
|
|
34
|
+
get contractId(): Address {
|
|
35
|
+
return changetype<Address>(this.contractIdBytes)
|
|
36
|
+
}
|
|
37
|
+
set contractId(value: Address) {
|
|
38
|
+
this.contractIdBytes = value
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* NFT token ID
|
|
43
|
+
* The unique identifier of the NFT within its collection
|
|
44
|
+
*/
|
|
45
|
+
get tokenId(): BigInt {
|
|
46
|
+
return changetype<BigInt>(this.tokenIdBytes)
|
|
47
|
+
}
|
|
48
|
+
set tokenId(value: BigInt) {
|
|
49
|
+
this.tokenIdBytes = value
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Amount staked
|
|
54
|
+
* - For ERC721: Always 1
|
|
55
|
+
* - For ERC1155: Variable quantity
|
|
56
|
+
*/
|
|
57
|
+
get amount(): BigInt {
|
|
58
|
+
return changetype<BigInt>(this.amountBytes)
|
|
59
|
+
}
|
|
60
|
+
set amount(value: BigInt) {
|
|
61
|
+
this.amountBytes = value
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { Address, BigInt, BigDecimal } from '@graphprotocol/graph-ts'
|
|
2
|
+
import { Protobuf } from 'as-proto/assembly'
|
|
3
|
+
import { PerpPositionPb } from '../proto/activePositions/activePositions/PerpPositionPb'
|
|
4
|
+
import { PerpSide } from '../proto/activePositions/activePositions/PerpSide'
|
|
5
|
+
import { BigDecimalPb } from '../bigDecimal/BigDecimalPb'
|
|
6
|
+
|
|
7
|
+
// Re-export enum for convenience
|
|
8
|
+
export { PerpSide } from '../proto/activePositions/activePositions/PerpSide'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* PerpPosition - Wrapper class for perpetual/leveraged trading positions
|
|
12
|
+
*
|
|
13
|
+
* Represents a perpetual futures position with margin, leverage, and PnL tracking
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const perp = new PerpPosition()
|
|
18
|
+
* perp.baseToken = "ETH"
|
|
19
|
+
* perp.quoteToken = "USD"
|
|
20
|
+
* perp.marginTokenAddress = USDC_ADDRESS
|
|
21
|
+
* perp.marginInitialAmount = BigInt.fromString("1000000000") // 1000 USDC (6 decimals)
|
|
22
|
+
* perp.entryPrice = BigDecimal.fromString("2500.50")
|
|
23
|
+
* perp.markPrice = BigDecimal.fromString("2600.00")
|
|
24
|
+
* perp.baseSize = BigDecimal.fromString("0.5")
|
|
25
|
+
* perp.unrealizedPnl = BigDecimal.fromString("49.75")
|
|
26
|
+
* perp.currentEquity = BigDecimal.fromString("1049.75")
|
|
27
|
+
* perp.leverage = BigDecimal.fromString("1.25")
|
|
28
|
+
* perp.side = PerpSide.PERP_SIDE_LONG
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export class PerpPosition extends PerpPositionPb {
|
|
32
|
+
// Base/Quote tokens are already strings in proto, no conversion needed
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Margin token contract address
|
|
36
|
+
* Usually a stablecoin like USDC, USDT, or DAI
|
|
37
|
+
*/
|
|
38
|
+
get marginTokenAddress(): Address {
|
|
39
|
+
return changetype<Address>(this.marginTokenAddressBytes)
|
|
40
|
+
}
|
|
41
|
+
set marginTokenAddress(value: Address) {
|
|
42
|
+
this.marginTokenAddressBytes = value
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Initial margin deposited
|
|
47
|
+
* The original collateral amount in margin token units (with decimals)
|
|
48
|
+
*/
|
|
49
|
+
get marginInitialAmount(): BigInt {
|
|
50
|
+
return changetype<BigInt>(this.marginInitialAmountBytes)
|
|
51
|
+
}
|
|
52
|
+
set marginInitialAmount(value: BigInt) {
|
|
53
|
+
this.marginInitialAmountBytes = value
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Entry price in quote token terms
|
|
58
|
+
* The price at which the position was opened
|
|
59
|
+
*/
|
|
60
|
+
get entryPrice(): BigDecimal {
|
|
61
|
+
if (this.entryPriceBytes.length == 0) return BigDecimal.zero()
|
|
62
|
+
return Protobuf.decode<BigDecimal>(this.entryPriceBytes, BigDecimalPb.decode)
|
|
63
|
+
}
|
|
64
|
+
set entryPrice(value: BigDecimal) {
|
|
65
|
+
this.entryPriceBytes = Protobuf.encode(value, BigDecimalPb.encode)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Current mark/index price
|
|
70
|
+
* The current market price used for PnL calculations
|
|
71
|
+
*/
|
|
72
|
+
get markPrice(): BigDecimal {
|
|
73
|
+
if (this.markPriceBytes.length == 0) return BigDecimal.zero()
|
|
74
|
+
return Protobuf.decode<BigDecimal>(this.markPriceBytes, BigDecimalPb.decode)
|
|
75
|
+
}
|
|
76
|
+
set markPrice(value: BigDecimal) {
|
|
77
|
+
this.markPriceBytes = Protobuf.encode(value, BigDecimalPb.encode)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Position size in base token terms
|
|
82
|
+
* e.g., 0.5 ETH for a 0.5 ETH-USD perpetual position
|
|
83
|
+
*/
|
|
84
|
+
get baseSize(): BigDecimal {
|
|
85
|
+
if (this.baseSizeBytes.length == 0) return BigDecimal.zero()
|
|
86
|
+
return Protobuf.decode<BigDecimal>(this.baseSizeBytes, BigDecimalPb.decode)
|
|
87
|
+
}
|
|
88
|
+
set baseSize(value: BigDecimal) {
|
|
89
|
+
this.baseSizeBytes = Protobuf.encode(value, BigDecimalPb.encode)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Unrealized PnL in margin token
|
|
94
|
+
* Can be negative for losing positions
|
|
95
|
+
*/
|
|
96
|
+
get unrealizedPnl(): BigDecimal {
|
|
97
|
+
if (this.unrealizedPnlBytes.length == 0) return BigDecimal.zero()
|
|
98
|
+
return Protobuf.decode<BigDecimal>(this.unrealizedPnlBytes, BigDecimalPb.decode)
|
|
99
|
+
}
|
|
100
|
+
set unrealizedPnl(value: BigDecimal) {
|
|
101
|
+
this.unrealizedPnlBytes = Protobuf.encode(value, BigDecimalPb.encode)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Current equity
|
|
106
|
+
* margin + unrealizedPnl = currentEquity
|
|
107
|
+
*/
|
|
108
|
+
get currentEquity(): BigDecimal {
|
|
109
|
+
if (this.currentEquityBytes.length == 0) return BigDecimal.zero()
|
|
110
|
+
return Protobuf.decode<BigDecimal>(this.currentEquityBytes, BigDecimalPb.decode)
|
|
111
|
+
}
|
|
112
|
+
set currentEquity(value: BigDecimal) {
|
|
113
|
+
this.currentEquityBytes = Protobuf.encode(value, BigDecimalPb.encode)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Current leverage ratio
|
|
118
|
+
* Calculated as notional value / equity
|
|
119
|
+
*/
|
|
120
|
+
get leverage(): BigDecimal {
|
|
121
|
+
if (this.leverageBytes.length == 0) return BigDecimal.zero()
|
|
122
|
+
return Protobuf.decode<BigDecimal>(this.leverageBytes, BigDecimalPb.decode)
|
|
123
|
+
}
|
|
124
|
+
set leverage(value: BigDecimal) {
|
|
125
|
+
this.leverageBytes = Protobuf.encode(value, BigDecimalPb.encode)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// side is already enum type from proto, no wrapper needed
|
|
129
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Address, BigInt } from '@graphprotocol/graph-ts'
|
|
2
|
-
import { TokenBalancePb } from '../proto/activePositions/TokenBalancePb'
|
|
2
|
+
import { TokenBalancePb } from '../proto/activePositions/activePositions/TokenBalancePb'
|
|
3
3
|
|
|
4
4
|
export class TokenBalance extends TokenBalancePb {
|
|
5
5
|
get tokenAddress(): Address {
|
|
@@ -22,4 +22,35 @@ export class TokenBalance extends TokenBalancePb {
|
|
|
22
22
|
set tokenId(value: BigInt) {
|
|
23
23
|
this.tokenIdBytes = value
|
|
24
24
|
}
|
|
25
|
+
|
|
26
|
+
// NEW: Claimable amount for vesting positions
|
|
27
|
+
/**
|
|
28
|
+
* Claimable amount for vesting positions
|
|
29
|
+
* Returns null if not set (non-vesting position)
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* // For vesting positions with claimable tokens
|
|
34
|
+
* tokenBalance.claimableAmount = BigInt.fromString("500000000000000000000") // 500 tokens
|
|
35
|
+
*
|
|
36
|
+
* // Check if claimable
|
|
37
|
+
* const claimable = tokenBalance.claimableAmount
|
|
38
|
+
* if (claimable !== null) {
|
|
39
|
+
* // Has claimable amount
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
get claimableAmount(): BigInt | null {
|
|
44
|
+
if (this.claimableAmountBytes.length == 0) {
|
|
45
|
+
return null
|
|
46
|
+
}
|
|
47
|
+
return changetype<BigInt>(this.claimableAmountBytes)
|
|
48
|
+
}
|
|
49
|
+
set claimableAmount(value: BigInt | null) {
|
|
50
|
+
if (value === null) {
|
|
51
|
+
this.claimableAmountBytes = new Uint8Array(0)
|
|
52
|
+
} else {
|
|
53
|
+
this.claimableAmountBytes = value
|
|
54
|
+
}
|
|
55
|
+
}
|
|
25
56
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Protobuf } from 'as-proto/assembly'
|
|
2
2
|
import { watcher } from '../watcher/watcher'
|
|
3
3
|
import { ActivePositionsResult } from './ActivePositionsResult'
|
|
4
|
-
import { ActivePositionsResultPb } from '../proto/activePositions/ActivePositionsResultPb'
|
|
4
|
+
import { ActivePositionsResultPb } from '../proto/activePositions/activePositions/ActivePositionsResultPb'
|
|
5
5
|
|
|
6
6
|
export namespace activePositions {
|
|
7
7
|
export function inputPosition<T>(): T {
|
|
@@ -27,6 +27,13 @@ export namespace activePositions {
|
|
|
27
27
|
result.rewardTokens.some((token) =>
|
|
28
28
|
token.balanceBytes.some((byte) => byte != 0)
|
|
29
29
|
)
|
|
30
|
+
|
|
31
|
+
// NEW: Check for staked NFTs
|
|
32
|
+
isActive = isActive || result.supplyNfts.length > 0
|
|
33
|
+
|
|
34
|
+
// NEW: Check for perpetual position
|
|
35
|
+
isActive = isActive || result.perpPosition !== null
|
|
36
|
+
|
|
30
37
|
isActive = isActive && !forceInactive
|
|
31
38
|
|
|
32
39
|
const resultMsg = Protobuf.encode(
|
package/API/v1/index.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export * from './activePositions/activePositions'
|
|
2
2
|
export * from './activePositions/ActivePositionsResult'
|
|
3
|
+
export * from './activePositions/NftItem'
|
|
4
|
+
export * from './activePositions/PerpPosition'
|
|
3
5
|
export * from './store/store'
|
|
4
6
|
export * from './log/log'
|
|
5
7
|
export * from './ethereum/ethereum'
|
|
@@ -8,3 +10,7 @@ export * from './bigDecimal/bigDecimal'
|
|
|
8
10
|
export * from './typeConversion/typeConversion'
|
|
9
11
|
export * from './crypto/crypto'
|
|
10
12
|
export * from './watcher/watcher'
|
|
13
|
+
export * from './watcher/WatcherInput'
|
|
14
|
+
|
|
15
|
+
// Proto types (for advanced users)
|
|
16
|
+
export { WatcherInputPb } from './proto/watcher/WatcherInputPb'
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// Code generated by protoc-gen-as. DO NOT EDIT.
|
|
2
|
+
// Versions:
|
|
3
|
+
// protoc-gen-as v1.3.0
|
|
4
|
+
// protoc v6.33.4
|
|
5
|
+
|
|
6
|
+
import { Writer, Reader } from "as-proto/assembly";
|
|
7
|
+
import { TokenBalancePb } from "./TokenBalancePb";
|
|
8
|
+
import { Timestamp } from "../google/protobuf/Timestamp";
|
|
9
|
+
import { NftItemPb } from "./NftItemPb";
|
|
10
|
+
import { PerpPositionPb } from "./PerpPositionPb";
|
|
11
|
+
|
|
12
|
+
export class ActivePositionsResultPb {
|
|
13
|
+
static encode(message: ActivePositionsResultPb, writer: Writer): void {
|
|
14
|
+
const supplyTokens = message.supplyTokens;
|
|
15
|
+
for (let i: i32 = 0; i < supplyTokens.length; ++i) {
|
|
16
|
+
writer.uint32(10);
|
|
17
|
+
writer.fork();
|
|
18
|
+
TokenBalancePb.encode(supplyTokens[i], writer);
|
|
19
|
+
writer.ldelim();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const borrowTokens = message.borrowTokens;
|
|
23
|
+
for (let i: i32 = 0; i < borrowTokens.length; ++i) {
|
|
24
|
+
writer.uint32(18);
|
|
25
|
+
writer.fork();
|
|
26
|
+
TokenBalancePb.encode(borrowTokens[i], writer);
|
|
27
|
+
writer.ldelim();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const rewardTokens = message.rewardTokens;
|
|
31
|
+
for (let i: i32 = 0; i < rewardTokens.length; ++i) {
|
|
32
|
+
writer.uint32(26);
|
|
33
|
+
writer.fork();
|
|
34
|
+
TokenBalancePb.encode(rewardTokens[i], writer);
|
|
35
|
+
writer.ldelim();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const positionToken = message.positionToken;
|
|
39
|
+
if (positionToken !== null) {
|
|
40
|
+
writer.uint32(34);
|
|
41
|
+
writer.fork();
|
|
42
|
+
TokenBalancePb.encode(positionToken, writer);
|
|
43
|
+
writer.ldelim();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const unlockTimestamp = message.unlockTimestamp;
|
|
47
|
+
if (unlockTimestamp !== null) {
|
|
48
|
+
writer.uint32(42);
|
|
49
|
+
writer.fork();
|
|
50
|
+
Timestamp.encode(unlockTimestamp, writer);
|
|
51
|
+
writer.ldelim();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
writer.uint32(50);
|
|
55
|
+
writer.bytes(message.poolAddressBytes);
|
|
56
|
+
|
|
57
|
+
const supplyNfts = message.supplyNfts;
|
|
58
|
+
for (let i: i32 = 0; i < supplyNfts.length; ++i) {
|
|
59
|
+
writer.uint32(58);
|
|
60
|
+
writer.fork();
|
|
61
|
+
NftItemPb.encode(supplyNfts[i], writer);
|
|
62
|
+
writer.ldelim();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const perpPosition = message.perpPosition;
|
|
66
|
+
if (perpPosition !== null) {
|
|
67
|
+
writer.uint32(66);
|
|
68
|
+
writer.fork();
|
|
69
|
+
PerpPositionPb.encode(perpPosition, writer);
|
|
70
|
+
writer.ldelim();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
static decode(reader: Reader, length: i32): ActivePositionsResultPb {
|
|
75
|
+
const end: usize = length < 0 ? reader.end : reader.ptr + length;
|
|
76
|
+
const message = new ActivePositionsResultPb();
|
|
77
|
+
|
|
78
|
+
while (reader.ptr < end) {
|
|
79
|
+
const tag = reader.uint32();
|
|
80
|
+
switch (tag >>> 3) {
|
|
81
|
+
case 1:
|
|
82
|
+
message.supplyTokens.push(
|
|
83
|
+
TokenBalancePb.decode(reader, reader.uint32())
|
|
84
|
+
);
|
|
85
|
+
break;
|
|
86
|
+
|
|
87
|
+
case 2:
|
|
88
|
+
message.borrowTokens.push(
|
|
89
|
+
TokenBalancePb.decode(reader, reader.uint32())
|
|
90
|
+
);
|
|
91
|
+
break;
|
|
92
|
+
|
|
93
|
+
case 3:
|
|
94
|
+
message.rewardTokens.push(
|
|
95
|
+
TokenBalancePb.decode(reader, reader.uint32())
|
|
96
|
+
);
|
|
97
|
+
break;
|
|
98
|
+
|
|
99
|
+
case 4:
|
|
100
|
+
message.positionToken = TokenBalancePb.decode(
|
|
101
|
+
reader,
|
|
102
|
+
reader.uint32()
|
|
103
|
+
);
|
|
104
|
+
break;
|
|
105
|
+
|
|
106
|
+
case 5:
|
|
107
|
+
message.unlockTimestamp = Timestamp.decode(reader, reader.uint32());
|
|
108
|
+
break;
|
|
109
|
+
|
|
110
|
+
case 6:
|
|
111
|
+
message.poolAddressBytes = reader.bytes();
|
|
112
|
+
break;
|
|
113
|
+
|
|
114
|
+
case 7:
|
|
115
|
+
message.supplyNfts.push(NftItemPb.decode(reader, reader.uint32()));
|
|
116
|
+
break;
|
|
117
|
+
|
|
118
|
+
case 8:
|
|
119
|
+
message.perpPosition = PerpPositionPb.decode(reader, reader.uint32());
|
|
120
|
+
break;
|
|
121
|
+
|
|
122
|
+
default:
|
|
123
|
+
reader.skipType(tag & 7);
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return message;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
supplyTokens: Array<TokenBalancePb>;
|
|
132
|
+
borrowTokens: Array<TokenBalancePb>;
|
|
133
|
+
rewardTokens: Array<TokenBalancePb>;
|
|
134
|
+
positionToken: TokenBalancePb | null;
|
|
135
|
+
unlockTimestamp: Timestamp | null;
|
|
136
|
+
poolAddressBytes: Uint8Array;
|
|
137
|
+
supplyNfts: Array<NftItemPb>;
|
|
138
|
+
perpPosition: PerpPositionPb | null;
|
|
139
|
+
|
|
140
|
+
constructor(
|
|
141
|
+
supplyTokens: Array<TokenBalancePb> = [],
|
|
142
|
+
borrowTokens: Array<TokenBalancePb> = [],
|
|
143
|
+
rewardTokens: Array<TokenBalancePb> = [],
|
|
144
|
+
positionToken: TokenBalancePb | null = null,
|
|
145
|
+
unlockTimestamp: Timestamp | null = null,
|
|
146
|
+
poolAddressBytes: Uint8Array = new Uint8Array(0),
|
|
147
|
+
supplyNfts: Array<NftItemPb> = [],
|
|
148
|
+
perpPosition: PerpPositionPb | null = null
|
|
149
|
+
) {
|
|
150
|
+
this.supplyTokens = supplyTokens;
|
|
151
|
+
this.borrowTokens = borrowTokens;
|
|
152
|
+
this.rewardTokens = rewardTokens;
|
|
153
|
+
this.positionToken = positionToken;
|
|
154
|
+
this.unlockTimestamp = unlockTimestamp;
|
|
155
|
+
this.poolAddressBytes = poolAddressBytes;
|
|
156
|
+
this.supplyNfts = supplyNfts;
|
|
157
|
+
this.perpPosition = perpPosition;
|
|
158
|
+
}
|
|
159
|
+
}
|