@cowprotocol/sdk-weiroll 0.1.0-monorepo.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/README.md +69 -0
- package/dist/index.d.mts +286 -0
- package/dist/index.d.ts +286 -0
- package/dist/index.js +650 -0
- package/dist/index.mjs +618 -0
- package/package.json +43 -0
package/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img width="400" src="https://github.com/cowprotocol/cow-sdk/raw/main/docs/images/CoW.png" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
# SDK Weiroll
|
|
6
|
+
|
|
7
|
+
This package provides adapter-agnostic Weiroll functionality for the CoW Protocol SDK. Weiroll is a powerful scripting language for executing complex multi-step transactions on Ethereum.
|
|
8
|
+
|
|
9
|
+
## Package Origin
|
|
10
|
+
|
|
11
|
+
This package represents a refactored version of the original [weiroll.js](https://github.com/weiroll/weiroll.js) library. The original library was tightly coupled to ethers.js v5, but this version has been completely refactored to:
|
|
12
|
+
|
|
13
|
+
- **Remove ethers v5 dependency** - Works with any supported blockchain library
|
|
14
|
+
- **Support multiple adapters** - Compatible with ethers v5, ethers v6, and viem
|
|
15
|
+
- **Maintain API compatibility** - 100% compatible with original weiroll.js API
|
|
16
|
+
- **Enable SDK integration** - Seamlessly integrate with the CoW Protocol SDK ecosystem
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @cowprotocol/sdk-weiroll
|
|
22
|
+
or
|
|
23
|
+
pnpm add @cowprotocol/sdk-weiroll
|
|
24
|
+
or
|
|
25
|
+
yarn add @cowprotocol/sdk-weiroll
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
### Individual package usage
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import {
|
|
34
|
+
WeirollPlanner,
|
|
35
|
+
createWeirollContract,
|
|
36
|
+
createWeirollDelegateCall,
|
|
37
|
+
WeirollCommandFlags,
|
|
38
|
+
} from '@cowprotocol/sdk-weiroll'
|
|
39
|
+
import { EthersV6Adapter } from '@cowprotocol/sdk-ethers-v6-adapter'
|
|
40
|
+
import { JsonRpcProvider, Wallet } from 'ethers'
|
|
41
|
+
|
|
42
|
+
// Configure the adapter
|
|
43
|
+
const provider = new JsonRpcProvider('YOUR_RPC_URL')
|
|
44
|
+
const wallet = new Wallet('YOUR_PRIVATE_KEY', provider)
|
|
45
|
+
const adapter = new EthersV6Adapter({ provider, signer: wallet })
|
|
46
|
+
|
|
47
|
+
// Create weiroll contract wrapper
|
|
48
|
+
const contract = adapter.getContract(contractAddress, abi)
|
|
49
|
+
const weirollContract = createWeirollContract(contract, WeirollCommandFlags.CALL)
|
|
50
|
+
|
|
51
|
+
// Plan weiroll transaction
|
|
52
|
+
const planner = new WeirollPlanner()
|
|
53
|
+
planner.add(weirollContract.someFunction(param1, param2))
|
|
54
|
+
|
|
55
|
+
// Generate delegate call
|
|
56
|
+
const evmCall = createWeirollDelegateCall((planner) => {
|
|
57
|
+
planner.add(weirollContract.someFunction(param1, param2))
|
|
58
|
+
})
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Features
|
|
62
|
+
|
|
63
|
+
- **Multi-step transactions** - Plan and execute complex transaction sequences
|
|
64
|
+
- **Gas optimization** - Efficient execution through delegate calls
|
|
65
|
+
- **Return value chaining** - Use outputs from one call as inputs to another
|
|
66
|
+
- **Adapter agnostic** - Works with ethers v5, v6, and viem
|
|
67
|
+
- **Type safety** - Full TypeScript support
|
|
68
|
+
|
|
69
|
+
> **Note:** This package maintains 100% API compatibility with the original weiroll.js library while adding support for multiple blockchain adapters.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { EvmCall } from '@cowprotocol/sdk-config';
|
|
2
|
+
import { GenericContract } from '@cowprotocol/sdk-common';
|
|
3
|
+
|
|
4
|
+
type ContractInterface = any;
|
|
5
|
+
type Contract$1 = any;
|
|
6
|
+
type FunctionFragment = any;
|
|
7
|
+
type Interface = any;
|
|
8
|
+
type ParamType = any;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @fileoverview Weiroll Multi-Adapter Implementation
|
|
12
|
+
*
|
|
13
|
+
* This file is based on the weiroll.js library (https://github.com/weiroll/weiroll.js),
|
|
14
|
+
* refactored to work with the CoW Protocol SDK's adapter system and to remove the
|
|
15
|
+
* dependency on Ethers v5.
|
|
16
|
+
*
|
|
17
|
+
* Original weiroll.js library:
|
|
18
|
+
* - Repository: https://github.com/weiroll/weiroll.js
|
|
19
|
+
* - License: MIT
|
|
20
|
+
* - Version: 0.3.0
|
|
21
|
+
*
|
|
22
|
+
* Refactoring changes:
|
|
23
|
+
* - Replaced Ethers v5 direct dependencies with adapter abstractions
|
|
24
|
+
* - Added support for multiple Ethereum libraries (Ethers v5, v6, Viem)
|
|
25
|
+
* - Maintained 100% API compatibility with original weiroll.js
|
|
26
|
+
*
|
|
27
|
+
* @see https://github.com/weiroll/weiroll.js - Original library
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Represents a value that can be passed to a function call.
|
|
32
|
+
*
|
|
33
|
+
* This can represent a literal value, a return value from a previous function call,
|
|
34
|
+
* or one of several internal placeholder value types.
|
|
35
|
+
*/
|
|
36
|
+
interface Value {
|
|
37
|
+
/** The ethers.js `ParamType` describing the type of this value. */
|
|
38
|
+
readonly param: ParamType;
|
|
39
|
+
}
|
|
40
|
+
declare class ReturnValue implements Value {
|
|
41
|
+
readonly param: ParamType;
|
|
42
|
+
readonly command: Command;
|
|
43
|
+
constructor(param: ParamType, command: Command);
|
|
44
|
+
}
|
|
45
|
+
declare class StateValue implements Value {
|
|
46
|
+
readonly param: ParamType;
|
|
47
|
+
constructor();
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* CommandFlags
|
|
51
|
+
* @description Flags that modify a command's execution
|
|
52
|
+
* @enum {number}
|
|
53
|
+
*/
|
|
54
|
+
declare enum CommandFlags {
|
|
55
|
+
/** Specifies that a call should be made using the DELEGATECALL opcode */
|
|
56
|
+
DELEGATECALL = 0,
|
|
57
|
+
/** Specifies that a call should be made using the CALL opcode */
|
|
58
|
+
CALL = 1,
|
|
59
|
+
/** Specifies that a call should be made using the STATICCALL opcode */
|
|
60
|
+
STATICCALL = 2,
|
|
61
|
+
/** Specifies that a call should be made using the CALL opcode, and that the first argument will be the value to send */
|
|
62
|
+
CALL_WITH_VALUE = 3,
|
|
63
|
+
/** A bitmask that selects calltype flags */
|
|
64
|
+
CALLTYPE_MASK = 3,
|
|
65
|
+
/** Specifies that this is an extended command, with an additional command word for indices. Internal use only. */
|
|
66
|
+
EXTENDED_COMMAND = 64,
|
|
67
|
+
/** Specifies that the return value of this call should be wrapped in a `bytes`. Internal use only. */
|
|
68
|
+
TUPLE_RETURN = 128
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Represents a call to a contract function as part of a Weiroll plan.
|
|
72
|
+
*
|
|
73
|
+
* A `FunctionCall` is created by calling functions on a [[Contract]] object, and consumed by
|
|
74
|
+
* passing it to [[Planner.add]], [[Planner.addSubplan]] or [[Planner.replaceState]]
|
|
75
|
+
*/
|
|
76
|
+
declare class FunctionCall {
|
|
77
|
+
/** The Contract this function is on. */
|
|
78
|
+
readonly contract: Contract;
|
|
79
|
+
/** Flags modifying the execution of this function call. */
|
|
80
|
+
readonly flags: CommandFlags;
|
|
81
|
+
/** An ethers.js Fragment that describes the function being called. */
|
|
82
|
+
readonly fragment: FunctionFragment;
|
|
83
|
+
/** An array of arguments to the function. */
|
|
84
|
+
readonly args: Value[];
|
|
85
|
+
/** If the call type is a call-with-value, this property holds the value that will be passed. */
|
|
86
|
+
readonly callvalue?: Value;
|
|
87
|
+
/** @hidden */
|
|
88
|
+
constructor(contract: Contract, flags: CommandFlags, fragment: FunctionFragment, args: Value[], callvalue?: Value);
|
|
89
|
+
/**
|
|
90
|
+
* Returns a new [[FunctionCall]] that sends value with the call.
|
|
91
|
+
* @param value The value (in wei) to send with the call
|
|
92
|
+
*/
|
|
93
|
+
withValue(value: Value): FunctionCall;
|
|
94
|
+
/**
|
|
95
|
+
* Returns a new [[FunctionCall]] whose return value will be wrapped as a `bytes`.
|
|
96
|
+
* This permits capturing the return values of functions with multiple return parameters,
|
|
97
|
+
* which weiroll does not otherwise support.
|
|
98
|
+
*/
|
|
99
|
+
rawValue(): FunctionCall;
|
|
100
|
+
/**
|
|
101
|
+
* Returns a new [[FunctionCall]] that executes a STATICCALL instead of a regular CALL.
|
|
102
|
+
*/
|
|
103
|
+
staticcall(): FunctionCall;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The type of all contract-specific functions on the [[Contract]] object.
|
|
107
|
+
*/
|
|
108
|
+
type ContractFunction = (...args: Array<any>) => FunctionCall;
|
|
109
|
+
declare class BaseContract {
|
|
110
|
+
/** The address of the contract */
|
|
111
|
+
readonly address: string;
|
|
112
|
+
/** Flags specifying the default execution options for all functions */
|
|
113
|
+
readonly commandflags: CommandFlags;
|
|
114
|
+
/** The ethers.js Interface representing the contract */
|
|
115
|
+
readonly interface: Interface;
|
|
116
|
+
/** A mapping from function names to [[ContractFunction]]s. */
|
|
117
|
+
readonly functions: {
|
|
118
|
+
[name: string]: ContractFunction;
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* @param address The address of the contract
|
|
122
|
+
* @param contractInterface The ethers.js Interface representing the contract
|
|
123
|
+
* @param commandflags Optional flags specifying the default execution options for all functions
|
|
124
|
+
*/
|
|
125
|
+
constructor(address: string, contractInterface: ContractInterface, commandflags?: CommandFlags);
|
|
126
|
+
/**
|
|
127
|
+
* Creates a [[Contract]] object from an ethers.js contract.
|
|
128
|
+
* All calls on the returned object will default to being standard CALL operations.
|
|
129
|
+
* Use this when you want your weiroll script to call a standard external contract.
|
|
130
|
+
* @param contract The ethers.js Contract object to wrap.
|
|
131
|
+
* @param commandflags Optionally specifies a non-default call type to use, such as
|
|
132
|
+
* [[CommandFlags.STATICCALL]].
|
|
133
|
+
*/
|
|
134
|
+
static createContract(contract: Contract$1, commandflags?: CommandFlags): Contract;
|
|
135
|
+
/**
|
|
136
|
+
* Creates a [[Contract]] object from an ethers.js contract.
|
|
137
|
+
* All calls on the returned object will default to being DELEGATECALL operations.
|
|
138
|
+
* Use this when you want your weiroll script to call a library specifically designed
|
|
139
|
+
* for use with weiroll.
|
|
140
|
+
* @param contract The ethers.js Contract object to wrap.
|
|
141
|
+
*/
|
|
142
|
+
static createLibrary(contract: Contract$1): Contract;
|
|
143
|
+
/** @hidden */
|
|
144
|
+
static getInterface(contractInterface: ContractInterface): Interface;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Provides a dynamically created interface to interact with Ethereum contracts via weiroll.
|
|
148
|
+
*
|
|
149
|
+
* Once created using the constructor or the [[Contract.createContract]] or [[Contract.createLibrary]]
|
|
150
|
+
* functions, the returned object is automatically populated with methods that match those on the
|
|
151
|
+
* supplied contract. For instance, if your contract has a method `add(uint, uint)`, you can call it on the
|
|
152
|
+
* [[Contract]] object:
|
|
153
|
+
* ```typescript
|
|
154
|
+
* // Assumes `Math` is an ethers.js Contract instance.
|
|
155
|
+
* const math = Contract.createLibrary(Math);
|
|
156
|
+
* const result = math.add(1, 2);
|
|
157
|
+
* ```
|
|
158
|
+
*
|
|
159
|
+
* Calling a contract function returns a [[FunctionCall]] object, which you can pass to [[Planner.add]],
|
|
160
|
+
* [[Planner.addSubplan]], or [[Planner.replaceState]] to add to the sequence of calls to plan.
|
|
161
|
+
*/
|
|
162
|
+
declare class Contract extends BaseContract {
|
|
163
|
+
readonly [key: string]: ContractFunction | any;
|
|
164
|
+
}
|
|
165
|
+
declare enum CommandType {
|
|
166
|
+
CALL = 0,
|
|
167
|
+
RAWCALL = 1,
|
|
168
|
+
SUBPLAN = 2
|
|
169
|
+
}
|
|
170
|
+
declare class Command {
|
|
171
|
+
readonly call: FunctionCall;
|
|
172
|
+
readonly type: CommandType;
|
|
173
|
+
constructor(call: FunctionCall, type: CommandType);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* [[Planner]] is the main class to use to specify a sequence of operations to execute for a
|
|
177
|
+
* weiroll script.
|
|
178
|
+
*
|
|
179
|
+
* To use a [[Planner]], construct it and call [[Planner.add]] with the function calls you wish
|
|
180
|
+
* to execute. For example:
|
|
181
|
+
* ```typescript
|
|
182
|
+
* const events = Contract.createLibrary(Events); // Assumes `Events` is an ethers.js contract object
|
|
183
|
+
* const planner = new Planner();
|
|
184
|
+
* planner.add(events.logUint(123));
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
declare class Planner {
|
|
188
|
+
/**
|
|
189
|
+
* Represents the current state of the planner.
|
|
190
|
+
* This value can be passed as an argument to a function that accepts a `bytes[]`. At runtime it will
|
|
191
|
+
* be replaced with the current state of the weiroll planner.
|
|
192
|
+
*/
|
|
193
|
+
readonly state: StateValue;
|
|
194
|
+
/** @hidden */
|
|
195
|
+
commands: Command[];
|
|
196
|
+
constructor();
|
|
197
|
+
/**
|
|
198
|
+
* Adds a new function call to the planner. Function calls are executed in the order they are added.
|
|
199
|
+
*
|
|
200
|
+
* If the function call has a return value, `add` returns an object representing that value, which you
|
|
201
|
+
* can pass to subsequent function calls. For example:
|
|
202
|
+
* ```typescript
|
|
203
|
+
* const math = Contract.createLibrary(Math); // Assumes `Math` is an ethers.js contract object
|
|
204
|
+
* const events = Contract.createLibrary(Events); // Assumes `Events` is an ethers.js contract object
|
|
205
|
+
* const planner = new Planner();
|
|
206
|
+
* const sum = planner.add(math.add(21, 21));
|
|
207
|
+
* planner.add(events.logUint(sum));
|
|
208
|
+
* ```
|
|
209
|
+
* @param call The [[FunctionCall]] to add to the planner
|
|
210
|
+
* @returns An object representing the return value of the call, or null if it does not return a value.
|
|
211
|
+
*/
|
|
212
|
+
add(call: FunctionCall): ReturnValue | null;
|
|
213
|
+
/**
|
|
214
|
+
* Adds a call to a subplan. This has the effect of instantiating a nested instance of the weiroll
|
|
215
|
+
* interpreter, and is commonly used for functionality such as flashloans, control flow, or anywhere
|
|
216
|
+
* else you may need to execute logic inside a callback.
|
|
217
|
+
*
|
|
218
|
+
* A [[FunctionCall]] passed to [[Planner.addSubplan]] must take another [[Planner]] object as one
|
|
219
|
+
* argument, and a placeholder representing the planner state, accessible as [[Planner.state]], as
|
|
220
|
+
* another. Exactly one of each argument must be provided.
|
|
221
|
+
*
|
|
222
|
+
* At runtime, the subplan is replaced by a list of commands for the subplanner (type `bytes32[]`),
|
|
223
|
+
* and `planner.state` is replaced by the current state of the parent planner instance (type `bytes[]`).
|
|
224
|
+
*
|
|
225
|
+
* If the `call` returns a `bytes[]`, this will be used to replace the parent planner's state after
|
|
226
|
+
* the call to the subplanner completes. Return values defined inside a subplan may be used outside that
|
|
227
|
+
* subplan - both in the parent planner and in subsequent subplans - only if the `call` returns the
|
|
228
|
+
* updated planner state.
|
|
229
|
+
*
|
|
230
|
+
* Example usage:
|
|
231
|
+
* ```
|
|
232
|
+
* const exchange = Contract.createLibrary(Exchange); // Assumes `Exchange` is an ethers.js contract
|
|
233
|
+
* const events = Contract.createLibrary(Events); // Assumes `Events` is an ethers.js contract
|
|
234
|
+
* const subplanner = new Planner();
|
|
235
|
+
* const outqty = subplanner.add(exchange.swap(tokenb, tokena, qty));
|
|
236
|
+
*
|
|
237
|
+
* const planner = new Planner();
|
|
238
|
+
* planner.addSubplan(exchange.flashswap(tokena, tokenb, qty, subplanner, planner.state));
|
|
239
|
+
* planner.add(events.logUint(outqty)); // Only works if `exchange.flashswap` returns updated state
|
|
240
|
+
* ```
|
|
241
|
+
* @param call The [[FunctionCall]] to add to the planner.
|
|
242
|
+
*/
|
|
243
|
+
addSubplan(call: FunctionCall): void;
|
|
244
|
+
/**
|
|
245
|
+
* Executes a [[FunctionCall]], and replaces the planner state with the value it
|
|
246
|
+
* returns. This can be used to execute functions that make arbitrary changes to
|
|
247
|
+
* the planner state. Note that the planner library is not aware of these changes -
|
|
248
|
+
* so it may produce invalid plans if you don't know what you're doing.
|
|
249
|
+
* @param call The [[FunctionCall]] to execute
|
|
250
|
+
*/
|
|
251
|
+
replaceState(call: FunctionCall): void;
|
|
252
|
+
private preplan;
|
|
253
|
+
private buildCommandArgs;
|
|
254
|
+
private buildCommands;
|
|
255
|
+
/**
|
|
256
|
+
* Builds an execution plan for all the commands added to the planner.
|
|
257
|
+
* @returns `commands` and `state`, which can be passed directly to the weiroll executor
|
|
258
|
+
* to execute the plan.
|
|
259
|
+
*/
|
|
260
|
+
plan(): {
|
|
261
|
+
commands: string[];
|
|
262
|
+
state: string[];
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
declare enum WeirollCommandFlags {
|
|
267
|
+
DELEGATECALL = 0,
|
|
268
|
+
CALL = 1,
|
|
269
|
+
STATICCALL = 2,
|
|
270
|
+
CALL_WITH_VALUE = 3,
|
|
271
|
+
CALLTYPE_MASK = 3,
|
|
272
|
+
EXTENDED_COMMAND = 64,
|
|
273
|
+
TUPLE_RETURN = 128
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Create a weiroll transaction
|
|
277
|
+
*
|
|
278
|
+
* @param addToPlanner - A function that adds the commands to the planner
|
|
279
|
+
*
|
|
280
|
+
* @returns An EVM call
|
|
281
|
+
*/
|
|
282
|
+
declare function createWeirollDelegateCall(addToPlanner: (planner: Planner) => void): EvmCall;
|
|
283
|
+
declare function createWeirollContract(contract: GenericContract, commandflags?: WeirollCommandFlags): Contract;
|
|
284
|
+
declare function createWeirollLibrary(contract: GenericContract): Contract;
|
|
285
|
+
|
|
286
|
+
export { WeirollCommandFlags, Contract as WeirollContract, Planner as WeirollPlanner, createWeirollContract, createWeirollDelegateCall, createWeirollLibrary };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { EvmCall } from '@cowprotocol/sdk-config';
|
|
2
|
+
import { GenericContract } from '@cowprotocol/sdk-common';
|
|
3
|
+
|
|
4
|
+
type ContractInterface = any;
|
|
5
|
+
type Contract$1 = any;
|
|
6
|
+
type FunctionFragment = any;
|
|
7
|
+
type Interface = any;
|
|
8
|
+
type ParamType = any;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @fileoverview Weiroll Multi-Adapter Implementation
|
|
12
|
+
*
|
|
13
|
+
* This file is based on the weiroll.js library (https://github.com/weiroll/weiroll.js),
|
|
14
|
+
* refactored to work with the CoW Protocol SDK's adapter system and to remove the
|
|
15
|
+
* dependency on Ethers v5.
|
|
16
|
+
*
|
|
17
|
+
* Original weiroll.js library:
|
|
18
|
+
* - Repository: https://github.com/weiroll/weiroll.js
|
|
19
|
+
* - License: MIT
|
|
20
|
+
* - Version: 0.3.0
|
|
21
|
+
*
|
|
22
|
+
* Refactoring changes:
|
|
23
|
+
* - Replaced Ethers v5 direct dependencies with adapter abstractions
|
|
24
|
+
* - Added support for multiple Ethereum libraries (Ethers v5, v6, Viem)
|
|
25
|
+
* - Maintained 100% API compatibility with original weiroll.js
|
|
26
|
+
*
|
|
27
|
+
* @see https://github.com/weiroll/weiroll.js - Original library
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Represents a value that can be passed to a function call.
|
|
32
|
+
*
|
|
33
|
+
* This can represent a literal value, a return value from a previous function call,
|
|
34
|
+
* or one of several internal placeholder value types.
|
|
35
|
+
*/
|
|
36
|
+
interface Value {
|
|
37
|
+
/** The ethers.js `ParamType` describing the type of this value. */
|
|
38
|
+
readonly param: ParamType;
|
|
39
|
+
}
|
|
40
|
+
declare class ReturnValue implements Value {
|
|
41
|
+
readonly param: ParamType;
|
|
42
|
+
readonly command: Command;
|
|
43
|
+
constructor(param: ParamType, command: Command);
|
|
44
|
+
}
|
|
45
|
+
declare class StateValue implements Value {
|
|
46
|
+
readonly param: ParamType;
|
|
47
|
+
constructor();
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* CommandFlags
|
|
51
|
+
* @description Flags that modify a command's execution
|
|
52
|
+
* @enum {number}
|
|
53
|
+
*/
|
|
54
|
+
declare enum CommandFlags {
|
|
55
|
+
/** Specifies that a call should be made using the DELEGATECALL opcode */
|
|
56
|
+
DELEGATECALL = 0,
|
|
57
|
+
/** Specifies that a call should be made using the CALL opcode */
|
|
58
|
+
CALL = 1,
|
|
59
|
+
/** Specifies that a call should be made using the STATICCALL opcode */
|
|
60
|
+
STATICCALL = 2,
|
|
61
|
+
/** Specifies that a call should be made using the CALL opcode, and that the first argument will be the value to send */
|
|
62
|
+
CALL_WITH_VALUE = 3,
|
|
63
|
+
/** A bitmask that selects calltype flags */
|
|
64
|
+
CALLTYPE_MASK = 3,
|
|
65
|
+
/** Specifies that this is an extended command, with an additional command word for indices. Internal use only. */
|
|
66
|
+
EXTENDED_COMMAND = 64,
|
|
67
|
+
/** Specifies that the return value of this call should be wrapped in a `bytes`. Internal use only. */
|
|
68
|
+
TUPLE_RETURN = 128
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Represents a call to a contract function as part of a Weiroll plan.
|
|
72
|
+
*
|
|
73
|
+
* A `FunctionCall` is created by calling functions on a [[Contract]] object, and consumed by
|
|
74
|
+
* passing it to [[Planner.add]], [[Planner.addSubplan]] or [[Planner.replaceState]]
|
|
75
|
+
*/
|
|
76
|
+
declare class FunctionCall {
|
|
77
|
+
/** The Contract this function is on. */
|
|
78
|
+
readonly contract: Contract;
|
|
79
|
+
/** Flags modifying the execution of this function call. */
|
|
80
|
+
readonly flags: CommandFlags;
|
|
81
|
+
/** An ethers.js Fragment that describes the function being called. */
|
|
82
|
+
readonly fragment: FunctionFragment;
|
|
83
|
+
/** An array of arguments to the function. */
|
|
84
|
+
readonly args: Value[];
|
|
85
|
+
/** If the call type is a call-with-value, this property holds the value that will be passed. */
|
|
86
|
+
readonly callvalue?: Value;
|
|
87
|
+
/** @hidden */
|
|
88
|
+
constructor(contract: Contract, flags: CommandFlags, fragment: FunctionFragment, args: Value[], callvalue?: Value);
|
|
89
|
+
/**
|
|
90
|
+
* Returns a new [[FunctionCall]] that sends value with the call.
|
|
91
|
+
* @param value The value (in wei) to send with the call
|
|
92
|
+
*/
|
|
93
|
+
withValue(value: Value): FunctionCall;
|
|
94
|
+
/**
|
|
95
|
+
* Returns a new [[FunctionCall]] whose return value will be wrapped as a `bytes`.
|
|
96
|
+
* This permits capturing the return values of functions with multiple return parameters,
|
|
97
|
+
* which weiroll does not otherwise support.
|
|
98
|
+
*/
|
|
99
|
+
rawValue(): FunctionCall;
|
|
100
|
+
/**
|
|
101
|
+
* Returns a new [[FunctionCall]] that executes a STATICCALL instead of a regular CALL.
|
|
102
|
+
*/
|
|
103
|
+
staticcall(): FunctionCall;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The type of all contract-specific functions on the [[Contract]] object.
|
|
107
|
+
*/
|
|
108
|
+
type ContractFunction = (...args: Array<any>) => FunctionCall;
|
|
109
|
+
declare class BaseContract {
|
|
110
|
+
/** The address of the contract */
|
|
111
|
+
readonly address: string;
|
|
112
|
+
/** Flags specifying the default execution options for all functions */
|
|
113
|
+
readonly commandflags: CommandFlags;
|
|
114
|
+
/** The ethers.js Interface representing the contract */
|
|
115
|
+
readonly interface: Interface;
|
|
116
|
+
/** A mapping from function names to [[ContractFunction]]s. */
|
|
117
|
+
readonly functions: {
|
|
118
|
+
[name: string]: ContractFunction;
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* @param address The address of the contract
|
|
122
|
+
* @param contractInterface The ethers.js Interface representing the contract
|
|
123
|
+
* @param commandflags Optional flags specifying the default execution options for all functions
|
|
124
|
+
*/
|
|
125
|
+
constructor(address: string, contractInterface: ContractInterface, commandflags?: CommandFlags);
|
|
126
|
+
/**
|
|
127
|
+
* Creates a [[Contract]] object from an ethers.js contract.
|
|
128
|
+
* All calls on the returned object will default to being standard CALL operations.
|
|
129
|
+
* Use this when you want your weiroll script to call a standard external contract.
|
|
130
|
+
* @param contract The ethers.js Contract object to wrap.
|
|
131
|
+
* @param commandflags Optionally specifies a non-default call type to use, such as
|
|
132
|
+
* [[CommandFlags.STATICCALL]].
|
|
133
|
+
*/
|
|
134
|
+
static createContract(contract: Contract$1, commandflags?: CommandFlags): Contract;
|
|
135
|
+
/**
|
|
136
|
+
* Creates a [[Contract]] object from an ethers.js contract.
|
|
137
|
+
* All calls on the returned object will default to being DELEGATECALL operations.
|
|
138
|
+
* Use this when you want your weiroll script to call a library specifically designed
|
|
139
|
+
* for use with weiroll.
|
|
140
|
+
* @param contract The ethers.js Contract object to wrap.
|
|
141
|
+
*/
|
|
142
|
+
static createLibrary(contract: Contract$1): Contract;
|
|
143
|
+
/** @hidden */
|
|
144
|
+
static getInterface(contractInterface: ContractInterface): Interface;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Provides a dynamically created interface to interact with Ethereum contracts via weiroll.
|
|
148
|
+
*
|
|
149
|
+
* Once created using the constructor or the [[Contract.createContract]] or [[Contract.createLibrary]]
|
|
150
|
+
* functions, the returned object is automatically populated with methods that match those on the
|
|
151
|
+
* supplied contract. For instance, if your contract has a method `add(uint, uint)`, you can call it on the
|
|
152
|
+
* [[Contract]] object:
|
|
153
|
+
* ```typescript
|
|
154
|
+
* // Assumes `Math` is an ethers.js Contract instance.
|
|
155
|
+
* const math = Contract.createLibrary(Math);
|
|
156
|
+
* const result = math.add(1, 2);
|
|
157
|
+
* ```
|
|
158
|
+
*
|
|
159
|
+
* Calling a contract function returns a [[FunctionCall]] object, which you can pass to [[Planner.add]],
|
|
160
|
+
* [[Planner.addSubplan]], or [[Planner.replaceState]] to add to the sequence of calls to plan.
|
|
161
|
+
*/
|
|
162
|
+
declare class Contract extends BaseContract {
|
|
163
|
+
readonly [key: string]: ContractFunction | any;
|
|
164
|
+
}
|
|
165
|
+
declare enum CommandType {
|
|
166
|
+
CALL = 0,
|
|
167
|
+
RAWCALL = 1,
|
|
168
|
+
SUBPLAN = 2
|
|
169
|
+
}
|
|
170
|
+
declare class Command {
|
|
171
|
+
readonly call: FunctionCall;
|
|
172
|
+
readonly type: CommandType;
|
|
173
|
+
constructor(call: FunctionCall, type: CommandType);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* [[Planner]] is the main class to use to specify a sequence of operations to execute for a
|
|
177
|
+
* weiroll script.
|
|
178
|
+
*
|
|
179
|
+
* To use a [[Planner]], construct it and call [[Planner.add]] with the function calls you wish
|
|
180
|
+
* to execute. For example:
|
|
181
|
+
* ```typescript
|
|
182
|
+
* const events = Contract.createLibrary(Events); // Assumes `Events` is an ethers.js contract object
|
|
183
|
+
* const planner = new Planner();
|
|
184
|
+
* planner.add(events.logUint(123));
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
declare class Planner {
|
|
188
|
+
/**
|
|
189
|
+
* Represents the current state of the planner.
|
|
190
|
+
* This value can be passed as an argument to a function that accepts a `bytes[]`. At runtime it will
|
|
191
|
+
* be replaced with the current state of the weiroll planner.
|
|
192
|
+
*/
|
|
193
|
+
readonly state: StateValue;
|
|
194
|
+
/** @hidden */
|
|
195
|
+
commands: Command[];
|
|
196
|
+
constructor();
|
|
197
|
+
/**
|
|
198
|
+
* Adds a new function call to the planner. Function calls are executed in the order they are added.
|
|
199
|
+
*
|
|
200
|
+
* If the function call has a return value, `add` returns an object representing that value, which you
|
|
201
|
+
* can pass to subsequent function calls. For example:
|
|
202
|
+
* ```typescript
|
|
203
|
+
* const math = Contract.createLibrary(Math); // Assumes `Math` is an ethers.js contract object
|
|
204
|
+
* const events = Contract.createLibrary(Events); // Assumes `Events` is an ethers.js contract object
|
|
205
|
+
* const planner = new Planner();
|
|
206
|
+
* const sum = planner.add(math.add(21, 21));
|
|
207
|
+
* planner.add(events.logUint(sum));
|
|
208
|
+
* ```
|
|
209
|
+
* @param call The [[FunctionCall]] to add to the planner
|
|
210
|
+
* @returns An object representing the return value of the call, or null if it does not return a value.
|
|
211
|
+
*/
|
|
212
|
+
add(call: FunctionCall): ReturnValue | null;
|
|
213
|
+
/**
|
|
214
|
+
* Adds a call to a subplan. This has the effect of instantiating a nested instance of the weiroll
|
|
215
|
+
* interpreter, and is commonly used for functionality such as flashloans, control flow, or anywhere
|
|
216
|
+
* else you may need to execute logic inside a callback.
|
|
217
|
+
*
|
|
218
|
+
* A [[FunctionCall]] passed to [[Planner.addSubplan]] must take another [[Planner]] object as one
|
|
219
|
+
* argument, and a placeholder representing the planner state, accessible as [[Planner.state]], as
|
|
220
|
+
* another. Exactly one of each argument must be provided.
|
|
221
|
+
*
|
|
222
|
+
* At runtime, the subplan is replaced by a list of commands for the subplanner (type `bytes32[]`),
|
|
223
|
+
* and `planner.state` is replaced by the current state of the parent planner instance (type `bytes[]`).
|
|
224
|
+
*
|
|
225
|
+
* If the `call` returns a `bytes[]`, this will be used to replace the parent planner's state after
|
|
226
|
+
* the call to the subplanner completes. Return values defined inside a subplan may be used outside that
|
|
227
|
+
* subplan - both in the parent planner and in subsequent subplans - only if the `call` returns the
|
|
228
|
+
* updated planner state.
|
|
229
|
+
*
|
|
230
|
+
* Example usage:
|
|
231
|
+
* ```
|
|
232
|
+
* const exchange = Contract.createLibrary(Exchange); // Assumes `Exchange` is an ethers.js contract
|
|
233
|
+
* const events = Contract.createLibrary(Events); // Assumes `Events` is an ethers.js contract
|
|
234
|
+
* const subplanner = new Planner();
|
|
235
|
+
* const outqty = subplanner.add(exchange.swap(tokenb, tokena, qty));
|
|
236
|
+
*
|
|
237
|
+
* const planner = new Planner();
|
|
238
|
+
* planner.addSubplan(exchange.flashswap(tokena, tokenb, qty, subplanner, planner.state));
|
|
239
|
+
* planner.add(events.logUint(outqty)); // Only works if `exchange.flashswap` returns updated state
|
|
240
|
+
* ```
|
|
241
|
+
* @param call The [[FunctionCall]] to add to the planner.
|
|
242
|
+
*/
|
|
243
|
+
addSubplan(call: FunctionCall): void;
|
|
244
|
+
/**
|
|
245
|
+
* Executes a [[FunctionCall]], and replaces the planner state with the value it
|
|
246
|
+
* returns. This can be used to execute functions that make arbitrary changes to
|
|
247
|
+
* the planner state. Note that the planner library is not aware of these changes -
|
|
248
|
+
* so it may produce invalid plans if you don't know what you're doing.
|
|
249
|
+
* @param call The [[FunctionCall]] to execute
|
|
250
|
+
*/
|
|
251
|
+
replaceState(call: FunctionCall): void;
|
|
252
|
+
private preplan;
|
|
253
|
+
private buildCommandArgs;
|
|
254
|
+
private buildCommands;
|
|
255
|
+
/**
|
|
256
|
+
* Builds an execution plan for all the commands added to the planner.
|
|
257
|
+
* @returns `commands` and `state`, which can be passed directly to the weiroll executor
|
|
258
|
+
* to execute the plan.
|
|
259
|
+
*/
|
|
260
|
+
plan(): {
|
|
261
|
+
commands: string[];
|
|
262
|
+
state: string[];
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
declare enum WeirollCommandFlags {
|
|
267
|
+
DELEGATECALL = 0,
|
|
268
|
+
CALL = 1,
|
|
269
|
+
STATICCALL = 2,
|
|
270
|
+
CALL_WITH_VALUE = 3,
|
|
271
|
+
CALLTYPE_MASK = 3,
|
|
272
|
+
EXTENDED_COMMAND = 64,
|
|
273
|
+
TUPLE_RETURN = 128
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Create a weiroll transaction
|
|
277
|
+
*
|
|
278
|
+
* @param addToPlanner - A function that adds the commands to the planner
|
|
279
|
+
*
|
|
280
|
+
* @returns An EVM call
|
|
281
|
+
*/
|
|
282
|
+
declare function createWeirollDelegateCall(addToPlanner: (planner: Planner) => void): EvmCall;
|
|
283
|
+
declare function createWeirollContract(contract: GenericContract, commandflags?: WeirollCommandFlags): Contract;
|
|
284
|
+
declare function createWeirollLibrary(contract: GenericContract): Contract;
|
|
285
|
+
|
|
286
|
+
export { WeirollCommandFlags, Contract as WeirollContract, Planner as WeirollPlanner, createWeirollContract, createWeirollDelegateCall, createWeirollLibrary };
|