Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batch buy #18

Merged
merged 11 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
65 changes: 51 additions & 14 deletions contracts/JoepegExchange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ contract JoepegExchange is
OrderTypes.TakerOrder calldata takerBid,
OrderTypes.MakerOrder calldata makerAsk
) external payable override nonReentrant {
// Transfer WAVAX if needed
_transferWAVAXIfNeeded(takerBid.price);
// Wrap AVAX sent to this contract
IWAVAX(WAVAX).deposit{value: msg.value}();
// Match orders
_matchAskWithTakerBidUsingAVAXAndWAVAX(takerBid, makerAsk);
}

function _matchAskWithTakerBidUsingAVAXAndWAVAX(
Max-3-7 marked this conversation as resolved.
Show resolved Hide resolved
OrderTypes.TakerOrder calldata takerBid,
OrderTypes.MakerOrder calldata makerAsk
) internal {
require(
(makerAsk.isOrderAsk) && (!takerBid.isOrderAsk),
"Order: Wrong sides"
Expand All @@ -199,20 +211,6 @@ contract JoepegExchange is
bytes32 askHash = makerAsk.hash();
_validateOrder(makerAsk, askHash);

// If not enough AVAX to cover the price, use WAVAX
if (takerBid.price > msg.value) {
IERC20(WAVAX).safeTransferFrom(
msg.sender,
address(this),
(takerBid.price - msg.value)
);
} else {
require(takerBid.price == msg.value, "Order: Msg.value too high");
}

// Wrap AVAX sent to this contract
IWAVAX(WAVAX).deposit{value: msg.value}();

// Retrieve execution parameters
(
bool isExecutionValid,
Expand Down Expand Up @@ -262,6 +260,19 @@ contract JoepegExchange is
);
}

function _transferWAVAXIfNeeded(uint256 cost) internal {
Max-3-7 marked this conversation as resolved.
Show resolved Hide resolved
// If not enough AVAX to cover the cost, use WAVAX
if (cost > msg.value) {
IERC20(WAVAX).safeTransferFrom(
msg.sender,
address(this),
(cost - msg.value)
);
} else {
require(cost == msg.value, "Order: Msg.value too high");
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the transfer WAVAX logic outside of _matchAskWithTakerBidUsingAVAXAndWAVAX because of require(takerBid.price == msg.value, "Order: Msg.value too high");

When you use batch buy with AVAX, you send more AVAX than the price of a single order so takerBid.price < msg.value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also moved IWAVAX(WAVAX).deposit{value: msg.value}(); for the same reason: you can only deposit msg.value once so it reverted in the batch buy for loop


/**
* @notice Match a takerBid with a matchAsk
* @param takerBid taker bid order
Expand Down Expand Up @@ -779,4 +790,30 @@ contract JoepegExchange is
"Strategy: Not whitelisted"
);
}

function batchBuyWithAVAXAndWAVAX(Trade[] calldata trades)
Max-3-7 marked this conversation as resolved.
Show resolved Hide resolved
Max-3-7 marked this conversation as resolved.
Show resolved Hide resolved
external
payable
nonReentrant
{
// Calculate the total cost of all orders
uint256 totalCost = 0;
Max-3-7 marked this conversation as resolved.
Show resolved Hide resolved
for (uint256 i = 0; i < trades.length; i++) {
Max-3-7 marked this conversation as resolved.
Show resolved Hide resolved
totalCost = totalCost + trades[i].takerBid.price;
Max-3-7 marked this conversation as resolved.
Show resolved Hide resolved
}

// Transfer WAVAX if needed
_transferWAVAXIfNeeded(totalCost);

// Wrap AVAX sent to this contract
IWAVAX(WAVAX).deposit{value: msg.value}();

// Match orders
for (uint256 i = 0; i < trades.length; i++) {
Max-3-7 marked this conversation as resolved.
Show resolved Hide resolved
_matchAskWithTakerBidUsingAVAXAndWAVAX(
trades[i].takerBid,
trades[i].makerAsk
);
}
}
}
11 changes: 10 additions & 1 deletion contracts/interfaces/IJoepegExchange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ import {IExecutionManager} from "./IExecutionManager.sol";

import {OrderTypes} from "../libraries/OrderTypes.sol";

interface IJoepegExchange {
interface IJoepegBuyBatcher {
struct Trade {
OrderTypes.TakerOrder takerBid;
OrderTypes.MakerOrder makerAsk;
}

function batchBuyWithAVAXAndWAVAX(Trade[] calldata trades) external payable;
}

interface IJoepegExchange is IJoepegBuyBatcher {
function matchAskWithTakerBidUsingAVAXAndWAVAX(
OrderTypes.TakerOrder calldata takerBid,
OrderTypes.MakerOrder calldata makerAsk
Expand Down
2 changes: 1 addition & 1 deletion deploy/CurrencyManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = async function ({
wavaxAddress = ethers.utils.getAddress(
"0xd00ae08403B9bbb9124bB305C09058E32C39A48c"
);
proxyOwner = deployer;
proxyOwner = "0xdB40a7b71642FE24CC546bdF4749Aa3c0B042f78";
} else {
throw new Error("Failed to find WAVAX address");
}
Expand Down
2 changes: 1 addition & 1 deletion deploy/ExecutionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = async function ({ getNamedAccounts, deployments, getChainId })
let proxyContract, proxyOwner;

if (chainId == 4 || chainId == 43113) {
proxyOwner = deployer;
proxyOwner = "0xdB40a7b71642FE24CC546bdF4749Aa3c0B042f78";
} else if (chainId == 43114 || chainId == 31337) {
// multisig
proxyOwner = "0x2fbB61a10B96254900C03F1644E9e1d2f5E76DD2";
Expand Down
2 changes: 1 addition & 1 deletion deploy/JoepegExchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = async function ({
wavaxAddress = ethers.utils.getAddress(
"0xd00ae08403B9bbb9124bB305C09058E32C39A48c"
);
proxyOwner = deployer;
proxyOwner = "0xdB40a7b71642FE24CC546bdF4749Aa3c0B042f78";
} else {
throw new Error("Failed to find WAVAX address");
}
Expand Down
2 changes: 1 addition & 1 deletion deploy/ProtocolFeeManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = async function ({
let proxyContract, proxyOwner;

if (chainId == 4 || chainId == 43113) {
proxyOwner = deployer;
proxyOwner = "0xdB40a7b71642FE24CC546bdF4749Aa3c0B042f78";
} else if (chainId == 43114 || chainId == 31337) {
// multisig
proxyOwner = "0x2fbB61a10B96254900C03F1644E9e1d2f5E76DD2";
Expand Down
2 changes: 1 addition & 1 deletion deploy/RoyaltyFeeManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = async function ({ getNamedAccounts, deployments, getChainId })
const chainId = await getChainId();

if (chainId == 4 || chainId == 43113) {
proxyOwner = deployer;
proxyOwner = "0xdB40a7b71642FE24CC546bdF4749Aa3c0B042f78";
} else if (chainId == 43114 || chainId == 31337) {
// multisig
proxyOwner = "0x2fbB61a10B96254900C03F1644E9e1d2f5E76DD2";
Expand Down
2 changes: 1 addition & 1 deletion deploy/RoyaltyFeeRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = async function ({ getNamedAccounts, deployments, getChainId })
const chainId = await getChainId();

if (chainId == 4 || chainId == 43113) {
proxyOwner = deployer;
proxyOwner = "0xdB40a7b71642FE24CC546bdF4749Aa3c0B042f78";
} else if (chainId == 43114 || chainId == 31337) {
// multisig
proxyOwner = "0x2fbB61a10B96254900C03F1644E9e1d2f5E76DD2";
Expand Down
2 changes: 1 addition & 1 deletion deploy/RoyaltyFeeSetter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = async function ({ getNamedAccounts, deployments, getChainId })
const chainId = await getChainId();

if (chainId == 4 || chainId == 43113) {
proxyOwner = deployer;
proxyOwner = "0xdB40a7b71642FE24CC546bdF4749Aa3c0B042f78";
} else if (chainId == 43114 || chainId == 31337) {
// multisig
proxyOwner = "0x2fbB61a10B96254900C03F1644E9e1d2f5E76DD2";
Expand Down
2 changes: 1 addition & 1 deletion deploy/TransferManagerERC1155.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = async function ({ getNamedAccounts, deployments, getChainId })
const chainId = getChainId();

if (chainId == 4 || chainId == 43113) {
proxyOwner = deployer;
proxyOwner = "0xdB40a7b71642FE24CC546bdF4749Aa3c0B042f78";
} else if (chainId == 43114 || chainId == 31337) {
// multisig
proxyOwner = "0x2fbB61a10B96254900C03F1644E9e1d2f5E76DD2";
Expand Down
2 changes: 1 addition & 1 deletion deploy/TransferManagerERC721.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = async function ({ getNamedAccounts, deployments, getChainId })
const chainId = getChainId();

if (chainId == 4 || chainId == 43113) {
proxyOwner = deployer;
proxyOwner = "0xdB40a7b71642FE24CC546bdF4749Aa3c0B042f78";
} else if (chainId == 43114 || chainId == 31337) {
// multisig
proxyOwner = "0x2fbB61a10B96254900C03F1644E9e1d2f5E76DD2";
Expand Down
2 changes: 1 addition & 1 deletion deploy/TransferManagerNonCompliantERC721.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = async function ({ getNamedAccounts, deployments, getChainId })
const chainId = getChainId();

if (chainId == 4 || chainId == 43113) {
proxyOwner = deployer;
proxyOwner = "0xdB40a7b71642FE24CC546bdF4749Aa3c0B042f78";
} else if (chainId == 43114 || chainId == 31337) {
// multisig
proxyOwner = "0x2fbB61a10B96254900C03F1644E9e1d2f5E76DD2";
Expand Down
2 changes: 1 addition & 1 deletion deploy/TransferSelectorNFT.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = async function ({ getNamedAccounts, deployments, getChainId })
const chainId = await getChainId();

if (chainId == 4 || chainId == 43113) {
proxyOwner = deployer;
proxyOwner = "0xdB40a7b71642FE24CC546bdF4749Aa3c0B042f78";
} else if (chainId == 43114 || chainId == 31337) {
// multisig
proxyOwner = "0x2fbB61a10B96254900C03F1644E9e1d2f5E76DD2";
Expand Down
8 changes: 2 additions & 6 deletions deployments/fuji/CurrencyManager.json

Large diffs are not rendered by default.

44 changes: 22 additions & 22 deletions deployments/fuji/CurrencyManager_Implementation.json

Large diffs are not rendered by default.

8 changes: 2 additions & 6 deletions deployments/fuji/ExecutionManager.json

Large diffs are not rendered by default.

40 changes: 20 additions & 20 deletions deployments/fuji/ExecutionManager_Implementation.json

Large diffs are not rendered by default.

Loading