@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
@@ -0,0 +1,5 @@
1
+ export * from './SvmAccountInfo'
2
+ export * from './SvmFindProgramAddress'
3
+ export * from './SvmMint'
4
+ export * from './SvmPdaSeed'
5
+ export * from './SvmTokenMetadataData'
@@ -1,238 +0,0 @@
1
- import { environment } from '../environment'
2
- import { TokenAmount } from '../tokens'
3
- import { ChainId } from '../types'
4
- import { Address, BigInt, Bytes } from '../types'
5
-
6
- import { Intent, IntentBuilder, OperationType } from './Intent'
7
-
8
- /**
9
- * Builder for creating Call intents with contract call operations.
10
- * Allows chaining multiple contract calls and configuring fees and settlement parameters.
11
- */
12
- export class CallBuilder extends IntentBuilder {
13
- private chainId: ChainId
14
- private calls: CallData[] = []
15
- private fee: TokenAmount | null = null
16
-
17
- /**
18
- * Creates a CallBuilder for the specified blockchain network.
19
- * @param chainId - The blockchain network identifier
20
- * @returns A new CallBuilder instance
21
- */
22
- static forChain(chainId: ChainId): CallBuilder {
23
- return new CallBuilder(chainId)
24
- }
25
-
26
- /**
27
- * Creates a CallBuilder with a pre-configured fee.
28
- * @param chainId - The blockchain network identifier
29
- * @param fee - The fee token amount to be charged for the call
30
- * @returns A new CallBuilder instance with fee already set
31
- */
32
- static forChainWithFee(chainId: ChainId, fee: TokenAmount): CallBuilder {
33
- const builder = new CallBuilder(chainId)
34
- builder.addFee(fee)
35
- return builder
36
- }
37
-
38
- /**
39
- * Creates a new CallBuilder instance.
40
- * @param chainId - The blockchain network identifier
41
- */
42
- constructor(chainId: ChainId) {
43
- super()
44
- this.chainId = chainId
45
- }
46
-
47
- /**
48
- * Adds a contract call to the intent.
49
- * @param target - The contract address to call
50
- * @param data - The call data (optional, defaults to empty bytes)
51
- * @param value - The native token value to send (optional, defaults to zero)
52
- * @returns This CallBuilder instance for method chaining
53
- */
54
- addCall(target: Address, data: Bytes = Bytes.empty(), value: BigInt = BigInt.zero()): CallBuilder {
55
- this.calls.push(new CallData(target, data, value))
56
- return this
57
- }
58
-
59
- /**
60
- * Sets the fee to be charged for executing this call intent.
61
- * @param fee - The fee token amount (must be on the same chain as the call)
62
- * @returns This CallBuilder instance for method chaining
63
- */
64
- addFee(fee: TokenAmount): CallBuilder {
65
- if (fee.token.chainId !== this.chainId) throw new Error('Fee token must be on the same chain')
66
- this.fee = fee
67
- return this
68
- }
69
-
70
- /**
71
- * Sets the settler address for this intent.
72
- * @param settler - The settler address as an Address instance
73
- * @returns This CallBuilder instance for method chaining
74
- */
75
- addSettler(settler: Address): CallBuilder {
76
- return changetype<CallBuilder>(super.addSettler(settler))
77
- }
78
-
79
- /**
80
- * Sets the settler address from a string.
81
- * @param settler - The settler address as a hex string
82
- * @returns This CallBuilder instance for method chaining
83
- */
84
- addSettlerAsString(settler: string): CallBuilder {
85
- return changetype<CallBuilder>(super.addSettlerAsString(settler))
86
- }
87
-
88
- /**
89
- * Sets the deadline for this intent.
90
- * @param deadline - The deadline as a timestamp
91
- * @returns This CallBuilder instance for method chaining
92
- */
93
- addDeadline(deadline: BigInt): CallBuilder {
94
- return changetype<CallBuilder>(super.addDeadline(deadline))
95
- }
96
-
97
- /**
98
- * Sets the user address for this intent.
99
- * @param user - The user address
100
- * @returns This CallBuilder instance for method chaining
101
- */
102
- addUser(user: Address): CallBuilder {
103
- return changetype<CallBuilder>(super.addUser(user))
104
- }
105
-
106
- /**
107
- * Sets the user address from a string.
108
- * @param user - The user address as a hex string
109
- * @returns This CallBuilder instance for method chaining
110
- */
111
- addUserAsString(user: string): CallBuilder {
112
- return changetype<CallBuilder>(super.addUserAsString(user))
113
- }
114
-
115
- /**
116
- * Sets the nonce for this intent.
117
- * @param nonce - A unique identifier to prevent replay attacks
118
- * @returns This CallBuilder instance for method chaining
119
- */
120
- addNonce(nonce: string): CallBuilder {
121
- return changetype<CallBuilder>(super.addNonce(nonce))
122
- }
123
-
124
- /**
125
- * Builds and returns the final Call intent.
126
- * @returns A new Call instance with all configured parameters
127
- */
128
- build(): Call {
129
- if (!this.fee) throw new Error('Call fee must be specified')
130
- return new Call(
131
- this.chainId,
132
- this.calls,
133
- this.fee as TokenAmount,
134
- this.settler,
135
- this.user,
136
- this.deadline,
137
- this.nonce
138
- )
139
- }
140
- }
141
-
142
- /**
143
- * Represents data for a single contract call within a Call intent.
144
- * Contains the target address, call data, and value to send.
145
- */
146
- @json
147
- export class CallData {
148
- public target: string
149
- public data: string
150
- public value: string
151
-
152
- /**
153
- * Creates a new CallData instance.
154
- * @param target - The contract address to call
155
- * @param data - The call data (optional, defaults to empty bytes)
156
- * @param value - The native token value to send (optional, defaults to zero)
157
- */
158
- constructor(target: Address, data: Bytes = Bytes.empty(), value: BigInt = BigInt.zero()) {
159
- this.target = target.toString()
160
- this.data = data.toHexString()
161
- this.value = value.toString()
162
- }
163
- }
164
-
165
- /**
166
- * Represents a Call intent containing one or more contract calls to be executed.
167
- */
168
- @json
169
- export class Call extends Intent {
170
- public chainId: ChainId
171
- public calls: CallData[]
172
- public feeToken: string
173
- public feeAmount: string
174
-
175
- /**
176
- * Creates a Call intent with a single contract call.
177
- * @param chainId - The blockchain network identifier
178
- * @param target - The contract address to call
179
- * @param data - The call data
180
- * @param fee - The fee token amount to be charged
181
- * @param value - The native token value to send (optional, defaults to zero)
182
- * @param settler - The settler address (optional)
183
- * @param user - The user address (optional)
184
- * @param deadline - The deadline timestamp (optional)
185
- * @param nonce - The nonce for replay protection (optional)
186
- * @returns A new Call instance
187
- */
188
- static create(
189
- chainId: ChainId,
190
- target: Address,
191
- data: Bytes,
192
- fee: TokenAmount,
193
- value: BigInt = BigInt.zero(),
194
- settler: Address | null = null,
195
- user: Address | null = null,
196
- deadline: BigInt | null = null,
197
- nonce: string | null = null
198
- ): Call {
199
- const callData = new CallData(target, data, value)
200
- return new Call(chainId, [callData], fee, settler, user, deadline, nonce)
201
- }
202
-
203
- /**
204
- * Creates a new Call intent.
205
- * @param chainId - The blockchain network identifier
206
- * @param calls - Array of contract calls to execute
207
- * @param fee - The fee token amount to be charged
208
- * @param settler - The settler address (optional)
209
- * @param user - The user address (optional)
210
- * @param deadline - The deadline timestamp (optional)
211
- * @param nonce - The nonce for replay protection (optional)
212
- */
213
- constructor(
214
- chainId: ChainId,
215
- calls: CallData[],
216
- fee: TokenAmount,
217
- settler: Address | null = null,
218
- user: Address | null = null,
219
- deadline: BigInt | null = null,
220
- nonce: string | null = null
221
- ) {
222
- super(OperationType.Call, settler, user, deadline, nonce)
223
-
224
- if (calls.length === 0) throw new Error('Call list cannot be empty')
225
-
226
- this.calls = calls
227
- this.feeToken = fee.token.address.toString()
228
- this.feeAmount = fee.amount.toString()
229
- this.chainId = chainId
230
- }
231
-
232
- /**
233
- * Sends this Call intent to the execution environment.
234
- */
235
- public send(): void {
236
- environment.call(this)
237
- }
238
- }
@@ -1,16 +0,0 @@
1
- import { Address, ChainId } from '../types'
2
-
3
- @json
4
- export class Call {
5
- public to: string
6
- public chainId: ChainId
7
- public timestamp: Date | null
8
- public data: string
9
-
10
- constructor(to: Address, chainId: ChainId, timestamp: Date | null, data: string) {
11
- this.to = to.toString()
12
- this.chainId = chainId
13
- this.data = data
14
- this.timestamp = timestamp
15
- }
16
- }
@@ -1,30 +0,0 @@
1
- import { join, Serializable, serialize } from '../helpers'
2
-
3
- /**
4
- * Represents a parameter for EVM decoding, used to process data returned from smart contract calls.
5
- * This class specifies the expected ABI type and the encoded data to be decoded.
6
- */
7
- export class EvmDecodeParam implements Serializable {
8
- private static readonly SERIALIZED_PREFIX: string = 'EvmDecodeParam'
9
-
10
- private _abi_type: string
11
- private _value: string
12
-
13
- /**
14
- * Creates a new EvmDecodeParam instance.
15
- * @param abi_type - The ABI type signature for decoding (e.g., "uint256", "address", "string", "(uint256,string)")
16
- * @param value - The encoded hex string data to be decoded
17
- */
18
- constructor(abi_type: string, value: string) {
19
- this._abi_type = abi_type
20
- this._value = value
21
- }
22
-
23
- toString(): string {
24
- return `${EvmDecodeParam.SERIALIZED_PREFIX}(${join([serialize(this._abi_type), serialize(this._value)])})`
25
- }
26
-
27
- serialize(): string {
28
- return this.toString()
29
- }
30
- }
@@ -1,54 +0,0 @@
1
- import { join, Serializable, serialize, serializeArray, Stringable } from '../helpers'
2
-
3
- /**
4
- * Represents a parameter for EVM encoding, used to prepare data for smart contract calls.
5
- * This class encapsulates both primitive values and complex structures (arrays, tuples)
6
- * in a format suitable for ABI encoding.
7
- */
8
- export class EvmEncodeParam implements Serializable {
9
- private static readonly SERIALIZED_PREFIX: string = 'EvmEncodeParam'
10
-
11
- private _type: string
12
- private _value: string
13
- private _values: EvmEncodeParam[]
14
-
15
- /**
16
- * Creates a new EvmEncodeParam instance.
17
- * @param type - The ABI type signature (e.g., "uint256", "address", "string", "()")
18
- * @param value - The serialized value for primitive types
19
- * @param values - Array of nested EvmEncodeParam instances for complex types
20
- */
21
- constructor(type: string, value: string, values: EvmEncodeParam[]) {
22
- this._type = type
23
- this._value = value
24
- this._values = values
25
- }
26
-
27
- /**
28
- * Creates an EvmEncodeParam for a primitive value.
29
- * @param type - The ABI type signature (e.g., "uint256", "address", "string")
30
- * @param value - The value to encode, must implement Stringable interface
31
- * @returns A new EvmEncodeParam instance representing the primitive value
32
- */
33
- static fromValue<T extends Stringable>(type: string, value: T): EvmEncodeParam {
34
- return new EvmEncodeParam(type, serialize(value), [])
35
- }
36
-
37
- /**
38
- * Creates an EvmEncodeParam for complex types like arrays or tuples.
39
- * @param type - The ABI type signature (e.g., "address[]", "()", "()[]")
40
- * @param values - Array of EvmEncodeParam instances representing the nested elements
41
- * @returns A new EvmEncodeParam instance representing the complex type
42
- */
43
- static fromValues(type: string, values: EvmEncodeParam[]): EvmEncodeParam {
44
- return new EvmEncodeParam(type, '', values)
45
- }
46
-
47
- toString(): string {
48
- return `${EvmEncodeParam.SERIALIZED_PREFIX}(${join([serialize(this._type), serialize(this._value), serializeArray(this._values)])})`
49
- }
50
-
51
- serialize(): string {
52
- return this.toString()
53
- }
54
- }