@mimicprotocol/lib-ts 0.0.1-rc.10

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.
@@ -0,0 +1,349 @@
1
+ import { environment } from '../environment'
2
+ import { Token, TokenAmount } from '../tokens'
3
+ import { Address, BigInt, ChainId } from '../types'
4
+
5
+ import { Intent, IntentBuilder, OperationType } from './Intent'
6
+
7
+ /**
8
+ * Builder for creating Transfer intents with token transfer operations.
9
+ * Supports multiple transfers within a single transaction on the same chain.
10
+ */
11
+ export class TransferBuilder extends IntentBuilder {
12
+ private chainId: ChainId
13
+ private transfers: TransferData[] = []
14
+ private fee: TokenAmount | null = null
15
+
16
+ /**
17
+ * Creates a TransferBuilder for a specific chain.
18
+ * @param chainId - The blockchain network identifier
19
+ * @returns A new TransferBuilder instance
20
+ */
21
+ static forChain(chainId: ChainId): TransferBuilder {
22
+ return new TransferBuilder(chainId)
23
+ }
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
+ /**
38
+ * Creates a new TransferBuilder instance.
39
+ * @param chainId - The blockchain network identifier
40
+ */
41
+ constructor(chainId: ChainId) {
42
+ super()
43
+ this.chainId = chainId
44
+ }
45
+
46
+ /**
47
+ * Adds a transfer to the intent.
48
+ * @param transfer - The transfer data configuration
49
+ * @returns This TransferBuilder instance for method chaining
50
+ */
51
+ addTransfer(transfer: TransferData): TransferBuilder {
52
+ this.transfers.push(transfer)
53
+ return this
54
+ }
55
+
56
+ /**
57
+ * Adds multiple transfers to the intent.
58
+ * @param transfers - Array of transfer data configurations
59
+ * @returns This TransferBuilder instance for method chaining
60
+ */
61
+ addTransfers(transfers: TransferData[]): TransferBuilder {
62
+ for (let i = 0; i < transfers.length; i++) this.transfers.push(transfers[i])
63
+ return this
64
+ }
65
+
66
+ /**
67
+ * Adds a transfer from a TokenAmount.
68
+ * @param tokenAmount - The token amount to transfer (must be on same chain)
69
+ * @param recipient - The address to receive the tokens
70
+ * @returns This TransferBuilder instance for method chaining
71
+ */
72
+ addTransferFromTokenAmount(tokenAmount: TokenAmount, recipient: Address): TransferBuilder {
73
+ if (tokenAmount.token.chainId !== this.chainId) throw new Error('Transfer tokens must be on the same chain')
74
+ return this.addTransfer(TransferData.fromTokenAmount(tokenAmount, recipient))
75
+ }
76
+
77
+ /**
78
+ * Adds a transfer from a 32-bit integer amount.
79
+ * @param token - The token to transfer (must be on same chain)
80
+ * @param amount - The amount as a whole number
81
+ * @param recipient - The address to receive the tokens
82
+ * @returns This TransferBuilder instance for method chaining
83
+ */
84
+ 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')
86
+ return this.addTransfer(TransferData.fromI32(token, amount, recipient))
87
+ }
88
+
89
+ /**
90
+ * Adds a transfer from a BigInt amount.
91
+ * @param token - The token to transfer (must be on same chain)
92
+ * @param amount - The amount in the token's smallest unit
93
+ * @param recipient - The address to receive the tokens
94
+ * @returns This TransferBuilder instance for method chaining
95
+ */
96
+ 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')
98
+ return this.addTransfer(TransferData.fromBigInt(token, amount, recipient))
99
+ }
100
+
101
+ /**
102
+ * Adds a transfer from a decimal string amount.
103
+ * @param token - The token to transfer (must be on same chain)
104
+ * @param amount - The amount as a decimal string
105
+ * @param recipient - The address to receive the tokens
106
+ * @returns This TransferBuilder instance for method chaining
107
+ */
108
+ 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')
110
+ return this.addTransfer(TransferData.fromStringDecimal(token, amount, recipient))
111
+ }
112
+
113
+ /**
114
+ * Adds multiple transfers from TokenAmounts to the same recipient.
115
+ * @param tokenAmounts - Array of token amounts to transfer (must be on same chain)
116
+ * @param recipient - The address to receive all the tokens
117
+ * @returns This TransferBuilder instance for method chaining
118
+ */
119
+ addTransfersFromTokenAmounts(tokenAmounts: TokenAmount[], recipient: Address): TransferBuilder {
120
+ for (let i = 0; i < tokenAmounts.length; i++) this.addTransferFromTokenAmount(tokenAmounts[i], recipient)
121
+ return this
122
+ }
123
+
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
+ /**
163
+ * Sets the user address for this intent.
164
+ * @param user - The user address
165
+ * @returns This TransferBuilder instance for method chaining
166
+ */
167
+ addUser(user: Address): TransferBuilder {
168
+ return changetype<TransferBuilder>(super.addUser(user))
169
+ }
170
+
171
+ /**
172
+ * Sets the user address from a string.
173
+ * @param user - The user address as a hex string
174
+ * @returns This TransferBuilder instance for method chaining
175
+ */
176
+ addUserAsString(user: string): TransferBuilder {
177
+ return changetype<TransferBuilder>(super.addUserAsString(user))
178
+ }
179
+
180
+ /**
181
+ * Sets the nonce for this intent.
182
+ * @param nonce - A unique identifier to prevent replay attacks
183
+ * @returns This TransferBuilder instance for method chaining
184
+ */
185
+ addNonce(nonce: string): TransferBuilder {
186
+ return changetype<TransferBuilder>(super.addNonce(nonce))
187
+ }
188
+
189
+ /**
190
+ * Builds and returns the final Transfer intent.
191
+ * @returns A new Transfer instance with all configured parameters
192
+ */
193
+ 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
+ )
204
+ }
205
+ }
206
+
207
+ /**
208
+ * Represents transfer data for a single token transfer operation.
209
+ * Specifies the token, amount, and recipient for the transfer.
210
+ */
211
+ @json
212
+ export class TransferData {
213
+ public token: string
214
+ public amount: string
215
+ public recipient: string
216
+
217
+ /**
218
+ * Creates TransferData from a TokenAmount.
219
+ * @param tokenAmount - The token amount to transfer
220
+ * @param recipient - The address to receive the tokens
221
+ * @returns A new TransferData instance
222
+ */
223
+ static fromTokenAmount(tokenAmount: TokenAmount, recipient: Address): TransferData {
224
+ return new TransferData(tokenAmount.token.address, tokenAmount.amount, recipient)
225
+ }
226
+
227
+ /**
228
+ * Creates TransferData from a 32-bit integer amount.
229
+ * @param token - The token to transfer
230
+ * @param amount - The amount as a whole number
231
+ * @param recipient - The address to receive the tokens
232
+ * @returns A new TransferData instance
233
+ */
234
+ static fromI32(token: Token, amount: i32, recipient: Address): TransferData {
235
+ return this.fromTokenAmount(TokenAmount.fromI32(token, amount), recipient)
236
+ }
237
+
238
+ /**
239
+ * Creates TransferData from a BigInt amount.
240
+ * @param token - The token to transfer
241
+ * @param amount - The amount in the token's smallest unit
242
+ * @param recipient - The address to receive the tokens
243
+ * @returns A new TransferData instance
244
+ */
245
+ static fromBigInt(token: Token, amount: BigInt, recipient: Address): TransferData {
246
+ return this.fromTokenAmount(TokenAmount.fromBigInt(token, amount), recipient)
247
+ }
248
+
249
+ /**
250
+ * Creates TransferData from a decimal string amount.
251
+ * @param token - The token to transfer
252
+ * @param amount - The amount as a decimal string
253
+ * @param recipient - The address to receive the tokens
254
+ * @returns A new TransferData instance
255
+ */
256
+ static fromStringDecimal(token: Token, amount: string, recipient: Address): TransferData {
257
+ return this.fromTokenAmount(TokenAmount.fromStringDecimal(token, amount), recipient)
258
+ }
259
+
260
+ /**
261
+ * Creates a new TransferData instance.
262
+ * @param token - The token address
263
+ * @param amount - The amount in the token's smallest unit
264
+ * @param recipient - The address to receive the tokens
265
+ */
266
+ constructor(token: Address, amount: BigInt, recipient: Address) {
267
+ this.token = token.toString()
268
+ this.amount = amount.toString()
269
+ this.recipient = recipient.toString()
270
+ }
271
+ }
272
+
273
+ /**
274
+ * Represents a Transfer intent for sending tokens to recipients on a blockchain network.
275
+ */
276
+ @json
277
+ export class Transfer extends Intent {
278
+ public chainId: ChainId
279
+ 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
+
314
+ /**
315
+ * Creates a new Transfer intent.
316
+ * @param chainId - The blockchain network identifier
317
+ * @param transfers - Array of transfer data configurations
318
+ * @param fee - The fee token amount for the transfers
319
+ * @param settler - The settler address (optional)
320
+ * @param user - The user address (optional)
321
+ * @param deadline - The deadline timestamp (optional)
322
+ * @param nonce - The nonce for replay protection (optional)
323
+ */
324
+ constructor(
325
+ chainId: ChainId,
326
+ transfers: TransferData[],
327
+ fee: TokenAmount,
328
+ settler: Address | null = null,
329
+ user: Address | null = null,
330
+ deadline: BigInt | null = null,
331
+ nonce: string | null = null
332
+ ) {
333
+ super(OperationType.Transfer, chainId, settler, user, deadline, nonce)
334
+
335
+ if (transfers.length === 0) throw new Error('Transfer list cannot be empty')
336
+
337
+ this.transfers = transfers
338
+ this.feeToken = fee.token.address.toString()
339
+ this.feeAmount = fee.amount.toString()
340
+ this.chainId = chainId
341
+ }
342
+
343
+ /**
344
+ * Sends this Transfer intent to the execution environment.
345
+ */
346
+ send(): void {
347
+ environment.transfer(this)
348
+ }
349
+ }
@@ -0,0 +1,4 @@
1
+ export * from './Call'
2
+ export * from './Intent'
3
+ export * from './Swap'
4
+ 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
+ export namespace log {
8
+ @external('log', '_log')
9
+ declare function _log(level: Level, msg: string): void
10
+
11
+ export enum Level {
12
+ CRITICAL = 0,
13
+ ERROR = 1,
14
+ WARNING = 2,
15
+ INFO = 3,
16
+ DEBUG = 4,
17
+ }
18
+
19
+ /**
20
+ * Logs a critical message that terminates the execution.
21
+ *
22
+ * @param msg Format string like "Value = {}, other = {}".
23
+ * @param args Format string arguments.
24
+ */
25
+ export function critical(msg: string, args: Array<string> = []): void {
26
+ _log(Level.CRITICAL, format(msg, args))
27
+ }
28
+
29
+ /**
30
+ * Logs an error message.
31
+ *
32
+ * @param msg Format string like "Value = {}, other = {}".
33
+ * @param args Format string arguments.
34
+ */
35
+ export function error(msg: string, args: Array<string> = []): void {
36
+ _log(Level.ERROR, format(msg, args))
37
+ }
38
+
39
+ /** Logs a warning message.
40
+ *
41
+ * @param msg Format string like "Value = {}, other = {}".
42
+ * @param args Format string arguments.
43
+ */
44
+ export function warning(msg: string, args: Array<string> = []): void {
45
+ _log(Level.WARNING, format(msg, args))
46
+ }
47
+
48
+ /** Logs an info message.
49
+ *
50
+ * @param msg Format string like "Value = {}, other = {}".
51
+ * @param args Format string arguments.
52
+ */
53
+ export function info(msg: string, args: Array<string> = []): void {
54
+ _log(Level.INFO, format(msg, args))
55
+ }
56
+
57
+ /** Logs a debug message.
58
+ *
59
+ * @param msg Format string like "Value = {}, other = {}".
60
+ * @param args Format string arguments.
61
+ */
62
+ export function debug(msg: string, args: Array<string> = []): void {
63
+ _log(Level.DEBUG, format(msg, args))
64
+ }
65
+ }
66
+
67
+ function format(fmt: string, args: string[]): string {
68
+ let out = ''
69
+ let argIndex = 0
70
+ for (let i: i32 = 0, len: i32 = fmt.length; i < len; i++) {
71
+ if (i < len - 1 && fmt.charCodeAt(i) == 0x7b /* '{' */ && fmt.charCodeAt(i + 1) == 0x7d /* '}' */) {
72
+ if (argIndex >= args.length) {
73
+ throw new Error('Too few arguments for format string: ' + fmt)
74
+ } else {
75
+ out += args[argIndex++]
76
+ i++
77
+ }
78
+ } else {
79
+ out += fmt.charAt(i)
80
+ }
81
+ }
82
+ return out
83
+ }
@@ -0,0 +1,27 @@
1
+ import { Address, ChainId } from '../types'
2
+
3
+ @json
4
+ class CallBase {
5
+ constructor(
6
+ public readonly to: string,
7
+ public readonly chainId: ChainId,
8
+ public readonly data: string
9
+ ) {}
10
+ }
11
+
12
+ @json
13
+ export class Call extends CallBase {
14
+ public readonly timestamp: i64
15
+
16
+ constructor(to: string, chainId: ChainId, timestamp: i64, data: string) {
17
+ super(to, chainId, data)
18
+ this.timestamp = timestamp
19
+ }
20
+
21
+ static from(to: Address, chainId: ChainId, timestamp: Date | null, data: string): CallBase {
22
+ const address = to.toString()
23
+ return timestamp
24
+ ? new Call(address, chainId, changetype<Date>(timestamp).getTime(), data)
25
+ : new CallBase(address, chainId, data)
26
+ }
27
+ }
@@ -0,0 +1,28 @@
1
+ import { Token } from '../tokens'
2
+
3
+ @json
4
+ class GetPriceBase {
5
+ constructor(
6
+ public readonly address: string,
7
+ public readonly chainId: i32
8
+ ) {}
9
+ }
10
+
11
+ @json
12
+ export class GetPrice extends GetPriceBase {
13
+ public readonly timestamp: i64
14
+
15
+ constructor(address: string, chainId: i32, timestamp: i64) {
16
+ super(address, chainId)
17
+ this.timestamp = timestamp
18
+ }
19
+
20
+ static fromToken(token: Token, timestamp: Date | null): GetPriceBase {
21
+ const address = token.address.toString()
22
+ const chainId = token.chainId
23
+
24
+ return timestamp
25
+ ? new GetPrice(address, chainId, changetype<Date>(timestamp).getTime())
26
+ : new GetPriceBase(address, chainId)
27
+ }
28
+ }
@@ -0,0 +1,82 @@
1
+ import { ListType } from '../helpers'
2
+ import { Token, TokenAmount, USD } from '../tokens'
3
+ import { Address, BigInt, ChainId } from '../types'
4
+
5
+ @json
6
+ class TokenQuery {
7
+ constructor(
8
+ public address: string,
9
+ public chainId: i32
10
+ ) {}
11
+
12
+ static fromToken(token: Token): TokenQuery {
13
+ return new TokenQuery(token.address.toString(), token.chainId)
14
+ }
15
+ }
16
+
17
+ @json
18
+ class GetRelevantTokensBase {
19
+ constructor(
20
+ public readonly owner: string,
21
+ public readonly chainIds: ChainId[],
22
+ public readonly usdMinAmount: string,
23
+ public readonly tokens: TokenQuery[],
24
+ public readonly tokenFilter: ListType
25
+ ) {}
26
+ }
27
+
28
+ @json
29
+ export class GetRelevantTokens extends GetRelevantTokensBase {
30
+ public readonly timestamp: i64
31
+
32
+ constructor(
33
+ owner: string,
34
+ chainIds: ChainId[],
35
+ usdMinAmount: string,
36
+ tokens: TokenQuery[],
37
+ tokenFilter: ListType,
38
+ timestamp: i64
39
+ ) {
40
+ super(owner, chainIds, usdMinAmount, tokens, tokenFilter)
41
+ this.timestamp = timestamp
42
+ }
43
+
44
+ static init(
45
+ owner: Address,
46
+ chainIds: ChainId[],
47
+ usdMinAmount: USD,
48
+ tokens: Token[],
49
+ tokenFilter: ListType,
50
+ timestamp: Date | null = null
51
+ ): GetRelevantTokensBase {
52
+ const ownerStr = owner.toString()
53
+ const usdMinAmountStr = usdMinAmount.toString()
54
+ const tokensQueries = tokens.map<TokenQuery>((token) => TokenQuery.fromToken(token))
55
+
56
+ return timestamp
57
+ ? new GetRelevantTokens(
58
+ ownerStr,
59
+ chainIds,
60
+ usdMinAmountStr,
61
+ tokensQueries,
62
+ tokenFilter,
63
+ changetype<Date>(timestamp).getTime()
64
+ )
65
+ : new GetRelevantTokensBase(ownerStr, chainIds, usdMinAmountStr, tokensQueries, tokenFilter)
66
+ }
67
+ }
68
+
69
+ @json
70
+ export class GetRelevantTokensResponse {
71
+ constructor(
72
+ public token: TokenQuery,
73
+ public amount: string
74
+ ) {}
75
+
76
+ toTokenAmount(): TokenAmount {
77
+ return TokenAmount.fromBigInt(
78
+ Token.fromString(this.token.address, this.token.chainId),
79
+ BigInt.fromString(this.amount)
80
+ )
81
+ }
82
+ }
@@ -0,0 +1,3 @@
1
+ export * from './Call'
2
+ export * from './GetPrice'
3
+ export * from './GetRelevantTokens'