@mimicprotocol/lib-ts 0.0.1-rc.13 → 0.0.1-rc.14

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mimicprotocol/lib-ts",
3
- "version": "0.0.1-rc.13",
3
+ "version": "0.0.1-rc.14",
4
4
  "license": "GPL-3.0",
5
5
  "private": false,
6
6
  "type": "module",
@@ -3,7 +3,7 @@ import { TokenAmount } from '../tokens'
3
3
  import { ChainId } from '../types'
4
4
  import { Address, BigInt, Bytes } from '../types'
5
5
 
6
- import { Intent, IntentBuilder, OperationType } from './Intent'
6
+ import { Intent, IntentBuilder, MaxFee, OperationType } from './Intent'
7
7
 
8
8
  /**
9
9
  * Builder for creating Call intents with contract call operations.
@@ -195,7 +195,8 @@ export class Call extends Intent {
195
195
  deadline: BigInt | null = null,
196
196
  nonce: string | null = null
197
197
  ) {
198
- super(OperationType.Call, chainId, maxFees, settler, user, deadline, nonce)
198
+ const fees: MaxFee[] = maxFees.map((fee: TokenAmount) => MaxFee.fromTokenAmount(fee))
199
+ super(OperationType.Call, chainId, fees, settler, user, deadline, nonce)
199
200
  if (calls.length === 0) throw new Error('Call list cannot be empty')
200
201
  if (maxFees.length == 0) throw new Error('At least a max fee must be specified')
201
202
 
@@ -1,7 +1,7 @@
1
1
  import { environment } from '../environment'
2
2
  import { evm } from '../evm'
3
3
  import { NULL_ADDRESS } from '../helpers'
4
- import { TokenAmount } from '../tokens'
4
+ import { Token, TokenAmount } from '../tokens'
5
5
  import { Address, BigInt, ChainId } from '../types'
6
6
 
7
7
  export enum OperationType {
@@ -94,6 +94,65 @@ export abstract class IntentBuilder {
94
94
  abstract build(): Intent
95
95
  }
96
96
 
97
+ /**
98
+ * Represents an intent max fee.
99
+ * Specifies the token address and the max amount to be paid for the intent.
100
+ */
101
+ @json
102
+ export class MaxFee {
103
+ token: string
104
+ amount: string
105
+
106
+ /**
107
+ * Creates a MaxFee from a TokenAmount.
108
+ * @param tokenAmount - The token amount to be used as max fee
109
+ * @returns A new MaxFee instance
110
+ */
111
+ static fromTokenAmount(tokenAmount: TokenAmount): MaxFee {
112
+ return new MaxFee(tokenAmount.token.address, tokenAmount.amount)
113
+ }
114
+
115
+ /**
116
+ * Creates a MaxFee from a 32-bit integer amount.
117
+ * @param token - The max fee token
118
+ * @param amount - The max fee amount
119
+ * @returns A new MaxFee instance
120
+ */
121
+ static fromI32(token: Token, amount: i32): MaxFee {
122
+ return this.fromTokenAmount(TokenAmount.fromI32(token, amount))
123
+ }
124
+
125
+ /**
126
+ * Creates a MaxFee from a BigInt amount.
127
+ * @param token - The max fee token
128
+ * @param amount - The max fee amount in the token's smallest unit
129
+ * @returns A new MaxFee instance
130
+ */
131
+ static fromBigInt(token: Token, amount: BigInt): MaxFee {
132
+ return this.fromTokenAmount(TokenAmount.fromBigInt(token, amount))
133
+ }
134
+
135
+ /**
136
+ * Creates a MaxFee from a decimal string amount.
137
+ * @param token - The max fee token
138
+ * @param amount - The max fee amount as a decimal string
139
+ * @returns A new MaxFee instance
140
+ */
141
+ static fromStringDecimal(token: Token, amount: string): MaxFee {
142
+ return this.fromTokenAmount(TokenAmount.fromStringDecimal(token, amount))
143
+ }
144
+
145
+ /**
146
+ * Creates a new MaxFee instance.
147
+ * @param token - The max fee token address
148
+ * @param amount - The max fee amount
149
+ */
150
+ constructor(token: Address, amount: BigInt) {
151
+ this.token = token.toString()
152
+ this.amount = amount.toString()
153
+ }
154
+ }
155
+
97
156
  let INTENT_INDEX: u32 = 0
98
157
  @json
