@mimicprotocol/lib-ts 0.0.1-rc.21 → 0.0.1-rc.22
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 +1 -1
- package/src/environment.ts +19 -7
- package/src/intents/{Call.ts → Call/EvmCall.ts} +59 -60
- package/src/intents/Call/SvmCall.ts +233 -0
- package/src/intents/Call/index.ts +2 -0
- package/src/intents/Intent.ts +2 -1
- package/src/tokens/BlockchainToken.ts +12 -2
- package/src/tokens/Token.ts +8 -0
- package/src/tokens/TokenAmount.ts +22 -2
- package/src/types/Address.ts +3 -3
- package/src/types/svm/SvmAccountMeta.ts +28 -0
package/package.json
CHANGED
package/src/environment.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { JSON } from 'json-as/assembly'
|
|
|
2
2
|
|
|
3
3
|
import { Context, SerializableContext } from './context'
|
|
4
4
|
import { ListType } from './helpers'
|
|
5
|
-
import { Swap, Transfer,
|
|
5
|
+
import { Swap, Transfer, EvmCall, SvmCall } from './intents'
|
|
6
6
|
import {
|
|
7
7
|
Call as CallQuery,
|
|
8
8
|
GetAccountsInfo,
|
|
@@ -17,10 +17,14 @@ import {
|
|
|
17
17
|
} from './queries'
|
|
18
18
|
import { BlockchainToken, Token, TokenAmount, USD } from './tokens'
|
|
19
19
|
import { Address, BigInt, ChainId } from './types'
|
|
20
|
+
import { log } from './log'
|
|
20
21
|
|
|
21
22
|
export namespace environment {
|
|
22
|
-
@external('environment', '
|
|
23
|
-
declare function
|
|
23
|
+
@external('environment', '_evmCall')
|
|
24
|
+
declare function _evmCall(params: string): void
|
|
25
|
+
|
|
26
|
+
@external('environment', '_svmCall')
|
|
27
|
+
declare function _svmCall(params: string): void
|
|
24
28
|
|
|
25
29
|
@external('environment', '_swap')
|
|
26
30
|
declare function _swap(params: string): void
|
|
@@ -47,11 +51,19 @@ export namespace environment {
|
|
|
47
51
|
declare function _getContext(): string
|
|
48
52
|
|
|
49
53
|
/**
|
|
50
|
-
* Generates a Call intent containing contract calls on the blockchain.
|
|
51
|
-
* @param call - The
|
|
54
|
+
* Generates a EVM Call intent containing contract calls on the blockchain.
|
|
55
|
+
* @param call - The EvmCall intent to generate
|
|
56
|
+
*/
|
|
57
|
+
export function evmCall(call: EvmCall): void {
|
|
58
|
+
_evmCall(JSON.stringify(call))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Generates a SVM Call intent containing contract calls on the blockchain.
|
|
63
|
+
* @param call - The SvmCall intent to generate
|
|
52
64
|
*/
|
|
53
|
-
export function
|
|
54
|
-
|
|
65
|
+
export function svmCall(call: SvmCall): void {
|
|
66
|
+
_svmCall(JSON.stringify(call))
|
|
55
67
|
}
|
|
56
68
|
|
|
57
69
|
/**
|
|
@@ -1,29 +1,28 @@
|
|
|
1
|
-
import { environment } from '
|
|
2
|
-
import { TokenAmount } from '
|
|
3
|
-
import { Address, BigInt, Bytes, ChainId } from '
|
|
4
|
-
|
|
5
|
-
import { Intent, IntentBuilder, IntentEvent, MaxFee, OperationType } from './Intent'
|
|
1
|
+
import { environment } from '../../environment'
|
|
2
|
+
import { TokenAmount } from '../../tokens'
|
|
3
|
+
import { Address, BigInt, Bytes, ChainId } from '../../types'
|
|
4
|
+
import { Intent, IntentBuilder, IntentEvent, MaxFee, OperationType } from '../Intent'
|
|
6
5
|
|
|
7
6
|
/**
|
|
8
|
-
* Builder for creating Call intents with contract call operations.
|
|
7
|
+
* Builder for creating EVM Call intents with contract call operations.
|
|
9
8
|
* Allows chaining multiple contract calls and configuring fees and settlement parameters.
|
|
10
9
|
*/
|
|
11
|
-
export class
|
|
10
|
+
export class EvmCallBuilder extends IntentBuilder {
|
|
12
11
|
private chainId: ChainId
|
|
13
|
-
private calls:
|
|
12
|
+
private calls: EvmCallData[] = []
|
|
14
13
|
|
|
15
14
|
/**
|
|
16
|
-
* Creates a
|
|
15
|
+
* Creates a EvmCallBuilder for the specified EVM blockchain network.
|
|
17
16
|
* @param chainId - The blockchain network identifier
|
|
18
|
-
* @returns A new
|
|
17
|
+
* @returns A new EvmCallBuilder instance
|
|
19
18
|
*/
|
|
20
|
-
static forChain(chainId: ChainId):
|
|
21
|
-
return new
|
|
19
|
+
static forChain(chainId: ChainId): EvmCallBuilder {
|
|
20
|
+
return new EvmCallBuilder(chainId)
|
|
22
21
|
}
|
|
23
22
|
|
|
24
23
|
/**
|
|
25
|
-
* Creates a new
|
|
26
|
-
* @param chainId - The blockchain network identifier
|
|
24
|
+
* Creates a new EvmCallBuilder instance.
|
|
25
|
+
* @param chainId - The EVM blockchain network identifier
|
|
27
26
|
*/
|
|
28
27
|
constructor(chainId: ChainId) {
|
|
29
28
|
super()
|
|
@@ -35,73 +34,73 @@ export class CallBuilder extends IntentBuilder {
|
|
|
35
34
|
* @param target - The contract address to call
|
|
36
35
|
* @param data - The call data (optional, defaults to empty bytes)
|
|
37
36
|
* @param value - The native token value to send (optional, defaults to zero)
|
|
38
|
-
* @returns This
|
|
37
|
+
* @returns This EvmCallBuilder instance for method chaining
|
|
39
38
|
*/
|
|
40
|
-
addCall(target: Address, data: Bytes = Bytes.empty(), value: BigInt = BigInt.zero()):
|
|
41
|
-
this.calls.push(new
|
|
39
|
+
addCall(target: Address, data: Bytes = Bytes.empty(), value: BigInt = BigInt.zero()): EvmCallBuilder {
|
|
40
|
+
this.calls.push(new EvmCallData(target, data, value))
|
|
42
41
|
return this
|
|
43
42
|
}
|
|
44
43
|
|
|
45
44
|
/**
|
|
46
45
|
* Sets the settler address for this intent.
|
|
47
46
|
* @param settler - The settler address as an Address instance
|
|
48
|
-
* @returns This
|
|
47
|
+
* @returns This EvmCallBuilder instance for method chaining
|
|
49
48
|
*/
|
|
50
|
-
addSettler(settler: Address):
|
|
51
|
-
return changetype<
|
|
49
|
+
addSettler(settler: Address): EvmCallBuilder {
|
|
50
|
+
return changetype<EvmCallBuilder>(super.addSettler(settler))
|
|
52
51
|
}
|
|
53
52
|
|
|
54
53
|
/**
|
|
55
54
|
* Sets the settler address from a string.
|
|
56
55
|
* @param settler - The settler address as a hex string
|
|
57
|
-
* @returns This
|
|
56
|
+
* @returns This EvmCallBuilder instance for method chaining
|
|
58
57
|
*/
|
|
59
|
-
addSettlerAsString(settler: string):
|
|
60
|
-
return changetype<
|
|
58
|
+
addSettlerAsString(settler: string): EvmCallBuilder {
|
|
59
|
+
return changetype<EvmCallBuilder>(super.addSettlerAsString(settler))
|
|
61
60
|
}
|
|
62
61
|
|
|
63
62
|
/**
|
|
64
63
|
* Sets the deadline for this intent.
|
|
65
64
|
* @param deadline - The deadline as a timestamp
|
|
66
|
-
* @returns This
|
|
65
|
+
* @returns This EvmCallBuilder instance for method chaining
|
|
67
66
|
*/
|
|
68
|
-
addDeadline(deadline: BigInt):
|
|
69
|
-
return changetype<
|
|
67
|
+
addDeadline(deadline: BigInt): EvmCallBuilder {
|
|
68
|
+
return changetype<EvmCallBuilder>(super.addDeadline(deadline))
|
|
70
69
|
}
|
|
71
70
|
|
|
72
71
|
/**
|
|
73
72
|
* Sets the user address for this intent.
|
|
74
73
|
* @param user - The user address
|
|
75
|
-
* @returns This
|
|
74
|
+
* @returns This EvmCallBuilder instance for method chaining
|
|
76
75
|
*/
|
|
77
|
-
addUser(user: Address):
|
|
78
|
-
return changetype<
|
|
76
|
+
addUser(user: Address): EvmCallBuilder {
|
|
77
|
+
return changetype<EvmCallBuilder>(super.addUser(user))
|
|
79
78
|
}
|
|
80
79
|
|
|
81
80
|
/**
|
|
82
81
|
* Sets the user address from a string.
|
|
83
82
|
* @param user - The user address as a hex string
|
|
84
|
-
* @returns This
|
|
83
|
+
* @returns This EvmCallBuilder instance for method chaining
|
|
85
84
|
*/
|
|
86
|
-
addUserAsString(user: string):
|
|
87
|
-
return changetype<
|
|
85
|
+
addUserAsString(user: string): EvmCallBuilder {
|
|
86
|
+
return changetype<EvmCallBuilder>(super.addUserAsString(user))
|
|
88
87
|
}
|
|
89
88
|
|
|
90
89
|
/**
|
|
91
90
|
* Sets the nonce for this intent.
|
|
92
91
|
* @param nonce - A unique identifier to prevent replay attacks
|
|
93
|
-
* @returns This
|
|
92
|
+
* @returns This EvmCallBuilder instance for method chaining
|
|
94
93
|
*/
|
|
95
|
-
addNonce(nonce: string):
|
|
96
|
-
return changetype<
|
|
94
|
+
addNonce(nonce: string): EvmCallBuilder {
|
|
95
|
+
return changetype<EvmCallBuilder>(super.addNonce(nonce))
|
|
97
96
|
}
|
|
98
97
|
|
|
99
98
|
/**
|
|
100
99
|
* Adds a max fee for this intent.
|
|
101
100
|
* @param fee - The max fee token amount (must be on same chain)
|
|
102
|
-
* @returns This
|
|
101
|
+
* @returns This EvmCallBuilder instance for method chaining
|
|
103
102
|
*/
|
|
104
|
-
addMaxFee(fee: TokenAmount):
|
|
103
|
+
addMaxFee(fee: TokenAmount): EvmCallBuilder {
|
|
105
104
|
if (!fee.token.hasChain(this.chainId)) throw new Error('Fee token must be on the same chain')
|
|
106
105
|
this.maxFees.push(fee)
|
|
107
106
|
return this
|
|
@@ -111,27 +110,27 @@ export class CallBuilder extends IntentBuilder {
|
|
|
111
110
|
* Sets an event for the intent.
|
|
112
111
|
* @param topic - The topic to be indexed in the event
|
|
113
112
|
* @param data - The event data
|
|
114
|
-
* @returns This
|
|
113
|
+
* @returns This EvmCallBuilder instance for method chaining
|
|
115
114
|
*/
|
|
116
|
-
addEvent(topic: Bytes, data: Bytes):
|
|
117
|
-
return changetype<
|
|
115
|
+
addEvent(topic: Bytes, data: Bytes): EvmCallBuilder {
|
|
116
|
+
return changetype<EvmCallBuilder>(super.addEvent(topic, data))
|
|
118
117
|
}
|
|
119
118
|
|
|
120
119
|
/**
|
|
121
120
|
* Sets multiple events for the intent.
|
|
122
121
|
* @param events - The list of events to be added
|
|
123
|
-
* @returns This
|
|
122
|
+
* @returns This EvmCallBuilder instance for method chaining
|
|
124
123
|
*/
|
|
125
|
-
addEvents(events: IntentEvent[]):
|
|
126
|
-
return changetype<
|
|
124
|
+
addEvents(events: IntentEvent[]): EvmCallBuilder {
|
|
125
|
+
return changetype<EvmCallBuilder>(super.addEvents(events))
|
|
127
126
|
}
|
|
128
127
|
|
|
129
128
|
/**
|
|
130
|
-
* Builds and returns the final
|
|
131
|
-
* @returns A new
|
|
129
|
+
* Builds and returns the final EvmCall intent.
|
|
130
|
+
* @returns A new EvmCall instance with all configured parameters
|
|
132
131
|
*/
|
|
133
|
-
build():
|
|
134
|
-
return new
|
|
132
|
+
build(): EvmCall {
|
|
133
|
+
return new EvmCall(
|
|
135
134
|
this.chainId,
|
|
136
135
|
this.calls,
|
|
137
136
|
this.maxFees,
|
|
@@ -149,13 +148,13 @@ export class CallBuilder extends IntentBuilder {
|
|
|
149
148
|
* Contains the target address, call data, and value to send.
|
|
150
149
|
*/
|
|
151
150
|
@json
|
|
152
|
-
export class
|
|
151
|
+
export class EvmCallData {
|
|
153
152
|
public target: string
|
|
154
153
|
public data: string
|
|
155
154
|
public value: string
|
|
156
155
|
|
|
157
156
|
/**
|
|
158
|
-
* Creates a new
|
|
157
|
+
* Creates a new EvmCallData instance.
|
|
159
158
|
* @param target - The contract address to call
|
|
160
159
|
* @param data - The call data (optional, defaults to empty bytes)
|
|
161
160
|
* @param value - The native token value to send (optional, defaults to zero)
|
|
@@ -171,12 +170,12 @@ export class CallData {
|
|
|
171
170
|
* Represents a Call intent containing one or more contract calls to be executed.
|
|
172
171
|
*/
|
|
173
172
|
@json
|
|
174
|
-
export class
|
|
173
|
+
export class EvmCall extends Intent {
|
|
175
174
|
public chainId: ChainId
|
|
176
|
-
public calls:
|
|
175
|
+
public calls: EvmCallData[]
|
|
177
176
|
|
|
178
177
|
/**
|
|
179
|
-
* Creates a
|
|
178
|
+
* Creates a EvmCall intent with a single contract call.
|
|
180
179
|
* @param chainId - The blockchain network identifier
|
|
181
180
|
* @param target - The contract address to call
|
|
182
181
|
* @param data - The call data
|
|
@@ -199,13 +198,13 @@ export class Call extends Intent {
|
|
|
199
198
|
deadline: BigInt | null = null,
|
|
200
199
|
nonce: string | null = null,
|
|
201
200
|
events: IntentEvent[] | null = null
|
|
202
|
-
):
|
|
203
|
-
const callData = new
|
|
204
|
-
return new
|
|
201
|
+
): EvmCall {
|
|
202
|
+
const callData = new EvmCallData(target, data, value)
|
|
203
|
+
return new EvmCall(chainId, [callData], [maxFee], settler, user, deadline, nonce, events)
|
|
205
204
|
}
|
|
206
205
|
|
|
207
206
|
/**
|
|
208
|
-
* Creates a new
|
|
207
|
+
* Creates a new EvmCall intent.
|
|
209
208
|
* @param chainId - The blockchain network identifier
|
|
210
209
|
* @param calls - Array of contract calls to execute
|
|
211
210
|
* @param maxFees - The list of max fees to pay for the call intent
|
|
@@ -216,7 +215,7 @@ export class Call extends Intent {
|
|
|
216
215
|
*/
|
|
217
216
|
constructor(
|
|
218
217
|
chainId: ChainId,
|
|
219
|
-
calls:
|
|
218
|
+
calls: EvmCallData[],
|
|
220
219
|
maxFees: TokenAmount[],
|
|
221
220
|
settler: Address | null = null,
|
|
222
221
|
user: Address | null = null,
|
|
@@ -225,7 +224,7 @@ export class Call extends Intent {
|
|
|
225
224
|
events: IntentEvent[] | null = null
|
|
226
225
|
) {
|
|
227
226
|
const fees: MaxFee[] = maxFees.map((fee: TokenAmount) => MaxFee.fromTokenAmount(fee))
|
|
228
|
-
super(OperationType.
|
|
227
|
+
super(OperationType.EvmCall, chainId, fees, settler, user, deadline, nonce, events)
|
|
229
228
|
if (calls.length === 0) throw new Error('Call list cannot be empty')
|
|
230
229
|
if (maxFees.length == 0) throw new Error('At least a max fee must be specified')
|
|
231
230
|
|
|
@@ -234,9 +233,9 @@ export class Call extends Intent {
|
|
|
234
233
|
}
|
|
235
234
|
|
|
236
235
|
/**
|
|
237
|
-
* Sends this
|
|
236
|
+
* Sends this EvmCall intent to the execution environment.
|
|
238
237
|
*/
|
|
239
238
|
public send(): void {
|
|
240
|
-
environment.
|
|
239
|
+
environment.evmCall(this)
|
|
241
240
|
}
|
|
242
241
|
}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { environment } from '../../environment'
|
|
2
|
+
import { TokenAmount } from '../../tokens'
|
|
3
|
+
import { Address, BigInt, Bytes, ChainId } from '../../types'
|
|
4
|
+
import { SvmAccountMeta } from '../../types/svm/SvmAccountMeta'
|
|
5
|
+
import { Intent, IntentBuilder, IntentEvent, MaxFee, OperationType } from '../Intent'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Builder for creating SVM Call intents with program call operations.
|
|
9
|
+
* Allows chaining multiple calls and configuring fees and settlement parameters.
|
|
10
|
+
*/
|
|
11
|
+
export class SvmCallBuilder extends IntentBuilder {
|
|
12
|
+
private chainId: ChainId
|
|
13
|
+
private instructions: SvmInstruction[] = []
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Creates a new SvmCallBuilder instance.
|
|
17
|
+
*/
|
|
18
|
+
constructor() {
|
|
19
|
+
super()
|
|
20
|
+
this.chainId = ChainId.SOLANA_MAINNET
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Adds an instruction to the intent.
|
|
25
|
+
* @param instruction - The instruction to add
|
|
26
|
+
* @returns This SvmCallBuilder instance for method chaining
|
|
27
|
+
*/
|
|
28
|
+
addInstruction(instruction: SvmInstruction): SvmCallBuilder {
|
|
29
|
+
this.instructions.push(instruction)
|
|
30
|
+
return this
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Sets the settler address for this intent.
|
|
35
|
+
* @param settler - The settler address as an Address instance
|
|
36
|
+
* @returns This SvmCallBuilder instance for method chaining
|
|
37
|
+
*/
|
|
38
|
+
addSettler(settler: Address): SvmCallBuilder {
|
|
39
|
+
return changetype<SvmCallBuilder>(super.addSettler(settler))
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Sets the settler address from a string.
|
|
44
|
+
* @param settler - The settler address as a hex string
|
|
45
|
+
* @returns This SvmCallBuilder instance for method chaining
|
|
46
|
+
*/
|
|
47
|
+
addSettlerAsString(settler: string): SvmCallBuilder {
|
|
48
|
+
return changetype<SvmCallBuilder>(super.addSettlerAsString(settler))
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Sets the deadline for this intent.
|
|
53
|
+
* @param deadline - The deadline as a timestamp
|
|
54
|
+
* @returns This SvmCallBuilder instance for method chaining
|
|
55
|
+
*/
|
|
56
|
+
addDeadline(deadline: BigInt): SvmCallBuilder {
|
|
57
|
+
return changetype<SvmCallBuilder>(super.addDeadline(deadline))
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Sets the user address for this intent.
|
|
62
|
+
* @param user - The user address
|
|
63
|
+
* @returns This SvmCallBuilder instance for method chaining
|
|
64
|
+
*/
|
|
65
|
+
addUser(user: Address): SvmCallBuilder {
|
|
66
|
+
return changetype<SvmCallBuilder>(super.addUser(user))
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Sets the user address from a string.
|
|
71
|
+
* @param user - The user address as a hex string
|
|
72
|
+
* @returns This SvmCallBuilder instance for method chaining
|
|
73
|
+
*/
|
|
74
|
+
addUserAsString(user: string): SvmCallBuilder {
|
|
75
|
+
return changetype<SvmCallBuilder>(super.addUserAsString(user))
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Sets the nonce for this intent.
|
|
80
|
+
* @param nonce - A unique identifier to prevent replay attacks
|
|
81
|
+
* @returns This SvmCallBuilder instance for method chaining
|
|
82
|
+
*/
|
|
83
|
+
addNonce(nonce: string): SvmCallBuilder {
|
|
84
|
+
return changetype<SvmCallBuilder>(super.addNonce(nonce))
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Adds a max fee for this intent.
|
|
89
|
+
* @param fee - The max fee token amount (must be on same chain)
|
|
90
|
+
* @returns This SvmCallBuilder instance for method chaining
|
|
91
|
+
*/
|
|
92
|
+
addMaxFee(fee: TokenAmount): SvmCallBuilder {
|
|
93
|
+
if (!fee.token.hasChain(this.chainId)) throw new Error('Fee token must be on the same chain')
|
|
94
|
+
this.maxFees.push(fee)
|
|
95
|
+
return this
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Sets an event for the intent.
|
|
100
|
+
* @param topic - The topic to be indexed in the event
|
|
101
|
+
* @param data - The event data
|
|
102
|
+
* @returns This SvmCallBuilder instance for method chaining
|
|
103
|
+
*/
|
|
104
|
+
addEvent(topic: Bytes, data: Bytes): SvmCallBuilder {
|
|
105
|
+
return changetype<SvmCallBuilder>(super.addEvent(topic, data))
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Sets multiple events for the intent.
|
|
110
|
+
* @param events - The list of events to be added
|
|
111
|
+
* @returns This SvmCallBuilder instance for method chaining
|
|
112
|
+
*/
|
|
113
|
+
addEvents(events: IntentEvent[]): SvmCallBuilder {
|
|
114
|
+
return changetype<SvmCallBuilder>(super.addEvents(events))
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Builds and returns the final SvmCall intent.
|
|
119
|
+
* @returns A new SvmCall instance with all configured parameters
|
|
120
|
+
*/
|
|
121
|
+
build(): SvmCall {
|
|
122
|
+
return new SvmCall(this.instructions, this.maxFees, this.settler, this.user, this.deadline, this.nonce, this.events)
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export class SvmInstructionBuilder {
|
|
127
|
+
private programId: Address = Address.zero(32)
|
|
128
|
+
private accountsMeta: SvmAccountMeta[] = []
|
|
129
|
+
private data: Bytes = Bytes.empty()
|
|
130
|
+
|
|
131
|
+
setProgram(programId: Address): SvmInstructionBuilder {
|
|
132
|
+
this.programId = programId
|
|
133
|
+
return this
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
setAccounts(accountsMeta: SvmAccountMeta[]): SvmInstructionBuilder {
|
|
137
|
+
this.accountsMeta = accountsMeta
|
|
138
|
+
return this
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
setDataFromBytes(data: Bytes): SvmInstructionBuilder {
|
|
142
|
+
this.data = data
|
|
143
|
+
return this
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
setDataFromHex(data: string): SvmInstructionBuilder {
|
|
147
|
+
return this.setDataFromBytes(Bytes.fromHexString(data))
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
instruction(): SvmInstruction {
|
|
151
|
+
return SvmInstruction.create(this.programId, this.accountsMeta, this.data)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
@json
|
|
156
|
+
export class SvmInstruction {
|
|
157
|
+
constructor(
|
|
158
|
+
public programId: string,
|
|
159
|
+
public accountsMeta: SvmAccountMeta[],
|
|
160
|
+
public data: string
|
|
161
|
+
) {}
|
|
162
|
+
|
|
163
|
+
static create(programId: Address, accountsMeta: SvmAccountMeta[], data: Bytes): SvmInstruction {
|
|
164
|
+
return new SvmInstruction(programId.toBase58String(), accountsMeta, data.toHexString())
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Represents a SVM Call intent containing one or more program calls to be executed.
|
|
170
|
+
*/
|
|
171
|
+
@json
|
|
172
|
+
export class SvmCall extends Intent {
|
|
173
|
+
public chainId: ChainId
|
|
174
|
+
public instructions: SvmInstruction[]
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Creates a SvmCall intent with a single program call.
|
|
178
|
+
* @param maxFee - The max fee to pay for the call intent
|
|
179
|
+
* @param settler - The settler address (optional)
|
|
180
|
+
* @param user - The user address (optional)
|
|
181
|
+
* @param deadline - The deadline timestamp (optional)
|
|
182
|
+
* @param nonce - The nonce for replay protection (optional)
|
|
183
|
+
* @returns A new Call instance
|
|
184
|
+
*/
|
|
185
|
+
static create(
|
|
186
|
+
programId: Address,
|
|
187
|
+
accountsMeta: SvmAccountMeta[],
|
|
188
|
+
data: Bytes,
|
|
189
|
+
maxFee: TokenAmount,
|
|
190
|
+
settler: Address | null = null,
|
|
191
|
+
user: Address | null = null,
|
|
192
|
+
deadline: BigInt | null = null,
|
|
193
|
+
nonce: string | null = null,
|
|
194
|
+
events: IntentEvent[] | null = null
|
|
195
|
+
): SvmCall {
|
|
196
|
+
const instruction = SvmInstruction.create(programId, accountsMeta, data)
|
|
197
|
+
return new SvmCall([instruction], [maxFee], settler, user, deadline, nonce, events)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Creates a new SvmCall intent.
|
|
202
|
+
* @param instructions - Array of instructions to execute
|
|
203
|
+
* @param maxFees - The list of max fees to pay for the call intent
|
|
204
|
+
* @param settler - The settler address (optional)
|
|
205
|
+
* @param user - The user address (optional)
|
|
206
|
+
* @param deadline - The deadline timestamp (optional)
|
|
207
|
+
* @param nonce - The nonce for replay protection (optional)
|
|
208
|
+
*/
|
|
209
|
+
constructor(
|
|
210
|
+
instructions: SvmInstruction[],
|
|
211
|
+
maxFees: TokenAmount[],
|
|
212
|
+
settler: Address | null = null,
|
|
213
|
+
user: Address | null = null,
|
|
214
|
+
deadline: BigInt | null = null,
|
|
215
|
+
nonce: string | null = null,
|
|
216
|
+
events: IntentEvent[] | null = null
|
|
217
|
+
) {
|
|
218
|
+
const fees: MaxFee[] = maxFees.map((fee: TokenAmount) => MaxFee.fromTokenAmount(fee))
|
|
219
|
+
super(OperationType.SvmCall, ChainId.SOLANA_MAINNET, fees, settler, user, deadline, nonce, events)
|
|
220
|
+
if (instructions.length === 0) throw new Error('Call list cannot be empty')
|
|
221
|
+
if (maxFees.length == 0) throw new Error('At least a max fee must be specified')
|
|
222
|
+
|
|
223
|
+
this.instructions = instructions
|
|
224
|
+
this.chainId = ChainId.SOLANA_MAINNET
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Sends this SvmCall intent to the execution environment.
|
|
229
|
+
*/
|
|
230
|
+
public send(): void {
|
|
231
|
+
environment.svmCall(this)
|
|
232
|
+
}
|
|
233
|
+
}
|
package/src/intents/Intent.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Address, ChainId } from '../types'
|
|
1
|
+
import { Address, ChainId, JSON } from '../types'
|
|
2
2
|
|
|
3
3
|
import { ERC20Token } from './ERC20Token'
|
|
4
4
|
import { SPLToken } from './SPLToken'
|
|
5
|
-
import { Token } from './Token'
|
|
5
|
+
import { SerializableToken, Token } from './Token'
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Represents a token on a blockchain network
|
|
@@ -49,6 +49,16 @@ export abstract class BlockchainToken extends Token {
|
|
|
49
49
|
return BlockchainToken.fromAddress(Address.fromString(address), chainId, decimals, symbol)
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Creates a BlockchainToken from a serialized string.
|
|
54
|
+
* @param serialized - The serialized string to parse
|
|
55
|
+
* @returns A new BlockchainToken instance
|
|
56
|
+
*/
|
|
57
|
+
static fromSerializable(serialized: string): BlockchainToken {
|
|
58
|
+
const data = JSON.parse<SerializableToken>(serialized)
|
|
59
|
+
return BlockchainToken.fromString(data.address, data.chainId)
|
|
60
|
+
}
|
|
61
|
+
|
|
52
62
|
/**
|
|
53
63
|
* Creates a new BlockchainToken instance
|
|
54
64
|
* @param address The token address in the corresponding blockchain (contract, mint, etc.)
|
package/src/tokens/Token.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { environment } from '../environment'
|
|
2
|
-
import { BigInt } from '../types'
|
|
2
|
+
import { BigInt, JSON } from '../types'
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import { BlockchainToken } from './BlockchainToken'
|
|
5
|
+
import { SerializableToken, Token } from './Token'
|
|
5
6
|
import { USD } from './USD'
|
|
6
7
|
|
|
7
8
|
/**
|
|
@@ -50,6 +51,17 @@ export class TokenAmount {
|
|
|
50
51
|
return new TokenAmount(token, amount)
|
|
51
52
|
}
|
|
52
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Creates a TokenAmount from a serialized string.
|
|
56
|
+
* @param serialized - The serialized string to parse
|
|
57
|
+
* @returns A new TokenAmount instance
|
|
58
|
+
*/
|
|
59
|
+
static fromSerializable(serialized: string): TokenAmount {
|
|
60
|
+
const data = JSON.parse<SerializableTokenAmount>(serialized)
|
|
61
|
+
const token = BlockchainToken.fromString(data.token.address, data.token.chainId)
|
|
62
|
+
return TokenAmount.fromStringDecimal(token, data.amount)
|
|
63
|
+
}
|
|
64
|
+
|
|
53
65
|
/**
|
|
54
66
|
* Creates a new TokenAmount instance.
|
|
55
67
|
* @param token - The token this amount represents
|
|
@@ -228,3 +240,11 @@ export class TokenAmount {
|
|
|
228
240
|
if (!this.token.equals(other)) throw new Error(`Cannot ${action} different tokens`)
|
|
229
241
|
}
|
|
230
242
|
}
|
|
243
|
+
|
|
244
|
+
@json
|
|
245
|
+
export class SerializableTokenAmount {
|
|
246
|
+
constructor(
|
|
247
|
+
public token: SerializableToken,
|
|
248
|
+
public amount: string
|
|
249
|
+
) {}
|
|
250
|
+
}
|
package/src/types/Address.ts
CHANGED
|
@@ -15,10 +15,10 @@ import { Option } from './Option'
|
|
|
15
15
|
*/
|
|
16
16
|
export class Address extends Bytes {
|
|
17
17
|
/**
|
|
18
|
-
* Returns
|
|
18
|
+
* Returns a zero address (default 20 bytes filled with zeroes).
|
|
19
19
|
*/
|
|
20
|
-
static zero(): Address {
|
|
21
|
-
const self = new ByteArray(
|
|
20
|
+
static zero(length: i32 = 20): Address {
|
|
21
|
+
const self = new ByteArray(length)
|
|
22
22
|
return changetype<Address>(self)
|
|
23
23
|
}
|
|
24
24
|
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Address } from '../Address'
|
|
2
|
+
|
|
3
|
+
@json
|
|
4
|
+
export class SvmAccountMeta {
|
|
5
|
+
constructor(
|
|
6
|
+
public pubkey: string,
|
|
7
|
+
public isWritable: bool = false,
|
|
8
|
+
public isSigner: bool = false
|
|
9
|
+
) {}
|
|
10
|
+
|
|
11
|
+
writable(): SvmAccountMeta {
|
|
12
|
+
this.isWritable = true
|
|
13
|
+
return this
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
signer(): SvmAccountMeta {
|
|
17
|
+
this.isSigner = true
|
|
18
|
+
return this
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static fromAddress(pubkey: Address): SvmAccountMeta {
|
|
22
|
+
return new SvmAccountMeta(pubkey.toString())
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static fromString(pubkey: string): SvmAccountMeta {
|
|
26
|
+
return new SvmAccountMeta(pubkey)
|
|
27
|
+
}
|
|
28
|
+
}
|