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
597 changes: 266 additions & 331 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions typescript/aqua/vitest.config.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineConfig } from 'vitest/config'
import tsconfigPaths from 'vite-tsconfig-paths';
import tsconfigPaths from 'vite-tsconfig-paths'

export default defineConfig({
root: __dirname,
Expand All @@ -10,7 +10,7 @@ export default defineConfig({
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
coverage: {
reportsDirectory: '../../coverage/typescript/aqua',
include: ['src']
}
}
include: ['src'],
},
},
})
2 changes: 1 addition & 1 deletion typescript/aqua/vitest.e2e.config.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineConfig } from 'vitest/config'
import tsconfigPaths from 'vite-tsconfig-paths';
import tsconfigPaths from 'vite-tsconfig-paths'

export default defineConfig({
root: __dirname,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { Opcode } from '../opcode'
* Sets initial token balances for the swap program
* @see https://github.com/1inch/swap-vm/blob/main/src/instructions/Balances.sol#L59
**/
export const SET_BALANCES_XD_OPCODE: Opcode<BalancesArgs> = new Opcode(
Symbol('Balances.setBalancesXD'),
export const staticBalancesXD: Opcode<BalancesArgs> = new Opcode(
Symbol('Balances.staticBalancesXD'),
BalancesArgs.CODER,
)

/**
* Reads token balances from program data or contract storage
* @see https://github.com/1inch/swap-vm/blob/main/src/instructions/Balances.sol#L89
**/
export const BALANCES_XD_OPCODE: Opcode<BalancesArgs> = new Opcode(
Symbol('Balances.balancesXD'),
export const dynamicBalancesXD: Opcode<BalancesArgs> = new Opcode(
Symbol('Balances.dynamicBalancesXD'),
BalancesArgs.CODER,
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
import { Address } from '@1inch/sdk-core'
import { Address, AddressHalf } from '@1inch/sdk-core'
import { RegularProgramBuilder } from '../../programs'

describe('Controls Integration', () => {
Expand All @@ -21,13 +21,14 @@ describe('Controls Integration', () => {
it('should build program with conditional jumps', () => {
const builder = new RegularProgramBuilder()

const program = builder.jumpIfExactIn({ nextPC: 50n }).jumpIfExactOut({ nextPC: 75n }).build()
const program = builder
.jumpIfTokenIn({ tokenTail: AddressHalf.fromAddress(USDC), nextPC: 50n })
.jumpIfTokenOut({ tokenTail: AddressHalf.fromAddress(WETH), nextPC: 75n })
.build()

const hex = program.toString()
expect(hex.substring(0, 4)).toBe('0x0b')
expect(hex.substring(4, 10)).toBe('020032')
expect(hex.substring(10, 12)).toBe('0c')
expect(hex.substring(12, 18)).toBe('02004b')
expect(hex.length).toBeGreaterThan(10)
})

it('should build program with token balance check', () => {
Expand All @@ -36,7 +37,7 @@ describe('Controls Integration', () => {
const program = builder.onlyTakerTokenBalanceNonZero({ token: USDC }).build()

const hex = program.toString()
expect(hex.substring(0, 4)).toBe('0x0d')
expect(hex.substring(0, 4)).toBe('0x0e')
expect(hex.substring(4, 6)).toBe('14')
expect(hex.substring(6, 46).toLowerCase()).toBe(USDC.toString().substring(2).toLowerCase())
})
Expand All @@ -54,7 +55,7 @@ describe('Controls Integration', () => {

const hex = program.toString()

expect(hex.substring(0, 4)).toBe('0x0e')
expect(hex.substring(0, 4)).toBe('0x0f')
expect(hex.substring(4, 6)).toBe('34')
})

Expand All @@ -71,7 +72,7 @@ describe('Controls Integration', () => {

const hex = program.toString()

expect(hex.substring(0, 4)).toBe('0x0f')
expect(hex.substring(0, 4)).toBe('0x10')
expect(hex.substring(4, 6)).toBe('1c')
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { BytesBuilder, BytesIter, add0x } from '@1inch/byte-utils'
import { HexString } from '@1inch/sdk-core'
import { DeadlineArgs } from './deadline-args'
import type { IArgsCoder } from '../types'

export class DeadlineArgsCoder implements IArgsCoder<DeadlineArgs> {
encode(args: DeadlineArgs): HexString {
const builder = new BytesBuilder()
builder.addUint40(args.deadline)

return new HexString(add0x(builder.asHex()))
}

decode(data: HexString): DeadlineArgs {
const iter = BytesIter.BigInt(data.toString())
const deadline = iter.nextUint40()

return new DeadlineArgs(deadline)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { HexString } from '@1inch/sdk-core'
import { DeadlineArgsCoder } from './deadline-args-coder'
import type { IArgsData } from '../types'

export class DeadlineArgs implements IArgsData {
public static readonly CODER = new DeadlineArgsCoder()

constructor(public readonly deadline: bigint) {}

static decode(data: HexString): DeadlineArgs {
return DeadlineArgs.CODER.decode(data)
}

toJSON(): Record<string, unknown> {
return {
deadline: this.deadline.toString(),
}
}
}
2 changes: 2 additions & 0 deletions typescript/swap-vm/src/swap-vm/instructions/controls/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export * from './opcodes'
export { JumpArgs } from './jump-args'
export { JumpIfTokenArgs } from './jump-if-token-args'
export { DeadlineArgs } from './deadline-args'
export { OnlyTakerTokenBalanceNonZeroArgs } from './only-taker-token-balance-non-zero-args'
export { OnlyTakerTokenBalanceGteArgs } from './only-taker-token-balance-gte-args'
export { OnlyTakerTokenSupplyShareGteArgs } from './only-taker-token-supply-share-gte-args'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { BytesBuilder, BytesIter, add0x } from '@1inch/byte-utils'
import { AddressHalf, HexString } from '@1inch/sdk-core'
import { JumpIfTokenArgs } from './jump-if-token-args'
import type { IArgsCoder } from '../types'

export class JumpIfTokenArgsCoder implements IArgsCoder<JumpIfTokenArgs> {
encode(args: JumpIfTokenArgs): HexString {
const builder = new BytesBuilder()
builder.addBytes(args.tokenTail.toString())
builder.addUint16(args.nextPC)

return new HexString(add0x(builder.asHex()))
}

decode(data: HexString): JumpIfTokenArgs {
const iter = BytesIter.HexString(data.toString())
const tokenTailBytes = iter.nextBytes(10)
const tokenTail = AddressHalf.fromHex(tokenTailBytes)
const nextPC = BigInt(iter.nextUint16())

return new JumpIfTokenArgs(tokenTail, nextPC)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { HexString, AddressHalf } from '@1inch/sdk-core'
import { JumpIfTokenArgsCoder } from './jump-if-token-args-coder'
import type { IArgsData } from '../types'

export class JumpIfTokenArgs implements IArgsData {
public static readonly CODER = new JumpIfTokenArgsCoder()

constructor(
public readonly tokenTail: AddressHalf,
public readonly nextPC: bigint,
) {}

static decode(data: HexString): JumpIfTokenArgs {
return JumpIfTokenArgs.CODER.decode(data)
}

toJSON(): Record<string, unknown> {
return {
tokenTail: this.tokenTail.toString(),
nextPC: this.nextPC,
}
}
}
31 changes: 12 additions & 19 deletions typescript/swap-vm/src/swap-vm/instructions/controls/opcodes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { JumpArgs } from './jump-args'
import { JumpIfTokenArgs } from './jump-if-token-args'
import { DeadlineArgs } from './deadline-args'
import { OnlyTakerTokenBalanceNonZeroArgs } from './only-taker-token-balance-non-zero-args'
import { OnlyTakerTokenBalanceGteArgs } from './only-taker-token-balance-gte-args'
import { OnlyTakerTokenSupplyShareGteArgs } from './only-taker-token-supply-share-gte-args'
Expand All @@ -11,29 +13,20 @@ import { Opcode } from '../opcode'
**/
export const jump: Opcode<JumpArgs> = new Opcode(Symbol('Controls.jump'), JumpArgs.CODER)

/**
* Jump to specified program counter if swap mode is exact input
* @see https://github.com/1inch/swap-vm/blob/main/src/instructions/Controls.sol#L59
**/
export const jumpIfExactIn: Opcode<JumpArgs> = new Opcode(
Symbol('Controls.jumpIfExactIn'),
JumpArgs.CODER,
export const jumpIfTokenIn: Opcode<JumpIfTokenArgs> = new Opcode(
Symbol('Controls.jumpIfTokenIn'),
JumpIfTokenArgs.CODER,
)

/**
* Jump to specified program counter if swap mode is exact output
* @see https://github.com/1inch/swap-vm/blob/main/src/instructions/Controls.sol#L68
**/
export const jumpIfExactOut: Opcode<JumpArgs> = new Opcode(
Symbol('Controls.jumpIfExactOut'),
JumpArgs.CODER,
export const jumpIfTokenOut: Opcode<JumpIfTokenArgs> = new Opcode(
Symbol('Controls.jumpIfTokenOut'),
JumpIfTokenArgs.CODER,
)

/**
* Reverts if the deadline has been reached
* @see https://github.com/1inch/swap-vm/blob/main/src/instructions/Controls.sol#L96
**/
export const deadline: Opcode<JumpArgs> = new Opcode(Symbol('Controls.deadline'), JumpArgs.CODER)
export const deadline: Opcode<DeadlineArgs> = new Opcode(
Symbol('Controls.deadline'),
DeadlineArgs.CODER,
)
/**
* Requires taker to hold any amount of specified token (supports NFTs)
* @see https://github.com/1inch/swap-vm/blob/main/src/instructions/Controls.sol#L77
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { Opcode } from '../opcode'
* Dutch auction with time-based decay on amountIn
* @see https://github.com/1inch/swap-vm/blob/main/src/instructions/DutchAuction.sol#L75
**/
export const dutchAuctionAmountIn1D = new Opcode(
Symbol('DutchAuction.dutchAuctionAmountIn1D'),
export const dutchAuctionBalanceIn1D = new Opcode(
Symbol('DutchAuction.dutchAuctionBalanceIn1D'),
DutchAuctionArgs.CODER,
)

/**
* Dutch auction with time-based decay on amountOut
* @see https://github.com/1inch/swap-vm/blob/main/src/instructions/DutchAuction.sol#L85
**/
export const dutchAuctionAmountOut1D = new Opcode(
Symbol('DutchAuction.dutchAuctionAmountOut1D'),
export const dutchAuctionBalanceOut1D = new Opcode(
Symbol('DutchAuction.dutchAuctionBalanceOut1D'),
DutchAuctionArgs.CODER,
)
18 changes: 9 additions & 9 deletions typescript/swap-vm/src/swap-vm/instructions/fee/opcodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@ import { ProtocolFeeArgs } from './protocol-fee-args'
import { Opcode } from '../opcode'

/**
* Applies flat fee to computed swap amount (same rate for exactIn and exactOut)
* Applies fee to amountIn
* @see https://github.com/1inch/swap-vm/blob/main/src/instructions/Fee.sol#L66
**/
export const flatFeeXD = new Opcode(Symbol('Fee.flatFeeXD'), FlatFeeArgs.CODER)
export const flatFeeAmountInXD = new Opcode(Symbol('Fee.flatFeeAmountInXD'), FlatFeeArgs.CODER)

/**
* Applies fee to amountIn
* Applies fee to amountOut
* @see https://github.com/1inch/swap-vm/blob/main/src/instructions/Fee.sol#L72
**/
export const flatFeeAmountInXD = new Opcode(Symbol('Fee.flatFeeAmountInXD'), FlatFeeArgs.CODER)
export const flatFeeAmountOutXD = new Opcode(Symbol('Fee.flatFeeAmountOutXD'), FlatFeeArgs.CODER)

/**
* Applies fee to amountOut
* Applies progressive fee to amountIn
* @see https://github.com/1inch/swap-vm/blob/main/src/instructions/Fee.sol#L78
**/
export const flatFeeAmountOutXD = new Opcode(Symbol('Fee.flatFeeAmountOutXD'), FlatFeeArgs.CODER)
export const progressiveFeeInXD = new Opcode(Symbol('Fee.progressiveFeeInXD'), FlatFeeArgs.CODER)

/**
* Applies progressive fee based on price impact
* @see https://github.com/1inch/swap-vm/blob/main/src/instructions/Fee.sol#L84
* Applies progressive fee to amountOut
* @see https://github.com/1inch/swap-vm/blob/main/src/instructions/Fee.sol#L106
**/
export const progressiveFeeXD = new Opcode(Symbol('Fee.progressiveFeeXD'), FlatFeeArgs.CODER)
export const progressiveFeeOutXD = new Opcode(Symbol('Fee.progressiveFeeOutXD'), FlatFeeArgs.CODER)

/**
* Applies protocol fee to amountOut with direct transfer
Expand Down
Loading
Loading