Skip to content

Commit

Permalink
refactor: fixed and added tests for bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
capedcrusader21 committed Oct 4, 2024
1 parent d852964 commit 90ee70f
Show file tree
Hide file tree
Showing 5 changed files with 702 additions and 117 deletions.
163 changes: 140 additions & 23 deletions packages/marketplace/test/exchange/Bundle.behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function shouldMatchOrdersForBundle() {
ERC721Contract: Contract,
ERC1155Contract: Contract,
LandContract: Contract,
protocolFeeSecondary: number,
protocolFeePrimary: number,
defaultFeeReceiver: Signer,
maker: Signer,
taker: Signer,
Expand All @@ -84,22 +84,31 @@ export function shouldMatchOrdersForBundle() {
orderLeft: Order,
orderRight: Order,
makerSig: string,
takerSig: string;
takerSig: string,
TSB_SECONDARY_MARKET_SELLER_ROLE: string;

describe('Bundle x ERC20', function () {
beforeEach(async function () {
({
ExchangeContractAsUser,
ExchangeContractAsAdmin,
OrderValidatorAsAdmin,
ERC20Contract,
ERC721Contract,
ERC1155Contract,
protocolFeeSecondary,
protocolFeePrimary,
defaultFeeReceiver,
user1: maker,
user2: taker,
TSB_SECONDARY_MARKET_SELLER_ROLE,
} = await loadFixture(deployFixtures));

// grant tsb bundle seller role to seller
await ExchangeContractAsAdmin.grantRole(
TSB_SECONDARY_MARKET_SELLER_ROLE,
await maker.getAddress()
);

priceDistribution = {
erc721Prices: [[4000000000]],
erc1155Prices: [[6000000000]],
Expand Down Expand Up @@ -228,6 +237,107 @@ export function shouldMatchOrdersForBundle() {
).to.be.revertedWith('Bundle price mismatch');
});

it('should not execute match order between Bundle and Bundle', async function () {
// Set up ERC721 for maker
await ERC721Contract.mint(await maker.getAddress(), 10);
await ERC721Contract.connect(maker).approve(
await ExchangeContractAsUser.getAddress(),
1
);

// Set up ERC1155 for taker
await ERC1155Contract.mint(await taker.getAddress(), 10, 50);

await ERC1155Contract.connect(taker).setApprovalForAll(
await ExchangeContractAsUser.getAddress(),
true
);

// Construct makerAsset bundle
priceDistribution = {
erc721Prices: [[10000000000]],
erc1155Prices: [[]],
quadPrices: [],
};

bundledERC721 = [
{
erc721Address: ERC721Contract.target,
ids: [10],
},
];

bundledERC1155 = [];

const bundleDataLeft: BundleData = {
bundledERC721,
bundledERC1155,
quads,
priceDistribution,
};

makerAsset = await AssetBundle(bundleDataLeft, 1);

// Construct takerAsset bundle
priceDistribution = {
erc721Prices: [[]],
erc1155Prices: [[10000000000]],
quadPrices: [],
};

bundledERC721 = [];

bundledERC1155 = [
{
erc1155Address: ERC1155Contract.target,
ids: [10],
supplies: [10],
},
];

const bundleDataRight: BundleData = {
bundledERC721,
bundledERC1155,
quads,
priceDistribution,
};

takerAsset = await AssetBundle(bundleDataRight, 1);

orderLeft = await OrderDefault(
maker,
makerAsset, // Bundle
ZeroAddress,
takerAsset, // Bundle
1,
0,
0
);
orderRight = await OrderDefault(
taker,
takerAsset, // Bundle
ZeroAddress,
makerAsset, // Bundle
1,
0,
0
);

makerSig = await signOrder(orderLeft, maker, OrderValidatorAsAdmin);
takerSig = await signOrder(orderRight, taker, OrderValidatorAsAdmin);

await expect(
ExchangeContractAsUser.matchOrders([
{
orderLeft, // passing Bundle as left order
signatureLeft: makerSig,
orderRight, // passing Bundle as right order
signatureRight: takerSig,
},
])
).to.be.revertedWith('exchange not allowed');
});

it('should execute a complete match order between ERC20 tokens and Bundle containing ERC20, ERC721 and ERC1155', async function () {
orderLeft = await OrderDefault(
maker, // ERC20
Expand Down Expand Up @@ -288,8 +398,8 @@ export function shouldMatchOrdersForBundle() {
).to.be.equal(1);

const protocolFees = {
erc721ProtocolFees: [[protocolFeeSecondary]],
erc1155ProtocolFees: [[protocolFeeSecondary]],
erc721ProtocolFees: [[protocolFeePrimary]],
erc1155ProtocolFees: [[protocolFeePrimary]],
quadProtocolFees: [],
};

Expand All @@ -310,9 +420,9 @@ export function shouldMatchOrdersForBundle() {
expect(
await ERC20Contract.balanceOf(defaultFeeReceiver.getAddress())
).to.be.equal(
(Number(protocolFeeSecondary) *
(Number(protocolFeePrimary) *
Number(priceDistribution.erc721Prices[0][0]) +
Number(protocolFeeSecondary) *
Number(protocolFeePrimary) *
Number(priceDistribution.erc1155Prices[0][0])) /
10000
);
Expand Down Expand Up @@ -477,7 +587,7 @@ export function shouldMatchOrdersForBundle() {

const protocolFees = {
erc721ProtocolFees: [[]],
erc1155ProtocolFees: [[protocolFeeSecondary]],
erc1155ProtocolFees: [[protocolFeePrimary]],
quadProtocolFees: [],
};

Expand All @@ -495,7 +605,7 @@ export function shouldMatchOrdersForBundle() {
expect(
await ERC20Contract.balanceOf(defaultFeeReceiver.getAddress())
).to.be.equal(
(Number(protocolFeeSecondary) *
(Number(protocolFeePrimary) *
Number(priceDistribution.erc1155Prices[0][0])) /
10000
);
Expand Down Expand Up @@ -684,7 +794,7 @@ export function shouldMatchOrdersForBundle() {

const protocolFees = {
erc721ProtocolFees: [[]],
erc1155ProtocolFees: [[protocolFeeSecondary]],
erc1155ProtocolFees: [[protocolFeePrimary]],
quadProtocolFees: [],
};

Expand All @@ -701,7 +811,7 @@ export function shouldMatchOrdersForBundle() {
await ERC20Contract.balanceOf(defaultFeeReceiver.getAddress())
).to.be.equal(
(2 *
(Number(protocolFeeSecondary) *
(Number(protocolFeePrimary) *
Number(priceDistribution.erc1155Prices[0][0]))) /
10000
);
Expand Down Expand Up @@ -798,7 +908,7 @@ export function shouldMatchOrdersForBundle() {

const protocolFees = {
erc721ProtocolFees: [[]],
erc1155ProtocolFees: [[protocolFeeSecondary]],
erc1155ProtocolFees: [[protocolFeePrimary]],
quadProtocolFees: [],
};

Expand All @@ -815,7 +925,7 @@ export function shouldMatchOrdersForBundle() {
expect(
await ERC20Contract.balanceOf(defaultFeeReceiver.getAddress())
).to.be.equal(
(Number(protocolFeeSecondary) *
(Number(protocolFeePrimary) *
Number(priceDistribution.erc1155Prices[0][0])) /
10000
); // 1 * partial fills => 1 * fee taken
Expand Down Expand Up @@ -869,7 +979,7 @@ export function shouldMatchOrdersForBundle() {
expect(
await ERC20Contract.balanceOf(defaultFeeReceiver.getAddress())
).to.be.equal(
(Number(protocolFeeSecondary) *
(Number(protocolFeePrimary) *
Number(priceDistribution.erc1155Prices[0][0]) *
2) /
10000
Expand All @@ -893,15 +1003,22 @@ export function shouldMatchOrdersForBundle() {
ERC20Contract,
ERC721Contract,
ERC1155Contract,
protocolFeeSecondary,
protocolFeePrimary,
defaultFeeReceiver,
user1: maker,
user2: taker,
LandContract,
LandAsAdmin,
landAdmin,
TSB_SECONDARY_MARKET_SELLER_ROLE,
} = await loadFixture(deployFixtures));

// grant tsb bundle seller role to seller
await ExchangeContractAsAdmin.grantRole(
TSB_SECONDARY_MARKET_SELLER_ROLE,
await maker.getAddress()
);

priceDistribution = {
erc721Prices: [[4000000000]], // price distribution without ERC721
erc1155Prices: [[5000000000]],
Expand Down Expand Up @@ -971,7 +1088,7 @@ export function shouldMatchOrdersForBundle() {
data: '0x',
};

// Create bundle for passing as right order
// Create bundle for passing as left order
bundleData = {
bundledERC721,
bundledERC1155,
Expand Down Expand Up @@ -1053,9 +1170,9 @@ export function shouldMatchOrdersForBundle() {
).to.be.equal(1);

const protocolFees = {
erc721ProtocolFees: [[protocolFeeSecondary]],
erc1155ProtocolFees: [[protocolFeeSecondary]],
quadProtocolFees: [protocolFeeSecondary, protocolFeeSecondary],
erc721ProtocolFees: [[protocolFeePrimary]],
erc1155ProtocolFees: [[protocolFeePrimary]],
quadProtocolFees: [protocolFeePrimary, protocolFeePrimary],
};

const expectedFinalReturn = calculateFinalPrice(
Expand All @@ -1075,13 +1192,13 @@ export function shouldMatchOrdersForBundle() {
expect(
await ERC20Contract.balanceOf(await defaultFeeReceiver.getAddress())
).to.be.equal(
(Number(protocolFeeSecondary) *
(Number(protocolFeePrimary) *
Number(priceDistribution.erc721Prices[0][0]) +
Number(protocolFeeSecondary) *
Number(protocolFeePrimary) *
Number(priceDistribution.erc1155Prices[0][0]) +
Number(protocolFeeSecondary) *
Number(protocolFeePrimary) *
Number(priceDistribution.quadPrices[0]) +
Number(protocolFeeSecondary) *
Number(protocolFeePrimary) *
Number(priceDistribution.quadPrices[1])) /
10000
);
Expand Down
Loading

1 comment on commit 90ee70f

@github-actions
Copy link

Choose a reason for hiding this comment

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

Coverage for this commit

98.27%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
packages/marketplace/contracts
   Exchange.sol94.67%93.33%94.74%96.15%131, 206, 77
   ExchangeCore.sol97.75%93.75%100%100%200, 82
   OrderValidator.sol98.48%96.15%100%100%37
   RoyaltiesRegistry.sol96.35%88.89%100%98.80%196, 218–219, 265, 66
   TransferManager.sol96.17%88.37%100%99.42%164, 226, 248, 319, 377, 415, 566, 571, 582, 604, 94
   Whitelist.sol76.19%60%85.71%82.76%106, 110–111, 125, 128, 144–145, 56, 68, 68–69, 73, 78
packages/marketplace/contracts/interfaces
   IOrderValidator.sol100%100%100%100%
   IRoyaltiesProvider.sol100%100%100%100%
   ITransferManager.sol100%100%100%100%
   IWhitelist.sol100%100%100%100%
packages/marketplace/contracts/libraries
   LibAsset.sol97.47%92.31%100%100%86, 88
   LibMath.sol100%100%100%100%
   LibOrder.sol100%100%100%100%

Please sign in to comment.