Skip to content

Commit 0687866

Browse files
committed
test: buyToken fail cases
1 parent c7fe002 commit 0687866

File tree

2 files changed

+50
-34
lines changed

2 files changed

+50
-34
lines changed

hardhat/contracts/LarsKristoHellheads.sol

+11-9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ contract LarsKristoHellheads is ERC721Royalty {
1717
uint256 amount
1818
);
1919

20+
event SetTokenForSale(address indexed owner, uint256 indexed tokenId, uint256 price);
21+
2022
address public author = 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266; // larskristo.eth
2123
address public operator = 0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC; // svpervnder.eth
2224

@@ -318,21 +320,13 @@ contract LarsKristoHellheads is ERC721Royalty {
318320
* @param price The price at which to sell the token.
319321
*/
320322
function setTokenForSale(uint256 tokenId, uint256 price) public {
321-
_requireTokenPriceSet(tokenId);
322-
323323
address owner = ownerOf(tokenId);
324324

325325
_checkAuthorized(owner, _msgSender(), tokenId);
326326

327327
_tokenPrices[tokenId] = price;
328-
}
329328

330-
/**
331-
* @dev Returns the base URI for token metadata.
332-
* @return The base URI string.
333-
*/
334-
function _baseURI() internal view virtual override returns (string memory) {
335-
return "https://blockchainassetregistry.infura-ipfs.io/ipfs/";
329+
emit SetTokenForSale(owner, tokenId, price);
336330
}
337331

338332
/**
@@ -353,6 +347,14 @@ contract LarsKristoHellheads is ERC721Royalty {
353347
return bytes(baseURI).length > 0 ? string.concat(baseURI, tokenURIhash) : "";
354348
}
355349

350+
/**
351+
* @dev Returns the base URI for token metadata.
352+
* @return The base URI string.
353+
*/
354+
function _baseURI() internal view virtual override returns (string memory) {
355+
return "https://blockchainassetregistry.infura-ipfs.io/ipfs/";
356+
}
357+
356358
/**
357359
* @dev Checks if the caller is the owner of the specified token.
358360
* @param tokenId The ID of the token to check ownership for.

hardhat/test/LarsKristoHellheads.ts

+39-25
Original file line numberDiff line numberDiff line change
@@ -134,33 +134,33 @@ describe("Lease", function () {
134134
});
135135

136136
it("buyToken", async function () {
137-
const [author, , operator, buyer] = await ethers.getSigners();
137+
const [author, someoneElse, operator, buyer] = await ethers.getSigners();
138138

139-
let authorBalance = await ethers.provider.getBalance(author.address);
140-
let operatorBalance = await ethers.provider.getBalance(operator.address);
141-
let buyerBalance = await ethers.provider.getBalance(buyer.address);
139+
// let authorBalance = await ethers.provider.getBalance(author.address);
140+
// let operatorBalance = await ethers.provider.getBalance(operator.address);
141+
// let buyerBalance = await ethers.provider.getBalance(buyer.address);
142142

143-
console.log({
144-
authorBalance: ethers.formatEther(authorBalance),
145-
operatorBalance: ethers.formatEther(operatorBalance),
146-
buyerBalance: ethers.formatEther(buyerBalance),
147-
});
143+
// console.log({
144+
// authorBalance: ethers.formatEther(authorBalance),
145+
// operatorBalance: ethers.formatEther(operatorBalance),
146+
// buyerBalance: ethers.formatEther(buyerBalance),
147+
// });
148148

149149
const ERC721 = await createERC721Contract();
150150

151151
await ERC721.connect(author).setApprovalForAll(operator.address, true);
152152

153153
await ERC721.connect(operator).setTokenForSale(0, BigInt(ethers.parseEther("1")));
154154

155-
authorBalance = await ethers.provider.getBalance(author.address);
156-
operatorBalance = await ethers.provider.getBalance(operator.address);
157-
buyerBalance = await ethers.provider.getBalance(buyer.address);
155+
// authorBalance = await ethers.provider.getBalance(author.address);
156+
// operatorBalance = await ethers.provider.getBalance(operator.address);
157+
// buyerBalance = await ethers.provider.getBalance(buyer.address);
158158

159-
console.log({
160-
authorBalance: ethers.formatEther(authorBalance),
161-
operatorBalance: ethers.formatEther(operatorBalance),
162-
buyerBalance: ethers.formatEther(buyerBalance),
163-
});
159+
// console.log({
160+
// authorBalance: ethers.formatEther(authorBalance),
161+
// operatorBalance: ethers.formatEther(operatorBalance),
162+
// buyerBalance: ethers.formatEther(buyerBalance),
163+
// });
164164

165165
ERC721.on("Transfer", (from, to, tokenId) => {
166166
// console.log({ from, to, tokenId });
@@ -187,14 +187,28 @@ describe("Lease", function () {
187187

188188
expect(ownerOf0).to.equal(buyer.address);
189189

190-
authorBalance = await ethers.provider.getBalance(author.address);
191-
operatorBalance = await ethers.provider.getBalance(operator.address);
192-
buyerBalance = await ethers.provider.getBalance(buyer.address);
190+
// authorBalance = await ethers.provider.getBalance(author.address);
191+
// operatorBalance = await ethers.provider.getBalance(operator.address);
192+
// buyerBalance = await ethers.provider.getBalance(buyer.address);
193193

194-
console.log({
195-
authorBalance: ethers.formatEther(authorBalance),
196-
operatorBalance: ethers.formatEther(operatorBalance),
197-
buyerBalance: ethers.formatEther(buyerBalance),
198-
});
194+
// console.log({
195+
// authorBalance: ethers.formatEther(authorBalance),
196+
// operatorBalance: ethers.formatEther(operatorBalance),
197+
// buyerBalance: ethers.formatEther(buyerBalance),
198+
// });
199+
200+
// only the buyer should set prices now
201+
await expect(ERC721.connect(author).setTokenForSale(0, BigInt(ethers.parseEther("2")))).to.be.reverted;
202+
await expect(ERC721.connect(operator).setTokenForSale(0, BigInt(ethers.parseEther("2")))).to.be.reverted;
203+
204+
await expect(ERC721.connect(buyer).setTokenForSale(0, BigInt(ethers.parseEther("2"))))
205+
.to.emit(ERC721, "SetTokenForSale")
206+
.withArgs(buyer.address, 0, BigInt(ethers.parseEther("2")));
207+
208+
const newPrice = await ERC721.getTokenPrice(0);
209+
expect(ethers.formatEther(newPrice)).to.equal("2.0");
210+
211+
// too poor to buy
212+
await expect(ERC721.connect(someoneElse).buyToken(0, { value: BigInt(ethers.parseEther("1")) })).to.be.reverted;
199213
});
200214
});

0 commit comments

Comments
 (0)