Skip to content

Commit

Permalink
feat(limit-order-protocol-facade): update limit order protocol contracts
Browse files Browse the repository at this point in the history
New version of limit order protocol

BREAKING CHANGE: LimitOrderProtocolFacade.simulateTransferFroms() renamed to simulateCalls()
  • Loading branch information
shoom3301 committed Jun 3, 2021
1 parent 02525b9 commit 7be31d8
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 62 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ yarn install @1inch/limit-order-protocol

## Protocol addresses

- Ethereum mainnet: `0x4aaffca65f5f9cbf51abf0f03d11d5f446bdf8e7`
- BSC mainnet: `0x35df9901e79aca6b920abbb53758ffb3de725af8`
- Polygon mainnet: `0x59a0a6d73e6a5224871f45e6d845ce1574063ade`
- Ethereum mainnet: `0x3ef51736315f52d568d6d2cf289419b9cfffe782`
- BSC mainnet: `0xe3456f4ee65e745a44ec3bcb83d0f2529d1b84eb`
- Polygon mainnet: `0xb707d89d29c189421163515c59e42147371d6857`

---

Expand Down Expand Up @@ -286,13 +286,13 @@ sendTransaction({

## Validate a limit order

`LimitOrderProtocolFacade.simulateTransferFroms()`
`LimitOrderProtocolFacade.simulateCalls()`

There is the possibility to check limit orders validity.
For example: you can check that a limit order is valid by predicates.

> **Under the hood:**
> On a `simulateTransferFroms()` call, the contract returns the string like `TRANSFERS_SUCCESSFUL_01101`
> On a `simulateCalls()` call, the contract returns the string like `CALL_RESULTS_01101`
> If that string contains at least one `0` symbol, then a limit order is invalid, otherwise - valid
### Example:
Expand All @@ -310,7 +310,7 @@ const addresses = [contractAddress];
const callDatas = [order.predicate];

try {
const result: boolean = await limitOrderProtocolFacade.simulateTransferFroms(addresses, callDatas);
const result: boolean = await limitOrderProtocolFacade.simulateCalls(addresses, callDatas);

console.log('Order validity: ', result);
} catch (error) {
Expand Down
102 changes: 85 additions & 17 deletions src/abi/LimitOrderProtocol.json
Original file line number Diff line number Diff line change
Expand Up @@ -255,21 +255,16 @@
},
{
"internalType": "uint256",
"name": "amount",
"name": "tokenId",
"type": "uint256"
},
{
"internalType": "contract IERC1155",
"internalType": "contract IERC721",
"name": "token",
"type": "address"
},
{
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
}
],
"name": "func_00cMjE8",
"name": "func_20xtkDI",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
Expand Down Expand Up @@ -297,7 +292,7 @@
"type": "address"
}
],
"name": "func_20xtkDI",
"name": "func_40aVqeY",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
Expand All @@ -316,16 +311,16 @@
},
{
"internalType": "uint256",
"name": "tokenId",
"name": "amount",
"type": "uint256"
},
{
"internalType": "contract IERC721",
"internalType": "contract IERC20",
"name": "token",
"type": "address"
}
],
"name": "func_40aVqeY",
"name": "func_50BkM4K",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
Expand All @@ -348,12 +343,22 @@
"type": "uint256"
},
{
"internalType": "contract IERC20",
"internalType": "contract IERC1155",
"name": "token",
"type": "address"
},
{
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
},
{
"internalType": "bytes",
"name": "data",
"type": "bytes"
}
],
"name": "func_50BkM4K",
"name": "func_733NCGU",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
Expand Down Expand Up @@ -431,8 +436,8 @@
{
"inputs": [
{
"internalType": "contract IERC20[]",
"name": "tokens",
"internalType": "address[]",
"name": "targets",
"type": "address[]"
},
{
Expand All @@ -441,7 +446,7 @@
"type": "bytes[]"
}
],
"name": "simulateTransferFroms",
"name": "simulateCalls",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
Expand Down Expand Up @@ -578,6 +583,40 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "contract AggregatorV3Interface",
"name": "oracle1",
"type": "address"
},
{
"internalType": "contract AggregatorV3Interface",
"name": "oracle2",
"type": "address"
},
{
"internalType": "uint256",
"name": "spread",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "doublePrice",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
Expand Down Expand Up @@ -910,6 +949,35 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "contract AggregatorV3Interface",
"name": "oracle",
"type": "address"
},
{
"internalType": "uint256",
"name": "inverseAndSpread",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "singlePrice",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
Expand Down
2 changes: 1 addition & 1 deletion src/limit-order-predicate.builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from '../test/predicate-snapshots';

describe('PredicateBuilder - for build limit order predicate', () => {
const contractAddress = '0x35df9901e79aca6b920abbb53758ffb3de725af8';
const contractAddress = '0xe3456f4ee65e745a44ec3bcb83d0f2529d1b84eb';
const walletAddress = '0xfb3c7eb936caa12b5a884d612393969a557d4307';
const WBNB_ADDRESS = '0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c';

Expand Down
2 changes: 1 addition & 1 deletion src/limit-order-protocol.const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const ZX = '0x';

export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';

export const SIMULATE_TRANSFER_PREFIX = 'TRANSFERS_SUCCESSFUL_';
export const CALL_RESULTS_PREFIX = 'CALL_RESULTS_';

export const LIMIT_ORDER_PROTOCOL_ABI: AbiItem[] = LimitOrderProtocolABISource;

Expand Down
24 changes: 12 additions & 12 deletions src/limit-order-protocol.facade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {FILL_ORDER_SNAPSHOT} from '../test/fill-order-sanpshot';
import {CANCEL_ORDER_SNAPSHOT} from '../test/cancel-order-snapshot';

describe('LimitOrderProtocolFacade - facade for Limit order protocol contract', () => {
const contractAddress = '0x35df9901e79aca6b920abbb53758ffb3de725af8';
const contractAddress = '0xe3456f4ee65e745a44ec3bcb83d0f2529d1b84eb';
const walletAddress = '0xfb3c7eb936cAA12B5A884d612393969A557d4307';
const chainId = 56;

Expand Down Expand Up @@ -151,19 +151,19 @@ describe('LimitOrderProtocolFacade - facade for Limit order protocol contract',
});

it('When order is partially filled, then must return remaining amount', async () => {
// Order 2 BUSD > 2 DAI, filled for 0.4
// Order WBNB > 1INCH, filled for 3%
const orderHash =
'0xf86942f65e9a7dea150cf39cb7f4202b2b51ff9a9a79719e1375dab3c7d98955';
'0x969ecf29e9b02b15e9640a6c741ea2db7984a00843485e0ed6aa4638e6860838';

const remaining = await facade.remaining(orderHash);

expect(remaining.toString()).toBe('1600000000000000000');
expect(remaining.toString()).toBe('2917591702620370');
});

it('When order is canceled, then must return zero', async () => {
// Canceled order
const orderHash =
'0xe55c99da9c8e88bbc3b7461f9a4e7798a3557ee8eff80be5c6706af49659e413';
'0x44579da3014205b53cafe1badd3a4e8327d769560a0f38efd9840c402670312c';

const remaining = await facade.remaining(orderHash);

Expand Down Expand Up @@ -222,7 +222,7 @@ describe('LimitOrderProtocolFacade - facade for Limit order protocol contract',
});
});

describe('simulateTransferFroms()', () => {
describe('simulateCalls()', () => {
it('When an order is invalid by the nonce predicate then must return false', async () => {
const timestamp = Math.floor(Date.now() / 1000) + 100;
const timestampBelow = limitOrderPredicateBuilder.timestampBelow(
Expand All @@ -243,7 +243,7 @@ describe('LimitOrderProtocolFacade - facade for Limit order protocol contract',
const tokens = [contractAddress, walletAddress];
const data = [order.predicate, order.makerAssetData];

const result = await facade.simulateTransferFroms(tokens, data);
const result = await facade.simulateCalls(tokens, data);

expect(result).toBe(false);
});
Expand All @@ -268,7 +268,7 @@ describe('LimitOrderProtocolFacade - facade for Limit order protocol contract',
const tokens = [contractAddress, walletAddress];
const data = [order.predicate, order.makerAssetData];

const result = await facade.simulateTransferFroms(tokens, data);
const result = await facade.simulateCalls(tokens, data);

expect(result).toBe(false);
});
Expand All @@ -293,7 +293,7 @@ describe('LimitOrderProtocolFacade - facade for Limit order protocol contract',
const tokens = [contractAddress, walletAddress];
const data = [order.predicate, order.makerAssetData];

const result = await facade.simulateTransferFroms(tokens, data);
const result = await facade.simulateCalls(tokens, data);

expect(result).toBe(true);
});
Expand All @@ -303,7 +303,7 @@ describe('LimitOrderProtocolFacade - facade for Limit order protocol contract',
const result = await facade.domainSeparator();

expect(result).toBe(
'0x61ba2d5643f6075b336dfa622a46fe041cab25e129bbabcfb273a7b4595522b5'
'0xc01dba435cd14ffa0d760af4ffb49214bf6c739da021e8377adedcb4c4db47e8'
);
});

Expand All @@ -327,14 +327,14 @@ describe('LimitOrderProtocolFacade - facade for Limit order protocol contract',
});

it("parseSimulateTransferError() return true when response doesn't contain any zero chars", () => {
const input = 'TRANSFERS_SUCCESSFUL_11';
const input = 'CALL_RESULTS_11';
const result = facade.parseSimulateTransferError(input);

expect(result).toBe(true);
});

it('parseSimulateTransferError() return false when response contain zero chars', () => {
const input = 'TRANSFERS_SUCCESSFUL_01';
const input = 'CALL_RESULTS_01';
const result = facade.parseSimulateTransferError(input);

expect(result).toBe(false);
Expand Down
29 changes: 19 additions & 10 deletions src/limit-order-protocol.facade.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
LIMIT_ORDER_PROTOCOL_ABI,
SIMULATE_TRANSFER_PREFIX,
CALL_RESULTS_PREFIX,
ZX,
} from './limit-order-protocol.const';
import {
Expand Down Expand Up @@ -110,9 +110,9 @@ export class LimitOrderProtocolFacade {
});
}

simulateTransferFroms(tokens: string[], data: unknown[]): Promise<boolean> {
simulateCalls(tokens: string[], data: unknown[]): Promise<boolean> {
const callData = this.getContractCallData(
LimitOrderProtocolMethods.simulateTransferFroms,
LimitOrderProtocolMethods.simulateCalls,
[tokens, data]
);

Expand Down Expand Up @@ -165,8 +165,8 @@ export class LimitOrderProtocolFacade {
parseSimulateTransferResponse(response: string): boolean | null {
const parsed = this.parseContractResponse(response);

if (parsed.startsWith(SIMULATE_TRANSFER_PREFIX)) {
const data = parsed.replace(SIMULATE_TRANSFER_PREFIX, '');
if (parsed.startsWith(CALL_RESULTS_PREFIX)) {
const data = parsed.replace(CALL_RESULTS_PREFIX, '');

return !data.includes('0');
}
Expand All @@ -175,11 +175,8 @@ export class LimitOrderProtocolFacade {
}

parseSimulateTransferError(error: Error | string): boolean | null {
const message =
typeof error === 'string'
? error
: JSON.stringify(error.message || error);
const regex = new RegExp('(' + SIMULATE_TRANSFER_PREFIX + '\\d+)');
const message = this.stringifyError(error);
const regex = new RegExp('(' + CALL_RESULTS_PREFIX + '\\d+)');
const match = message.match(regex);

if (match) {
Expand All @@ -195,4 +192,16 @@ export class LimitOrderProtocolFacade {
ZX + response.slice(10)
);
}

private stringifyError(error: Error | string | unknown): string {
if (typeof error === 'string') {
return error;
}

if (error instanceof Error) {
return error.toString();
}

return JSON.stringify(error);
}
}
2 changes: 1 addition & 1 deletion src/model/limit-order-protocol.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ export enum LimitOrderProtocolMethods {
transferFrom = 'transferFrom',
checkPredicate = 'checkPredicate',
remainingsRaw = 'remainingsRaw',
simulateTransferFroms = 'simulateTransferFroms',
simulateCalls = 'simulateCalls',
DOMAIN_SEPARATOR = 'DOMAIN_SEPARATOR',
}
Loading

0 comments on commit 7be31d8

Please sign in to comment.