Skip to content

Instantly share code, notes, and snippets.

@0xfoobar
Created August 17, 2021 06:46
Show Gist options
  • Save 0xfoobar/eb9fd6f94d5255010c7bf2a1478a385b to your computer and use it in GitHub Desktop.
Save 0xfoobar/eb9fd6f94d5255010c7bf2a1478a385b to your computer and use it in GitHub Desktop.
const { expect } = require("chai");
describe("NFTOptions", function () {
const penguAddr = "0xBd3531dA5CF5857e7CfAA92426877b022e612cf8";
const penguOwner = "0x2c2c2F6DBe08942280d4F7626b6BAd5C11D20215";
const ids = ["2036", "4205"];
const buyer = "0xe5ee2B9d5320f2D1492e16567F36b578372B3d9F";
const erc721Abi = [
"function approve(address to, uint256 tokenId)"
];
const penguContract = new ethers.Contract(penguAddr, erc721Abi, ethers.provider);
it("should create an option, sell it, and redeem it", async function () {
const Options = await ethers.getContractFactory("NFTOptions");
const options = await Options.deploy();
await options.deployed();
await setupImpersonator(penguOwner);
await setupImpersonator(buyer);
const penguOwnerSigner = await ethers.provider.getSigner(penguOwner);
const buyerSigner = await ethers.provider.getSigner(buyer);
let recent = await ethers.provider.getBlockNumber();
let block = await ethers.provider.getBlock(recent);
let time = block.timestamp + 3600 * 24;
let strikePrice = ethers.utils.parseEther("0.1");
let optionPrice = ethers.utils.parseEther("0.1");
console.log("Approving");
await penguContract.connect(penguOwnerSigner).approve(options.address, ids[0]);
console.log("Creating offer");
await options.connect(penguOwnerSigner).createOffer(
penguOwner,
buyer,
penguAddr,
ids[0],
time,
strikePrice,
optionPrice
);
let offer = await options.getOffer(penguAddr, ids[0]);
// console.log(offer);
console.log("Accepting offer");
await options.connect(buyerSigner).acceptOffer(
penguAddr,
ids[0],
offer.nonce,
{
"value": offer.optionPrice
}
);
let balanceSeller = await options.balances(penguOwner);
expect(balanceSeller).to.gte(ethers.BigNumber.from(0));
console.log(`Seller balance is ${balanceSeller}`);
await timeTravel(3600 * 24);
await options.connect(buyerSigner).claimOption(
penguAddr,
ids[0],
{
"value": offer.strikePrice
}
);
balanceSeller = await options.balances(penguOwner);
expect(balanceSeller).to.gte(ethers.BigNumber.from(0));
console.log(`Seller balance is ${balanceSeller}`);
});
});
async function setupImpersonator(addr) {
const impersonateTx = await hre.network.provider.request({
method: "hardhat_impersonateAccount",
params: [addr],
});
}
async function timeTravel(time) {
await network.provider.send("evm_increaseTime", [time]);
let transact = await network.provider.send("evm_mine");
return transact;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment