Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions contracts/src/swap-vm/TestMakerHooks.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
pragma solidity ^0.8.30;

import {IMakerHooks} from "@1inch/swap-vm/interfaces/IMakerHooks.sol";

contract TestMakerHooks is IMakerHooks {
constructor() {}

event HookCalled(string name, bytes makerData, bytes takerData);

function call(address target, bytes memory data) external {
(bool success, bytes memory result) = target.call(data);
require(success, string(result));
}

function preTransferIn(
address,
address,
address,
address,
uint256,
uint256,
bytes32,
bytes calldata makerData,
bytes calldata takerData
) external override {
emit HookCalled("preTransferIn", makerData, takerData);
}

function postTransferIn(
address,
address,
address,
address,
uint256,
uint256,
bytes32,
bytes calldata makerData,
bytes calldata takerData
) external override {
emit HookCalled("postTransferIn", makerData, takerData);
}

function preTransferOut(
address,
address,
address,
address,
uint256,
uint256,
bytes32,
bytes calldata makerData,
bytes calldata takerData
) external override {
emit HookCalled("preTransferOut", makerData, takerData);
}

function postTransferOut(
address,
address,
address,
address,
uint256,
uint256,
bytes32,
bytes calldata makerData,
bytes calldata takerData
) external override {
emit HookCalled("postTransferOut", makerData, takerData);
}
}
2 changes: 1 addition & 1 deletion typescript/aqua/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@1inch/aqua-sdk",
"version": "1.0.4",
"version": "0.0.1",
"description": "1inch Aqua Protocol SDK",
"author": "@1inch",
"license": "LicenseRef-Degensoft-Aqua-Source-1.1",
Expand Down
2 changes: 1 addition & 1 deletion typescript/sdk-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@1inch/sdk-core",
"version": "1.0.2",
"version": "0.0.1",
"description": "Core utilities and types for 1inch SDKs",
"author": "@1inch",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion typescript/swap-vm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@1inch/swap-vm-sdk",
"version": "1.0.5",
"version": "0.0.1",
"description": "1inch Swap VM SDK",
"author": "@1inch",
"license": "LicenseRef-Degensoft-SwapVM-1.1",
Expand Down
41 changes: 1 addition & 40 deletions typescript/swap-vm/src/swap-vm-contract/swap-vm-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import { encodeFunctionData } from 'viem'
import type { CallInfo, Address } from '@1inch/sdk-core'
import { HexString } from '@1inch/sdk-core'
import { BytesBuilder, trim0x } from '@1inch/byte-utils'
import assert from 'node:assert'
import type { QuoteArgs, SwapArgs } from './types'
import type { TakerTraits } from '../swap-vm'
import { SWAP_VM_ABI } from '../abi/SwapVM.abi'
import type { Order } from '../swap-vm/order'

Expand Down Expand Up @@ -55,12 +52,6 @@ export class SwapVMContract {
* @see https://github.com/1inch/swap-vm/blob/main/src/SwapVM.sol#L124
*/
static encodeSwapCallData(args: SwapArgs): HexString {
const sigPlusTakerTraitsAndData = this.buildSigPlusTakerTraitsAndData(
args.order,
args.takerTraits,
args.signature,
)

const result = encodeFunctionData({
abi: SWAP_VM_ABI,
functionName: 'swap',
Expand All @@ -69,7 +60,7 @@ export class SwapVMContract {
args.tokenIn.toString(),
args.tokenOut.toString(),
args.amount,
sigPlusTakerTraitsAndData.toString(),
args.takerTraits.encode().toString(),
],
})

Expand Down Expand Up @@ -106,36 +97,6 @@ export class SwapVMContract {
}
}

/**
* Build sigPlusTakerTraitsAndData parameter from components
* Structure depends on whether signature is required or using Aqua
*/
private static buildSigPlusTakerTraitsAndData(
order: Order,
takerTraits: TakerTraits,
signature?: HexString,
additionalData?: HexString,
): HexString {
const useAquaInsteadOfSignature = order.traits.useAquaInsteadOfSignature

const builder = new BytesBuilder()

if (!useAquaInsteadOfSignature) {
assert(signature, 'signature required when useOfAquaInsteadOfSignature disabled')
const sigBytes = trim0x(signature.toString())
builder.addUint16(BigInt(sigBytes.length / 2))
builder.addBytes(signature.toString())
}

builder.addBytes(takerTraits.encode().toString())

if (additionalData) {
builder.addBytes(additionalData.toString())
}

return new HexString(builder.asHex())
}

public swap(args: SwapArgs): CallInfo {
return SwapVMContract.buildSwapTx(this.address, args)
}
Expand Down
9 changes: 1 addition & 8 deletions typescript/swap-vm/src/swap-vm-contract/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@ type SwapVmSwapInfo = {
}
export type QuoteArgs = SwapVmSwapInfo

export type QuoteNonViewArgs = SwapVmSwapInfo

export type SwapArgs = SwapVmSwapInfo & {
/**
* Optional - not needed if using Aqua
*/
signature?: HexString
}
export type SwapArgs = SwapVmSwapInfo

export type QuoteResult = {
amountIn: bigint
Expand Down
4 changes: 2 additions & 2 deletions typescript/swap-vm/src/swap-vm/taker-traits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class TakerTraits {
static decode(packed: HexString): TakerTraits {
const iter = BytesIter.BigInt(packed.toString())

const offsets = Array.from({ length: 9 }, () => Number(iter.nextUint16()))
const offsets = Array.from({ length: 9 }, () => Number(iter.nextUint16())).reverse()
const flags = new BN(iter.nextUint16())

const dataStr = trim0x(packed.toString()).slice(40)
Expand Down Expand Up @@ -286,7 +286,7 @@ export class TakerTraits {
{ sum: 0, offsets: [] as number[], data: [] as string[] },
)

offsets.forEach((offset) => builder.addUint16(BigInt(offset)))
offsets.reverse().forEach((offset) => builder.addUint16(BigInt(offset)))

let flags = new BN(0n)
flags = flags.setBit(TakerTraits.IS_EXACT_IN_BIT_FLAG, this.exactIn)
Expand Down
9 changes: 7 additions & 2 deletions typescript/swap-vm/tests/setup-evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import Aqua from '@contracts/Aqua.sol/Aqua.json'
import TestTrader from '@contracts/TestTrader.sol/TestTrader.json'
import TestAquaSwapVMRouter from '@contracts/TestAquaSwapVMRouter.sol/TestAquaSwapVMRouter.json'
import TestCustomSwapVM from '@contracts/TestCustomSwapVM.sol/TestCustomSwapVM.json'
import TestMakerHooks from '@contracts/TestMakerHooks.sol/TestMakerHooks.json'

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

const nonce = await deployer.getTransactionCount({ address: account.address })
const [swapVMAquaRouter, customSwapVM, testTrader] = await Promise.all([
const [swapVMAquaRouter, customSwapVM, makerHooks, testTrader] = await Promise.all([
deploy(TestAquaSwapVMRouter as ContractParams, [aqua], deployer, nonce),
deploy(TestCustomSwapVM as ContractParams, [aqua], deployer, nonce + 1),
deploy(TestMakerHooks as ContractParams, [], deployer, nonce + 2),
deploy(
TestTrader as ContractParams,
[aqua, [ADDRESSES.WETH, ADDRESSES.USDC]],
deployer,
nonce + 2,
nonce + 3,
),
])

Expand All @@ -209,6 +212,7 @@ async function deployContracts(transport: Transport, chain: Chain): Promise<Test
testTrader,
swapVMAquaRouter,
customSwapVM,
makerHooks,
}
}

Expand Down Expand Up @@ -278,6 +282,7 @@ export type TestAddresses = {
testTrader: Hex
swapVMAquaRouter: Hex
customSwapVM: Hex
makerHooks: Hex
}

export type TestClient<
Expand Down
Loading
Loading