@mimicprotocol/lib-ts 0.0.1-rc.9 → 0.0.1

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.
Files changed (77) hide show
  1. package/CHANGELOG.md +134 -0
  2. package/README.md +7 -7
  3. package/constants.d.ts +1 -0
  4. package/constants.js +1 -0
  5. package/index.ts +3 -0
  6. package/package.json +18 -4
  7. package/src/chains/Arbitrum.ts +14 -0
  8. package/src/chains/Avalanche.ts +15 -0
  9. package/src/chains/BNB.ts +15 -0
  10. package/src/chains/Base.ts +14 -0
  11. package/src/chains/BaseSepolia.ts +7 -0
  12. package/src/chains/Ethereum.ts +7 -7
  13. package/src/chains/Gnosis.ts +14 -0
  14. package/src/chains/Optimism.ts +7 -7
  15. package/src/chains/Polygon.ts +10 -7
  16. package/src/chains/Sonic.ts +13 -0
  17. package/src/chains/index.ts +7 -0
  18. package/src/context/Context.ts +102 -7
  19. package/src/environment.ts +165 -53
  20. package/src/evm.ts +5 -4
  21. package/src/helpers/BorshDeserializer.ts +133 -0
  22. package/src/helpers/consensus.ts +35 -0
  23. package/src/helpers/constants.ts +7 -1
  24. package/src/helpers/index.ts +5 -0
  25. package/src/helpers/math.ts +20 -0
  26. package/src/helpers/serialize.ts +5 -125
  27. package/src/helpers/strings.ts +82 -5
  28. package/src/intents/Call/EvmCall.ts +283 -0
  29. package/src/intents/Call/SvmCall.ts +278 -0
  30. package/src/intents/Call/index.ts +2 -0
  31. package/src/intents/Intent.ts +178 -5
  32. package/src/intents/Swap.ts +136 -44
  33. package/src/intents/Transfer.ts +103 -58
  34. package/src/log.ts +83 -0
  35. package/src/queries/EvmCallQuery.ts +43 -0
  36. package/src/queries/QueryResponse.ts +26 -0
  37. package/src/queries/RelevantTokensQuery.ts +82 -0
  38. package/src/queries/SubgraphQuery.ts +50 -0
  39. package/src/queries/SvmAccountsInfoQuery.ts +65 -0
  40. package/src/queries/TokenPriceQuery.ts +47 -0
  41. package/src/queries/index.ts +6 -1
  42. package/src/storage/index.ts +1 -0
  43. package/src/storage/storage.ts +46 -0
  44. package/src/svm.ts +27 -0
  45. package/src/tokens/BlockchainToken.ts +108 -0
  46. package/src/tokens/DenominationToken.ts +70 -0
  47. package/src/tokens/ERC20Token.ts +192 -0
  48. package/src/tokens/SPLToken.ts +162 -0
  49. package/src/tokens/Token.ts +55 -155
  50. package/src/tokens/TokenAmount.ts +72 -30
  51. package/src/tokens/TokenProvider.ts +54 -0
  52. package/src/tokens/Tokens.ts +186 -0
  53. package/src/tokens/USD.ts +9 -6
  54. package/src/tokens/index.ts +6 -0
  55. package/src/types/Address.ts +86 -14
  56. package/src/types/BigInt.ts +14 -22
  57. package/src/types/ByteArray.ts +41 -3
  58. package/src/types/Bytes.ts +7 -0
  59. package/src/types/ChainId.ts +9 -1
  60. package/src/types/Option.ts +35 -0
  61. package/src/types/Result.ts +68 -0
  62. package/src/types/TriggerType.ts +4 -0
  63. package/src/types/evm/EvmDecodeParam.ts +7 -0
  64. package/src/types/evm/EvmEncodeParam.ts +31 -0
  65. package/src/types/evm/index.ts +2 -0
  66. package/src/types/index.ts +8 -2
  67. package/src/types/svm/SvmAccountInfo.ts +32 -0
  68. package/src/types/svm/SvmAccountMeta.ts +28 -0
  69. package/src/types/svm/SvmFindProgramAddress.ts +32 -0
  70. package/src/types/svm/SvmMint.ts +44 -0
  71. package/src/types/svm/SvmPdaSeed.ts +19 -0
  72. package/src/types/svm/SvmTokenMetadataData.ts +29 -0
  73. package/src/types/svm/index.ts +5 -0
  74. package/src/intents/Call.ts +0 -238
  75. package/src/queries/Call.ts +0 -16
  76. package/src/types/EvmDecodeParam.ts +0 -30
  77. package/src/types/EvmEncodeParam.ts +0 -54