99
158
  export abstract class Intent {
@@ -102,14 +161,13 @@ export abstract class Intent {
102
161
  public user: string
103
162
  public deadline: string
104
163
  public nonce: string
105
- public maxFeeTokens: string[]
106
- public maxFeeAmounts: string[]
164
+ public maxFees: MaxFee[]
107
165
 
108
166
  /**
109
167
  * Creates a new intent.
110
168
  * @param op - The type of intent to be created
111
169
  * @param chainId - The chain ID for fetch the settler
112
- * @param maxFees - The list of max fees to pay for the intent
170
+ * @param maxFees - The list of max fees to pay for the intent (optional)
113
171
  * @param settler - The settler address (optional)
114
172
  * @param user - The user address (optional)
115
173
  * @param deadline - The deadline timestamp (optional)
@@ -118,7 +176,7 @@ export abstract class Intent {
118
176
  protected constructor(
119
177
  op: OperationType,
120
178
  chainId: ChainId,
121
- maxFees: TokenAmount[],
179
+ maxFees: MaxFee[] | null,
122
180
  settler: Address | null,
123
181
  user: Address | null,
124
182
  deadline: BigInt | null,
@@ -126,12 +184,11 @@ export abstract class Intent {
126
184
  ) {
127
185
  const context = environment.getContext()
128
186
  this.op = op
187
+ this.maxFees = maxFees || []
129
188
  this.settler = settler ? settler.toString() : context.findSettler(chainId).toString()
130
189
  this.deadline = deadline ? deadline.toString() : (context.timestamp / 1000 + DEFAULT_DEADLINE).toString()
131
190
  this.user = user ? user.toString() : context.user.toString()
132
191
  this.nonce = nonce ? nonce : evm.keccak(`${context.configSig}${context.timestamp}${++INTENT_INDEX}`)
133
- this.maxFeeTokens = maxFees.map((fee: TokenAmount) => fee.token.address.toString())
134
- this.maxFeeAmounts = maxFees.map((fee: TokenAmount) => fee.amount.toString())
135
192
 
136
193
  if (!this.user || this.user == NULL_ADDRESS) throw new Error('A user must be specified')
137
194
  if (!this.settler || this.settler == NULL_ADDRESS) throw new Error('A settler contract must be specified')
@@ -2,7 +2,7 @@ import { environment } from '../environment'
2
2
  import { ERC20Token, Token, TokenAmount } from '../tokens'
3
3
  import { Address, BigInt, ChainId } from '../types'
4
4
 
5
- import { Intent, IntentBuilder, OperationType } from './Intent'
5
+ import { Intent, IntentBuilder, MaxFee, OperationType } from './Intent'
6
6
 
7
7
  /**
8
8
  * Builder for creating Swap intents with token exchange operations.
@@ -420,7 +420,8 @@ export class Swap extends Intent {
420
420
  nonce: string | null = null,
421
421
  maxFees: TokenAmount[] | null = null
422
422
  ) {
423
- super(OperationType.Swap, sourceChain, maxFees || [], settler, user, deadline, nonce)
423
+ const fees: MaxFee[] = maxFees ? maxFees.map((fee: TokenAmount) => MaxFee.fromTokenAmount(fee)) : []
424
+ super(OperationType.Swap, sourceChain, fees, settler, user, deadline, nonce)
424
425
  if (tokensIn.length === 0) throw new Error('TokenIn list cannot be empty')
425
426
  if (tokensOut.length === 0) throw new Error('TokenOut list cannot be empty')
426
427
  }
@@ -2,7 +2,7 @@ import { environment } from '../environment'
2
2
  import { ERC20Token, Token, TokenAmount } from '../tokens'
3
3
  import { Address, BigInt, ChainId } from '../types'
4
4
 
5
- import { Intent, IntentBuilder, OperationType } from './Intent'
5
+ import { Intent, IntentBuilder, MaxFee, OperationType } from './Intent'
6
6
 
7
7
  /**
8
8
  * Builder for creating Transfer intents with token transfer operations.
@@ -306,7 +306,8 @@ export class Transfer extends Intent {
306
306
  deadline: BigInt | null = null,
307
307
  nonce: string | null = null
308
308
  ) {
309
- super(OperationType.Transfer, chainId, maxFees, settler, user, deadline, nonce)
309
+ const fees: MaxFee[] = maxFees.map((fee: TokenAmount) => MaxFee.fromTokenAmount(fee))
310
+ super(OperationType.Transfer, chainId, fees, settler, user, deadline, nonce)
310
311
  if (transfers.length === 0) throw new Error('Transfer list cannot be empty')
311
312
  if (maxFees.length == 0) throw new Error('At least a max fee must be specified')
312
313