forked from koshikraj/ottopus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintent.test.ts
More file actions
213 lines (191 loc) · 7.92 KB
/
Copy pathintent.test.ts
File metadata and controls
213 lines (191 loc) · 7.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { describe, expect, it } from 'vitest'
import {
amountSchema,
bridgeIntentSchema,
crossesChains,
destinationChainOf,
sourceChainOf,
swapIntentSchema,
transferIntentSchema,
} from './intent.js'
describe('amounts', () => {
it('takes base units as a string', () => {
expect(amountSchema.parse('500000000')).toBe('500000000')
})
it('rejects a number, a float and a negative', () => {
// 10^18 wei exceeds Number.MAX_SAFE_INTEGER, so a float would quietly lose
// precision on an amount someone is about to sign.
expect(() => amountSchema.parse(500 as unknown as string)).toThrow()
expect(() => amountSchema.parse('1.5')).toThrow()
expect(() => amountSchema.parse('-1')).toThrow()
})
})
describe('transfer intent', () => {
it('normalises the destination account', () => {
const intent = transferIntentSchema.parse({
kind: 'transfer',
asset: 'eip155:8453/erc20:0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
amount: '500000000',
to: 'eip155:8453:0xD8DA6BF26964AF9D7EED9E03E53415D37AA96045',
})
expect(intent.to).toBe('eip155:8453:0xd8da6bf26964af9d7eed9e03e53415d37aa96045')
})
it('rejects a bare token symbol', () => {
const bad = { kind: 'transfer', asset: 'USDC', amount: '1', to: 'eip155:1:0xd8da6bf26964af9d7eed9e03e53415d37aa96045' }
expect(() => transferIntentSchema.parse(bad)).toThrow()
})
it('leaves fromAccount optional, so the scorer can choose', () => {
const intent = transferIntentSchema.parse({
kind: 'transfer',
asset: 'eip155:1/slip44:60',
amount: '1',
to: 'eip155:1:0xd8da6bf26964af9d7eed9e03e53415d37aa96045',
})
expect(intent.fromAccount).toBeUndefined()
})
})
describe('swap intent', () => {
const base = { kind: 'swap', from: 'eip155:8453/slip44:60', to: 'eip155:8453/erc20:0x833589fcd6edb6e08f4c7c32d4f71b54bda02913' }
it('accepts exactly one fixed side', () => {
expect(swapIntentSchema.parse({ ...base, amountIn: '1000' }).amountIn).toBe('1000')
expect(swapIntentSchema.parse({ ...base, amountOut: '1000' }).amountOut).toBe('1000')
})
it('rejects both sides fixed, which has no single answer', () => {
expect(() => swapIntentSchema.parse({ ...base, amountIn: '1', amountOut: '1' })).toThrow()
})
it('rejects neither side fixed', () => {
expect(() => swapIntentSchema.parse(base)).toThrow()
})
it('caps slippage, because a wide tolerance is a real loss', () => {
expect(() => swapIntentSchema.parse({ ...base, amountIn: '1', slippageBps: 5000 })).toThrow()
expect(() => swapIntentSchema.parse({ ...base, amountIn: '1', slippageBps: 0 })).toThrow()
expect(swapIntentSchema.parse({ ...base, amountIn: '1', slippageBps: 50 }).slippageBps).toBe(50)
})
})
describe('rejects zero amounts', () => {
it('will not build a plan for nothing', () => {
expect(() => amountSchema.parse('0')).toThrow(/greater than zero/)
expect(() => amountSchema.parse('000')).toThrow(/greater than zero/)
})
})
describe('every identifier in an intent must be on one chain', () => {
it('rejects a transfer whose asset and recipient are on different chains', () => {
expect(() =>
transferIntentSchema.parse({
kind: 'transfer',
asset: 'eip155:8453/erc20:0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
amount: '1',
to: 'eip155:1:0xd8da6bf26964af9d7eed9e03e53415d37aa96045',
}),
).toThrow(/same chain/)
})
it('rejects a transfer funded from an account on a third chain', () => {
expect(() =>
transferIntentSchema.parse({
kind: 'transfer',
asset: 'eip155:8453/erc20:0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
amount: '1',
to: 'eip155:8453:0xd8da6bf26964af9d7eed9e03e53415d37aa96045',
fromAccount: 'eip155:56:0x0000000000000000000000000000000000000001',
}),
).toThrow(/same chain/)
})
it('rejects a cross-chain swap, which is a bridge', () => {
expect(() =>
swapIntentSchema.parse({
kind: 'swap',
from: 'eip155:8453/slip44:60',
to: 'eip155:1/erc20:0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
amountIn: '1',
}),
).toThrow(/bridge/)
})
it('accepts a same-chain transfer', () => {
const ok = transferIntentSchema.parse({
kind: 'transfer',
asset: 'eip155:8453/erc20:0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
amount: '1',
to: 'eip155:8453:0xd8da6bf26964af9d7eed9e03e53415d37aa96045',
fromAccount: 'eip155:8453:0x0000000000000000000000000000000000000001',
})
expect(ok.kind).toBe('transfer')
})
})
describe('bridge intent', () => {
it('rejects a bridge that does not cross chains, and names what that is', () => {
expect(() =>
bridgeIntentSchema.parse({
kind: 'bridge',
from: 'eip155:8453/erc20:0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
to: 'eip155:8453/slip44:60',
amountIn: '1',
}),
).toThrow(/must cross chains/)
})
/**
* The case the old shape could not express. A bridge used to be `{asset,
* amount, toChain}`, which only moved the same asset elsewhere — so "USDC
* on Base for ETH on BNB Chain", the thing people ask for, had nowhere to
* live.
*/
it('accepts a different asset on the other side', () => {
const ok = bridgeIntentSchema.parse({
kind: 'bridge',
from: 'eip155:8453/erc20:0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
to: 'eip155:56/slip44:714',
amountIn: '1',
})
expect(ok).toMatchObject({ from: 'eip155:8453/erc20:0x833589fcd6edb6e08f4c7c32d4f71b54bda02913', to: 'eip155:56/slip44:714', amountIn: '1' })
})
it('insists on exactly one side being fixed, like a swap', () => {
const both = { kind: 'bridge', from: 'eip155:8453/erc20:0x833589fcd6edb6e08f4c7c32d4f71b54bda02913', to: 'eip155:56/slip44:714' }
expect(() => bridgeIntentSchema.parse({ ...both, amountIn: '1', amountOut: '2' })).toThrow(/exactly one/)
expect(() => bridgeIntentSchema.parse(both)).toThrow(/exactly one/)
})
it('reads the destination chain off the asset, and knows it is crossing', () => {
const bridge = bridgeIntentSchema.parse({
kind: 'bridge',
from: 'eip155:8453/erc20:0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
to: 'eip155:56/slip44:714',
amountIn: '1',
})
expect(sourceChainOf(bridge)).toEqual({ namespace: 'eip155', reference: '8453' })
expect(destinationChainOf(bridge)).toEqual({ namespace: 'eip155', reference: '56' })
expect(crossesChains(bridge)).toBe(true)
})
/** A transfer's `to` is an account, not an asset; reading it as one threw. */
it('reads a transfer’s destination from its asset, never from its recipient', () => {
const transfer = transferIntentSchema.parse({
kind: 'transfer',
asset: 'eip155:8453/erc20:0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
amount: '1',
to: 'eip155:8453:0xd8da6bf26964af9d7eed9e03e53415d37aa96045',
})
expect(destinationChainOf(transfer)).toEqual({ namespace: 'eip155', reference: '8453' })
expect(crossesChains(transfer)).toBe(false)
})
it('says a swap and a transfer stay on one chain', () => {
const swap = swapIntentSchema.parse({
kind: 'swap',
from: 'eip155:8453/erc20:0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
to: 'eip155:8453/slip44:60',
amountIn: '1',
})
expect(crossesChains(swap)).toBe(false)
expect(destinationChainOf(swap)).toEqual(sourceChainOf(swap))
})
})
describe('note', () => {
it('is kept, trimmed, and bounded', () => {
const base = {
kind: 'transfer' as const,
asset: 'eip155:8453/slip44:60',
amount: '1',
to: 'eip155:8453:0xd8da6bf26964af9d7eed9e03e53415d37aa96045',
}
expect(transferIntentSchema.parse({ ...base, note: ' rent ' }).note).toBe('rent')
expect(transferIntentSchema.parse(base).note).toBeUndefined()
expect(() => transferIntentSchema.parse({ ...base, note: 'x'.repeat(201) })).toThrow()
expect(() => transferIntentSchema.parse({ ...base, note: ' ' })).toThrow()
})
})