Skip to content

Commit

Permalink
fix: add logs and new zap address
Browse files Browse the repository at this point in the history
  • Loading branch information
quagliero committed Jan 10, 2023
1 parent 644b0a4 commit c518143
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 85 deletions.
2 changes: 1 addition & 1 deletion packages/constants/src/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const NFTX_MARKETPLACE_ZAP = {

export const NFTX_MARKETPLACE_0X_ZAP = {
[Network.Mainnet]: '0xbbc53022Af15Bb973AD906577c84784c47C14371',
[Network.Goerli]: '0x14141d76D162cB20CdA7C42254C0349294e501Db',
[Network.Goerli]: '0x177246Adb119ed83f982d1e3c4859F354578D5eF',
};

export const NFTX_VAULT_FACTORY = {
Expand Down
29 changes: 15 additions & 14 deletions packages/trade/src/trade/buy/buy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ const buyErc721 = async ({
const ids = getExactTokenIds(tokenIds);
const targetBuys = getTotalTokenIds(tokenIds);
const amount = targetBuys + randomBuys;
const address = getChainConstant(NFTX_MARKETPLACE_ZAP, network);
const contract = getContract({
network,
signer,
abi: NFTXMarketplaceZap,
address: getChainConstant(NFTX_MARKETPLACE_ZAP, network),
address,
});
const path = [getChainConstant(WETH_TOKEN, network), vaultAddress];
const args = [vaultId, amount, ids, path, userAddress];
Expand All @@ -84,16 +85,16 @@ const buyErc721 = async ({
});

const gasLimit = increaseGasLimit({ estimate: gasEstimate, amount: 7 });
const overrides = omitNil({
value: maxPrice?.toString(),
gasLimit,
maxFeePerGas,
maxPriorityFeePerGas,
});

console.debug(address, 'buyAndRedeem', ...args, overrides);

return contract.buyAndRedeem(
...args,
omitNil({
value: maxPrice?.toString(),
gasLimit,
maxFeePerGas,
maxPriorityFeePerGas,
})
);
return contract.buyAndRedeem(...args, overrides);
};

const buy0xErc721 = async ({
Expand All @@ -113,10 +114,11 @@ const buy0xErc721 = async ({
randomBuys: number;
slippage: number;
}) => {
const address = getChainConstant(NFTX_MARKETPLACE_0X_ZAP, network);
const { vaultId, id: vaultAddress } = vault;
const contract = getContract({
abi: NftxMarketplace0xZap,
address: getChainConstant(NFTX_MARKETPLACE_0X_ZAP, network),
address,
network,
signer,
});
Expand Down Expand Up @@ -162,11 +164,10 @@ const buy0xErc721 = async ({
maxPriorityFeePerGas,
});

console.debug('buyAndRedeem', ...args, overrides);
console.debug(address, 'buyAndRedeem', ...args, overrides);

// try {
const result = await contract.buyAndRedeem(...args, overrides);
return result;
return contract.buyAndRedeem(...args, overrides);
// } catch (e) {
// if (e?.code === 4001) {
// throw e;
Expand Down
77 changes: 47 additions & 30 deletions packages/trade/src/trade/sell/sell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const sell0xErc721 = async ({
const fee = calculateSellFee({ vault, amount });
const sellAmount = parseEther(`${amount}`).sub(fee);
const ids = getUniqueTokenIds(tokenIds);
const address = getChainConstant(NFTX_MARKETPLACE_0X_ZAP, network);

const { data } = await fetch0xQuote({
network,
Expand All @@ -61,14 +62,27 @@ const sell0xErc721 = async ({
slippagePercentage: slippage,
type: 'quote',
});

const args = [vaultId, ids, data, userAddress];
const contract = getContract({
network,
signer,
address: getChainConstant(NFTX_MARKETPLACE_0X_ZAP, network),
address,
abi: nftxMarketplace0xZap,
});

return contract.mintAndSell721(vaultId, ids, data, userAddress);
const { gasEstimate, maxFeePerGas, maxPriorityFeePerGas } =
await estimateGasAndFees({
contract,
method: 'mintAndSell721',
args,
});
const gasLimit = increaseGasLimit({ estimate: gasEstimate, amount: 3 });

const overrides = omitNil({ gasLimit, maxFeePerGas, maxPriorityFeePerGas });
console.debug(address, 'mintAndSell721', ...args, overrides);

return contract.mintAndSell721(...args, overrides);
};

const sellErc721 = async ({
Expand All @@ -89,11 +103,12 @@ const sellErc721 = async ({
signer: Signer;
slippage: number;
}) => {
const address = getChainConstant(NFTX_MARKETPLACE_ZAP, network);
const contract = getContract({
network,
signer,
abi: nftxMarketplaceZap,
address: getChainConstant(NFTX_MARKETPLACE_ZAP, network),
address,
});
const ids = getUniqueTokenIds(tokenIds);
let { price: minPrice } = await fetchVaultSellPrice({
Expand All @@ -116,23 +131,11 @@ const sellErc721 = async ({
args,
});
const gasLimit = increaseGasLimit({ estimate: gasEstimate, amount: 7 });
const overrides = omitNil({ gasLimit, maxFeePerGas, maxPriorityFeePerGas });

try {
// Attempt an EIP1559 transaction
return contract.mintAndSell721(
...args,
omitNil({ gasLimit, maxFeePerGas, maxPriorityFeePerGas })
);
} catch {
// Fallback to a legacy tx
return contract.mintAndSell721(
vaultId,
ids,
minPrice,
userAddress,
omitNil({ gasLimit })
);
}
console.debug(address, 'mintAndSell721', ...args, overrides);

return contract.mintAndSell721(...args, overrides);
};

const sellErc1155 = async ({
Expand All @@ -153,11 +156,12 @@ const sellErc1155 = async ({
network: number;
signer: Signer;
}) => {
const address = getChainConstant(NFTX_MARKETPLACE_ZAP, network);
const contract = getContract({
network,
signer,
abi: nftxMarketplaceZap,
address: getChainConstant(NFTX_MARKETPLACE_ZAP, network),
address,
});
const ids = getUniqueTokenIds(tokenIds);
const amounts = getTokenIdAmounts(tokenIds);
Expand All @@ -177,15 +181,15 @@ const sellErc1155 = async ({
const { gasEstimate, maxFeePerGas, maxPriorityFeePerGas } =
await estimateGasAndFees({ contract, method: 'mintAndSell1155', args });
const gasLimit = increaseGasLimit({ estimate: gasEstimate, amount: 7 });
const overrides = omitNil({
gasLimit,
maxFeePerGas,
maxPriorityFeePerGas,
});

console.debug(address, 'mintAndSell1155', ...args, overrides);

return contract.mintAndSell1155(
...args,
omitNil({
gasLimit,
maxFeePerGas,
maxPriorityFeePerGas,
})
);
return contract.mintAndSell1155(...args, overrides);
};

const sell0xErc1155 = async ({
Expand All @@ -210,6 +214,7 @@ const sell0xErc1155 = async ({
const amount = getTotalTokenIds(tokenIds);
const fee = calculateSellFee({ vault, amount });
const sellAmount = parseEther(`${amount}`).sub(fee);
const address = getChainConstant(NFTX_MARKETPLACE_0X_ZAP, network);

const { data } = await fetch0xQuote({
network,
Expand All @@ -223,11 +228,23 @@ const sell0xErc1155 = async ({
const contract = getContract({
network,
signer,
address: getChainConstant(NFTX_MARKETPLACE_0X_ZAP, network),
address,
abi: nftxMarketplace0xZap,
});

return contract.mintAndSell1155(vaultId, ids, amounts, data, userAddress);
const args = [vaultId, ids, amounts, data, userAddress];

const { gasEstimate, maxFeePerGas, maxPriorityFeePerGas } =
await estimateGasAndFees({ contract, method: 'mintAndSell1155', args });
const gasLimit = increaseGasLimit({ estimate: gasEstimate, amount: 3 });
const overrides = omitNil({
gasLimit,
maxFeePerGas,
maxPriorityFeePerGas,
});

console.debug(address, 'mintAndSell1155', args, overrides);
return contract.mintAndSell1155(...args, overrides);
};

const matrix = {
Expand Down
Loading

0 comments on commit c518143

Please sign in to comment.