@@ -1,17 +1,16 @@
1
1
  import { environment } from '../environment'
2
- import { Token, TokenAmount } from '../tokens'
3
- import { Address, BigInt, ChainId } from '../types'
2
+ import { ERC20Token, Token, TokenAmount } from '../tokens'
3
+ import { Address, BigInt, Bytes, ChainId } from '../types'
4
4
 
5
- import { Intent, IntentBuilder, OperationType } from './Intent'
5
+ import { Intent, IntentBuilder, IntentEvent, MaxFee, OperationType } from './Intent'
6
6
 
7
7
  /**
8
8
  * Builder for creating Transfer intents with token transfer operations.
9
9
  * Supports multiple transfers within a single transaction on the same chain.
10
10
  */
11
11
  export class TransferBuilder extends IntentBuilder {
12
- private chainId: ChainId
13
- private transfers: TransferData[] = []
14
- private fee: TokenAmount | null = null
12
+ protected chainId: ChainId
13
+ protected transfers: TransferData[] = []
15
14
 
16
15
  /**
17
16
  * Creates a TransferBuilder for a specific chain.
@@ -22,23 +21,11 @@ export class TransferBuilder extends IntentBuilder {
22
21
  return new TransferBuilder(chainId)
23
22
  }
24
23
 
25
- /**
26
- * Creates a TransferBuilder for a specific chain with a pre-configured fee.
27
- * @param chainId - The blockchain network identifier
28
- * @param fee - The fee token amount for the transfer
29
- * @returns A new TransferBuilder instance with fee set
30
- */
31
- static forChainWithFee(chainId: ChainId, fee: TokenAmount): TransferBuilder {
32
- const builder = new TransferBuilder(chainId)
33
- builder.addFee(fee)
34
- return builder
35
- }
36
-
37
24
  /**
38
25
  * Creates a new TransferBuilder instance.
39
26
  * @param chainId - The blockchain network identifier
40
27
  */
41
- constructor(chainId: ChainId) {
28
+ private constructor(chainId: ChainId) {
42
29
  super()
43
30
  this.chainId = chainId
44
31
  }
@@ -63,6 +50,33 @@ export class TransferBuilder extends IntentBuilder {
63
50
  return this
64
51
  }
65
52
 
53
+ /**
54
+ * Adds the transfers from another TransferBuilder to this TransferBuilder.
55
+ * @param builder - The TransferBuilder to add the transfers from
56
+ * @returns This TransferBuilder instance for method chaining
57
+ */
58
+ addTransfersFromBuilder(builder: TransferBuilder): TransferBuilder {
59
+ return this.addTransfers(builder.getTransfers())
60
+ }
61
+
62
+ /**
63
+ * Adds the transfers from multiple TransferBuilders to this TransferBuilder.
64
+ * @param builders - The TransferBuilders to add the transfers from
65
+ * @returns This TransferBuilder instance for method chaining
66
+ */
67
+ addTransfersFromBuilders(builders: TransferBuilder[]): TransferBuilder {
68
+ for (let i = 0; i < builders.length; i++) this.addTransfersFromBuilder(builders[i])
69
+ return this
70
+ }
71
+
72
+ /**
73
+ * Returns a copy of the transfers array.
74
+ * @returns A copy of the transfers array
75
+ */
76
+ getTransfers(): TransferData[] {
77
+ return this.transfers.slice(0)
78
+ }
79
+
66
80
  /**
67
81
  * Adds a transfer from a TokenAmount.
68
82
  * @param tokenAmount - The token amount to transfer (must be on same chain)
@@ -70,7 +84,7 @@ export class TransferBuilder extends IntentBuilder {
70
84
  * @returns This TransferBuilder instance for method chaining
71
85
  */
72
86
  addTransferFromTokenAmount(tokenAmount: TokenAmount, recipient: Address): TransferBuilder {
73
- if (tokenAmount.token.chainId !== this.chainId) throw new Error('Transfer tokens must be on the same chain')
87
+ if (!tokenAmount.token.hasChain(this.chainId)) throw new Error('Transfer tokens must be on the same chain')
74
88
  return this.addTransfer(TransferData.fromTokenAmount(tokenAmount, recipient))
75
89
  }
76
90
 
@@ -82,7 +96,7 @@ export class TransferBuilder extends IntentBuilder {
82
96
  * @returns This TransferBuilder instance for method chaining
83
97
  */
84
98
  addTransferFromI32(token: Token, amount: i32, recipient: Address): TransferBuilder {
85
- if (token.chainId !== this.chainId) throw new Error('Transfer tokens must be on the same chain')
99
+ if (!token.hasChain(this.chainId)) throw new Error('Transfer tokens must be on the same chain')
86
100
  return this.addTransfer(TransferData.fromI32(token, amount, recipient))
87
101
  }
88
102
 
@@ -94,7 +108,7 @@ export class TransferBuilder extends IntentBuilder {
94
108
  * @returns This TransferBuilder instance for method chaining
95
109
  */
96
110
  addTransferFromBigInt(token: Token, amount: BigInt, recipient: Address): TransferBuilder {
97
- if (token.chainId !== this.chainId) throw new Error('Transfer tokens must be on the same chain')
111
+ if (!token.hasChain(this.chainId)) throw new Error('Transfer tokens must be on the same chain')
98
112
  return this.addTransfer(TransferData.fromBigInt(token, amount, recipient))
99
113
  }
100
114
 
@@ -106,7 +120,7 @@ export class TransferBuilder extends IntentBuilder {
106
120
  * @returns This TransferBuilder instance for method chaining
107
121
  */
108
122
  addTransferFromStringDecimal(token: Token, amount: string, recipient: Address): TransferBuilder {
109
- if (token.chainId !== this.chainId) throw new Error('Transfer tokens must be on the same chain')
123
+ if (!token.hasChain(this.chainId)) throw new Error('Transfer tokens must be on the same chain')
110
124
  return this.addTransfer(TransferData.fromStringDecimal(token, amount, recipient))
111
125
  }
112
126
 
@@ -121,17 +135,6 @@ export class TransferBuilder extends IntentBuilder {
121
135
  return this
122
136
  }
123
137
 
124
- /**
125
- * Sets the fee for this transfer intent.
126
- * @param fee - The fee token amount (must be on same chain)
127
- * @returns This TransferBuilder instance for method chaining
128
- */
129
- addFee(fee: TokenAmount): TransferBuilder {
130
- if (fee.token.chainId !== this.chainId) throw new Error('Fee token must be on the same chain')
131
- this.fee = fee
132
- return this
133
- }
134
-
135
138
  /**
136
139
  * Sets the settler address for this intent.
137
140
  * @param settler - The settler address as an Address instance
@@ -143,7 +146,7 @@ export class TransferBuilder extends IntentBuilder {
143
146
 
144
147
  /**
145
148
  * Sets the settler address from a string.
146
- * @param settler - The settler address as a hex string
149
+ * @param settler - The settler address as a hex or base58 string accordingly
147
150
  * @returns This TransferBuilder instance for method chaining
148
151
  */
149
152
  addSettlerAsString(settler: string): TransferBuilder {
@@ -186,20 +189,50 @@ export class TransferBuilder extends IntentBuilder {
186
189
  return changetype<TransferBuilder>(super.addNonce(nonce))
187
190
  }
188
191
 
192
+ /**
193
+ * Adds a max fee for this intent.
194
+ * @param fee - The max fee token amount (must be on same chain)
195
+ * @returns This TransferBuilder instance for method chaining
196
+ */
197
+ addMaxFee(fee: TokenAmount): TransferBuilder {
198
+ if (!fee.token.hasChain(this.chainId)) throw new Error('Fee token must be on the same chain')
199
+ this.maxFees.push(fee)
200
+ return this
201
+ }
202
+
203
+ /**
204
+ * Sets an event for the intent.
205
+ * @param topic - The topic to be indexed in the event
206
+ * @param data - The event data
207
+ * @returns This TransferBuilder instance for method chaining
208
+ */
209
+ addEvent(topic: Bytes, data: Bytes): TransferBuilder {
210
+ return changetype<TransferBuilder>(super.addEvent(topic, data))
211
+ }
212
+
213
+ /**
214
+ * Sets multiple events for the intent.
215
+ * @param events - The list of events to be added
216
+ * @returns This TransferBuilder instance for method chaining
217
+ */
218
+ addEvents(events: IntentEvent[]): TransferBuilder {
219
+ return changetype<TransferBuilder>(super.addEvents(events))
220
+ }
221
+
189
222
  /**
190
223
  * Builds and returns the final Transfer intent.
191
224
  * @returns A new Transfer instance with all configured parameters
192
225
  */
193
226
  build(): Transfer {
194
- if (!this.fee) throw new Error('Transfer fee must be specified')
195
227
  return new Transfer(
196
228
  this.chainId,
197
229
  this.transfers,
198
- this.fee as TokenAmount,
230
+ this.maxFees,
199
231
  this.settler,
200
232
  this.user,
201
233
  this.deadline,
202
- this.nonce
234
+ this.nonce,
235
+ this.events
203
236
  )
204
237
  }
205
238
  }
@@ -277,16 +310,13 @@ export class TransferData {
277
310
  export class Transfer extends Intent {
278
311
  public chainId: ChainId
279
312
  public transfers: TransferData[]
280
- public feeToken: string
281
- public feeAmount: string
282
313
 
283
314
  /**
284
315
  * Creates a simple single-token transfer intent.
285
- * @param chainId - The blockchain network identifier
286
- * @param token - The token address to transfer
316
+ * @param token - The Token to transfer
287
317
  * @param amount - The amount to transfer
288
318
  * @param recipient - The address to receive the tokens
289
- * @param fee - The fee amount for the transfer
319
+ * @param maxFee - The max fee to pay for the transfer intent
290
320
  * @param settler - The settler address (optional)
291
321
  * @param user - The user address (optional)
292
322
  * @param deadline - The deadline timestamp (optional)
@@ -294,28 +324,36 @@ export class Transfer extends Intent {
294
324
  * @returns A new Transfer instance
295
325
  */
296
326
  static create(
297
- chainId: ChainId,
298
- token: Address,
327
+ token: Token,
299
328
  amount: BigInt,
300
329
  recipient: Address,
301
- fee: BigInt,
330
+ maxFee: BigInt,
302
331
  settler: Address | null = null,
303
332
  user: Address | null = null,
304
333
  deadline: BigInt | null = null,
305
- nonce: string | null = null
334
+ nonce: string | null = null,
335
+ events: IntentEvent[] | null = null
306
336
  ): Transfer {
307
- const transferToken = Token.fromAddress(token, chainId)
308
- const transferAmount = TokenAmount.fromBigInt(transferToken, amount)
337
+ const transferAmount = TokenAmount.fromBigInt(token, amount)
309
338
  const transferData = TransferData.fromTokenAmount(transferAmount, recipient)
310
- const feeAmount = TokenAmount.fromBigInt(transferToken, fee)
311
- return new Transfer(chainId, [transferData], feeAmount, settler, user, deadline, nonce)
339
+ const maxFees = [TokenAmount.fromBigInt(token, maxFee)]
340
+ return new Transfer(
341
+ token instanceof ERC20Token ? (token as ERC20Token).chainId : ChainId.SOLANA_MAINNET,
342
+ [transferData],
343
+ maxFees,
344
+ settler,
345
+ user,
346
+ deadline,
347
+ nonce,
348
+ events
349
+ )
312
350
  }
313
351
 
314
352
  /**
315
353
  * Creates a new Transfer intent.
316
354
  * @param chainId - The blockchain network identifier
317
355
  * @param transfers - Array of transfer data configurations
318
- * @param fee - The fee token amount for the transfers
356
+ * @param maxFees - The list of max fees to pay for the transfer intent
319
357
  * @param settler - The settler address (optional)
320
358
  * @param user - The user address (optional)
321
359
  * @param deadline - The deadline timestamp (optional)
@@ -324,19 +362,19 @@ export class Transfer extends Intent {
324
362
  constructor(
325
363
  chainId: ChainId,
326
364
  transfers: TransferData[],
327
- fee: TokenAmount,
365
+ maxFees: TokenAmount[],
328
366
  settler: Address | null = null,
329
367
  user: Address | null = null,
330
368
  deadline: BigInt | null = null,
331
- nonce: string | null = null
369
+ nonce: string | null = null,
370
+ events: IntentEvent[] | null = null
332
371
  ) {
333
- super(OperationType.Transfer, settler, user, deadline, nonce)
334
-
372
+ const fees: MaxFee[] = maxFees.map((fee: TokenAmount) => MaxFee.fromTokenAmount(fee))
373
+ super(OperationType.Transfer, chainId, fees, settler, user, deadline, nonce, events)
335
374
  if (transfers.length === 0) throw new Error('Transfer list cannot be empty')
375
+ if (maxFees.length == 0) throw new Error('At least a max fee must be specified')
336
376
 
337
377
  this.transfers = transfers
338
- this.feeToken = fee.token.address.toString()
339
- this.feeAmount = fee.amount.toString()
340
378
  this.chainId = chainId
341
379
  }
342
380
 
@@ -346,4 +384,11 @@ export class Transfer extends Intent {
346
384
  send(): void {
347
385
  environment.transfer(this)
348
386
  }
387
+
388
+ /**
389
+ * Whether the chainId is Solana or not
390
+ */
391
+ isSVM(): bool {
392
+ return this.chainId === ChainId.SOLANA_MAINNET
393
+ }
349
394
  }
package/src/log.ts ADDED
@@ -0,0 +1,83 @@
1
+ // eslint-disable-next-line no-secrets/no-secrets
2
+ // This file is based on code from "The Graph Tooling" (https://github.com/graphprotocol/graph-tooling/tree/7faa3098b2e6c61f09fc81b8b2d333e66b0080d1).
3
+ // Licensed under the MIT License.
4
+ // Copyright (c) 2018 Graph Protocol, Inc. and contributors.
5
+ // Modified by Mimic Protocol, 2025.
6
+
7
+ import { Stringable } from './helpers'
8
+
9
+ export namespace log {
10
+ @external('log', '_log')
11
+ declare function _log(level: Level, msg: string): void
12
+
13
+ export enum Level {
14
+ CRITICAL = 0,
15
+ ERROR = 1,
16
+ WARNING = 2,
17
+ INFO = 3,
18
+ DEBUG = 4,
19
+ }
20
+
21
+ /**
22
+ * Logs a critical message that terminates the execution.
23
+ *
24
+ * @param msg Format string like "Value = {}, other = {}".
25
+ * @param args Format string arguments.
26
+ */
27
+ export function critical<T extends Stringable>(msg: string, args: Array<T> = []): void {
28
+ _log(Level.CRITICAL, format(msg, args))
29
+ }
30
+
31
+ /**
32
+ * Logs an error message.
33
+ *
34
+ * @param msg Format string like "Value = {}, other = {}".
35
+ * @param args Format string arguments.
36
+ */
37
+ export function error<T extends Stringable>(msg: string, args: Array<T> = []): void {
38
+ _log(Level.ERROR, format(msg, args))
39
+ }
40
+
41
+ /** Logs a warning message.
42
+ *
43
+ * @param msg Format string like "Value = {}, other = {}".
44
+ * @param args Format string arguments.
45
+ */
46
+ export function warning<T extends Stringable>(msg: string, args: Array<T> = []): void {
47
+ _log(Level.WARNING, format(msg, args))
48
+ }
49
+
50
+ /** Logs an info message.
51
+ *
52
+ * @param msg Format string like "Value = {}, other = {}".
53
+ * @param args Format string arguments.
54
+ */
55
+ export function info<T extends Stringable>(msg: string, args: Array<T> = []): void {
56
+ _log(Level.INFO, format(msg, args))
57
+ }
58
+
59
+ /** Logs a debug message.
60
+ *
61
+ * @param msg Format string like "Value = {}, other = {}".
62
+ * @param args Format string arguments.
63
+ */
64
+ export function debug<T extends Stringable>(msg: string, args: Array<T> = []): void {
65
+ _log(Level.DEBUG, format(msg, args))
66
+ }
67
+ }
68
+
69
+ function format<T extends Stringable>(fmt: string, args: Array<T>): string {
70
+ let out = ''
71
+ let argIndex = 0
72
+ const argsStr = args.map<string>((a) => a.toString())
73
+ for (let i: i32 = 0, len: i32 = fmt.length; i < len; i++) {
74
+ if (i < len - 1 && fmt.charCodeAt(i) == 0x7b /* '{' */ && fmt.charCodeAt(i + 1) == 0x7d /* '}' */) {
75
+ if (argIndex >= argsStr.length) throw new Error('Too few arguments for format string: ' + fmt)
76
+ out += argsStr[argIndex++]
77
+ i++
78
+ } else {
79
+ out += fmt.charAt(i)
80
+ }
81
+ }
82
+ return out
83
+ }
@@ -0,0 +1,43 @@
1
+ import { Address, ChainId, Result } from '../types'
2
+
3
+ import { QueryResponseBase } from './QueryResponse'
4
+
5
+ @json
6
+ class EvmCallQueryBase {
7
+ constructor(
8
+ public readonly to: string,
9
+ public readonly chainId: ChainId,
10
+ public readonly data: string
11
+ ) {}
12
+ }
13
+
14
+ @json
15
+ export class EvmCallQuery extends EvmCallQueryBase {
16
+ public readonly timestamp: i64
17
+
18
+ constructor(to: string, chainId: ChainId, timestamp: i64, data: string) {
19
+ super(to, chainId, data)
20
+ this.timestamp = timestamp
21
+ }
22
+
23
+ static from(to: Address, chainId: ChainId, timestamp: Date | null, data: string): EvmCallQueryBase {
24
+ const address = to.toString()
25
+ return timestamp
26
+ ? new EvmCallQuery(address, chainId, changetype<Date>(timestamp).getTime(), data)
27
+ : new EvmCallQueryBase(address, chainId, data)
28
+ }
29
+ }
30
+
31
+ @json
32
+ export class EvmCallQueryResponse extends QueryResponseBase {
33
+ public data: string
34
+
35
+ constructor(success: string, data: string, error: string) {
36
+ super(success, error)
37
+ this.data = data
38
+ }
39
+
40
+ toResult(): Result<string, string> {
41
+ return this.buildResult<string>(this.data, 'Unknown error getting evm call')
42
+ }
43
+ }
@@ -0,0 +1,26 @@
1
+ import { JSON } from 'json-as'
2
+
3
+ import { replaceJsonBooleans } from '../helpers'
4
+ import { Result } from '../types'
5
+
6
+ @json
7
+ export class QueryResponseBase {
8
+ constructor(
9
+ public success: string, // boolean as string due to json-as bug
10
+ public error: string
11
+ ) {}
12
+
13
+ static fromJson<T extends QueryResponseBase>(json: string): T {
14
+ return JSON.parse<T>(replaceJsonBooleans(json))
15
+ }
16
+
17
+ protected buildResult<TData, TResult = TData>(
18
+ data: TData,
19
+ defaultError: string,
20
+ transform: (data: TData) => TResult = (data: TData) => changetype<TResult>(data)
21
+ ): Result<TResult, string> {
22
+ if (this.success !== 'true') return Result.err<TResult, string>(this.error.length > 0 ? this.error : defaultError)
23
+
24
+ return Result.ok<TResult, string>(transform(data))
25
+ }
26
+ }
@@ -0,0 +1,82 @@
1
+ import { ListType } from '../helpers'
2
+ import { BlockchainToken, TokenAmount, USD } from '../tokens'
3
+ import { Address, BigInt, ChainId, Result } from '../types'
4
+
5
+ import { QueryResponseBase } from './QueryResponse'
6
+
7
+ @json
8
+ export class TokenQuery {
9
+ constructor(
10
+ public address: string,
11
+ public chainId: i32
12
+ ) {}
13
+
14
+ static fromToken(token: BlockchainToken): TokenQuery {
15
+ return new TokenQuery(token.address.toString(), token.chainId)
16
+ }
17
+ }
18
+
19
+ @json
20
+ export class RelevantTokensQuery {
21
+ constructor(
22
+ public readonly owner: string,
23
+ public readonly chainIds: ChainId[],
24
+ public readonly usdMinAmount: string,
25
+ public readonly tokens: TokenQuery[],
26
+ public readonly tokenFilter: ListType
27
+ ) {}
28
+
29
+ static init(
30
+ owner: Address,
31
+ chainIds: ChainId[],
32
+ usdMinAmount: USD,
33
+ tokens: BlockchainToken[],
34
+ tokenFilter: ListType
35
+ ): RelevantTokensQuery {
36
+ const ownerStr = owner.toString()
37
+ const usdMinAmountStr = usdMinAmount.value.toString()
38
+ const tokensQueries = tokens.map<TokenQuery>((token) => TokenQuery.fromToken(token))
39
+ return new RelevantTokensQuery(ownerStr, chainIds, usdMinAmountStr, tokensQueries, tokenFilter)
40
+ }
41
+ }
42
+
43
+ @json
44
+ export class TokenBalanceQuery {
45
+ constructor(
46
+ public token: TokenQuery,
47
+ public balance: string
48
+ ) {}
49
+
50
+ toTokenAmount(): TokenAmount {
51
+ return TokenAmount.fromBigInt(
52
+ BlockchainToken.fromString(this.token.address, this.token.chainId),
53
+ BigInt.fromString(this.balance)
54
+ )
55
+ }
56
+ }
57
+
58
+ @json
59
+ export class RelevantTokensQueryResult {
60
+ constructor(
61
+ public timestamp: i64,
62
+ public balances: TokenBalanceQuery[]
63
+ ) {}
64
+ }
65
+
66
+ @json
67
+ export class RelevantTokensQueryResponse extends QueryResponseBase {
68
+ public data: RelevantTokensQueryResult[]
69
+
70
+ constructor(success: string, data: RelevantTokensQueryResult[], error: string) {
71
+ super(success, error)
72
+ this.data = data
73
+ }
74
+
75
+ toResult(): Result<TokenBalanceQuery[][], string> {
76
+ return this.buildResult<RelevantTokensQueryResult[], TokenBalanceQuery[][]>(
77
+ this.data,
78
+ 'Unknown error getting relevant tokens',
79
+ (data) => data.map<TokenBalanceQuery[]>((response: RelevantTokensQueryResult) => response.balances)
80
+ )
81
+ }
82
+ }
@@ -0,0 +1,50 @@
1
+ import { ChainId, Result } from '../types'
2
+
3
+ import { QueryResponseBase } from './QueryResponse'
4
+
5
+ @json
6
+ class SubgraphQueryBase {
7
+ constructor(
8
+ public readonly chainId: ChainId,
9
+ public readonly subgraphId: string,
10
+ public readonly query: string
11
+ ) {}
12
+ }
13
+
14
+ @json
15
+ export class SubgraphQuery extends SubgraphQueryBase {
16
+ public readonly timestamp: i64
17
+
18
+ constructor(chainId: ChainId, timestamp: i64, subgraphId: string, query: string) {
19
+ super(chainId, subgraphId, query)
20
+ this.timestamp = timestamp
21
+ }
22
+
23
+ static from(chainId: ChainId, subgraphId: string, query: string, timestamp: Date | null): SubgraphQueryBase {
24
+ return timestamp
25
+ ? new SubgraphQuery(chainId, changetype<Date>(timestamp).getTime(), subgraphId, query)
26
+ : new SubgraphQueryBase(chainId, subgraphId, query)
27
+ }
28
+ }
29
+
30
+ @json
31
+ export class SubgraphQueryResult {
32
+ constructor(
33
+ public blockNumber: i64,
34
+ public data: string
35
+ ) {}
36
+ }
37
+
38
+ @json
39
+ export class SubgraphQueryResponse extends QueryResponseBase {
40
+ public data: SubgraphQueryResult
41
+
42
+ constructor(success: string, data: SubgraphQueryResult, error: string) {
43
+ super(success, error)
44
+ this.data = data
45
+ }
46
+
47
+ toResult(): Result<SubgraphQueryResult, string> {
48
+ return this.buildResult<SubgraphQueryResult>(this.data, 'Unknown error getting subgraph query')
49
+ }
50
+ }
@@ -0,0 +1,65 @@
1
+ import { Address, Result, SerializableSvmAccountInfo, SvmAccountInfo } from '../types'
2
+
3
+ import { QueryResponseBase } from './QueryResponse'
4
+
5
+ @json
6
+ class SvmAccountsInfoQueryBase {
7
+ constructor(public readonly publicKeys: string[]) {}
8
+ }
9
+
10
+ @json
11
+ export class SvmAccountsInfoQuery extends SvmAccountsInfoQueryBase {
12
+ public readonly timestamp: i64
13
+
14
+ constructor(publicKeys: string[], timestamp: i64) {
15
+ super(publicKeys)
16
+ this.timestamp = timestamp
17
+ }
18
+
19
+ static from(publicKeys: Address[], timestamp: Date | null): SvmAccountsInfoQueryBase {
20
+ const strPublicKeys = publicKeys.map((pk: Address) => pk.toString())
21
+ return timestamp
22
+ ? new SvmAccountsInfoQuery(strPublicKeys, changetype<Date>(timestamp).getTime())
23
+ : new SvmAccountsInfoQueryBase(strPublicKeys)
24
+ }
25
+ }
26
+
27
+ @json
28
+ export class SerializableSvmAccountsInfoQueryResult {
29
+ constructor(
30
+ public accountsInfo: SerializableSvmAccountInfo[],
31
+ public slot: string
32
+ ) {}
33
+ }
34
+
35
+ export class SvmAccountsInfoQueryResult {
36
+ constructor(
37
+ public accountsInfo: SvmAccountInfo[],
38
+ public slot: string
39
+ ) {}
40
+
41
+ static fromSerializable(serializable: SerializableSvmAccountsInfoQueryResult): SvmAccountsInfoQueryResult {
42
+ return new SvmAccountsInfoQueryResult(
43
+ serializable.accountsInfo.map((acc: SerializableSvmAccountInfo) => SvmAccountInfo.fromSerializable(acc)),
44
+ serializable.slot
45
+ )
46
+ }
47
+ }
48
+
49
+ @json
50
+ export class SvmAccountsInfoQueryResponse extends QueryResponseBase {
51
+ public data: SerializableSvmAccountsInfoQueryResult
52
+
53
+ constructor(success: string, data: SerializableSvmAccountsInfoQueryResult, error: string) {
54
+ super(success, error)
55
+ this.data = data
56
+ }
57
+
58
+ toResult(): Result<SvmAccountsInfoQueryResult, string> {
59
+ return this.buildResult<SerializableSvmAccountsInfoQueryResult, SvmAccountsInfoQueryResult>(
60
+ this.data,
61
+ 'Unknown error getting SVM accounts info',
62
+ (data) => SvmAccountsInfoQueryResult.fromSerializable(data)
63
+ )
64
+ }
65
+ }
@@ -0,0 +1,47 @@
1
+ import { BlockchainToken, USD } from '../tokens'
2
+ import { BigInt, Result } from '../types'
3
+
4
+ import { QueryResponseBase } from './QueryResponse'
5
+
6
+ @json
7
+ class TokenPriceQueryBase {
8
+ constructor(
9
+ public readonly address: string,
10
+ public readonly chainId: i32
11
+ ) {}
12
+ }
13
+
14
+ @json
15
+ export class TokenPriceQuery extends TokenPriceQueryBase {
16
+ public readonly timestamp: i64
17
+
18
+ constructor(address: string, chainId: i32, timestamp: i64) {
19
+ super(address, chainId)
20
+ this.timestamp = timestamp
21
+ }
22
+
23
+ static fromToken(token: BlockchainToken, timestamp: Date | null): TokenPriceQueryBase {
24
+ const address = token.address.toString()
25
+ const chainId = token.chainId
26
+
27
+ return timestamp
28
+ ? new TokenPriceQuery(address, chainId, changetype<Date>(timestamp).getTime())
29
+ : new TokenPriceQueryBase(address, chainId)
30
+ }
31
+ }
32
+
33
+ @json
34
+ export class TokenPriceQueryResponse extends QueryResponseBase {
35
+ public data: string[]
36
+
37
+ constructor(success: string, data: string[], error: string) {
38
+ super(success, error)
39
+ this.data = data
40
+ }
41
+
42
+ toResult(): Result<USD[], string> {
43
+ return this.buildResult<string[], USD[]>(this.data, 'Unknown error getting price', (data) =>
44
+ data.map<USD>((price) => USD.fromBigInt(BigInt.fromString(price)))
45
+ )
46
+ }
47
+ }