@mimicprotocol/lib-ts 0.0.1-rc.9 → 0.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.
Files changed (80) hide show
  1. package/CHANGELOG.md +144 -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 +150 -71
  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 +199 -0
  29. package/src/intents/Call/EvmDynamicCall.ts +272 -0
  30. package/src/intents/Call/SvmCall.ts +204 -0
  31. package/src/intents/Call/index.ts +3 -0
  32. package/src/intents/Intent.ts +347 -35
  33. package/src/intents/Operation.ts +114 -0
  34. package/src/intents/Swap.ts +127 -114
  35. package/src/intents/Transfer.ts +72 -123
  36. package/src/intents/index.ts +1 -0
  37. package/src/log.ts +83 -0
  38. package/src/queries/EvmCallQuery.ts +43 -0
  39. package/src/queries/QueryResponse.ts +26 -0
  40. package/src/queries/RelevantTokensQuery.ts +82 -0
  41. package/src/queries/SubgraphQuery.ts +50 -0
  42. package/src/queries/SvmAccountsInfoQuery.ts +65 -0
  43. package/src/queries/TokenPriceQuery.ts +47 -0
  44. package/src/queries/index.ts +6 -1
  45. package/src/storage/index.ts +1 -0
  46. package/src/storage/storage.ts +40 -0
  47. package/src/svm.ts +27 -0
  48. package/src/tokens/BlockchainToken.ts +108 -0
  49. package/src/tokens/DenominationToken.ts +70 -0
  50. package/src/tokens/ERC20Token.ts +192 -0
  51. package/src/tokens/SPLToken.ts +162 -0
  52. package/src/tokens/Token.ts +55 -155
  53. package/src/tokens/TokenAmount.ts +72 -30
  54. package/src/tokens/TokenProvider.ts +54 -0
  55. package/src/tokens/Tokens.ts +186 -0
  56. package/src/tokens/USD.ts +9 -6
  57. package/src/tokens/index.ts +6 -0
  58. package/src/types/Address.ts +86 -14
  59. package/src/types/BigInt.ts +14 -22
  60. package/src/types/ByteArray.ts +41 -3
  61. package/src/types/Bytes.ts +7 -0
  62. package/src/types/ChainId.ts +9 -1
  63. package/src/types/Option.ts +35 -0
  64. package/src/types/Result.ts +68 -0
  65. package/src/types/TriggerType.ts +4 -0
  66. package/src/types/evm/EvmDecodeParam.ts +7 -0
  67. package/src/types/evm/EvmEncodeParam.ts +31 -0
  68. package/src/types/evm/index.ts +2 -0
  69. package/src/types/index.ts +8 -2
  70. package/src/types/svm/SvmAccountInfo.ts +32 -0
  71. package/src/types/svm/SvmAccountMeta.ts +28 -0
  72. package/src/types/svm/SvmFindProgramAddress.ts +32 -0
  73. package/src/types/svm/SvmMint.ts +44 -0
  74. package/src/types/svm/SvmPdaSeed.ts +19 -0
  75. package/src/types/svm/SvmTokenMetadataData.ts +29 -0
  76. package/src/types/svm/index.ts +5 -0
  77. package/src/intents/Call.ts +0 -238
  78. package/src/queries/Call.ts +0 -16
  79. package/src/types/EvmDecodeParam.ts +0 -30
  80. package/src/types/EvmEncodeParam.ts +0 -54
@@ -1,17 +1,17 @@
1
1
  import { environment } from '../environment'
2
2
  import { Token, TokenAmount } from '../tokens'
3
- import { Address, BigInt, ChainId } from '../types'
3
+ import { Address, BigInt, Bytes, ChainId } from '../types'
4
4
 
5
- import { Intent, IntentBuilder, OperationType } from './Intent'
5
+ import { IntentBuilder } from './Intent'
6
+ import { Operation, OperationBuilder, OperationEvent, OperationType } from './Operation'
6
7
 
