tempo.ts 0.6.2 → 0.7.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 +128 -0
- package/README.md +6 -2
- package/dist/ox/Transaction.js +1 -1
- package/dist/ox/Transaction.js.map +1 -1
- package/dist/server/Handler.d.ts +346 -0
- package/dist/server/Handler.d.ts.map +1 -0
- package/dist/server/Handler.js +441 -0
- package/dist/server/Handler.js.map +1 -0
- package/dist/server/Kv.d.ts +16 -0
- package/dist/server/Kv.d.ts.map +1 -0
- package/dist/server/Kv.js +25 -0
- package/dist/server/Kv.js.map +1 -0
- package/dist/server/index.d.ts +3 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +3 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/internal/requestListener.d.ts +124 -0
- package/dist/server/internal/requestListener.d.ts.map +1 -0
- package/dist/server/internal/requestListener.js +174 -0
- package/dist/server/internal/requestListener.js.map +1 -0
- package/dist/viem/Actions/amm.d.ts +51 -1245
- package/dist/viem/Actions/amm.d.ts.map +1 -1
- package/dist/viem/Actions/amm.js +15 -478
- package/dist/viem/Actions/amm.js.map +1 -1
- package/dist/viem/Actions/reward.d.ts +0 -1067
- package/dist/viem/Actions/reward.d.ts.map +1 -1
- package/dist/viem/Actions/reward.js +4 -212
- package/dist/viem/Actions/reward.js.map +1 -1
- package/dist/viem/Decorator.d.ts +0 -263
- package/dist/viem/Decorator.d.ts.map +1 -1
- package/dist/viem/Decorator.js +0 -10
- package/dist/viem/Decorator.js.map +1 -1
- package/dist/viem/Storage.d.ts +23 -0
- package/dist/viem/Storage.d.ts.map +1 -0
- package/dist/viem/Storage.js +47 -0
- package/dist/viem/Storage.js.map +1 -0
- package/dist/viem/Transport.d.ts +10 -1
- package/dist/viem/Transport.d.ts.map +1 -1
- package/dist/viem/Transport.js +22 -3
- package/dist/viem/Transport.js.map +1 -1
- package/dist/viem/internal/utils.d.ts +6 -0
- package/dist/viem/internal/utils.d.ts.map +1 -1
- package/dist/viem/internal/utils.js +24 -0
- package/dist/viem/internal/utils.js.map +1 -1
- package/dist/wagmi/Actions/amm.d.ts +0 -225
- package/dist/wagmi/Actions/amm.d.ts.map +1 -1
- package/dist/wagmi/Actions/amm.js +0 -248
- package/dist/wagmi/Actions/amm.js.map +1 -1
- package/dist/wagmi/Actions/reward.d.ts +0 -110
- package/dist/wagmi/Actions/reward.d.ts.map +1 -1
- package/dist/wagmi/Actions/reward.js +0 -121
- package/dist/wagmi/Actions/reward.js.map +1 -1
- package/dist/wagmi/Connector.d.ts +6 -17
- package/dist/wagmi/Connector.d.ts.map +1 -1
- package/dist/wagmi/Connector.js +17 -43
- package/dist/wagmi/Connector.js.map +1 -1
- package/dist/wagmi/Hooks/amm.d.ts +0 -236
- package/dist/wagmi/Hooks/amm.d.ts.map +1 -1
- package/dist/wagmi/Hooks/amm.js +0 -285
- package/dist/wagmi/Hooks/amm.js.map +1 -1
- package/dist/wagmi/Hooks/reward.d.ts +0 -88
- package/dist/wagmi/Hooks/reward.d.ts.map +1 -1
- package/dist/wagmi/Hooks/reward.js +0 -103
- package/dist/wagmi/Hooks/reward.js.map +1 -1
- package/dist/wagmi/KeyManager.d.ts +57 -0
- package/dist/wagmi/KeyManager.d.ts.map +1 -0
- package/dist/wagmi/KeyManager.js +101 -0
- package/dist/wagmi/KeyManager.js.map +1 -0
- package/dist/wagmi/index.d.ts +1 -0
- package/dist/wagmi/index.d.ts.map +1 -1
- package/dist/wagmi/index.js +1 -0
- package/dist/wagmi/index.js.map +1 -1
- package/package.json +8 -2
- package/src/ox/Transaction.ts +1 -1
- package/src/ox/e2e.test.ts +7 -0
- package/src/server/Handler.test.ts +566 -0
- package/src/server/Handler.ts +577 -0
- package/src/server/Kv.ts +40 -0
- package/src/server/index.ts +2 -0
- package/src/server/internal/requestListener.ts +285 -0
- package/src/viem/Actions/amm.test.ts +10 -284
- package/src/viem/Actions/amm.ts +88 -768
- package/src/viem/Actions/reward.test.ts +4 -212
- package/src/viem/Actions/reward.ts +4 -291
- package/src/viem/Decorator.ts +0 -294
- package/src/viem/Storage.ts +88 -0
- package/src/viem/Transport.ts +40 -2
- package/src/viem/e2e.test.ts +106 -3
- package/src/viem/internal/utils.ts +21 -0
- package/src/wagmi/Actions/amm.test.ts +7 -85
- package/src/wagmi/Actions/amm.ts +0 -346
- package/src/wagmi/Actions/reward.test.ts +0 -99
- package/src/wagmi/Actions/reward.ts +0 -203
- package/src/wagmi/Connector.test.ts +4 -1
- package/src/wagmi/Connector.ts +24 -58
- package/src/wagmi/Hooks/amm.test.ts +8 -200
- package/src/wagmi/Hooks/amm.ts +0 -443
- package/src/wagmi/Hooks/reward.test.ts +1 -142
- package/src/wagmi/Hooks/reward.ts +0 -196
- package/src/wagmi/KeyManager.ts +159 -0
- package/src/wagmi/index.ts +1 -0
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
// Credit: https://github.com/mjackson/remix-the-web/blob/main/packages/node-fetch-server/src/lib/request-listener.ts
|
|
3
|
+
|
|
4
|
+
import type * as http from 'node:http'
|
|
5
|
+
import type * as http2 from 'node:http2'
|
|
6
|
+
|
|
7
|
+
export interface RequestListenerOptions {
|
|
8
|
+
/**
|
|
9
|
+
* Overrides the host portion of the incoming request URL. By default the request URL host is
|
|
10
|
+
* derived from the HTTP `Host` header.
|
|
11
|
+
*
|
|
12
|
+
* For example, if you have a `$HOST` environment variable that contains the hostname of your
|
|
13
|
+
* server, you can use it to set the host of all incoming request URLs like so:
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* createRequestListener(handler, { host: process.env.HOST })
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
host?: string
|
|
20
|
+
/**
|
|
21
|
+
* An error handler that determines the response when the request handler throws an error. By
|
|
22
|
+
* default a 500 Internal Server Error response will be sent.
|
|
23
|
+
*/
|
|
24
|
+
onError?: ErrorHandler
|
|
25
|
+
/**
|
|
26
|
+
* Overrides the protocol of the incoming request URL. By default the request URL protocol is
|
|
27
|
+
* derived from the connection protocol. So e.g. when serving over HTTPS (using
|
|
28
|
+
* `https.createServer()`), the request URL will begin with `https:`.
|
|
29
|
+
*/
|
|
30
|
+
protocol?: string
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Wraps a fetch handler in a Node.js request listener that can be used with:
|
|
35
|
+
*
|
|
36
|
+
* - [`http.createServer()`](https://nodejs.org/api/http.html#httpcreateserveroptions-requestlistener)
|
|
37
|
+
* - [`https.createServer()`](https://nodejs.org/api/https.html#httpscreateserveroptions-requestlistener)
|
|
38
|
+
* - [`http2.createServer()`](https://nodejs.org/api/http2.html#http2createserveroptions-onrequesthandler)
|
|
39
|
+
* - [`http2.createSecureServer()`](https://nodejs.org/api/http2.html#http2createsecureserveroptions-onrequesthandler)
|
|
40
|
+
*
|
|
41
|
+
* Example:
|
|
42
|
+
*
|
|
43
|
+
* ```ts
|
|
44
|
+
* import * as http from 'node:http';
|
|
45
|
+
* import { createRequestListener } from '@mjackson/node-fetch-server';
|
|
46
|
+
*
|
|
47
|
+
* async function handler(request) {
|
|
48
|
+
* return new Response('Hello, world!');
|
|
49
|
+
* }
|
|
50
|
+
*
|
|
51
|
+
* let server = http.createServer(
|
|
52
|
+
* createRequestListener(handler)
|
|
53
|
+
* );
|
|
54
|
+
*
|
|
55
|
+
* server.listen(3000);
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* @param handler The fetch handler to use for processing incoming requests.
|
|
59
|
+
* @param options Request listener options.
|
|
60
|
+
* @returns A Node.js request listener function.
|
|
61
|
+
*/
|
|
62
|
+
export function fromFetchHandler(
|
|
63
|
+
handler: FetchHandler,
|
|
64
|
+
options?: RequestListenerOptions,
|
|
65
|
+
): http.RequestListener {
|
|
66
|
+
const onError = options?.onError ?? defaultErrorHandler
|
|
67
|
+
|
|
68
|
+
return async (req, res) => {
|
|
69
|
+
const request = createRequest(req, res, options)
|
|
70
|
+
const client = {
|
|
71
|
+
address: req.socket.remoteAddress!,
|
|
72
|
+
family: req.socket.remoteFamily! as ClientAddress['family'],
|
|
73
|
+
port: req.socket.remotePort!,
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
let response: Response
|
|
77
|
+
try {
|
|
78
|
+
response = await handler(request, client)
|
|
79
|
+
} catch (error) {
|
|
80
|
+
try {
|
|
81
|
+
response = (await onError(error)) ?? internalServerError()
|
|
82
|
+
} catch (error) {
|
|
83
|
+
console.error(`There was an error in the error handler: ${error}`)
|
|
84
|
+
response = internalServerError()
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
await sendResponse(res, response)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function defaultErrorHandler(error: unknown): Response {
|
|
93
|
+
console.error(error)
|
|
94
|
+
return internalServerError()
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function internalServerError(): Response {
|
|
98
|
+
return new Response(
|
|
99
|
+
// "Internal Server Error"
|
|
100
|
+
new Uint8Array([
|
|
101
|
+
73, 110, 116, 101, 114, 110, 97, 108, 32, 83, 101, 114, 118, 101, 114, 32,
|
|
102
|
+
69, 114, 114, 111, 114,
|
|
103
|
+
]),
|
|
104
|
+
{
|
|
105
|
+
headers: {
|
|
106
|
+
'Content-Type': 'text/plain',
|
|
107
|
+
},
|
|
108
|
+
status: 500,
|
|
109
|
+
},
|
|
110
|
+
)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export type RequestOptions = Omit<RequestListenerOptions, 'onError'>
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Creates a [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) object from
|
|
117
|
+
*
|
|
118
|
+
* - a [`http.IncomingMessage`](https://nodejs.org/api/http.html#class-httpincomingmessage)/[`http.ServerResponse`](https://nodejs.org/api/http.html#class-httpserverresponse) pair
|
|
119
|
+
* - a [`http2.Http2ServerRequest`](https://nodejs.org/api/http2.html#class-http2http2serverrequest)/[`http2.Http2ServerResponse`](https://nodejs.org/api/http2.html#class-http2http2serverresponse) pair
|
|
120
|
+
*
|
|
121
|
+
* @param req The incoming request object.
|
|
122
|
+
* @param res The server response object.
|
|
123
|
+
* @param options
|
|
124
|
+
* @returns A request object.
|
|
125
|
+
*/
|
|
126
|
+
export function createRequest(
|
|
127
|
+
req: http.IncomingMessage | http2.Http2ServerRequest,
|
|
128
|
+
res: http.ServerResponse | http2.Http2ServerResponse,
|
|
129
|
+
options?: RequestOptions,
|
|
130
|
+
): Request {
|
|
131
|
+
const controller = new AbortController()
|
|
132
|
+
res.on('close', () => {
|
|
133
|
+
controller.abort()
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
const method = req.method ?? 'GET'
|
|
137
|
+
const headers = createHeaders(req)
|
|
138
|
+
|
|
139
|
+
const protocol =
|
|
140
|
+
options?.protocol ??
|
|
141
|
+
('encrypted' in req.socket && req.socket.encrypted ? 'https:' : 'http:')
|
|
142
|
+
const host = options?.host ?? headers.get('Host') ?? 'localhost'
|
|
143
|
+
const url = new URL(req.url!, `${protocol}//${host}`)
|
|
144
|
+
|
|
145
|
+
const init: RequestInit = { headers, method, signal: controller.signal }
|
|
146
|
+
|
|
147
|
+
if (method !== 'GET' && method !== 'HEAD') {
|
|
148
|
+
init.body = new ReadableStream({
|
|
149
|
+
start(controller) {
|
|
150
|
+
req.on('data', (chunk) => {
|
|
151
|
+
controller.enqueue(
|
|
152
|
+
new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength),
|
|
153
|
+
)
|
|
154
|
+
})
|
|
155
|
+
req.on('end', () => {
|
|
156
|
+
controller.close()
|
|
157
|
+
})
|
|
158
|
+
},
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
// init.duplex = 'half' must be set when body is a ReadableStream, and Node follows the spec.
|
|
162
|
+
// However, this property is not defined in the TypeScript types for RequestInit, so we have
|
|
163
|
+
// to cast it here in order to set it without a type error.
|
|
164
|
+
// See https://fetch.spec.whatwg.org/#dom-requestinit-duplex
|
|
165
|
+
;(init as { duplex: 'half' }).duplex = 'half'
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return new Request(url, init)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Creates a [`Headers`](https://developer.mozilla.org/en-US/docs/Web/API/Headers) object from the headers in a Node.js
|
|
173
|
+
* [`http.IncomingMessage`](https://nodejs.org/api/http.html#class-httpincomingmessage)/[`http2.Http2ServerRequest`](https://nodejs.org/api/http2.html#class-http2http2serverrequest).
|
|
174
|
+
*
|
|
175
|
+
* @param req The incoming request object.
|
|
176
|
+
* @returns A headers object.
|
|
177
|
+
*/
|
|
178
|
+
export function createHeaders(
|
|
179
|
+
req: http.IncomingMessage | http2.Http2ServerRequest,
|
|
180
|
+
): Headers {
|
|
181
|
+
const headers = new Headers()
|
|
182
|
+
|
|
183
|
+
const rawHeaders = req.rawHeaders
|
|
184
|
+
for (let i = 0; i < rawHeaders.length; i += 2) {
|
|
185
|
+
if (rawHeaders[i]?.startsWith(':')) continue
|
|
186
|
+
headers.append(rawHeaders[i]!, rawHeaders[i + 1]!)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return headers
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Sends a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) to the client using a Node.js
|
|
194
|
+
* [`http.ServerResponse`](https://nodejs.org/api/http.html#class-httpserverresponse)/[`http2.Http2ServerResponse`](https://nodejs.org/api/http2.html#class-http2http2serverresponse)
|
|
195
|
+
* object.
|
|
196
|
+
*
|
|
197
|
+
* @param res The server response object.
|
|
198
|
+
* @param response The response to send.
|
|
199
|
+
*/
|
|
200
|
+
export async function sendResponse(
|
|
201
|
+
res: http.ServerResponse | http2.Http2ServerResponse,
|
|
202
|
+
response: Response,
|
|
203
|
+
): Promise<void> {
|
|
204
|
+
// Iterate over response.headers so we are sure to send multiple Set-Cookie headers correctly.
|
|
205
|
+
// These would incorrectly be merged into a single header if we tried to use
|
|
206
|
+
// `Object.fromEntries(response.headers.entries())`.
|
|
207
|
+
const headers: Record<string, string | string[]> = {}
|
|
208
|
+
for (const [key, value] of response.headers as any) {
|
|
209
|
+
if (key in headers) {
|
|
210
|
+
if (Array.isArray(headers[key])) {
|
|
211
|
+
headers[key].push(value)
|
|
212
|
+
} else {
|
|
213
|
+
headers[key] = [headers[key] as string, value]
|
|
214
|
+
}
|
|
215
|
+
} else {
|
|
216
|
+
headers[key] = value
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
;(res as any).writeHead(response.status, headers)
|
|
221
|
+
|
|
222
|
+
if (response.body != null && res.req.method !== 'HEAD') {
|
|
223
|
+
for await (const chunk of readStream(response.body)) {
|
|
224
|
+
// @ts-expect-error - Node typings for http2 require a 2nd parameter to write but it's optional
|
|
225
|
+
res.write(chunk)
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
res.end()
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export async function* readStream(
|
|
233
|
+
stream: ReadableStream<Uint8Array>,
|
|
234
|
+
): AsyncIterable<Uint8Array> {
|
|
235
|
+
const reader = stream.getReader()
|
|
236
|
+
|
|
237
|
+
while (true) {
|
|
238
|
+
const { done, value } = await reader.read()
|
|
239
|
+
if (done) break
|
|
240
|
+
yield value
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export interface ClientAddress {
|
|
245
|
+
/**
|
|
246
|
+
* The IP address of the client that sent the request.
|
|
247
|
+
*
|
|
248
|
+
* [Node.js Reference](https://nodejs.org/api/net.html#socketremoteaddress)
|
|
249
|
+
*/
|
|
250
|
+
address: string
|
|
251
|
+
/**
|
|
252
|
+
* The family of the client IP address.
|
|
253
|
+
*
|
|
254
|
+
* [Node.js Reference](https://nodejs.org/api/net.html#socketremotefamily)
|
|
255
|
+
*/
|
|
256
|
+
family: 'IPv4' | 'IPv6'
|
|
257
|
+
/**
|
|
258
|
+
* The remote port of the client that sent the request.
|
|
259
|
+
*
|
|
260
|
+
* [Node.js Reference](https://nodejs.org/api/net.html#socketremoteport)
|
|
261
|
+
*/
|
|
262
|
+
port: number
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* A function that handles an error that occurred during request handling. May return a response to
|
|
267
|
+
* send to the client, or `undefined` to allow the server to send a default error response.
|
|
268
|
+
*
|
|
269
|
+
* [MDN `Response` Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response)
|
|
270
|
+
*/
|
|
271
|
+
export type ErrorHandler = (
|
|
272
|
+
error: unknown,
|
|
273
|
+
) => undefined | Response | Promise<undefined | Response>
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* A function that handles an incoming request and returns a response.
|
|
277
|
+
*
|
|
278
|
+
* [MDN `Request` Reference](https://developer.mozilla.org/en-US/docs/Web/API/Request)
|
|
279
|
+
*
|
|
280
|
+
* [MDN `Response` Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response)
|
|
281
|
+
*/
|
|
282
|
+
export type FetchHandler = (
|
|
283
|
+
request: Request,
|
|
284
|
+
client: ClientAddress,
|
|
285
|
+
) => Response | Promise<Response>
|
|
@@ -3,14 +3,9 @@ import { Abis, Actions } from 'tempo.ts/viem'
|
|
|
3
3
|
import { parseUnits } from 'viem'
|
|
4
4
|
import { writeContractSync } from 'viem/actions'
|
|
5
5
|
import { describe, expect, test } from 'vitest'
|
|
6
|
-
import {
|
|
7
|
-
accounts,
|
|
8
|
-
clientWithAccount,
|
|
9
|
-
setupPoolWithLiquidity,
|
|
10
|
-
} from '../../../test/viem/config.js'
|
|
6
|
+
import { accounts, clientWithAccount } from '../../../test/viem/config.js'
|
|
11
7
|
|
|
12
8
|
const account = accounts[0]
|
|
13
|
-
const account2 = accounts[1]
|
|
14
9
|
|
|
15
10
|
describe('getPool', () => {
|
|
16
11
|
test('default', async () => {
|
|
@@ -41,78 +36,6 @@ describe('getLiquidityBalance', () => {
|
|
|
41
36
|
|
|
42
37
|
describe('mint', () => {
|
|
43
38
|
test('default', async () => {
|
|
44
|
-
// Create a new token for testing
|
|
45
|
-
const { token } = await Actions.token.createSync(clientWithAccount, {
|
|
46
|
-
name: 'Test Token',
|
|
47
|
-
symbol: 'TEST',
|
|
48
|
-
currency: 'USD',
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
// Grant issuer role to mint tokens
|
|
52
|
-
await Actions.token.grantRolesSync(clientWithAccount, {
|
|
53
|
-
token,
|
|
54
|
-
roles: ['issuer'],
|
|
55
|
-
to: clientWithAccount.account.address,
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
// Mint some tokens to account
|
|
59
|
-
await Actions.token.mintSync(clientWithAccount, {
|
|
60
|
-
to: clientWithAccount.account.address,
|
|
61
|
-
amount: parseUnits('1000', 6),
|
|
62
|
-
token,
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
// Add liquidity to pool
|
|
66
|
-
const { receipt: mintReceipt, ...mintResult } = await Actions.amm.mintSync(
|
|
67
|
-
clientWithAccount,
|
|
68
|
-
{
|
|
69
|
-
userToken: {
|
|
70
|
-
address: token,
|
|
71
|
-
amount: parseUnits('100', 6),
|
|
72
|
-
},
|
|
73
|
-
validatorToken: {
|
|
74
|
-
address: 1n,
|
|
75
|
-
amount: parseUnits('100', 6),
|
|
76
|
-
},
|
|
77
|
-
to: account.address,
|
|
78
|
-
},
|
|
79
|
-
)
|
|
80
|
-
const { sender, userToken, ...rest } = mintResult
|
|
81
|
-
expect(mintReceipt).toBeDefined()
|
|
82
|
-
expect(sender).toBe(clientWithAccount.account.address)
|
|
83
|
-
expect(userToken).toBe(token)
|
|
84
|
-
expect(rest).toMatchInlineSnapshot(`
|
|
85
|
-
{
|
|
86
|
-
"amountUserToken": 100000000n,
|
|
87
|
-
"amountValidatorToken": 100000000n,
|
|
88
|
-
"liquidity": 4999999999999000n,
|
|
89
|
-
"validatorToken": "0x20C0000000000000000000000000000000000001",
|
|
90
|
-
}
|
|
91
|
-
`)
|
|
92
|
-
|
|
93
|
-
// Verify pool reserves
|
|
94
|
-
const pool = await Actions.amm.getPool(clientWithAccount, {
|
|
95
|
-
userToken: token,
|
|
96
|
-
validatorToken: 1n,
|
|
97
|
-
})
|
|
98
|
-
expect(pool).toMatchInlineSnapshot(`
|
|
99
|
-
{
|
|
100
|
-
"reserveUserToken": 100000000n,
|
|
101
|
-
"reserveValidatorToken": 100000000n,
|
|
102
|
-
"totalSupply": 5000000000000000n,
|
|
103
|
-
}
|
|
104
|
-
`)
|
|
105
|
-
|
|
106
|
-
// Verify LP token balance
|
|
107
|
-
const lpBalance = await Actions.amm.getLiquidityBalance(clientWithAccount, {
|
|
108
|
-
address: account.address,
|
|
109
|
-
userToken: token,
|
|
110
|
-
validatorToken: 1n,
|
|
111
|
-
})
|
|
112
|
-
expect(lpBalance).toBeGreaterThan(0n)
|
|
113
|
-
})
|
|
114
|
-
|
|
115
|
-
test('behavior: single-sided mint (mintWithValidatorToken)', async () => {
|
|
116
39
|
// Create a new token for testing
|
|
117
40
|
const { token } = await Actions.token.createSync(clientWithAccount, {
|
|
118
41
|
name: 'Test Token 2',
|
|
@@ -136,15 +59,10 @@ describe('mint', () => {
|
|
|
136
59
|
|
|
137
60
|
// First, establish initial liquidity with two-sided mint
|
|
138
61
|
await Actions.amm.mintSync(clientWithAccount, {
|
|
139
|
-
userToken: {
|
|
140
|
-
address: token,
|
|
141
|
-
amount: parseUnits('100', 6),
|
|
142
|
-
},
|
|
143
|
-
validatorToken: {
|
|
144
|
-
address: 1n,
|
|
145
|
-
amount: parseUnits('100', 6),
|
|
146
|
-
},
|
|
147
62
|
to: account.address,
|
|
63
|
+
userTokenAddress: token,
|
|
64
|
+
validatorTokenAddress: 1n,
|
|
65
|
+
validatorTokenAmount: parseUnits('100', 6),
|
|
148
66
|
})
|
|
149
67
|
|
|
150
68
|
// Get initial pool state
|
|
@@ -157,13 +75,9 @@ describe('mint', () => {
|
|
|
157
75
|
const { receipt: mintReceipt, ...mintResult } = await Actions.amm.mintSync(
|
|
158
76
|
clientWithAccount,
|
|
159
77
|
{
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
validatorToken: {
|
|
164
|
-
address: 1n,
|
|
165
|
-
amount: parseUnits('50', 6),
|
|
166
|
-
},
|
|
78
|
+
userTokenAddress: token,
|
|
79
|
+
validatorTokenAddress: 1n,
|
|
80
|
+
validatorTokenAmount: parseUnits('50', 6),
|
|
167
81
|
to: account.address,
|
|
168
82
|
},
|
|
169
83
|
)
|
|
@@ -188,149 +102,6 @@ describe('mint', () => {
|
|
|
188
102
|
})
|
|
189
103
|
})
|
|
190
104
|
|
|
191
|
-
describe('burn', () => {
|
|
192
|
-
test('default', async () => {
|
|
193
|
-
const { tokenAddress } = await setupPoolWithLiquidity(clientWithAccount)
|
|
194
|
-
|
|
195
|
-
// Get LP balance before burn
|
|
196
|
-
const lpBalanceBefore = await Actions.amm.getLiquidityBalance(
|
|
197
|
-
clientWithAccount,
|
|
198
|
-
{
|
|
199
|
-
address: account.address,
|
|
200
|
-
userToken: tokenAddress,
|
|
201
|
-
validatorToken: 1n,
|
|
202
|
-
},
|
|
203
|
-
)
|
|
204
|
-
|
|
205
|
-
// Burn half of LP tokens
|
|
206
|
-
const {
|
|
207
|
-
receipt: burnReceipt,
|
|
208
|
-
userToken,
|
|
209
|
-
...burnResult
|
|
210
|
-
} = await Actions.amm.burnSync(clientWithAccount, {
|
|
211
|
-
userToken: tokenAddress,
|
|
212
|
-
validatorToken: 1n,
|
|
213
|
-
liquidity: lpBalanceBefore / 2n,
|
|
214
|
-
to: account2.address,
|
|
215
|
-
})
|
|
216
|
-
const { sender, to, ...rest } = burnResult
|
|
217
|
-
|
|
218
|
-
expect(burnReceipt).toBeDefined()
|
|
219
|
-
expect(userToken).toBe(tokenAddress)
|
|
220
|
-
expect(sender).toBe(account.address)
|
|
221
|
-
expect(to).toBe(account2.address)
|
|
222
|
-
expect(rest).toMatchInlineSnapshot(`
|
|
223
|
-
{
|
|
224
|
-
"amountUserToken": 49999999n,
|
|
225
|
-
"amountValidatorToken": 49999999n,
|
|
226
|
-
"liquidity": 2499999999999500n,
|
|
227
|
-
"validatorToken": "0x20C0000000000000000000000000000000000001",
|
|
228
|
-
}
|
|
229
|
-
`)
|
|
230
|
-
|
|
231
|
-
// Verify LP balance decreased
|
|
232
|
-
const lpBalanceAfter = await Actions.amm.getLiquidityBalance(
|
|
233
|
-
clientWithAccount,
|
|
234
|
-
{
|
|
235
|
-
address: account.address,
|
|
236
|
-
userToken: tokenAddress,
|
|
237
|
-
validatorToken: 1n,
|
|
238
|
-
},
|
|
239
|
-
)
|
|
240
|
-
expect(lpBalanceAfter).toBeLessThan(lpBalanceBefore)
|
|
241
|
-
expect(lpBalanceAfter).toBe(lpBalanceBefore / 2n)
|
|
242
|
-
|
|
243
|
-
// Verify pool reserves decreased
|
|
244
|
-
const pool = await Actions.amm.getPool(clientWithAccount, {
|
|
245
|
-
userToken: tokenAddress,
|
|
246
|
-
validatorToken: 1n,
|
|
247
|
-
})
|
|
248
|
-
expect(pool).toMatchInlineSnapshot(`
|
|
249
|
-
{
|
|
250
|
-
"reserveUserToken": 50000001n,
|
|
251
|
-
"reserveValidatorToken": 50000001n,
|
|
252
|
-
"totalSupply": 2500000000000500n,
|
|
253
|
-
}
|
|
254
|
-
`)
|
|
255
|
-
})
|
|
256
|
-
})
|
|
257
|
-
|
|
258
|
-
describe('rebalanceSwap', () => {
|
|
259
|
-
test('default', async () => {
|
|
260
|
-
const { tokenAddress } = await setupPoolWithLiquidity(clientWithAccount)
|
|
261
|
-
|
|
262
|
-
// Get balance before swap
|
|
263
|
-
const balanceBefore = await Actions.token.getBalance(clientWithAccount, {
|
|
264
|
-
token: tokenAddress,
|
|
265
|
-
account: account2.address,
|
|
266
|
-
})
|
|
267
|
-
|
|
268
|
-
// Perform rebalance swap
|
|
269
|
-
const {
|
|
270
|
-
receipt: swapReceipt,
|
|
271
|
-
swapper,
|
|
272
|
-
userToken,
|
|
273
|
-
...swapResult
|
|
274
|
-
} = await Actions.amm.rebalanceSwapSync(clientWithAccount, {
|
|
275
|
-
userToken: tokenAddress,
|
|
276
|
-
validatorToken: 1n,
|
|
277
|
-
amountOut: parseUnits('10', 6),
|
|
278
|
-
to: account2.address,
|
|
279
|
-
account: account,
|
|
280
|
-
})
|
|
281
|
-
expect(swapReceipt).toBeDefined()
|
|
282
|
-
expect(userToken).toBe(tokenAddress)
|
|
283
|
-
expect(swapper).toBe(account.address)
|
|
284
|
-
expect(swapResult).toMatchInlineSnapshot(`
|
|
285
|
-
{
|
|
286
|
-
"amountIn": 9985001n,
|
|
287
|
-
"amountOut": 10000000n,
|
|
288
|
-
"validatorToken": "0x20C0000000000000000000000000000000000001",
|
|
289
|
-
}
|
|
290
|
-
`)
|
|
291
|
-
|
|
292
|
-
// Verify balance increased
|
|
293
|
-
const balanceAfter = await Actions.token.getBalance(clientWithAccount, {
|
|
294
|
-
token: tokenAddress,
|
|
295
|
-
account: account2.address,
|
|
296
|
-
})
|
|
297
|
-
expect(balanceAfter).toBe(balanceBefore + parseUnits('10', 6))
|
|
298
|
-
})
|
|
299
|
-
})
|
|
300
|
-
|
|
301
|
-
describe('watchRebalanceSwap', () => {
|
|
302
|
-
test('default', async () => {
|
|
303
|
-
const { tokenAddress } = await setupPoolWithLiquidity(clientWithAccount)
|
|
304
|
-
|
|
305
|
-
let eventArgs: any = null
|
|
306
|
-
const unwatch = Actions.amm.watchRebalanceSwap(clientWithAccount, {
|
|
307
|
-
onRebalanceSwap: (args) => {
|
|
308
|
-
eventArgs = args
|
|
309
|
-
},
|
|
310
|
-
})
|
|
311
|
-
|
|
312
|
-
// Perform rebalance swap
|
|
313
|
-
await Actions.amm.rebalanceSwapSync(clientWithAccount, {
|
|
314
|
-
userToken: tokenAddress,
|
|
315
|
-
validatorToken: 1n,
|
|
316
|
-
amountOut: parseUnits('10', 6),
|
|
317
|
-
to: account2.address,
|
|
318
|
-
account: account,
|
|
319
|
-
})
|
|
320
|
-
|
|
321
|
-
await setTimeout(1000)
|
|
322
|
-
|
|
323
|
-
expect(eventArgs).toBeDefined()
|
|
324
|
-
expect(eventArgs.userToken.toLowerCase()).toBe(tokenAddress.toLowerCase())
|
|
325
|
-
expect(eventArgs.validatorToken.toLowerCase()).toBe(
|
|
326
|
-
'0x20c0000000000000000000000000000000000001',
|
|
327
|
-
)
|
|
328
|
-
expect(eventArgs.amountOut).toBe(parseUnits('10', 6))
|
|
329
|
-
|
|
330
|
-
unwatch()
|
|
331
|
-
})
|
|
332
|
-
})
|
|
333
|
-
|
|
334
105
|
describe('watchMint', () => {
|
|
335
106
|
test('default', async () => {
|
|
336
107
|
// Create a new token for testing
|
|
@@ -371,14 +142,9 @@ describe('watchMint', () => {
|
|
|
371
142
|
|
|
372
143
|
// Add liquidity to pool
|
|
373
144
|
await Actions.amm.mintSync(clientWithAccount, {
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
},
|
|
378
|
-
validatorToken: {
|
|
379
|
-
address: 1n,
|
|
380
|
-
amount: parseUnits('100', 6),
|
|
381
|
-
},
|
|
145
|
+
userTokenAddress: token,
|
|
146
|
+
validatorTokenAddress: 1n,
|
|
147
|
+
validatorTokenAmount: parseUnits('100', 6),
|
|
382
148
|
to: account.address,
|
|
383
149
|
})
|
|
384
150
|
|
|
@@ -389,48 +155,8 @@ describe('watchMint', () => {
|
|
|
389
155
|
expect(eventArgs.validatorToken.address.toLowerCase()).toBe(
|
|
390
156
|
'0x20c0000000000000000000000000000000000001',
|
|
391
157
|
)
|
|
392
|
-
expect(eventArgs.userToken.amount).toBe(parseUnits('100', 6))
|
|
393
158
|
expect(eventArgs.validatorToken.amount).toBe(parseUnits('100', 6))
|
|
394
159
|
|
|
395
160
|
unwatch()
|
|
396
161
|
})
|
|
397
162
|
})
|
|
398
|
-
|
|
399
|
-
describe('watchBurn', () => {
|
|
400
|
-
test('default', async () => {
|
|
401
|
-
const { tokenAddress } = await setupPoolWithLiquidity(clientWithAccount)
|
|
402
|
-
|
|
403
|
-
// Get LP balance
|
|
404
|
-
const lpBalance = await Actions.amm.getLiquidityBalance(clientWithAccount, {
|
|
405
|
-
userToken: tokenAddress,
|
|
406
|
-
validatorToken: 1n,
|
|
407
|
-
address: account.address,
|
|
408
|
-
})
|
|
409
|
-
|
|
410
|
-
let eventArgs: any = null
|
|
411
|
-
const unwatch = Actions.amm.watchBurn(clientWithAccount, {
|
|
412
|
-
onBurn: (args) => {
|
|
413
|
-
eventArgs = args
|
|
414
|
-
},
|
|
415
|
-
})
|
|
416
|
-
|
|
417
|
-
// Burn LP tokens
|
|
418
|
-
await Actions.amm.burnSync(clientWithAccount, {
|
|
419
|
-
userToken: tokenAddress,
|
|
420
|
-
validatorToken: 1n,
|
|
421
|
-
liquidity: lpBalance / 2n,
|
|
422
|
-
to: account.address,
|
|
423
|
-
})
|
|
424
|
-
|
|
425
|
-
await setTimeout(1000)
|
|
426
|
-
|
|
427
|
-
expect(eventArgs).toBeDefined()
|
|
428
|
-
expect(eventArgs.userToken.toLowerCase()).toBe(tokenAddress.toLowerCase())
|
|
429
|
-
expect(eventArgs.validatorToken.toLowerCase()).toBe(
|
|
430
|
-
'0x20c0000000000000000000000000000000000001',
|
|
431
|
-
)
|
|
432
|
-
expect(eventArgs.liquidity).toBe(lpBalance / 2n)
|
|
433
|
-
|
|
434
|
-
unwatch()
|
|
435
|
-
})
|
|
436
|
-
})
|