Skip to content

Commit

Permalink
feat(limit-order-contract): add methods without args (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
vbrvk authored Mar 15, 2024
1 parent 1a7b82b commit 30fdbed
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ npm install '@1inch/limit-order-sdk'

## Docs
- [Limit Order](./src/limit-order/README.md)
- [Limit Order Contract](./src/limit-order-contract/README.md)

## Usage examples

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"scripts": {
"build": "tsc",
"docs:generate": "tsdoc --src=src/limit-order/* --dest=src/limit-order/README.md",
"docs:generate": "tsdoc --src=src/limit-order/* --dest=src/limit-order/README.md && tsdoc --src=src/limit-order-contract/* --dest=src/limit-order-contract/README.md",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"lint:ci": "eslint \"{src,apps,libs,test}/**/*.ts\"",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --ignore-path .eslintignore --fix",
Expand Down
41 changes: 41 additions & 0 deletions src/limit-order-contract/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## :factory: LimitOrderContract

### Methods

- [getFillOrderCalldata](#gear-getfillordercalldata)
- [getFillContractOrderCalldata](#gear-getfillcontractordercalldata)
- [getFillOrderArgsCalldata](#gear-getfillorderargscalldata)
- [getFillContractOrderArgsCalldata](#gear-getfillcontractorderargscalldata)

#### :gear: getFillOrderCalldata

Fill order WITHOUT an extension and taker interaction

| Method | Type |
| ---------- | ---------- |
| `getFillOrderCalldata` | `(order: LimitOrderV4Struct, signature: string, takerTraits: TakerTraits, amount: bigint) => string` |

#### :gear: getFillContractOrderCalldata

Fill contract order (order maker is smart-contract) WITHOUT an extension and taker interaction

| Method | Type |
| ---------- | ---------- |
| `getFillContractOrderCalldata` | `(order: LimitOrderV4Struct, signature: string, takerTraits: TakerTraits, amount: bigint) => string` |

#### :gear: getFillOrderArgsCalldata

Fill order WITH an extension or taker interaction

| Method | Type |
| ---------- | ---------- |
| `getFillOrderArgsCalldata` | `(order: LimitOrderV4Struct, signature: string, takerTraits: TakerTraits, amount: bigint) => string` |

#### :gear: getFillContractOrderArgsCalldata

Fill contract order (order maker is smart-contract) WITH an extension or taker interaction

| Method | Type |
| ---------- | ---------- |
| `getFillContractOrderArgsCalldata` | `(order: LimitOrderV4Struct, signature: string, takerTraits: TakerTraits, amount: bigint) => string` |

74 changes: 74 additions & 0 deletions src/limit-order-contract/limit-order-contract.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,78 @@
import {Interface, Signature} from 'ethers'
import assert from 'assert'
import {LimitOrderV4Struct, TakerTraits} from '../limit-order'
import LOP_V4_ABI from '../abi/AggregationRouterV6.abi.json'
import {ZX} from '../constants'

const lopContract = new Interface(LOP_V4_ABI)

/**
* @see https://github.com/1inch/limit-order-protocol/blob/3169ea46932ef44114a215a60d1d91ef022b416d/contracts/OrderMixin.sol#L27
*/
export class LimitOrderContract {
/**
* Fill order WITHOUT an extension and taker interaction
*
* @see getFillOrderArgsCalldata
* @see getFillContractOrderCalldata
*/
static getFillOrderCalldata(
order: LimitOrderV4Struct,
signature: string,
takerTraits: TakerTraits,
amount: bigint
): string {
const {r, yParityAndS: vs} = Signature.from(signature)
const {args, trait} = takerTraits.encode()

assert(
args === ZX,
'takerTraits contains args data, use LimitOrderContract.getFillOrderArgsCalldata method'
)

return lopContract.encodeFunctionData('fillOrder', [
order,
r,
vs,
amount,
trait
])
}

/**
* Fill contract order (order maker is smart-contract) WITHOUT an extension and taker interaction
*
* @see getFillContractOrderArgsCalldata
* @see getFillOrderCalldata
*/
static getFillContractOrderCalldata(
order: LimitOrderV4Struct,
signature: string,
takerTraits: TakerTraits,
amount: bigint
): string {
const {args, trait} = takerTraits.encode()

assert(
args === ZX,
'takerTraits contains args data, use LimitOrderContract.getFillContractOrderArgsCalldata method'
)

return lopContract.encodeFunctionData('fillContractOrder', [
order,
signature,
amount,
trait,
args
])
}

/**
* Fill order WITH an extension or taker interaction
*
* @see getFillOrderCalldata
* @see getFillContractOrderArgsCalldata
*/
static getFillOrderArgsCalldata(
order: LimitOrderV4Struct,
signature: string,
Expand All @@ -24,6 +92,12 @@ export class LimitOrderContract {
])
}

/**
* Fill contract order (order maker is smart-contract) WITH an extension or taker interaction
*
* @see getFillOrderArgsCalldata
* @see getFillContractOrderCalldata
*/
static getFillContractOrderArgsCalldata(
order: LimitOrderV4Struct,
signature: string,
Expand Down
7 changes: 7 additions & 0 deletions src/limit-order/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@ Calculates maker amount by linear proportion

### Methods

- [decode](#gear-decode)
- [default](#gear-default)
- [keccak256](#gear-keccak256)
- [isEmpty](#gear-isempty)
- [encode](#gear-encode)

#### :gear: decode

| Method | Type |
| ---------- | ---------- |
| `decode` | `(bytes: string) => Extension` |

#### :gear: default

| Method | Type |
Expand Down
3 changes: 2 additions & 1 deletion src/limit-order/taker-traits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {BitMask, BN, getBytesCount, trim0x} from '@1inch/byte-utils'
import {Extension} from './extension'
import {Interaction} from './interaction'
import {Address} from '../address'
import {ZX} from '../constants'

export enum AmountMode {
/**
Expand Down Expand Up @@ -251,7 +252,7 @@ export class TakerTraits {
.setMask(TakerTraits.ARGS_INTERACTION_LENGTH_MASK, interactionLen)

const args =
(this.receiver?.toString() || '0x') +
(this.receiver?.toString() || ZX) +
trim0x(this.extension?.encode() || '') +
trim0x(this.interaction?.encode() || '')

Expand Down

0 comments on commit 30fdbed

Please sign in to comment.