7
8
  /**
8
9
  * Builder for creating Transfer intents with token transfer operations.
9
10
  * Supports multiple transfers within a single transaction on the same chain.
10
11
  */
11
- export class TransferBuilder extends IntentBuilder {
12
- private chainId: ChainId
13
- private transfers: TransferData[] = []
14
- private fee: TokenAmount | null = null
12
+ export class TransferBuilder extends OperationBuilder {
13
+ protected chainId: ChainId
14
+ protected transfers: TransferData[] = []
15
15
 
16
16
  /**
17
17
  * Creates a TransferBuilder for a specific chain.
@@ -22,23 +22,11 @@ export class TransferBuilder extends IntentBuilder {
22
22
  return new TransferBuilder(chainId)
23
23
  }
24
24
 
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
25
  /**
38
26
  * Creates a new TransferBuilder instance.
39
27
  * @param chainId - The blockchain network identifier
40
28
  */
41
- constructor(chainId: ChainId) {
29
+ private constructor(chainId: ChainId) {
42
30
  super()
43
31
  this.chainId = chainId
44
32
  }
@@ -63,6 +51,33 @@ export class TransferBuilder extends IntentBuilder {
63
51
  return this
64
52
  }
65
53
 
54
+ /**
55
+ * Adds the transfers from another TransferBuilder to this TransferBuilder.
56
+ * @param builder - The TransferBuilder to add the transfers from
57
+ * @returns This TransferBuilder instance for method chaining
58
+ */
59
+ addTransfersFromBuilder(builder: TransferBuilder): TransferBuilder {
60
+ return this.addTransfers(builder.getTransfers())
61
+ }
62
+
63
+ /**
64
+ * Adds the transfers from multiple TransferBuilders to this TransferBuilder.
65
+ * @param builders - The TransferBuilders to add the transfers from
66
+ * @returns This TransferBuilder instance for method chaining
67
+ */
68
+ addTransfersFromBuilders(builders: TransferBuilder[]): TransferBuilder {
69
+ for (let i = 0; i < builders.length; i++) this.addTransfersFromBuilder(builders[i])
70
+ return this
71
+ }
72
+
73
+ /**
74
+ * Returns a copy of the transfers array.
75
+ * @returns A copy of the transfers array
76
+ */
77
+ getTransfers(): TransferData[] {
78
+ return this.transfers.slice(0)
79
+ }
80
+
66
81
  /**
67
82
  * Adds a transfer from a TokenAmount.
68
83
  * @param tokenAmount - The token amount to transfer (must be on same chain)
@@ -70,7 +85,7 @@ export class TransferBuilder extends IntentBuilder {
70
85
  * @returns This TransferBuilder instance for method chaining
71
86
  */
72
87
  addTransferFromTokenAmount(tokenAmount: TokenAmount, recipient: Address): TransferBuilder {
73
- if (tokenAmount.token.chainId !== this.chainId) throw new Error('Transfer tokens must be on the same chain')
88
+ if (!tokenAmount.token.hasChain(this.chainId)) throw new Error('Transfer tokens must be on the same chain')
74
89
  return this.addTransfer(TransferData.fromTokenAmount(tokenAmount, recipient))
75
90
  }
76
91
 
@@ -82,7 +97,7 @@ export class TransferBuilder extends IntentBuilder {
82
97
  * @returns This TransferBuilder instance for method chaining
83
98
  */
84
99
  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')
100
+ if (!token.hasChain(this.chainId)) throw new Error('Transfer tokens must be on the same chain')
86
101
  return this.addTransfer(TransferData.fromI32(token, amount, recipient))
87
102
  }
88
103
 
@@ -94,7 +109,7 @@ export class TransferBuilder extends IntentBuilder {
94
109
  * @returns This TransferBuilder instance for method chaining
95
110
  */
96
111
  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')
112
+ if (!token.hasChain(this.chainId)) throw new Error('Transfer tokens must be on the same chain')
98
113
  return this.addTransfer(TransferData.fromBigInt(token, amount, recipient))
99
114
  }
100
115
 
@@ -106,7 +121,7 @@ export class TransferBuilder extends IntentBuilder {
106
121
  * @returns This TransferBuilder instance for method chaining
107
122
  */
108
123
  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')
124
+ if (!token.hasChain(this.chainId)) throw new Error('Transfer tokens must be on the same chain')
110
125
  return this.addTransfer(TransferData.fromStringDecimal(token, amount, recipient))
111
126
  }
112
127
 
@@ -121,44 +136,6 @@ export class TransferBuilder extends IntentBuilder {
121
136
  return this
122
137
  }
123
138
 
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
- /**
136
- * Sets the settler address for this intent.
137
- * @param settler - The settler address as an Address instance
138
- * @returns This TransferBuilder instance for method chaining
139
- */
140
- addSettler(settler: Address): TransferBuilder {
141
- return changetype<TransferBuilder>(super.addSettler(settler))
142
- }
143
-
144
- /**
145
- * Sets the settler address from a string.
146
- * @param settler - The settler address as a hex string
147
- * @returns This TransferBuilder instance for method chaining
148
- */
149
- addSettlerAsString(settler: string): TransferBuilder {
150
- return changetype<TransferBuilder>(super.addSettlerAsString(settler))
151
- }
152
-
153
- /**
154
- * Sets the deadline for this intent.
155
- * @param deadline - The deadline as a timestamp
156
- * @returns This TransferBuilder instance for method chaining
157
- */
158
- addDeadline(deadline: BigInt): TransferBuilder {
159
- return changetype<TransferBuilder>(super.addDeadline(deadline))
160
- }
161
-
162
139
  /**
163
140
  * Sets the user address for this intent.
164
141
  * @param user - The user address
@@ -178,12 +155,22 @@ export class TransferBuilder extends IntentBuilder {
178
155
  }
179
156
 
180
157
  /**
181
- * Sets the nonce for this intent.
182
- * @param nonce - A unique identifier to prevent replay attacks
158
+ * Sets an event for the intent.
159
+ * @param topic - The topic to be indexed in the event
160
+ * @param data - The event data
183
161
  * @returns This TransferBuilder instance for method chaining
184
162
  */
185
- addNonce(nonce: string): TransferBuilder {
186
- return changetype<TransferBuilder>(super.addNonce(nonce))
163
+ addEvent(topic: Bytes, data: Bytes): TransferBuilder {
164
+ return changetype<TransferBuilder>(super.addEvent(topic, data))
165
+ }
166
+
167
+ /**
168
+ * Sets multiple events for the intent.
169
+ * @param events - The list of events to be added
170
+ * @returns This TransferBuilder instance for method chaining
171
+ */
172
+ addEvents(events: OperationEvent[]): TransferBuilder {
173
+ return changetype<TransferBuilder>(super.addEvents(events))
187
174
  }
188
175
 
189
176
  /**
@@ -191,16 +178,16 @@ export class TransferBuilder extends IntentBuilder {
191
178
  * @returns A new Transfer instance with all configured parameters
192
179
  */
193
180
  build(): Transfer {
194
- if (!this.fee) throw new Error('Transfer fee must be specified')
195
- return new Transfer(
196
- this.chainId,
197
- this.transfers,
198
- this.fee as TokenAmount,
199
- this.settler,
200
- this.user,
201
- this.deadline,
202
- this.nonce
203
- )
181
+ return new Transfer(this.chainId, this.transfers, this.user, this.events)
182
+ }
183
+
184
+ /**
185
+ * Builds this operation and sends it inside an intent with the provided fee data.
186
+ * @param maxFee - The max fee to pay for the intent
187
+ * @param feePayer - The fee payer for the intent (optional)
188
+ */
189
+ send(maxFee: TokenAmount, feePayer: Address | null = null): void {
190
+ this.build().send(maxFee, feePayer)
204
191
  }
205
192
  }
206
193
 
@@ -274,48 +261,14 @@ export class TransferData {
274
261
  * Represents a Transfer intent for sending tokens to recipients on a blockchain network.
275
262
  */
276
263
  @json
277
- export class Transfer extends Intent {
278
- public chainId: ChainId
264
+ export class Transfer extends Operation {
279
265
  public transfers: TransferData[]
280
- public feeToken: string
281
- public feeAmount: string
282
-
283
- /**
284
- * Creates a simple single-token transfer intent.
285
- * @param chainId - The blockchain network identifier
286
- * @param token - The token address to transfer
287
- * @param amount - The amount to transfer
288
- * @param recipient - The address to receive the tokens
289
- * @param fee - The fee amount for the transfer
290
- * @param settler - The settler address (optional)
291
- * @param user - The user address (optional)
292
- * @param deadline - The deadline timestamp (optional)
293
- * @param nonce - The nonce for replay protection (optional)
294
- * @returns A new Transfer instance
295
- */
296
- static create(
297
- chainId: ChainId,
298
- token: Address,
299
- amount: BigInt,
300
- recipient: Address,
301
- fee: BigInt,
302
- settler: Address | null = null,
303
- user: Address | null = null,
304
- deadline: BigInt | null = null,
305
- nonce: string | null = null
306
- ): Transfer {
307
- const transferToken = Token.fromAddress(token, chainId)
308
- const transferAmount = TokenAmount.fromBigInt(transferToken, amount)
309
- const transferData = TransferData.fromTokenAmount(transferAmount, recipient)
310
- const feeAmount = TokenAmount.fromBigInt(transferToken, fee)
311
- return new Transfer(chainId, [transferData], feeAmount, settler, user, deadline, nonce)
312
- }
313
266
 
314
267
  /**
315
268
  * Creates a new Transfer intent.
316
269
  * @param chainId - The blockchain network identifier
317
270
  * @param transfers - Array of transfer data configurations
318
- * @param fee - The fee token amount for the transfers
271
+ * @param maxFees - The list of max fees to pay for the transfer intent
319
272
  * @param settler - The settler address (optional)
320
273
  * @param user - The user address (optional)
321
274
  * @param deadline - The deadline timestamp (optional)
@@ -324,26 +277,22 @@ export class Transfer extends Intent {
324
277
  constructor(
325
278
  chainId: ChainId,
326
279
  transfers: TransferData[],
327
- fee: TokenAmount,
328
- settler: Address | null = null,
329
280
  user: Address | null = null,
330
- deadline: BigInt | null = null,
331
- nonce: string | null = null
281
+ events: OperationEvent[] | null = null
332
282
  ) {
333
- super(OperationType.Transfer, settler, user, deadline, nonce)
334
-
283
+ super(OperationType.Transfer, chainId, user, events)
335
284
  if (transfers.length === 0) throw new Error('Transfer list cannot be empty')
336
-
337
285
  this.transfers = transfers
338
- this.feeToken = fee.token.address.toString()
339
- this.feeAmount = fee.amount.toString()
340
- this.chainId = chainId
341
286
  }
342
287
 
343
288
  /**
344
289
  * Sends this Transfer intent to the execution environment.
290
+ * @param maxFee - The max fee to pay for the intent
291
+ * @param feePayer - The fee payer for the intent (optional)
345
292
  */
346
- send(): void {
347
- environment.transfer(this)
293
+ send(maxFee: TokenAmount, feePayer: Address | null = null): void {
294
+ const intentBuilder = new IntentBuilder().addMaxFee(maxFee).addOperation(this)
295
+ if (feePayer) intentBuilder.addFeePayer(feePayer)
296
+ environment.sendIntent(intentBuilder.build())
348
297
  }
349
298
  }
@@ -1,4 +1,5 @@
1
1
  export * from './Call'
2
2
  export * from './Intent'
3
+ export * from './Operation'
3
4
  export * from './Swap'
4
5
  export * from './Transfer'
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
+ }