Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Use AssetBuyer for orders with fees #561

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"dependencies": {
"0x.js": "^6.0.11",
"@0x/asset-buyer": "^6.1.9",
"@0x/connect": "^5.0.12",
"@0x/web3-wrapper": "^6.0.6",
"@types/async-retry": "^1.4.1",
Expand Down
4 changes: 3 additions & 1 deletion src/services/contract_wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ let contractWrappers: ContractWrappers;
export const getContractWrappers = async () => {
if (!contractWrappers) {
const web3Wrapper = await getWeb3Wrapper();
contractWrappers = new ContractWrappers(web3Wrapper.getProvider(), { networkId: NETWORK_ID });
contractWrappers = new ContractWrappers(web3Wrapper.getProvider(), {
networkId: NETWORK_ID,
});
}

return contractWrappers;
Expand Down
9 changes: 5 additions & 4 deletions src/services/web3_wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MetamaskSubprovider } from '0x.js';
import { Web3Wrapper } from '@0x/web3-wrapper';

import { sleep } from '../util/sleep';
Expand All @@ -18,16 +19,16 @@ export const initializeWeb3Wrapper = async (): Promise<Web3Wrapper | null> => {

if (ethereum) {
try {
web3Wrapper = new Web3Wrapper(ethereum);
web3Wrapper = new Web3Wrapper(new MetamaskSubprovider(ethereum));
// Request account access if needed
await ethereum.enable();

// Subscriptions register
ethereum.on('accountsChanged', async (accounts: []) => {
ethereum.on('accountsChanged', async (_accounts: []) => {
// Reload to avoid MetaMask bug: "MetaMask - RPC Error: Internal JSON-RPC"
location.reload();
});
ethereum.on('networkChanged', async (network: number) => {
ethereum.on('networkChanged', async (_network: number) => {
location.reload();
});

Expand All @@ -37,7 +38,7 @@ export const initializeWeb3Wrapper = async (): Promise<Web3Wrapper | null> => {
return null;
}
} else if (web3) {
web3Wrapper = new Web3Wrapper(web3.currentProvider);
web3Wrapper = new Web3Wrapper(new MetamaskSubprovider(web3.currentProvider));
return web3Wrapper;
} else {
// The user does not have metamask installed
Expand Down
5 changes: 2 additions & 3 deletions src/store/blockchain/actions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// tslint:disable:max-file-line-count
import { BigNumber, MetamaskSubprovider, signatureUtils } from '0x.js';
import { BigNumber, signatureUtils } from '0x.js';
import { createAction, createAsyncAction } from 'typesafe-actions';

import { COLLECTIBLE_ADDRESS, NETWORK_ID, START_BLOCK_LIMIT } from '../../common/constants';
Expand Down Expand Up @@ -483,8 +483,7 @@ export const createSignedCollectibleOrder: ThunkCreator = (
);
}

const provider = new MetamaskSubprovider(web3Wrapper.getProvider());
return signatureUtils.ecSignOrderAsync(provider, order, ethAccount);
return signatureUtils.ecSignOrderAsync(web3Wrapper.getProvider(), order, ethAccount);
} catch (error) {
throw new SignedOrderException(error.message);
}
Expand Down
9 changes: 6 additions & 3 deletions src/store/collectibles/actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MetamaskSubprovider, signatureUtils, SignedOrder } from '0x.js';
import { signatureUtils, SignedOrder } from '0x.js';
import { createAction, createAsyncAction } from 'typesafe-actions';

import { ZERO_ADDRESS } from '../../common/constants';
Expand Down Expand Up @@ -65,8 +65,11 @@ export const submitBuyCollectible: ThunkCreator<Promise<string>> = (order: Signe
takerAssetAmount: order.makerAssetAmount,
};

const provider = new MetamaskSubprovider(web3Wrapper.getProvider());
const buySignedOrder = await signatureUtils.ecSignOrderAsync(provider, buyOrder, ethAccount);
const buySignedOrder = await signatureUtils.ecSignOrderAsync(
web3Wrapper.getProvider(),
buyOrder,
ethAccount,
);
tx = await contractWrappers.dutchAuction.matchOrdersAsync(buySignedOrder, order, ethAccount, gasOptions);
} else {
tx = await contractWrappers.forwarder.marketBuyOrdersWithEthAsync(
Expand Down
36 changes: 18 additions & 18 deletions src/store/relayer/actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { BigNumber, SignedOrder } from '0x.js';
import { assetDataUtils, BigNumber, SignedOrder } from '0x.js';
import { AssetBuyer } from '@0x/asset-buyer';
import { createAction } from 'typesafe-actions';

import { ZERO_ADDRESS } from '../../common/constants';
import { NETWORK_ID, RELAYER_URL } from '../../common/constants';
import { INSUFFICIENT_ORDERS_TO_FILL_AMOUNT_ERR } from '../../exceptions/common';
import { InsufficientOrdersAmountException } from '../../exceptions/insufficient_orders_amount_exception';
import { RelayerException } from '../../exceptions/relayer_exception';
Expand Down Expand Up @@ -184,26 +185,25 @@ export const submitMarketOrder: ThunkCreator<Promise<{ txHash: string; amountInR
const quoteToken = getQuoteToken(state) as Token;
const contractWrappers = await getContractWrappers();

// Check if the order is fillable using the forwarder
const ethBalance = getEthBalance(state) as BigNumber;
const ethAmountRequired = amounts.reduce((total: BigNumber, currentValue: BigNumber) => {
return total.plus(currentValue);
}, new BigNumber(0));
const isEthBalanceEnough = ethBalance.isGreaterThan(ethAmountRequired);
const assetBuyer = AssetBuyer.getAssetBuyerForStandardRelayerAPIUrl(
contractWrappers.getProvider(),
RELAYER_URL,
{
networkId: NETWORK_ID,
},
);
const buyQuote = await assetBuyer.getBuyQuoteAsync(
assetDataUtils.encodeERC20AssetData(baseToken.address),
amount,
);
const ethBalance = getEthBalance(state);
const isEthBalanceEnough = ethBalance.isGreaterThan(buyQuote.worstCaseQuoteInfo.totalEthAmount);

const isMarketBuyForwarder = isBuy && isWeth(quoteToken.symbol) && isEthBalanceEnough;

let txHash;
if (isMarketBuyForwarder) {
txHash = await contractWrappers.forwarder.marketBuyOrdersWithEthAsync(
ordersToFill,
amount,
ethAccount,
ethAmountRequired,
[],
0,
ZERO_ADDRESS,
getTransactionOptions(gasPrice),
);
txHash = await assetBuyer.executeBuyQuoteAsync(buyQuote);
} else {
txHash = await contractWrappers.exchange.batchFillOrdersAsync(
ordersToFill,
Expand Down
5 changes: 2 additions & 3 deletions src/store/ui/actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigNumber, MetamaskSubprovider, signatureUtils } from '0x.js';
import { BigNumber, signatureUtils } from '0x.js';
import { createAction } from 'typesafe-actions';

import { COLLECTIBLE_ADDRESS } from '../../common/constants';
Expand Down Expand Up @@ -289,8 +289,7 @@ export const createSignedOrder: ThunkCreator = (amount: BigNumber, price: BigNum
side,
);

const provider = new MetamaskSubprovider(web3Wrapper.getProvider());
return signatureUtils.ecSignOrderAsync(provider, order, ethAccount);
return signatureUtils.ecSignOrderAsync(web3Wrapper.getProvider(), order, ethAccount);
} catch (error) {
throw new SignedOrderException(error.message);
}
Expand Down
Loading