Skip to content

Commit

Permalink
Fixed the balance checks
Browse files Browse the repository at this point in the history
  • Loading branch information
howardjp committed May 9, 2023
1 parent 21430a9 commit 4c0cc0e
Show file tree
Hide file tree
Showing 19 changed files with 48 additions and 71 deletions.
6 changes: 5 additions & 1 deletion contracts/ELEN_E6883_NFT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ contract ELEN_E6883_NFT is ERC721URIStorage, Ownable {
return Listings[tokenId].active;
}

// Buy an NFT
function getBalanceOf(address addr) public view virtual returns (uint256) {
return addr.balance;
}

// Buy an NFT
function purchaseNFT(uint256 tokenId) public payable {
require(_exists(tokenId), "Token ID does not exist");
require(msg.value >= Listings[tokenId].price, "Not enough ether to cover asking price");
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"homepage": "https://github.com/truffle-box/nft-box#readme",
"dependencies": {
"@openzeppelin/contracts": "^4.5.0",
"bn.js": "^5.2.1",
"dotenv": "^16.0.0",
"typescript": "^4.6.4"
},
Expand Down
13 changes: 5 additions & 8 deletions scripts/balanceOf.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
//SPDX-License-Identifier: MIT

const getErrorMessage = require("../scripts/getErrorMessage.js");
var ELEN_E6883_NFT = artifacts.require("./ELEN_E6883_NFT.sol");

function getErrorMessage(error) {
if (error instanceof Error) return error.message
return String(error)
}

const ownerOf = async (unique_id) => {
const balanceOf = async (addr) => {
try {
const nftContract = await ELEN_E6883_NFT.deployed();
const txn = await nftContract.ownerOf(unique_id);
return txn.toLowerCase();
const txn = await nftContract.getBalanceOf(addr);
return txn;
} catch(err) {
console.log('Doh! ', getErrorMessage(err));
}
}

module.exports = ownerOf;
module.exports = balanceOf;
6 changes: 1 addition & 5 deletions scripts/createNFT.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

var ELEN_E6883_NFT = artifacts.require("./ELEN_E6883_NFT.sol");
const util = require('util')

function getErrorMessage(error) {
if (error instanceof Error) return error.message
return String(error)
}
const getErrorMessage = require("../scripts/getErrorMessage.js");

const createNFT = async (unique_id, name, description) => {
try {
Expand Down
5 changes: 1 addition & 4 deletions scripts/createSale.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

var ELEN_E6883_NFT = artifacts.require("./ELEN_E6883_NFT.sol");
const util = require('util')
const getErrorMessage = require("../scripts/getErrorMessage.js");

function getErrorMessage(error) {
if (error instanceof Error) return error.message
return String(error)
}

const createSale = async (unique_id, price) => {
try {
Expand Down
4 changes: 3 additions & 1 deletion scripts/getErrorMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
function getErrorMessage(error) {
if (error instanceof Error) return error.message
return String(error)
}
}

module.exports = getErrorMessage;
5 changes: 1 addition & 4 deletions scripts/getPrice.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

var ELEN_E6883_NFT = artifacts.require("./ELEN_E6883_NFT.sol");
const util = require('util')
const getErrorMessage = require("../scripts/getErrorMessage.js");

function getErrorMessage(error) {
if (error instanceof Error) return error.message
return String(error)
}

const getPrice = async (unique_id) => {
try {
Expand Down
5 changes: 1 addition & 4 deletions scripts/getURIData.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
//SPDX-License-Identifier: MIT

var ELEN_E6883_NFT = artifacts.require("./ELEN_E6883_NFT.sol");
const getErrorMessage = require("../scripts/getErrorMessage.js");

function getErrorMessage(error) {
if (error instanceof Error) return error.message
return String(error)
}

const getURIData = async (token_id) => {
try {
Expand Down
5 changes: 1 addition & 4 deletions scripts/isNFTForSale.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
//SPDX-License-Identifier: MIT

var ELEN_E6883_NFT = artifacts.require("./ELEN_E6883_NFT.sol");
const getErrorMessage = require("../scripts/getErrorMessage.js");

function getErrorMessage(error) {
if (error instanceof Error) return error.message
return String(error)
}

const isNFTForSale = async (unique_id) => {
try {
Expand Down
5 changes: 1 addition & 4 deletions scripts/listNFTForSale.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
//SPDX-License-Identifier: MIT

var ELEN_E6883_NFT = artifacts.require("./ELEN_E6883_NFT.sol");
const getErrorMessage = require("../scripts/getErrorMessage.js");

function getErrorMessage(error) {
if (error instanceof Error) return error.message
return String(error)
}

const listNFTForSale = async (unique_id, price) => {
try {
Expand Down
6 changes: 1 addition & 5 deletions scripts/mint.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
If you want to mint more than one NFT, just pass in the number
*/
var ELEN_E6883_NFT = artifacts.require("./ELEN_E6883_NFT.sol");

function getErrorMessage(error) {
if (error instanceof Error) return error.message
return String(error)
}
const getErrorMessage = require("../scripts/getErrorMessage.js");

const main = async (cb) => {
try {
Expand Down
6 changes: 1 addition & 5 deletions scripts/ownerOf.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
//SPDX-License-Identifier: MIT

var ELEN_E6883_NFT = artifacts.require("./ELEN_E6883_NFT.sol");

function getErrorMessage(error) {
if (error instanceof Error) return error.message
return String(error)
}
const getErrorMessage = require("../scripts/getErrorMessage.js");

const ownerOf = async (unique_id) => {
try {
Expand Down
5 changes: 1 addition & 4 deletions scripts/purchaseNFT.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
//SPDX-License-Identifier: MIT

var ELEN_E6883_NFT = artifacts.require("./ELEN_E6883_NFT.sol");
const getErrorMessage = require("../scripts/getErrorMessage.js");

function getErrorMessage(error) {
if (error instanceof Error) return error.message
return String(error)
}

const purchaseNFT = async (unique_id, price) => {
try {
Expand Down
5 changes: 1 addition & 4 deletions scripts/removeNFTFromSale.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
//SPDX-License-Identifier: MIT

var ELEN_E6883_NFT = artifacts.require("./ELEN_E6883_NFT.sol");
const getErrorMessage = require("../scripts/getErrorMessage.js");

function getErrorMessage(error) {
if (error instanceof Error) return error.message
return String(error)
}

const removeNFTFromSale = async (unique_id, price) => {
try {
Expand Down
5 changes: 1 addition & 4 deletions scripts/setOwner.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

var ELEN_E6883_NFT = artifacts.require("./ELEN_E6883_NFT.sol");
const util = require('util')
const getErrorMessage = require("../scripts/getErrorMessage.js");

function getErrorMessage(error) {
if (error instanceof Error) return error.message
return String(error)
}

const setOwner = async (unique_id, newAddress) => {
try {
Expand Down
5 changes: 1 addition & 4 deletions scripts/transferNFT.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
//SPDX-License-Identifier: MIT

var ELEN_E6883_NFT = artifacts.require("./ELEN_E6883_NFT.sol");
const getErrorMessage = require("../scripts/getErrorMessage.js");

function getErrorMessage(error) {
if (error instanceof Error) return error.message
return String(error)
}

const transferNFT = async (unique_id, newOwner) => {
try {
Expand Down
17 changes: 15 additions & 2 deletions test/testELEN_E6883_NFT.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
//SPDX-License-Identifier: MIT

const BN = require('bn.js');

const balanceOf = require("../scripts/balanceOf.js");
const createNFT = require("../scripts/createNFT.js");
const getPrice = require("../scripts/getPrice.js");
const getURIData = require("../scripts/getURIData.js");
Expand Down Expand Up @@ -62,28 +67,34 @@ contract("ELEN_E6883_NFT", function (accounts) {
it("TC5: Purchase the NFT from a seller", async () => {
const name = "Test NFT #5";
const desc = "This is a test NFT description for test case 5";
const price = 1;
let price = 1000000; // 1000000wei

const tokenID = await createNFT(1220, name, desc);
await listNFTForSale(1220, price);

// Here we make a custom call to the purchase NFT to test the contract
// the JS code can be exercised elsewhere
const nftContract = await ELEN_E6883_NFT.deployed();
var account0_balance_old = await balanceOf(accounts[0]);
const txn = await nftContract.purchaseNFT(1220, {from : accounts[1], value : price});
var account0_balance_new = await balanceOf(accounts[0]);

const currOwner = await ownerOf(1220);

assert.equal(account0_balance_old.add(new BN(price)).toString(), account0_balance_new.toString(), "The buyers's account balance is incorrect");
assert.equal(currOwner, accounts[1].toLowerCase(), "The owners should be the same.");
});

it("TC6: Fail to purchase the NFT from a seller", async () => {
const name = "Test NFT #5";
const desc = "This is a test NFT description for test case 5";
const price = 2;
let price = 1000000; // 1000000wei

const tokenID = await createNFT(1221, name, desc);
await listNFTForSale(1221, price);

var account0_balance_old = await balanceOf(accounts[0]);

try {
// Here we make a custom call to the purchase NFT to test the contract
// the JS code can be exercised elsewhere
Expand All @@ -92,8 +103,10 @@ contract("ELEN_E6883_NFT", function (accounts) {
} catch(err) {
// Gracefully do nothing
}
var account0_balance_new = await balanceOf(accounts[0]);

const currOwner = await ownerOf(1221);
assert.equal(account0_balance_old.toString(), account0_balance_new.toString(), "The buyers's account balance is incorrect");
assert.equal(currOwner, accounts[0].toLowerCase(), "The owners should be the same.");
});
//*** VK end code #3 ***
Expand Down
9 changes: 1 addition & 8 deletions truffle-config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const path = require("path");

require('dotenv').config();
const HDWalletProvider = require('@truffle/hdwallet-provider');
const { INFURA_API_KEY, MNEMONIC } = process.env;

module.exports = {
contracts_build_directory: path.join(__dirname, "client/src/contracts"),
Expand All @@ -12,15 +10,10 @@ module.exports = {
port: 7545,
network_id: "*"
},

dashboard: {
port: 24012
},
sepolia: {
provider: () => new HDWalletProvider(MNEMONIC, INFURA_API_KEY),
network_id: '11155111',
gas: 4465030
}
},
mocha: {},
compilers: {
Expand Down

0 comments on commit 4c0cc0e

Please sign in to comment.