Closed easonchai closed 4 years ago
Four contracts in total:
Suggestions:
Contract EstateAgent.sol need to have "receive() external payable {}" line so it can receive Eth.
I'm gonna write here what you have to do so you can start testing your contracts: Providing you are using truffle and your contracts are saved in the contract folder:
Inside the truffle-config.js, set a network as: networks: { develop: { host: "127.0.0.1", // Localhost (default: none) port: 7545, // Standard Ethereum port (default: none) network_id: "5777", // Any network (default: none) },
Run command "truffle develop" to start local blockchain
In the migration file, write: const Administration = artifacts.require("Administration"); const DecentramallToken = artifacts.require("DecentramallToken"); const EstateAgent = artifacts.require("EstateAgent"); const RentalAgent = artifacts.require("RentalAgent"); const agent = '0xfe54a4bf10e879fc18b0facc4c8c3c72f9e082c5'; //ONE OF YOUR LOCAL BLOCKCHAIN ADDRESS
module.exports = function (deployer) { deployer.then(async () => { await deployer.deploy(DecentramallToken, agent); await deployer.deploy(EstateAgent, 100, 1); await deployer.deploy(RentalAgent, DecentramallToken.address, EstateAgent.address); }); };
4.In a second console, run "truffle migrate --reset" and "truffle test --network develop" The test should start
Don't know where to upload the test file (I will delete this comment after):
I'm using 2 libraries: "@openzeppelin/test-helpers" and "chai"
const { BN, ether, expectRevert } = require("@openzeppelin/test-helpers"); const { expect } = require("chai"); const Administration = artifacts.require("Administration"); const DecentramallToken = artifacts.require("DecentramallToken"); const EstateAgent = artifacts.require("EstateAgent"); const RentalAgent = artifacts.require("RentalAgent");
//Testing DecentramallToken.sol contract("DecentramallToken", function(accounts){ const admin = accounts[0]; const agent = accounts[1]; const purchaser = accounts[2]; const renter = accounts[3]; const hacker = accounts[4];
//Before each unit test
beforeEach(async function() {
this.decentramallTokenInstance = await DecentramallToken.new(agent);
});
// function mint(address purchaser) public onlyAgent returns(uint256){
// uint256 tokenId = uint256(keccak256(abi.encodePacked(purchaser)));
// _safeMint(purchaser, tokenId, "");
// return (tokenId);
// }
it("Testing mint() function", async function() {
//store token id returned by function
const token = await this.decentramallTokenInstance.mint.call(purchaser, {from: agent});
//mint
await this.decentramallTokenInstance.mint(purchaser, {from: agent});
//Verify totalSupply equal to 1
let totalSupply = await this.decentramallTokenInstance.totalSupply();
expect(totalSupply).to.be.bignumber.equal(new BN(1));
//Verifying modifier is effective
await expectRevert(this.decentramallTokenInstance.mint(hacker, {from: hacker}),"Not an agent!");
//Verifying
const legitimacy = await this.decentramallTokenInstance.verifyLegitimacy.call(purchaser, token);
expect(legitimacy).to.be.equal(true);
});
// function burn(uint256 tokenId) public onlyAgent{
// _burn(tokenId);
// }
it("Testing burn() function", async function() {
//store token id returned by function
const token = await this.decentramallTokenInstance.mint.call(purchaser, {from: agent});
//mint
await this.decentramallTokenInstance.mint(purchaser, {from: agent});
//burn
await this.decentramallTokenInstance.burn(token, {from: agent});
//Verify totalSupply equal to 0
let totalSupply = await this.decentramallTokenInstance.totalSupply();
expect(totalSupply).to.be.bignumber.equal(new BN(0));
});
// function verifyLegitimacy(address sender, uint256 tokenId) public view returns (bool) {
// return _exists(tokenId) && ownerOf(tokenId) == sender;
// }
it("Testing verifyLegitimacy() function", async function() {
//store token id returned by function
const token = await this.decentramallTokenInstance.mint.call(purchaser, {from: agent});
//mint
await this.decentramallTokenInstance.mint(purchaser, {from: agent});
//Verifying legitimacy
const legitimacy = await this.decentramallTokenInstance.verifyLegitimacy.call(purchaser, token);
expect(legitimacy).to.be.equal(true);
});
});
//Testing EstateAgent.sol contract("EstateAgent", function(accounts){ const admin = accounts[0]; const agent = accounts[1]; const purchaser = accounts[2]; const renter = accounts[3]; const hacker = accounts[4];
//Before each unit test
beforeEach(async function() {
this.estateAgentTokenInstance = await EstateAgent.new(10, 1);
});
// function withdraw(address payable to, uint256 amount) external onlyAdmin{
// require(address(this).balance > 0 && amount < address(this).balance, "Impossible");
// to.transfer(amount);
// emit Withdraw(to, amount);
// }
it("Testing withdraw() function", async function() {
//transfering 2 ETH
await web3.eth.sendTransaction({from:purchaser, to:this.estateAgentTokenInstance.address, value:"2000000000000000000"})
//check balance
let estateAgentBalanceBefore = await web3.eth.getBalance(this.estateAgentTokenInstance.address);
//withdraw 1 ETH
await this.estateAgentTokenInstance.withdraw(admin, new BN('1000000000000000000'), {from: admin});
//recheck balance
let estateAgentBalanceAfter = await web3.eth.getBalance(this.estateAgentTokenInstance.address);
//assertion
expect(estateAgentBalanceBefore).to.be.bignumber.equal(estateAgentBalanceAfter.sub(new BN('1000000000000000000')))
});
//ERROR CONTRACT CAN'T RECEIVE ETH
});
I recommend that after fixing the failing test, we review this PR and merge it.
I removed the request for review for others, as we are all focused on different things at the moment and I knew the code well enough because I've worked on it yesterday. We will keep improving as we go :clap: thanks.
TODO: