Skip to content

Commit fce6869

Browse files
authored
tests(swap-vm): tests maker hooks (#14)
* chore: tests maker hooks * Update swap-vm.spec.ts
1 parent 70fac61 commit fce6869

File tree

9 files changed

+275
-63
lines changed

9 files changed

+275
-63
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
pragma solidity ^0.8.30;
2+
3+
import {IMakerHooks} from "@1inch/swap-vm/interfaces/IMakerHooks.sol";
4+
5+
contract TestMakerHooks is IMakerHooks {
6+
constructor() {}
7+
8+
event HookCalled(string name, bytes makerData, bytes takerData);
9+
10+
function call(address target, bytes memory data) external {
11+
(bool success, bytes memory result) = target.call(data);
12+
require(success, string(result));
13+
}
14+
15+
function preTransferIn(
16+
address,
17+
address,
18+
address,
19+
address,
20+
uint256,
21+
uint256,
22+
bytes32,
23+
bytes calldata makerData,
24+
bytes calldata takerData
25+
) external override {
26+
emit HookCalled("preTransferIn", makerData, takerData);
27+
}
28+
29+
function postTransferIn(
30+
address,
31+
address,
32+
address,
33+
address,
34+
uint256,
35+
uint256,
36+
bytes32,
37+
bytes calldata makerData,
38+
bytes calldata takerData
39+
) external override {
40+
emit HookCalled("postTransferIn", makerData, takerData);
41+
}
42+
43+
function preTransferOut(
44+
address,
45+
address,
46+
address,
47+
address,
48+
uint256,
49+
uint256,
50+
bytes32,
51+
bytes calldata makerData,
52+
bytes calldata takerData
53+
) external override {
54+
emit HookCalled("preTransferOut", makerData, takerData);
55+
}
56+
57+
function postTransferOut(
58+
address,
59+
address,
60+
address,
61+
address,
62+
uint256,
63+
uint256,
64+
bytes32,
65+
bytes calldata makerData,
66+
bytes calldata takerData
67+
) external override {
68+
emit HookCalled("postTransferOut", makerData, takerData);
69+
}
70+
}

typescript/aqua/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@1inch/aqua-sdk",
3-
"version": "1.0.4",
3+
"version": "0.0.1",
44
"description": "1inch Aqua Protocol SDK",
55
"author": "@1inch",
66
"license": "LicenseRef-Degensoft-Aqua-Source-1.1",

typescript/sdk-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@1inch/sdk-core",
3-
"version": "1.0.2",
3+
"version": "0.0.1",
44
"description": "Core utilities and types for 1inch SDKs",
55
"author": "@1inch",
66
"repository": {

typescript/swap-vm/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@1inch/swap-vm-sdk",
3-
"version": "1.0.5",
3+
"version": "0.0.1",
44
"description": "1inch Swap VM SDK",
55
"author": "@1inch",
66
"license": "LicenseRef-Degensoft-SwapVM-1.1",

typescript/swap-vm/src/swap-vm-contract/swap-vm-contract.ts

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
import { encodeFunctionData } from 'viem'
44
import type { CallInfo, Address } from '@1inch/sdk-core'
55
import { HexString } from '@1inch/sdk-core'
6-
import { BytesBuilder, trim0x } from '@1inch/byte-utils'
7-
import assert from 'node:assert'
86
import type { QuoteArgs, SwapArgs } from './types'
9-
import type { TakerTraits } from '../swap-vm'
107
import { SWAP_VM_ABI } from '../abi/SwapVM.abi'
118
import type { Order } from '../swap-vm/order'
129

@@ -55,12 +52,6 @@ export class SwapVMContract {
5552
* @see https://github.com/1inch/swap-vm/blob/main/src/SwapVM.sol#L124
5653
*/
5754
static encodeSwapCallData(args: SwapArgs): HexString {
58-
const sigPlusTakerTraitsAndData = this.buildSigPlusTakerTraitsAndData(
59-
args.order,
60-
args.takerTraits,
61-
args.signature,
62-
)
63-
6455
const result = encodeFunctionData({
6556
abi: SWAP_VM_ABI,
6657
functionName: 'swap',
@@ -69,7 +60,7 @@ export class SwapVMContract {
6960
args.tokenIn.toString(),
7061
args.tokenOut.toString(),
7162
args.amount,
72-
sigPlusTakerTraitsAndData.toString(),
63+
args.takerTraits.encode().toString(),
7364
],
7465
})
7566

@@ -106,36 +97,6 @@ export class SwapVMContract {
10697
}
10798
}
10899

109-
/**
110-
* Build sigPlusTakerTraitsAndData parameter from components
111-
* Structure depends on whether signature is required or using Aqua
112-
*/
113-
private static buildSigPlusTakerTraitsAndData(
114-
order: Order,
115-
takerTraits: TakerTraits,
116-
signature?: HexString,
117-
additionalData?: HexString,
118-
): HexString {
119-
const useAquaInsteadOfSignature = order.traits.useAquaInsteadOfSignature
120-
121-
const builder = new BytesBuilder()
122-
123-
if (!useAquaInsteadOfSignature) {
124-
assert(signature, 'signature required when useOfAquaInsteadOfSignature disabled')
125-
const sigBytes = trim0x(signature.toString())
126-
builder.addUint16(BigInt(sigBytes.length / 2))
127-
builder.addBytes(signature.toString())
128-
}
129-
130-
builder.addBytes(takerTraits.encode().toString())
131-
132-
if (additionalData) {
133-
builder.addBytes(additionalData.toString())
134-
}
135-
136-
return new HexString(builder.asHex())
137-
}
138-
139100
public swap(args: SwapArgs): CallInfo {
140101
return SwapVMContract.buildSwapTx(this.address, args)
141102
}

typescript/swap-vm/src/swap-vm-contract/types.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,7 @@ type SwapVmSwapInfo = {
1313
}
1414
export type QuoteArgs = SwapVmSwapInfo
1515

16-
export type QuoteNonViewArgs = SwapVmSwapInfo
17-
18-
export type SwapArgs = SwapVmSwapInfo & {
19-
/**
20-
* Optional - not needed if using Aqua
21-
*/
22-
signature?: HexString
23-
}
16+
export type SwapArgs = SwapVmSwapInfo
2417

2518
export type QuoteResult = {
2619
amountIn: bigint

typescript/swap-vm/src/swap-vm/taker-traits.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ export class TakerTraits {
286286
{ sum: 0, offsets: [] as number[], data: [] as string[] },
287287
)
288288

289-
offsets.forEach((offset) => builder.addUint16(BigInt(offset)))
289+
offsets.reverse().forEach((offset) => builder.addUint16(BigInt(offset)))
290290

291291
let flags = new BN(0n)
292292
flags = flags.setBit(TakerTraits.IS_EXACT_IN_BIT_FLAG, this.exactIn)

typescript/swap-vm/tests/setup-evm.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import Aqua from '@contracts/Aqua.sol/Aqua.json'
3535
import TestTrader from '@contracts/TestTrader.sol/TestTrader.json'
3636
import TestAquaSwapVMRouter from '@contracts/TestAquaSwapVMRouter.sol/TestAquaSwapVMRouter.json'
3737
import TestCustomSwapVM from '@contracts/TestCustomSwapVM.sol/TestCustomSwapVM.json'
38+
import TestMakerHooks from '@contracts/TestMakerHooks.sol/TestMakerHooks.json'
3839

3940
import { TestWallet, ADDRESSES } from '@1inch/sdk-core/test-utils'
4041
import { privateKeyToAccount } from 'viem/accounts'
@@ -90,6 +91,7 @@ export class ReadyEvmFork {
9091
const mappings = [
9192
[this.addresses.aqua, 'aqua'],
9293
[this.addresses.swapVMAquaRouter, 'swapVMAquaRouter'],
94+
[this.addresses.customSwapVM, 'customSwapVM'],
9395
[ADDRESSES.USDC, 'USDC'],
9496
[ADDRESSES.WETH, 'WETH'],
9597
[await this.liqProvider.getAddress(), 'liqProvider'],
@@ -193,14 +195,15 @@ async function deployContracts(transport: Transport, chain: Chain): Promise<Test
193195
const aqua = await deploy(Aqua as ContractParams, [], deployer)
194196

195197
const nonce = await deployer.getTransactionCount({ address: account.address })
196-
const [swapVMAquaRouter, customSwapVM, testTrader] = await Promise.all([
198+
const [swapVMAquaRouter, customSwapVM, makerHooks, testTrader] = await Promise.all([
197199
deploy(TestAquaSwapVMRouter as ContractParams, [aqua], deployer, nonce),
198200
deploy(TestCustomSwapVM as ContractParams, [aqua], deployer, nonce + 1),
201+
deploy(TestMakerHooks as ContractParams, [], deployer, nonce + 2),
199202
deploy(
200203
TestTrader as ContractParams,
201204
[aqua, [ADDRESSES.WETH, ADDRESSES.USDC]],
202205
deployer,
203-
nonce + 2,
206+
nonce + 3,
204207
),
205208
])
206209

@@ -209,6 +212,7 @@ async function deployContracts(transport: Transport, chain: Chain): Promise<Test
209212
testTrader,
210213
swapVMAquaRouter,
211214
customSwapVM,
215+
makerHooks,
212216
}
213217
}
214218

@@ -278,6 +282,7 @@ export type TestAddresses = {
278282
testTrader: Hex
279283
swapVMAquaRouter: Hex
280284
customSwapVM: Hex
285+
makerHooks: Hex
281286
}
282287

283288
export type TestClient<

0 commit comments

Comments
 (0)