decentramall / decentramall-ui

Your Unstoppable Decentralized Shopping Destination 🛍️🛒
https://decentramall.on.fleek.co/
GNU General Public License v3.0
5 stars 0 forks source link

Created token & agent #4

Closed easonchai closed 4 years ago

easonchai commented 4 years ago

TODO:

easonchai commented 4 years ago

Four contracts in total:

Suggestions:

raphaelpg commented 4 years ago

Contract EstateAgent.sol need to have "receive() external payable {}" line so it can receive Eth.

raphaelpg commented 4 years ago

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:

  1. 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) },

  2. Run command "truffle develop" to start local blockchain

  3. 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

raphaelpg commented 4 years ago

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

});

vibern0 commented 4 years ago

I recommend that after fixing the failing test, we review this PR and merge it.

vibern0 commented 4 years ago

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.