@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.
- package/CHANGELOG.md +134 -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 +165 -53
- 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 +283 -0
- package/src/intents/Call/SvmCall.ts +278 -0
- package/src/intents/Call/index.ts +2 -0
- package/src/intents/Intent.ts +178 -5
- package/src/intents/Swap.ts +136 -44
- package/src/intents/Transfer.ts +103 -58
- 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 +46 -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/Intent.ts
CHANGED
|
@@ -1,53 +1,204 @@
|
|
|
1
1
|
import { environment } from '../environment'
|
|
2
2
|
import { evm } from '../evm'
|
|
3
3
|
import { NULL_ADDRESS } from '../helpers'
|
|
4
|
-
import {
|
|
4
|
+
import { Token, TokenAmount } from '../tokens'
|
|
5
|
+
import { Address, BigInt, Bytes, ChainId } from '../types'
|
|
5
6
|
|
|
6
7
|
export enum OperationType {
|
|
7
8
|
Swap,
|
|
8
9
|
Transfer,
|
|
9
|
-
|
|
10
|
+
EvmCall,
|
|
11
|
+
SvmCall,
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
const DEFAULT_DEADLINE = 5 * 60 // 5 minutes in seconds
|
|
13
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Base builder for creating intents.
|
|
18
|
+
*/
|
|
14
19
|
export abstract class IntentBuilder {
|
|
15
20
|
protected user: Address | null = null
|
|
16
21
|
protected settler: Address | null = null
|
|
17
22
|
protected deadline: BigInt | null = null
|
|
18
23
|
protected nonce: string | null = null
|
|
24
|
+
protected maxFees: TokenAmount[] = []
|
|
25
|
+
protected events: IntentEvent[] = []
|
|
19
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Sets the settler address for this intent.
|
|
29
|
+
* @param settler - The settler address as an Address instance
|
|
30
|
+
* @returns This IntentBuilder instance for method chaining
|
|
31
|
+
*/
|
|
20
32
|
addSettler(settler: Address): IntentBuilder {
|
|
21
33
|
this.settler = settler
|
|
22
34
|
return this
|
|
23
35
|
}
|
|
24
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Sets the settler address from a string.
|
|
39
|
+
* @param settler - The settler address as a hex string
|
|
40
|
+
* @returns This IntentBuilder instance for method chaining
|
|
41
|
+
*/
|
|
25
42
|
addSettlerAsString(settler: string): IntentBuilder {
|
|
26
43
|
return this.addSettler(Address.fromString(settler))
|
|
27
44
|
}
|
|
28
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Sets the deadline for this intent.
|
|
48
|
+
* @param deadline - The deadline as a timestamp
|
|
49
|
+
* @returns This IntentBuilder instance for method chaining
|
|
50
|
+
*/
|
|
29
51
|
addDeadline(deadline: BigInt): IntentBuilder {
|
|
30
52
|
this.deadline = deadline
|
|
31
53
|
return this
|
|
32
54
|
}
|
|
33
55
|
|
|
56
|
+
/**
|
|
57
|
+
* Sets the user address for this intent.
|
|
58
|
+
* @param user - The user address
|
|
59
|
+
* @returns This IntentBuilder instance for method chaining
|
|
60
|
+
*/
|
|
34
61
|
addUser(user: Address): IntentBuilder {
|
|
35
62
|
this.user = user
|
|
36
63
|
return this
|
|
37
64
|
}
|
|
38
65
|
|
|
66
|
+
/**
|
|
67
|
+
* Sets the user address from a string.
|
|
68
|
+
* @param user - The user address as a hex string
|
|
69
|
+
* @returns This IntentBuilder instance for method chaining
|
|
70
|
+
*/
|
|
39
71
|
addUserAsString(user: string): IntentBuilder {
|
|
40
72
|
return this.addUser(Address.fromString(user))
|
|
41
73
|
}
|
|
42
74
|
|
|
75
|
+
/**
|
|
76
|
+
* Sets the nonce for this intent.
|
|
77
|
+
* @param nonce - The nonce to be set for the intent
|
|
78
|
+
* @returns This IntentBuilder instance for method chaining
|
|
79
|
+
*/
|
|
43
80
|
addNonce(nonce: string): IntentBuilder {
|
|
44
81
|
this.nonce = nonce
|
|
45
82
|
return this
|
|
46
83
|
}
|
|
47
84
|
|
|
85
|
+
/**
|
|
86
|
+
* Sets an event for the intent.
|
|
87
|
+
* @param topic - The topic to be indexed in the event
|
|
88
|
+
* @param data - The event data
|
|
89
|
+
* @returns This IntentBuilder instance for method chaining
|
|
90
|
+
*/
|
|
91
|
+
addEvent(topic: Bytes, data: Bytes): IntentBuilder {
|
|
92
|
+
const event = new IntentEvent(topic, data)
|
|
93
|
+
this.events.push(event)
|
|
94
|
+
return this
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Sets multiple events for the intent.
|
|
99
|
+
* @param events - The list of events to be added
|
|
100
|
+
* @returns This IntentBuilder instance for method chaining
|
|
101
|
+
*/
|
|
102
|
+
addEvents(events: IntentEvent[]): IntentBuilder {
|
|
103
|
+
for (let i = 0; i < events.length; i++) {
|
|
104
|
+
this.events.push(events[i])
|
|
105
|
+
}
|
|
106
|
+
return this
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Adds a max fee for this intent.
|
|
111
|
+
* @param fee - The max fee token amount (must be on same chain)
|
|
112
|
+
* @returns This IntentBuilder instance for method chaining
|
|
113
|
+
*/
|
|
114
|
+
abstract addMaxFee(fee: TokenAmount): IntentBuilder
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Builds and returns the final intent.
|
|
118
|
+
* @returns A new intent
|
|
119
|
+
*/
|
|
48
120
|
abstract build(): Intent
|
|
49
121
|
}
|
|
50
122
|
|
|
123
|
+
/**
|
|
124
|
+
* Represents an intent max fee.
|
|
125
|
+
* Specifies the token address and the max amount to be paid for the intent.
|
|
126
|
+
*/
|
|
127
|
+
@json
|
|
128
|
+
export class MaxFee {
|
|
129
|
+
token: string
|
|
130
|
+
amount: string
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Creates a MaxFee from a TokenAmount.
|
|
134
|
+
* @param tokenAmount - The token amount to be used as max fee
|
|
135
|
+
* @returns A new MaxFee instance
|
|
136
|
+
*/
|
|
137
|
+
static fromTokenAmount(tokenAmount: TokenAmount): MaxFee {
|
|
138
|
+
return new MaxFee(tokenAmount.token.address, tokenAmount.amount)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Creates a MaxFee from a 32-bit integer amount.
|
|
143
|
+
* @param token - The max fee token
|
|
144
|
+
* @param amount - The max fee amount
|
|
145
|
+
* @returns A new MaxFee instance
|
|
146
|
+
*/
|
|
147
|
+
static fromI32(token: Token, amount: i32): MaxFee {
|
|
148
|
+
return this.fromTokenAmount(TokenAmount.fromI32(token, amount))
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Creates a MaxFee from a BigInt amount.
|
|
153
|
+
* @param token - The max fee token
|
|
154
|
+
* @param amount - The max fee amount in the token's smallest unit
|
|
155
|
+
* @returns A new MaxFee instance
|
|
156
|
+
*/
|
|
157
|
+
static fromBigInt(token: Token, amount: BigInt): MaxFee {
|
|
158
|
+
return this.fromTokenAmount(TokenAmount.fromBigInt(token, amount))
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Creates a MaxFee from a decimal string amount.
|
|
163
|
+
* @param token - The max fee token
|
|
164
|
+
* @param amount - The max fee amount as a decimal string
|
|
165
|
+
* @returns A new MaxFee instance
|
|
166
|
+
*/
|
|
167
|
+
static fromStringDecimal(token: Token, amount: string): MaxFee {
|
|
168
|
+
return this.fromTokenAmount(TokenAmount.fromStringDecimal(token, amount))
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Creates a new MaxFee instance.
|
|
173
|
+
* @param token - The max fee token address
|
|
174
|
+
* @param amount - The max fee amount
|
|
175
|
+
*/
|
|
176
|
+
constructor(token: Address, amount: BigInt) {
|
|
177
|
+
this.token = token.toString()
|
|
178
|
+
this.amount = amount.toString()
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Represents an intent event.
|
|
184
|
+
* Specifies the topic and data for the event. The topic is an indexed parameter for the EVM events.
|
|
185
|
+
*/
|
|
186
|
+
@json
|
|
187
|
+
export class IntentEvent {
|
|
188
|
+
topic: string
|
|
189
|
+
data: string
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Creates a new Intent Event instance.
|
|
193
|
+
* @param topic - the topic that is going to be index in the event
|
|
194
|
+
* @param data - The event data
|
|
195
|
+
*/
|
|
196
|
+
constructor(topic: Bytes, data: Bytes) {
|
|
197
|
+
this.topic = topic.toHexString()
|
|
198
|
+
this.data = data.toHexString()
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
51
202
|
let INTENT_INDEX: u32 = 0
|
|
52
203
|
@json
|
|
53
204
|
export abstract class Intent {
|
|
@@ -56,24 +207,46 @@ export abstract class Intent {
|
|
|
56
207
|
public user: string
|
|
57
208
|
public deadline: string
|
|
58
209
|
public nonce: string
|
|
210
|
+
public maxFees: MaxFee[]
|
|
211
|
+
public events: IntentEvent[]
|
|
59
212
|
|
|
213
|
+
/**
|
|
214
|
+
* Creates a new intent.
|
|
215
|
+
* @param op - The type of intent to be created
|
|
216
|
+
* @param chainId - The chain ID for fetch the settler
|
|
217
|
+
* @param maxFees - The list of max fees to pay for the intent (optional)
|
|
218
|
+
* @param settler - The settler address (optional)
|
|
219
|
+
* @param user - The user address (optional)
|
|
220
|
+
* @param deadline - The deadline timestamp (optional)
|
|
221
|
+
* @param nonce - The nonce for replay protection (optional)
|
|
222
|
+
*/
|
|
60
223
|
protected constructor(
|
|
61
224
|
op: OperationType,
|
|
225
|
+
chainId: ChainId,
|
|
226
|
+
maxFees: MaxFee[] | null,
|
|
62
227
|
settler: Address | null,
|
|
63
228
|
user: Address | null,
|
|
64
229
|
deadline: BigInt | null,
|
|
65
|
-
nonce: string | null
|
|
230
|
+
nonce: string | null,
|
|
231
|
+
events: IntentEvent[] | null
|
|
66
232
|
) {
|
|
67
233
|
const context = environment.getContext()
|
|
68
234
|
this.op = op
|
|
69
|
-
this.
|
|
235
|
+
this.maxFees = maxFees || []
|
|
236
|
+
this.settler = settler ? settler.toString() : context.findSettler(chainId).toString()
|
|
70
237
|
this.deadline = deadline ? deadline.toString() : (context.timestamp / 1000 + DEFAULT_DEADLINE).toString()
|
|
71
238
|
this.user = user ? user.toString() : context.user.toString()
|
|
72
|
-
this.
|
|
239
|
+
this.events = events || []
|
|
240
|
+
this.nonce = nonce
|
|
241
|
+
? nonce
|
|
242
|
+
: evm.keccak(`${context.triggerSig}${context.timestamp}${context.triggerPayload.data}${++INTENT_INDEX}`)
|
|
73
243
|
|
|
74
244
|
if (!this.user || this.user == NULL_ADDRESS) throw new Error('A user must be specified')
|
|
75
245
|
if (!this.settler || this.settler == NULL_ADDRESS) throw new Error('A settler contract must be specified')
|
|
76
246
|
}
|
|
77
247
|
|
|
248
|
+
/**
|
|
249
|
+
* Sends this intent to the execution environment.
|
|
250
|
+
*/
|
|
78
251
|
abstract send(): void
|
|
79
252
|
}
|
package/src/intents/Swap.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
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 { Intent, IntentBuilder, IntentEvent, MaxFee, OperationType } from './Intent'
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Builder for creating Swap intents with token exchange operations.
|
|
9
9
|
* Supports both single-chain and cross-chain swaps with multiple input and output tokens.
|
|
10
10
|
*/
|
|
11
11
|
export class SwapBuilder extends IntentBuilder {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
protected sourceChain: ChainId
|
|
13
|
+
protected destinationChain: ChainId
|
|
14
|
+
protected tokensIn: SwapTokenIn[] = []
|
|
15
|
+
protected tokensOut: SwapTokenOut[] = []
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* Creates a SwapBuilder for a single-chain swap.
|
|
@@ -38,7 +38,7 @@ export class SwapBuilder extends IntentBuilder {
|
|
|
38
38
|
* @param sourceChain - The source blockchain network identifier
|
|
39
39
|
* @param destinationChain - The destination blockchain network identifier
|
|
40
40
|
*/
|
|
41
|
-
constructor(sourceChain: ChainId, destinationChain: ChainId) {
|
|
41
|
+
private constructor(sourceChain: ChainId, destinationChain: ChainId) {
|
|
42
42
|
super()
|
|
43
43
|
this.sourceChain = sourceChain
|
|
44
44
|
this.destinationChain = destinationChain
|
|
@@ -49,7 +49,7 @@ export class SwapBuilder extends IntentBuilder {
|
|
|
49
49
|
* @param tokenIn - The token input configuration
|
|
50
50
|
* @returns This SwapBuilder instance for method chaining
|
|
51
51
|
*/
|
|
52
|
-
addTokenIn(tokenIn:
|
|
52
|
+
addTokenIn(tokenIn: SwapTokenIn): SwapBuilder {
|
|
53
53
|
this.tokensIn.push(tokenIn)
|
|
54
54
|
return this
|
|
55
55
|
}
|
|
@@ -59,17 +59,44 @@ export class SwapBuilder extends IntentBuilder {
|
|
|
59
59
|
* @param tokensIn - Array of token input configurations
|
|
60
60
|
* @returns This SwapBuilder instance for method chaining
|
|
61
61
|
*/
|
|
62
|
-
addTokensIn(tokensIn:
|
|
62
|
+
addTokensIn(tokensIn: SwapTokenIn[]): SwapBuilder {
|
|
63
63
|
for (let i = 0; i < tokensIn.length; i++) this.addTokenIn(tokensIn[i])
|
|
64
64
|
return this
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Adds the tokens in from another SwapBuilder to this SwapBuilder.
|
|
69
|
+
* @param builder - The SwapBuilder to add the tokens in from
|
|
70
|
+
* @returns This SwapBuilder instance for method chaining
|
|
71
|
+
*/
|
|
72
|
+
addTokensInFromBuilder(builder: SwapBuilder): SwapBuilder {
|
|
73
|
+
return this.addTokensIn(builder.getTokensIn())
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Adds the tokens in from multiple SwapBuilders to this SwapBuilder.
|
|
78
|
+
* @param builders - The SwapBuilders to add the tokens in from
|
|
79
|
+
* @returns This SwapBuilder instance for method chaining
|
|
80
|
+
*/
|
|
81
|
+
addTokensInFromBuilders(builders: SwapBuilder[]): SwapBuilder {
|
|
82
|
+
for (let i = 0; i < builders.length; i++) this.addTokensInFromBuilder(builders[i])
|
|
83
|
+
return this
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Returns a copy of the tokens in array.
|
|
88
|
+
* @returns A copy of the tokens in array
|
|
89
|
+
*/
|
|
90
|
+
getTokensIn(): SwapTokenIn[] {
|
|
91
|
+
return this.tokensIn.slice(0)
|
|
92
|
+
}
|
|
93
|
+
|
|
67
94
|
/**
|
|
68
95
|
* Adds an output token to the swap.
|
|
69
96
|
* @param tokenOut - The token output configuration
|
|
70
97
|
* @returns This SwapBuilder instance for method chaining
|
|
71
98
|
*/
|
|
72
|
-
addTokenOut(tokenOut:
|
|
99
|
+
addTokenOut(tokenOut: SwapTokenOut): SwapBuilder {
|
|
73
100
|
this.tokensOut.push(tokenOut)
|
|
74
101
|
return this
|
|
75
102
|
}
|
|
@@ -79,19 +106,46 @@ export class SwapBuilder extends IntentBuilder {
|
|
|
79
106
|
* @param tokensOut - Array of token output configurations
|
|
80
107
|
* @returns This SwapBuilder instance for method chaining
|
|
81
108
|
*/
|
|
82
|
-
addTokensOut(tokensOut:
|
|
109
|
+
addTokensOut(tokensOut: SwapTokenOut[]): SwapBuilder {
|
|
83
110
|
for (let i = 0; i < tokensOut.length; i++) this.addTokenOut(tokensOut[i])
|
|
84
111
|
return this
|
|
85
112
|
}
|
|
86
113
|
|
|
114
|
+
/**
|
|
115
|
+
* Adds the tokens out from another SwapBuilder to this SwapBuilder.
|
|
116
|
+
* @param builder - The SwapBuilder to add the tokens out from
|
|
117
|
+
* @returns This SwapBuilder instance for method chaining
|
|
118
|
+
*/
|
|
119
|
+
addTokensOutFromBuilder(builder: SwapBuilder): SwapBuilder {
|
|
120
|
+
return this.addTokensOut(builder.getTokensOut())
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Adds the tokens out from multiple SwapBuilders to this SwapBuilder.
|
|
125
|
+
* @param builders - The SwapBuilders to add the tokens out from
|
|
126
|
+
* @returns This SwapBuilder instance for method chaining
|
|
127
|
+
*/
|
|
128
|
+
addTokensOutFromBuilders(builders: SwapBuilder[]): SwapBuilder {
|
|
129
|
+
for (let i = 0; i < builders.length; i++) this.addTokensOutFromBuilder(builders[i])
|
|
130
|
+
return this
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Returns a copy of the tokens out array.
|
|
135
|
+
* @returns A copy of the tokens out array
|
|
136
|
+
*/
|
|
137
|
+
getTokensOut(): SwapTokenOut[] {
|
|
138
|
+
return this.tokensOut.slice(0)
|
|
139
|
+
}
|
|
140
|
+
|
|
87
141
|
/**
|
|
88
142
|
* Adds an input token from a TokenAmount.
|
|
89
143
|
* @param tokenAmount - The token amount to swap from (must be on source chain)
|
|
90
144
|
* @returns This SwapBuilder instance for method chaining
|
|
91
145
|
*/
|
|
92
146
|
addTokenInFromTokenAmount(tokenAmount: TokenAmount): SwapBuilder {
|
|
93
|
-
if (tokenAmount.token.
|
|
94
|
-
return this.addTokenIn(
|
|
147
|
+
if (!tokenAmount.token.hasChain(this.sourceChain)) throw new Error('Tokens in must be on the same chain')
|
|
148
|
+
return this.addTokenIn(SwapTokenIn.fromTokenAmount(tokenAmount))
|
|
95
149
|
}
|
|
96
150
|
|
|
97
151
|
/**
|
|
@@ -111,8 +165,8 @@ export class SwapBuilder extends IntentBuilder {
|
|
|
111
165
|
* @returns This SwapBuilder instance for method chaining
|
|
112
166
|
*/
|
|
113
167
|
addTokenInFromStringDecimal(token: Token, amount: string): SwapBuilder {
|
|
114
|
-
if (token.
|
|
115
|
-
return this.addTokenIn(
|
|
168
|
+
if (!token.hasChain(this.sourceChain)) throw new Error('Tokens in must be on the source chain')
|
|
169
|
+
return this.addTokenIn(SwapTokenIn.fromStringDecimal(token, amount))
|
|
116
170
|
}
|
|
117
171
|
|
|
118
172
|
/**
|
|
@@ -122,8 +176,9 @@ export class SwapBuilder extends IntentBuilder {
|
|
|
122
176
|
* @returns This SwapBuilder instance for method chaining
|
|
123
177
|
*/
|
|
124
178
|
addTokenOutFromTokenAmount(tokenAmount: TokenAmount, recipient: Address): SwapBuilder {
|
|
125
|
-
if (tokenAmount.token.
|
|
126
|
-
|
|
179
|
+
if (!tokenAmount.token.hasChain(this.destinationChain))
|
|
180
|
+
throw new Error('Tokens out must be on the destination chain')
|
|
181
|
+
return this.addTokenOut(SwapTokenOut.fromTokenAmount(tokenAmount, recipient))
|
|
127
182
|
}
|
|
128
183
|
|
|
129
184
|
/**
|
|
@@ -145,8 +200,8 @@ export class SwapBuilder extends IntentBuilder {
|
|
|
145
200
|
* @returns This SwapBuilder instance for method chaining
|
|
146
201
|
*/
|
|
147
202
|
addTokenOutFromStringDecimal(token: Token, amount: string, recipient: Address): SwapBuilder {
|
|
148
|
-
if (token.
|
|
149
|
-
return this.addTokenOut(
|
|
203
|
+
if (!token.hasChain(this.destinationChain)) throw new Error('Tokens out must be on the destination chain')
|
|
204
|
+
return this.addTokenOut(SwapTokenOut.fromStringDecimal(token, amount, recipient))
|
|
150
205
|
}
|
|
151
206
|
|
|
152
207
|
/**
|
|
@@ -203,6 +258,36 @@ export class SwapBuilder extends IntentBuilder {
|
|
|
203
258
|
return changetype<SwapBuilder>(super.addNonce(nonce))
|
|
204
259
|
}
|
|
205
260
|
|
|
261
|
+
/**
|
|
262
|
+
* Adds a max fee for this intent.
|
|
263
|
+
* @param fee - The max fee token amount (must be on same chain)
|
|
264
|
+
* @returns This SwapBuilder instance for method chaining
|
|
265
|
+
*/
|
|
266
|
+
addMaxFee(fee: TokenAmount): SwapBuilder {
|
|
267
|
+
if (!fee.token.hasChain(this.destinationChain)) throw new Error('Fee token must be on the destination chain')
|
|
268
|
+
this.maxFees.push(fee)
|
|
269
|
+
return this
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Sets an event for the intent.
|
|
274
|
+
* @param topic - The topic to be indexed in the event
|
|
275
|
+
* @param data - The event data
|
|
276
|
+
* @returns This SwapBuilder instance for method chaining
|
|
277
|
+
*/
|
|
278
|
+
addEvent(topic: Bytes, data: Bytes): SwapBuilder {
|
|
279
|
+
return changetype<SwapBuilder>(super.addEvent(topic, data))
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Sets multiple events for the intent.
|
|
284
|
+
* @param events - The list of events to be added
|
|
285
|
+
* @returns This SwapBuilder instance for method chaining
|
|
286
|
+
*/
|
|
287
|
+
addEvents(events: IntentEvent[]): SwapBuilder {
|
|
288
|
+
return changetype<SwapBuilder>(super.addEvents(events))
|
|
289
|
+
}
|
|
290
|
+
|
|
206
291
|
/**
|
|
207
292
|
* Builds and returns the final Swap intent.
|
|
208
293
|
* @returns A new Swap instance with all configured parameters
|
|
@@ -218,7 +303,9 @@ export class SwapBuilder extends IntentBuilder {
|
|
|
218
303
|
this.settler,
|
|
219
304
|
this.user,
|
|
220
305
|
this.deadline,
|
|
221
|
-
this.nonce
|
|
306
|
+
this.nonce,
|
|
307
|
+
this.maxFees,
|
|
308
|
+
this.events
|
|
222
309
|
)
|
|
223
310
|
}
|
|
224
311
|
}
|
|
@@ -228,7 +315,7 @@ export class SwapBuilder extends IntentBuilder {
|
|
|
228
315
|
* Specifies the token address and amount to be swapped.
|
|
229
316
|
*/
|
|
230
317
|
@json
|
|
231
|
-
export class
|
|
318
|
+
export class SwapTokenIn {
|
|
232
319
|
token: string
|
|
233
320
|
amount: string
|
|
234
321
|
|
|
@@ -237,8 +324,8 @@ export class TokenIn {
|
|
|
237
324
|
* @param tokenAmount - The token amount to swap from
|
|
238
325
|
* @returns A new TokenIn instance
|
|
239
326
|
*/
|
|
240
|
-
static fromTokenAmount(tokenAmount: TokenAmount):
|
|
241
|
-
return new
|
|
327
|
+
static fromTokenAmount(tokenAmount: TokenAmount): SwapTokenIn {
|
|
328
|
+
return new SwapTokenIn(tokenAmount.token.address, tokenAmount.amount)
|
|
242
329
|
}
|
|
243
330
|
|
|
244
331
|
/**
|
|
@@ -247,7 +334,7 @@ export class TokenIn {
|
|
|
247
334
|
* @param amount - The amount as a whole number
|
|
248
335
|
* @returns A new TokenIn instance
|
|
249
336
|
*/
|
|
250
|
-
static fromI32(token: Token, amount: i32):
|
|
337
|
+
static fromI32(token: Token, amount: i32): SwapTokenIn {
|
|
251
338
|
return this.fromTokenAmount(TokenAmount.fromI32(token, amount))
|
|
252
339
|
}
|
|
253
340
|
|
|
@@ -257,7 +344,7 @@ export class TokenIn {
|
|
|
257
344
|
* @param amount - The amount in the token's smallest unit
|
|
258
345
|
* @returns A new TokenIn instance
|
|
259
346
|
*/
|
|
260
|
-
static fromBigInt(token: Token, amount: BigInt):
|
|
347
|
+
static fromBigInt(token: Token, amount: BigInt): SwapTokenIn {
|
|
261
348
|
return this.fromTokenAmount(TokenAmount.fromBigInt(token, amount))
|
|
262
349
|
}
|
|
263
350
|
|
|
@@ -267,7 +354,7 @@ export class TokenIn {
|
|
|
267
354
|
* @param amount - The amount as a decimal string
|
|
268
355
|
* @returns A new TokenIn instance
|
|
269
356
|
*/
|
|
270
|
-
static fromStringDecimal(token: Token, amount: string):
|
|
357
|
+
static fromStringDecimal(token: Token, amount: string): SwapTokenIn {
|
|
271
358
|
return this.fromTokenAmount(TokenAmount.fromStringDecimal(token, amount))
|
|
272
359
|
}
|
|
273
360
|
|
|
@@ -287,7 +374,7 @@ export class TokenIn {
|
|
|
287
374
|
* Specifies the token address, minimum amount to receive, and recipient.
|
|
288
375
|
*/
|
|
289
376
|
@json
|
|
290
|
-
export class
|
|
377
|
+
export class SwapTokenOut {
|
|
291
378
|
token: string
|
|
292
379
|
minAmount: string
|
|
293
380
|
recipient: string
|
|
@@ -298,8 +385,8 @@ export class TokenOut {
|
|
|
298
385
|
* @param recipient - The address to receive the tokens
|
|
299
386
|
* @returns A new TokenOut instance
|
|
300
387
|
*/
|
|
301
|
-
static fromTokenAmount(tokenAmount: TokenAmount, recipient: Address):
|
|
302
|
-
return new
|
|
388
|
+
static fromTokenAmount(tokenAmount: TokenAmount, recipient: Address): SwapTokenOut {
|
|
389
|
+
return new SwapTokenOut(tokenAmount.token.address, tokenAmount.amount, recipient)
|
|
303
390
|
}
|
|
304
391
|
|
|
305
392
|
/**
|
|
@@ -309,7 +396,7 @@ export class TokenOut {
|
|
|
309
396
|
* @param recipient - The address to receive the tokens
|
|
310
397
|
* @returns A new TokenOut instance
|
|
311
398
|
*/
|
|
312
|
-
static fromI32(token: Token, amount: i32, recipient: Address):
|
|
399
|
+
static fromI32(token: Token, amount: i32, recipient: Address): SwapTokenOut {
|
|
313
400
|
return this.fromTokenAmount(TokenAmount.fromI32(token, amount), recipient)
|
|
314
401
|
}
|
|
315
402
|
|
|
@@ -320,7 +407,7 @@ export class TokenOut {
|
|
|
320
407
|
* @param recipient - The address to receive the tokens
|
|
321
408
|
* @returns A new TokenOut instance
|
|
322
409
|
*/
|
|
323
|
-
static fromBigInt(token: Token, amount: BigInt, recipient: Address):
|
|
410
|
+
static fromBigInt(token: Token, amount: BigInt, recipient: Address): SwapTokenOut {
|
|
324
411
|
return this.fromTokenAmount(TokenAmount.fromBigInt(token, amount), recipient)
|
|
325
412
|
}
|
|
326
413
|
|
|
@@ -331,7 +418,7 @@ export class TokenOut {
|
|
|
331
418
|
* @param recipient - The address to receive the tokens
|
|
332
419
|
* @returns A new TokenOut instance
|
|
333
420
|
*/
|
|
334
|
-
static fromStringDecimal(token: Token, amount: string, recipient: Address):
|
|
421
|
+
static fromStringDecimal(token: Token, amount: string, recipient: Address): SwapTokenOut {
|
|
335
422
|
return this.fromTokenAmount(TokenAmount.fromStringDecimal(token, amount), recipient)
|
|
336
423
|
}
|
|
337
424
|
|
|
@@ -356,9 +443,9 @@ export class Swap extends Intent {
|
|
|
356
443
|
/**
|
|
357
444
|
* Creates a simple single-chain swap intent.
|
|
358
445
|
* @param chainId - The blockchain network identifier
|
|
359
|
-
* @param tokenIn - The input token
|
|
446
|
+
* @param tokenIn - The input token
|
|
360
447
|
* @param amountIn - The amount to swap from
|
|
361
|
-
* @param tokenOut - The output token
|
|
448
|
+
* @param tokenOut - The output token
|
|
362
449
|
* @param minAmountOut - The minimum amount to receive
|
|
363
450
|
* @param settler - The settler address (optional)
|
|
364
451
|
* @param user - The user address (optional)
|
|
@@ -368,20 +455,21 @@ export class Swap extends Intent {
|
|
|
368
455
|
*/
|
|
369
456
|
static create(
|
|
370
457
|
chainId: ChainId,
|
|
371
|
-
tokenIn:
|
|
458
|
+
tokenIn: Token,
|
|
372
459
|
amountIn: BigInt,
|
|
373
|
-
tokenOut:
|
|
460
|
+
tokenOut: Token,
|
|
374
461
|
minAmountOut: BigInt,
|
|
375
462
|
settler: Address | null = null,
|
|
376
463
|
user: Address | null = null,
|
|
377
464
|
deadline: BigInt | null = null,
|
|
378
|
-
nonce: string | null = null
|
|
465
|
+
nonce: string | null = null,
|
|
466
|
+
events: IntentEvent[] | null = null
|
|
379
467
|
): Swap {
|
|
380
468
|
const context = environment.getContext()
|
|
381
469
|
const recipient = user || context.user
|
|
382
|
-
const swapIn =
|
|
383
|
-
const swapOut =
|
|
384
|
-
return new Swap(chainId, [swapIn], [swapOut], chainId, settler, user, deadline, nonce)
|
|
470
|
+
const swapIn = SwapTokenIn.fromBigInt(tokenIn, amountIn)
|
|
471
|
+
const swapOut = SwapTokenOut.fromBigInt(tokenOut, minAmountOut, recipient)
|
|
472
|
+
return new Swap(chainId, [swapIn], [swapOut], chainId, settler, user, deadline, nonce, [], events)
|
|
385
473
|
}
|
|
386
474
|
|
|
387
475
|
/**
|
|
@@ -394,18 +482,22 @@ export class Swap extends Intent {
|
|
|
394
482
|
* @param user - The user address (optional)
|
|
395
483
|
* @param deadline - The deadline timestamp (optional)
|
|
396
484
|
* @param nonce - The nonce for replay protection (optional)
|
|
485
|
+
* @param maxFees - The list of max fees to pay for the swap intent (optional)
|
|
397
486
|
*/
|
|
398
487
|
constructor(
|
|
399
488
|
public sourceChain: ChainId,
|
|
400
|
-
public tokensIn:
|
|
401
|
-
public tokensOut:
|
|
489
|
+
public tokensIn: SwapTokenIn[],
|
|
490
|
+
public tokensOut: SwapTokenOut[],
|
|
402
491
|
public destinationChain: ChainId,
|
|
403
492
|
settler: Address | null = null,
|
|
404
493
|
user: Address | null = null,
|
|
405
494
|
deadline: BigInt | null = null,
|
|
406
|
-
nonce: string | null = null
|
|
495
|
+
nonce: string | null = null,
|
|
496
|
+
maxFees: TokenAmount[] | null = null,
|
|
497
|
+
events: IntentEvent[] | null = null
|
|
407
498
|
) {
|
|
408
|
-
|
|
499
|
+
const fees: MaxFee[] = maxFees ? maxFees.map((fee: TokenAmount) => MaxFee.fromTokenAmount(fee)) : []
|
|
500
|
+
super(OperationType.Swap, sourceChain, fees, settler, user, deadline, nonce, events)
|
|
409
501
|
if (tokensIn.length === 0) throw new Error('TokenIn list cannot be empty')
|
|
410
502
|
if (tokensOut.length === 0) throw new Error('TokenOut list cannot be empty')
|
|
411
503
|
}
|