Skip to content

Commit

Permalink
add test to get back to 100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
0age committed Mar 31, 2022
1 parent 8b71d27 commit d0704a2
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
14 changes: 14 additions & 0 deletions contracts/test/EIP1271Wallet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ contract EIP1271Wallet {

bool public showRevertMessage;

mapping (bytes32 => bool) public digestApproved;

constructor(address _owner) {
owner = _owner;
showRevertMessage = true;
Expand All @@ -25,6 +27,10 @@ contract EIP1271Wallet {
showRevertMessage = showMessage;
}

function registerDigest(bytes32 digest, bool approved) external {
digestApproved[digest] = approved;
}

function approveERC20(ERC20ApprovalInterface token, address operator, uint256 amount) external {
if (msg.sender != owner) {
revert ("Only owner");
Expand All @@ -45,6 +51,14 @@ contract EIP1271Wallet {
bytes32 digest,
bytes memory signature
) external view returns (bytes4) {
if (digestApproved[digest]) {
return _EIP_1271_MAGIC_VALUE;
}

if (signature.length != 65) {
revert();
}

bytes32 r;
bytes32 s;
uint8 v;
Expand Down
117 changes: 117 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2634,6 +2634,123 @@ describe(`Consideration (version: ${VERSION}) — initial test suite`, function
});
});
});
it("ERC721 <=> ERC20 (basic, EIP-1271 signature w/ non-standard length)", async () => {
// Seller mints nft to contract
const nftId = ethers.BigNumber.from(randomHex());
await testERC721.mint(sellerContract.address, nftId);

// Seller approves marketplace contract to transfer NFT
await whileImpersonating(seller.address, provider, async () => {
await expect(sellerContract.connect(seller).approveNFT(testERC721.address, marketplaceContract.address))
.to.emit(testERC721, "ApprovalForAll")
.withArgs(sellerContract.address, marketplaceContract.address, true);
});

// Buyer mints ERC20
const tokenAmount = ethers.BigNumber.from(randomLarge()).add(100);
await testERC20.mint(buyer.address, tokenAmount);

// Buyer approves marketplace contract to transfer tokens
await whileImpersonating(buyer.address, provider, async () => {
await expect(testERC20.connect(buyer).approve(marketplaceContract.address, tokenAmount))
.to.emit(testERC20, "Approval")
.withArgs(buyer.address, marketplaceContract.address, tokenAmount);
});

const offer = [
{
itemType: 2, // ERC721
token: testERC721.address,
identifierOrCriteria: nftId,
startAmount: ethers.BigNumber.from(1),
endAmount: ethers.BigNumber.from(1),
},
];

const consideration = [
{
itemType: 1, // ERC20
token: testERC20.address,
identifierOrCriteria: 0, // ignored for ERC20
startAmount: tokenAmount.sub(100),
endAmount: tokenAmount.sub(100),
recipient: sellerContract.address,
},
{
itemType: 1, // ERC20
token: testERC20.address,
identifierOrCriteria: 0, // ignored for ERC20
startAmount: ethers.BigNumber.from(50),
endAmount: ethers.BigNumber.from(50),
recipient: zone.address,
},
{
itemType: 1, // ERC20
token: testERC20.address,
identifierOrCriteria: 0, // ignored for ERC20
startAmount: ethers.BigNumber.from(50),
endAmount: ethers.BigNumber.from(50),
recipient: owner.address,
},
];

const { order, orderHash, value } = await createOrder(
sellerContract,
zone,
offer,
consideration,
0, // FULL_OPEN
[],
null,
seller
);

const basicOrderParameters = {
offerer: order.parameters.offerer,
zone: order.parameters.zone,
orderType: order.parameters.orderType,
token: order.parameters.offer[0].token,
identifier: order.parameters.offer[0].identifierOrCriteria,
startTime: order.parameters.startTime,
endTime: order.parameters.endTime,
salt: order.parameters.salt,
signature: "0x", // EMPTY
additionalRecipients: [
{
amount: ethers.BigNumber.from(50),
recipient: zone.address,
},
{
amount: ethers.BigNumber.from(50),
recipient: owner.address,
}
],
};

// Fails before seller contract approves the digest
await whileImpersonating(buyer.address, provider, async () => {
await expect(marketplaceContract.connect(buyer).fulfillBasicERC20ForERC721Order(testERC20.address, tokenAmount.sub(100), basicOrderParameters)).to.be.reverted;
});

// Compute the digest based on the order hash
const domainSeparator = await marketplaceContract.DOMAIN_SEPARATOR();
const digest = ethers.utils.keccak256(`0x1901${domainSeparator.slice(2)}${orderHash.slice(2)}`);

// Seller approves the digest
await whileImpersonating(seller.address, provider, async () => {
await sellerContract.connect(seller).registerDigest(digest, true);
});

// Now it succeeds
await whileImpersonating(buyer.address, provider, async () => {
await withBalanceChecks([order], 0, null, async () => {
const tx = await marketplaceContract.connect(buyer).fulfillBasicERC20ForERC721Order(testERC20.address, tokenAmount.sub(100), basicOrderParameters);
const receipt = await tx.wait();
await checkExpectedEvents(receipt, [{order, orderHash, fulfiller: buyer.address}]);
return receipt;
});
});
});
it("ERC721 <=> ERC20 (match)", async () => {
// Seller mints nft
const nftId = ethers.BigNumber.from(randomHex());
Expand Down

0 comments on commit d0704a2

Please sign in to comment.