@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
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { environment } from '../environment'
|
|
2
|
+
import { NULL_ADDRESS } from '../helpers'
|
|
3
|
+
import { Address, Bytes, ChainId } from '../types'
|
|
4
|
+
|
|
5
|
+
export enum OperationType {
|
|
6
|
+
Swap,
|
|
7
|
+
Transfer,
|
|
8
|
+
EvmCall,
|
|
9
|
+
CrossChainSwap,
|
|
10
|
+
EvmDynamicCall,
|
|
11
|
+
SvmCall,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Represents an operation event.
|
|
16
|
+
* Specifies the topic and data for the event. The topic is an indexed parameter for the EVM events.
|
|
17
|
+
*/
|
|
18
|
+
@json
|
|
19
|
+
export class OperationEvent {
|
|
20
|
+
topic: string
|
|
21
|
+
data: string
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Creates a new Operation Event instance.
|
|
25
|
+
* @param topic - the topic that is going to be index in the event
|
|
26
|
+
* @param data - The event data
|
|
27
|
+
*/
|
|
28
|
+
constructor(topic: Bytes, data: Bytes) {
|
|
29
|
+
this.topic = topic.toHexString()
|
|
30
|
+
this.data = data.toHexString()
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Base builder for creating operations.
|
|
36
|
+
*/
|
|
37
|
+
export abstract class OperationBuilder {
|
|
38
|
+
protected user: Address | null = null
|
|
39
|
+
protected events: OperationEvent[] = []
|
|
40
|
+
|
|
41
|
+
abstract build(): Operation
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Sets the user address for this intent.
|
|
45
|
+
* @param user - The user address
|
|
46
|
+
* @returns This OperationBuilder instance for method chaining
|
|
47
|
+
*/
|
|
48
|
+
addUser(user: Address): OperationBuilder {
|
|
49
|
+
this.user = user
|
|
50
|
+
return this
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Sets the user address from a string.
|
|
55
|
+
* @param user - The user address as a hex string
|
|
56
|
+
* @returns This OperationBuilder instance for method chaining
|
|
57
|
+
*/
|
|
58
|
+
addUserAsString(user: string): OperationBuilder {
|
|
59
|
+
return this.addUser(Address.fromString(user))
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Sets an event for the intent.
|
|
64
|
+
* @param topic - The topic to be indexed in the event
|
|
65
|
+
* @param data - The event data
|
|
66
|
+
* @returns This OperationBuilder instance for method chaining
|
|
67
|
+
*/
|
|
68
|
+
addEvent(topic: Bytes, data: Bytes): OperationBuilder {
|
|
69
|
+
const event = new OperationEvent(topic, data)
|
|
70
|
+
this.events.push(event)
|
|
71
|
+
return this
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Sets multiple events for the intent.
|
|
76
|
+
* @param events - The list of events to be added
|
|
77
|
+
* @returns This OperationBuilder instance for method chaining
|
|
78
|
+
*/
|
|
79
|
+
addEvents(events: OperationEvent[]): OperationBuilder {
|
|
80
|
+
for (let i = 0; i < events.length; i++) {
|
|
81
|
+
this.events.push(events[i])
|
|
82
|
+
}
|
|
83
|
+
return this
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
@json
|
|
88
|
+
export abstract class Operation {
|
|
89
|
+
public opType: OperationType
|
|
90
|
+
public chainId: ChainId
|
|
91
|
+
public user: string
|
|
92
|
+
public events: OperationEvent[]
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Creates a new operation.
|
|
96
|
+
* @param opType - The type of operation to be created
|
|
97
|
+
* @param chainId - The source chain id for the operation
|
|
98
|
+
* @param user - The user address (optional)
|
|
99
|
+
* @param events - The list of events for the operation (optional)
|
|
100
|
+
*/
|
|
101
|
+
protected constructor(
|
|
102
|
+
opType: OperationType,
|
|
103
|
+
chainId: ChainId,
|
|
104
|
+
user: Address | null,
|
|
105
|
+
events: OperationEvent[] | null
|
|
106
|
+
) {
|
|
107
|
+
const context = environment.getContext()
|
|
108
|
+
this.opType = opType
|
|
109
|
+
this.chainId = chainId
|
|
110
|
+
this.user = user ? user.toString() : context.user.toString()
|
|
111
|
+
this.events = events || []
|
|
112
|
+
if (!this.user || this.user == NULL_ADDRESS) throw new Error('A user must be specified')
|
|
113
|
+
}
|
|
114
|
+
}
|
package/src/intents/Swap.ts
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
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 Swap intents with token exchange operations.
|
|
9
10
|
* Supports both single-chain and cross-chain swaps with multiple input and output tokens.
|
|
10
11
|
*/
|
|
11
|
-
export class SwapBuilder extends
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
export class SwapBuilder extends OperationBuilder {
|
|
13
|
+
protected sourceChain: ChainId
|
|
14
|
+
protected destinationChain: ChainId
|
|
15
|
+
protected tokensIn: SwapTokenIn[] = []
|
|
16
|
+
protected tokensOut: SwapTokenOut[] = []
|
|
16
17
|
|
|
17
18
|
/**
|
|
18
19
|
* Creates a SwapBuilder for a single-chain swap.
|
|
@@ -38,7 +39,7 @@ export class SwapBuilder extends IntentBuilder {
|
|
|
38
39
|
* @param sourceChain - The source blockchain network identifier
|
|
39
40
|
* @param destinationChain - The destination blockchain network identifier
|
|
40
41
|
*/
|
|
41
|
-
constructor(sourceChain: ChainId, destinationChain: ChainId) {
|
|
42
|
+
private constructor(sourceChain: ChainId, destinationChain: ChainId) {
|
|
42
43
|
super()
|
|
43
44
|
this.sourceChain = sourceChain
|
|
44
45
|
this.destinationChain = destinationChain
|
|
@@ -49,7 +50,7 @@ export class SwapBuilder extends IntentBuilder {
|
|
|
49
50
|
* @param tokenIn - The token input configuration
|
|
50
51
|
* @returns This SwapBuilder instance for method chaining
|
|
51
52
|
*/
|
|
52
|
-
addTokenIn(tokenIn:
|
|
53
|
+
addTokenIn(tokenIn: SwapTokenIn): SwapBuilder {
|
|
53
54
|
this.tokensIn.push(tokenIn)
|
|
54
55
|
return this
|
|
55
56
|
}
|
|
@@ -59,17 +60,44 @@ export class SwapBuilder extends IntentBuilder {
|
|
|
59
60
|
* @param tokensIn - Array of token input configurations
|
|
60
61
|
* @returns This SwapBuilder instance for method chaining
|
|
61
62
|
*/
|
|
62
|
-
addTokensIn(tokensIn:
|
|
63
|
+
addTokensIn(tokensIn: SwapTokenIn[]): SwapBuilder {
|
|
63
64
|
for (let i = 0; i < tokensIn.length; i++) this.addTokenIn(tokensIn[i])
|
|
64
65
|
return this
|
|
65
66
|
}
|
|
66
67
|
|
|
68
|
+
/**
|
|
69
|
+
* Adds the tokens in from another SwapBuilder to this SwapBuilder.
|
|
70
|
+
* @param builder - The SwapBuilder to add the tokens in from
|
|
71
|
+
* @returns This SwapBuilder instance for method chaining
|
|
72
|
+
*/
|
|
73
|
+
addTokensInFromBuilder(builder: SwapBuilder): SwapBuilder {
|
|
74
|
+
return this.addTokensIn(builder.getTokensIn())
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Adds the tokens in from multiple SwapBuilders to this SwapBuilder.
|
|
79
|
+
* @param builders - The SwapBuilders to add the tokens in from
|
|
80
|
+
* @returns This SwapBuilder instance for method chaining
|
|
81
|
+
*/
|
|
82
|
+
addTokensInFromBuilders(builders: SwapBuilder[]): SwapBuilder {
|
|
83
|
+
for (let i = 0; i < builders.length; i++) this.addTokensInFromBuilder(builders[i])
|
|
84
|
+
return this
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Returns a copy of the tokens in array.
|
|
89
|
+
* @returns A copy of the tokens in array
|
|
90
|
+
*/
|
|
91
|
+
getTokensIn(): SwapTokenIn[] {
|
|
92
|
+
return this.tokensIn.slice(0)
|
|
93
|
+
}
|
|
94
|
+
|
|
67
95
|
/**
|
|
68
96
|
* Adds an output token to the swap.
|
|
69
97
|
* @param tokenOut - The token output configuration
|
|
70
98
|
* @returns This SwapBuilder instance for method chaining
|
|
71
99
|
*/
|
|
72
|
-
addTokenOut(tokenOut:
|
|
100
|
+
addTokenOut(tokenOut: SwapTokenOut): SwapBuilder {
|
|
73
101
|
this.tokensOut.push(tokenOut)
|
|
74
102
|
return this
|
|
75
103
|
}
|
|
@@ -79,19 +107,46 @@ export class SwapBuilder extends IntentBuilder {
|
|
|
79
107
|
* @param tokensOut - Array of token output configurations
|
|
80
108
|
* @returns This SwapBuilder instance for method chaining
|
|
81
109
|
*/
|
|
82
|
-
addTokensOut(tokensOut:
|
|
110
|
+
addTokensOut(tokensOut: SwapTokenOut[]): SwapBuilder {
|
|
83
111
|
for (let i = 0; i < tokensOut.length; i++) this.addTokenOut(tokensOut[i])
|
|
84
112
|
return this
|
|
85
113
|
}
|
|
86
114
|
|
|
115
|
+
/**
|
|
116
|
+
* Adds the tokens out from another SwapBuilder to this SwapBuilder.
|
|
117
|
+
* @param builder - The SwapBuilder to add the tokens out from
|
|
118
|
+
* @returns This SwapBuilder instance for method chaining
|
|
119
|
+
*/
|
|
120
|
+
addTokensOutFromBuilder(builder: SwapBuilder): SwapBuilder {
|
|
121
|
+
return this.addTokensOut(builder.getTokensOut())
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Adds the tokens out from multiple SwapBuilders to this SwapBuilder.
|
|
126
|
+
* @param builders - The SwapBuilders to add the tokens out from
|
|
127
|
+
* @returns This SwapBuilder instance for method chaining
|
|
128
|
+
*/
|
|
129
|
+
addTokensOutFromBuilders(builders: SwapBuilder[]): SwapBuilder {
|
|
130
|
+
for (let i = 0; i < builders.length; i++) this.addTokensOutFromBuilder(builders[i])
|
|
131
|
+
return this
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Returns a copy of the tokens out array.
|
|
136
|
+
* @returns A copy of the tokens out array
|
|
137
|
+
*/
|
|
138
|
+
getTokensOut(): SwapTokenOut[] {
|
|
139
|
+
return this.tokensOut.slice(0)
|
|
140
|
+
}
|
|
141
|
+
|
|
87
142
|
/**
|
|
88
143
|
* Adds an input token from a TokenAmount.
|
|
89
144
|
* @param tokenAmount - The token amount to swap from (must be on source chain)
|
|
90
145
|
* @returns This SwapBuilder instance for method chaining
|
|
91
146
|
*/
|
|
92
147
|
addTokenInFromTokenAmount(tokenAmount: TokenAmount): SwapBuilder {
|
|
93
|
-
if (tokenAmount.token.
|
|
94
|
-
return this.addTokenIn(
|
|
148
|
+
if (!tokenAmount.token.hasChain(this.sourceChain)) throw new Error('Tokens in must be on the same chain')
|
|
149
|
+
return this.addTokenIn(SwapTokenIn.fromTokenAmount(tokenAmount))
|
|
95
150
|
}
|
|
96
151
|
|
|
97
152
|
/**
|
|
@@ -111,8 +166,8 @@ export class SwapBuilder extends IntentBuilder {
|
|
|
111
166
|
* @returns This SwapBuilder instance for method chaining
|
|
112
167
|
*/
|
|
113
168
|
addTokenInFromStringDecimal(token: Token, amount: string): SwapBuilder {
|
|
114
|
-
if (token.
|
|
115
|
-
return this.addTokenIn(
|
|
169
|
+
if (!token.hasChain(this.sourceChain)) throw new Error('Tokens in must be on the source chain')
|
|
170
|
+
return this.addTokenIn(SwapTokenIn.fromStringDecimal(token, amount))
|
|
116
171
|
}
|
|
117
172
|
|
|
118
173
|
/**
|
|
@@ -122,8 +177,9 @@ export class SwapBuilder extends IntentBuilder {
|
|
|
122
177
|
* @returns This SwapBuilder instance for method chaining
|
|
123
178
|
*/
|
|
124
179
|
addTokenOutFromTokenAmount(tokenAmount: TokenAmount, recipient: Address): SwapBuilder {
|
|
125
|
-
if (tokenAmount.token.
|
|
126
|
-
|
|
180
|
+
if (!tokenAmount.token.hasChain(this.destinationChain))
|
|
181
|
+
throw new Error('Tokens out must be on the destination chain')
|
|
182
|
+
return this.addTokenOut(SwapTokenOut.fromTokenAmount(tokenAmount, recipient))
|
|
127
183
|
}
|
|
128
184
|
|
|
129
185
|
/**
|
|
@@ -145,35 +201,8 @@ export class SwapBuilder extends IntentBuilder {
|
|
|
145
201
|
* @returns This SwapBuilder instance for method chaining
|
|
146
202
|
*/
|
|
147
203
|
addTokenOutFromStringDecimal(token: Token, amount: string, recipient: Address): SwapBuilder {
|
|
148
|
-
if (token.
|
|
149
|
-
return this.addTokenOut(
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Sets the settler address for this intent.
|
|
154
|
-
* @param settler - The settler address as an Address instance
|
|
155
|
-
* @returns This SwapBuilder instance for method chaining
|
|
156
|
-
*/
|
|
157
|
-
addSettler(settler: Address): SwapBuilder {
|
|
158
|
-
return changetype<SwapBuilder>(super.addSettler(settler))
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Sets the settler address from a string.
|
|
163
|
-
* @param settler - The settler address as a hex string
|
|
164
|
-
* @returns This SwapBuilder instance for method chaining
|
|
165
|
-
*/
|
|
166
|
-
addSettlerAsString(settler: string): SwapBuilder {
|
|
167
|
-
return changetype<SwapBuilder>(super.addSettlerAsString(settler))
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Sets the deadline for this intent.
|
|
172
|
-
* @param deadline - The deadline as a timestamp
|
|
173
|
-
* @returns This SwapBuilder instance for method chaining
|
|
174
|
-
*/
|
|
175
|
-
addDeadline(deadline: BigInt): SwapBuilder {
|
|
176
|
-
return changetype<SwapBuilder>(super.addDeadline(deadline))
|
|
204
|
+
if (!token.hasChain(this.destinationChain)) throw new Error('Tokens out must be on the destination chain')
|
|
205
|
+
return this.addTokenOut(SwapTokenOut.fromStringDecimal(token, amount, recipient))
|
|
177
206
|
}
|
|
178
207
|
|
|
179
208
|
/**
|
|
@@ -195,12 +224,22 @@ export class SwapBuilder extends IntentBuilder {
|
|
|
195
224
|
}
|
|
196
225
|
|
|
197
226
|
/**
|
|
198
|
-
* Sets
|
|
199
|
-
* @param
|
|
227
|
+
* Sets an event for the intent.
|
|
228
|
+
* @param topic - The topic to be indexed in the event
|
|
229
|
+
* @param data - The event data
|
|
200
230
|
* @returns This SwapBuilder instance for method chaining
|
|
201
231
|
*/
|
|
202
|
-
|
|
203
|
-
return changetype<SwapBuilder>(super.
|
|
232
|
+
addEvent(topic: Bytes, data: Bytes): SwapBuilder {
|
|
233
|
+
return changetype<SwapBuilder>(super.addEvent(topic, data))
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Sets multiple events for the intent.
|
|
238
|
+
* @param events - The list of events to be added
|
|
239
|
+
* @returns This SwapBuilder instance for method chaining
|
|
240
|
+
*/
|
|
241
|
+
addEvents(events: OperationEvent[]): SwapBuilder {
|
|
242
|
+
return changetype<SwapBuilder>(super.addEvents(events))
|
|
204
243
|
}
|
|
205
244
|
|
|
206
245
|
/**
|
|
@@ -210,16 +249,16 @@ export class SwapBuilder extends IntentBuilder {
|
|
|
210
249
|
build(): Swap {
|
|
211
250
|
if (this.tokensIn.length === 0 || this.tokensOut.length === 0) throw new Error('Tokens in and out are required')
|
|
212
251
|
|
|
213
|
-
return new Swap(
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
)
|
|
252
|
+
return new Swap(this.sourceChain, this.tokensIn, this.tokensOut, this.destinationChain, this.user, this.events)
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Builds this operation and sends it inside an intent with the provided fee data.
|
|
257
|
+
* @param maxFee - The max fee to pay for the intent (optional for swaps)
|
|
258
|
+
* @param feePayer - The fee payer for the intent (optional)
|
|
259
|
+
*/
|
|
260
|
+
send(maxFee: TokenAmount | null = null, feePayer: Address | null = null): void {
|
|
261
|
+
this.build().send(maxFee, feePayer)
|
|
223
262
|
}
|
|
224
263
|
}
|
|
225
264
|
|
|
@@ -228,7 +267,7 @@ export class SwapBuilder extends IntentBuilder {
|
|
|
228
267
|
* Specifies the token address and amount to be swapped.
|
|
229
268
|
*/
|
|
230
269
|
@json
|
|
231
|
-
export class
|
|
270
|
+
export class SwapTokenIn {
|
|
232
271
|
token: string
|
|
233
272
|
amount: string
|
|
234
273
|
|
|
@@ -237,8 +276,8 @@ export class TokenIn {
|
|
|
237
276
|
* @param tokenAmount - The token amount to swap from
|
|
238
277
|
* @returns A new TokenIn instance
|
|
239
278
|
*/
|
|
240
|
-
static fromTokenAmount(tokenAmount: TokenAmount):
|
|
241
|
-
return new
|
|
279
|
+
static fromTokenAmount(tokenAmount: TokenAmount): SwapTokenIn {
|
|
280
|
+
return new SwapTokenIn(tokenAmount.token.address, tokenAmount.amount)
|
|
242
281
|
}
|
|
243
282
|
|
|
244
283
|
/**
|
|
@@ -247,7 +286,7 @@ export class TokenIn {
|
|
|
247
286
|
* @param amount - The amount as a whole number
|
|
248
287
|
* @returns A new TokenIn instance
|
|
249
288
|
*/
|
|
250
|
-
static fromI32(token: Token, amount: i32):
|
|
289
|
+
static fromI32(token: Token, amount: i32): SwapTokenIn {
|
|
251
290
|
return this.fromTokenAmount(TokenAmount.fromI32(token, amount))
|
|
252
291
|
}
|
|
253
292
|
|
|
@@ -257,7 +296,7 @@ export class TokenIn {
|
|
|
257
296
|
* @param amount - The amount in the token's smallest unit
|
|
258
297
|
* @returns A new TokenIn instance
|
|
259
298
|
*/
|
|
260
|
-
static fromBigInt(token: Token, amount: BigInt):
|
|
299
|
+
static fromBigInt(token: Token, amount: BigInt): SwapTokenIn {
|
|
261
300
|
return this.fromTokenAmount(TokenAmount.fromBigInt(token, amount))
|
|
262
301
|
}
|
|
263
302
|
|
|
@@ -267,7 +306,7 @@ export class TokenIn {
|
|
|
267
306
|
* @param amount - The amount as a decimal string
|
|
268
307
|
* @returns A new TokenIn instance
|
|
269
308
|
*/
|
|
270
|
-
static fromStringDecimal(token: Token, amount: string):
|
|
309
|
+
static fromStringDecimal(token: Token, amount: string): SwapTokenIn {
|
|
271
310
|
return this.fromTokenAmount(TokenAmount.fromStringDecimal(token, amount))
|
|
272
311
|
}
|
|
273
312
|
|
|
@@ -287,7 +326,7 @@ export class TokenIn {
|
|
|
287
326
|
* Specifies the token address, minimum amount to receive, and recipient.
|
|
288
327
|
*/
|
|
289
328
|
@json
|
|
290
|
-
export class
|
|
329
|
+
export class SwapTokenOut {
|
|
291
330
|
token: string
|
|
292
331
|
minAmount: string
|
|
293
332
|
recipient: string
|
|
@@ -298,8 +337,8 @@ export class TokenOut {
|
|
|
298
337
|
* @param recipient - The address to receive the tokens
|
|
299
338
|
* @returns A new TokenOut instance
|
|
300
339
|
*/
|
|
301
|
-
static fromTokenAmount(tokenAmount: TokenAmount, recipient: Address):
|
|
302
|
-
return new
|
|
340
|
+
static fromTokenAmount(tokenAmount: TokenAmount, recipient: Address): SwapTokenOut {
|
|
341
|
+
return new SwapTokenOut(tokenAmount.token.address, tokenAmount.amount, recipient)
|
|
303
342
|
}
|
|
304
343
|
|
|
305
344
|
/**
|
|
@@ -309,7 +348,7 @@ export class TokenOut {
|
|
|
309
348
|
* @param recipient - The address to receive the tokens
|
|
310
349
|
* @returns A new TokenOut instance
|
|
311
350
|
*/
|
|
312
|
-
static fromI32(token: Token, amount: i32, recipient: Address):
|
|
351
|
+
static fromI32(token: Token, amount: i32, recipient: Address): SwapTokenOut {
|
|
313
352
|
return this.fromTokenAmount(TokenAmount.fromI32(token, amount), recipient)
|
|
314
353
|
}
|
|
315
354
|
|
|
@@ -320,7 +359,7 @@ export class TokenOut {
|
|
|
320
359
|
* @param recipient - The address to receive the tokens
|
|
321
360
|
* @returns A new TokenOut instance
|
|
322
361
|
*/
|
|
323
|
-
static fromBigInt(token: Token, amount: BigInt, recipient: Address):
|
|
362
|
+
static fromBigInt(token: Token, amount: BigInt, recipient: Address): SwapTokenOut {
|
|
324
363
|
return this.fromTokenAmount(TokenAmount.fromBigInt(token, amount), recipient)
|
|
325
364
|
}
|
|
326
365
|
|
|
@@ -331,7 +370,7 @@ export class TokenOut {
|
|
|
331
370
|
* @param recipient - The address to receive the tokens
|
|
332
371
|
* @returns A new TokenOut instance
|
|
333
372
|
*/
|
|
334
|
-
static fromStringDecimal(token: Token, amount: string, recipient: Address):
|
|
373
|
+
static fromStringDecimal(token: Token, amount: string, recipient: Address): SwapTokenOut {
|
|
335
374
|
return this.fromTokenAmount(TokenAmount.fromStringDecimal(token, amount), recipient)
|
|
336
375
|
}
|
|
337
376
|
|
|
@@ -352,38 +391,7 @@ export class TokenOut {
|
|
|
352
391
|
* Represents a Swap intent for exchanging tokens between blockchain networks.
|
|
353
392
|
*/
|
|
354
393
|
@json
|
|
355
|
-
export class Swap extends
|
|
356
|
-
/**
|
|
357
|
-
* Creates a simple single-chain swap intent.
|
|
358
|
-
* @param chainId - The blockchain network identifier
|
|
359
|
-
* @param tokenIn - The input token address
|
|
360
|
-
* @param amountIn - The amount to swap from
|
|
361
|
-
* @param tokenOut - The output token address
|
|
362
|
-
* @param minAmountOut - The minimum amount to receive
|
|
363
|
-
* @param settler - The settler address (optional)
|
|
364
|
-
* @param user - The user address (optional)
|
|
365
|
-
* @param deadline - The deadline timestamp (optional)
|
|
366
|
-
* @param nonce - The nonce for replay protection (optional)
|
|
367
|
-
* @returns A new Swap instance
|
|
368
|
-
*/
|
|
369
|
-
static create(
|
|
370
|
-
chainId: ChainId,
|
|
371
|
-
tokenIn: Address,
|
|
372
|
-
amountIn: BigInt,
|
|
373
|
-
tokenOut: Address,
|
|
374
|
-
minAmountOut: BigInt,
|
|
375
|
-
settler: Address | null = null,
|
|
376
|
-
user: Address | null = null,
|
|
377
|
-
deadline: BigInt | null = null,
|
|
378
|
-
nonce: string | null = null
|
|
379
|
-
): Swap {
|
|
380
|
-
const context = environment.getContext()
|
|
381
|
-
const recipient = user || context.user
|
|
382
|
-
const swapIn = TokenIn.fromBigInt(Token.fromAddress(tokenIn, chainId), amountIn)
|
|
383
|
-
const swapOut = TokenOut.fromBigInt(Token.fromAddress(tokenOut, chainId), minAmountOut, recipient)
|
|
384
|
-
return new Swap(chainId, [swapIn], [swapOut], chainId, settler, user, deadline, nonce)
|
|
385
|
-
}
|
|
386
|
-
|
|
394
|
+
export class Swap extends Operation {
|
|
387
395
|
/**
|
|
388
396
|
* Creates a new Swap intent.
|
|
389
397
|
* @param sourceChain - The source blockchain network identifier
|
|
@@ -394,26 +402,31 @@ export class Swap extends Intent {
|
|
|
394
402
|
* @param user - The user address (optional)
|
|
395
403
|
* @param deadline - The deadline timestamp (optional)
|
|
396
404
|
* @param nonce - The nonce for replay protection (optional)
|
|
405
|
+
* @param maxFees - The list of max fees to pay for the swap intent (optional)
|
|
397
406
|
*/
|
|
398
407
|
constructor(
|
|
399
408
|
public sourceChain: ChainId,
|
|
400
|
-
public tokensIn:
|
|
401
|
-
public tokensOut:
|
|
409
|
+
public tokensIn: SwapTokenIn[],
|
|
410
|
+
public tokensOut: SwapTokenOut[],
|
|
402
411
|
public destinationChain: ChainId,
|
|
403
|
-
settler: Address | null = null,
|
|
404
412
|
user: Address | null = null,
|
|
405
|
-
|
|
406
|
-
nonce: string | null = null
|
|
413
|
+
events: OperationEvent[] | null = null
|
|
407
414
|
) {
|
|
408
|
-
|
|
415
|
+
const opType = sourceChain == destinationChain ? OperationType.Swap : OperationType.CrossChainSwap
|
|
416
|
+
super(opType, sourceChain, user, events)
|
|
409
417
|
if (tokensIn.length === 0) throw new Error('TokenIn list cannot be empty')
|
|
410
418
|
if (tokensOut.length === 0) throw new Error('TokenOut list cannot be empty')
|
|
411
419
|
}
|
|
412
420
|
|
|
413
421
|
/**
|
|
414
422
|
* Sends this Swap intent to the execution environment.
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
423
|
+
* @param maxFee - The max fee to pay for the intent (optional for swaps)
|
|
424
|
+
* @param feePayer - The fee payer for the intent (optional)
|
|
425
|
+
*/
|
|
426
|
+
send(maxFee: TokenAmount | null = null, feePayer: Address | null = null): void {
|
|
427
|
+
const intentBuilder = new IntentBuilder().addOperation(this)
|
|
428
|
+
if (maxFee) intentBuilder.addMaxFee(maxFee)
|
|
429
|
+
if (feePayer) intentBuilder.addFeePayer(feePayer)
|
|
430
|
+
environment.sendIntent(intentBuilder.build())
|
|
418
431
|
}
|
|
419
432
|
}
|