@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.
- package/CHANGELOG.md +144 -0
- package/README.md +7 -7
- package/constants.d.ts +1 -0
- package/constants.js +1 -0
- package/index.ts +3 -0
- package/package.json +18 -4
- package/src/chains/Arbitrum.ts +14 -0
- package/src/chains/Avalanche.ts +15 -0
- package/src/chains/BNB.ts +15 -0
- package/src/chains/Base.ts +14 -0
- package/src/chains/BaseSepolia.ts +7 -0
- package/src/chains/Ethereum.ts +7 -7
- package/src/chains/Gnosis.ts +14 -0
- package/src/chains/Optimism.ts +7 -7
- package/src/chains/Polygon.ts +10 -7
- package/src/chains/Sonic.ts +13 -0
- package/src/chains/index.ts +7 -0
- package/src/context/Context.ts +102 -7
- package/src/environment.ts +150 -71
- package/src/evm.ts +5 -4
- package/src/helpers/BorshDeserializer.ts +133 -0
- package/src/helpers/consensus.ts +35 -0
- package/src/helpers/constants.ts +7 -1
- package/src/helpers/index.ts +5 -0
- package/src/helpers/math.ts +20 -0
- package/src/helpers/serialize.ts +5 -125
- package/src/helpers/strings.ts +82 -5
- package/src/intents/Call/EvmCall.ts +199 -0
- package/src/intents/Call/EvmDynamicCall.ts +272 -0
- package/src/intents/Call/SvmCall.ts +204 -0
- package/src/intents/Call/index.ts +3 -0
- package/src/intents/Intent.ts +347 -35
- package/src/intents/Operation.ts +114 -0
- package/src/intents/Swap.ts +127 -114
- package/src/intents/Transfer.ts +72 -123
- package/src/intents/index.ts +1 -0
- package/src/log.ts +83 -0
- package/src/queries/EvmCallQuery.ts +43 -0
- package/src/queries/QueryResponse.ts +26 -0
- package/src/queries/RelevantTokensQuery.ts +82 -0
- package/src/queries/SubgraphQuery.ts +50 -0
- package/src/queries/SvmAccountsInfoQuery.ts +65 -0
- package/src/queries/TokenPriceQuery.ts +47 -0
- package/src/queries/index.ts +6 -1
- package/src/storage/index.ts +1 -0
- package/src/storage/storage.ts +40 -0
- package/src/svm.ts +27 -0
- package/src/tokens/BlockchainToken.ts +108 -0
- package/src/tokens/DenominationToken.ts +70 -0
- package/src/tokens/ERC20Token.ts +192 -0
- package/src/tokens/SPLToken.ts +162 -0
- package/src/tokens/Token.ts +55 -155
- package/src/tokens/TokenAmount.ts +72 -30
- package/src/tokens/TokenProvider.ts +54 -0
- package/src/tokens/Tokens.ts +186 -0
- package/src/tokens/USD.ts +9 -6
- package/src/tokens/index.ts +6 -0
- package/src/types/Address.ts +86 -14
- package/src/types/BigInt.ts +14 -22
- package/src/types/ByteArray.ts +41 -3
- package/src/types/Bytes.ts +7 -0
- package/src/types/ChainId.ts +9 -1
- package/src/types/Option.ts +35 -0
- package/src/types/Result.ts +68 -0
- package/src/types/TriggerType.ts +4 -0
- package/src/types/evm/EvmDecodeParam.ts +7 -0
- package/src/types/evm/EvmEncodeParam.ts +31 -0
- package/src/types/evm/index.ts +2 -0
- package/src/types/index.ts +8 -2
- package/src/types/svm/SvmAccountInfo.ts +32 -0
- package/src/types/svm/SvmAccountMeta.ts +28 -0
- package/src/types/svm/SvmFindProgramAddress.ts +32 -0
- package/src/types/svm/SvmMint.ts +44 -0
- package/src/types/svm/SvmPdaSeed.ts +19 -0
- package/src/types/svm/SvmTokenMetadataData.ts +29 -0
- package/src/types/svm/index.ts +5 -0
- package/src/intents/Call.ts +0 -238
- package/src/queries/Call.ts +0 -16
- package/src/types/EvmDecodeParam.ts +0 -30
- package/src/types/EvmEncodeParam.ts +0 -54
package/src/intents/Transfer.ts
CHANGED
|
@@ -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 {
|
|
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
|
|
12
|
-
|
|
13
|
-
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
|
182
|
-
* @param
|
|
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
|
-
|
|
186
|
-
return changetype<TransferBuilder>(super.
|
|
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
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
331
|
-
nonce: string | null = null
|
|
281
|
+
events: OperationEvent[] | null = null
|
|
332
282
|
) {
|
|
333
|
-
super(OperationType.Transfer,
|
|
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
|
-
|
|
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
|
}
|
package/src/intents/index.ts
CHANGED
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
|
+
}
|