freeCodeCamp / web3-curriculum

Half of the Web3 Curriculum
BSD 3-Clause "New" or "Revised" License
156 stars 76 forks source link

[HELP]: related to corrections in my code. #105

Open Marupillasriram opened 12 months ago

Marupillasriram commented 12 months ago

Project

build-a-video-game-marketplace-blockchain

Lesson Number

open project(single lesson)

Question

7) ✗ Running node buy-item.js should add a transaction at the end of the transactions.json array with the correct buyerAddress, sellerAddress, price, itemBought, and signature properties. Note: You may need to pass the balance check test right below this (test 8) for this to pass 8) ✗ Running node buy-item.js should not add a transaction if that address doesn't have enough funds to buy the item 9) ✗ Running node sell-item.js should add a transaction at the end of the transactions.json array with the correct buyerAddress, sellerAddress, price, itemSold, and signature properties. Note: You may need to pass the item check test right below this (test 10) for this to pass 10) ✓ Running node sell-item.js should not add a transaction if that address doesn't have that item to sell

Code and Screenshots

sell-item.js


import EC from 'elliptic';
// Add your code below
import { writeFileSync, readFileSync } from 'fs';
import sha256 from 'crypto-js/sha256.js';
import {
  getAddressItems,
  getTransactions,
  getItemPrice,
  writeTransactions,
  getWalletAddressFromName,
} from './blockchain-helpers.js';

const ec = new EC.ec('p192');

const sellerPrivateKey = process.argv[2];
const itemSold = process.argv[3];

const price = getItemPrice(itemSold);

const transactions = getTransactions();

const walletFile = readFileSync('./wallets.json');
const wallets = JSON.parse(walletFile);
const keys = Object.keys(wallets);

function getTransaction(privateKey) {
  for (const key of keys) {
    const person = wallets[key];

    if (privateKey === person.privateKey) {
      const fromKeyPair = ec.keyFromPrivate(sellerPrivateKey, 'hex');
      const hash = sha256(price + key + itemSold).toString();
      const signature = fromKeyPair.sign(hash).toDER('hex');

      const newTransaction = {
        buyerAddress: 'null',
        sellerAddress: person.publicKey,
        itemSold,
        signature,
      };

      return newTransaction;
    }
  }

  return null; // Return null if no matching private key is found
}

function getaddress(privateKey) {
  for (const key of keys) {
    const person = wallets[key];

    if (privateKey === person.privateKey) {

      //console.log(key.publicKey);
      return key;
    }
  }
}

const newTransaction = getTransaction(sellerPrivateKey);

if (newTransaction !== null) {
  const nameOfAddress = getaddress(sellerPrivateKey);
  const address = getWalletAddressFromName(nameOfAddress);
  const items = getAddressItems(address);
  if (items.hasOwnProperty(itemSold)) {
    const value = items[itemSold];
    if (value > 0) {
      transactions.push(newTransaction);
      writeTransactions(transactions);
    }
    else {
      console.log('it does not have that item.')
    }
    }
} else {
  console.log('No matching private key found.');
}

image

buy-item.js

import { writeFileSync, readFileSync } from 'fs';
import sha256 from 'crypto-js/sha256.js';
import {
  getAddressBalance,
  getTransactions,
  getItemPrice,
  writeTransactions,
  getWalletAddressFromName,
} from './blockchain-helpers.js';

import EC from 'elliptic';
const ec = new EC.ec('p192');

const buyerPrivateKey = process.argv[2];
const itemBought = process.argv[3];

const price = getItemPrice(itemBought);

const transactions = getTransactions();

const walletFile = readFileSync('./wallets.json');
const wallets = JSON.parse(walletFile);
const keys = Object.keys(wallets);

function getTransaction(privateKey) {
  for (const key of keys) {
    const person = wallets[key];

    if (privateKey === person.privateKey) {
      const fromKeyPair = ec.keyFromPrivate(buyerPrivateKey, 'hex');
      const hash = sha256(price + key + itemBought).toString();
      const signature = fromKeyPair.sign(hash).toDER('hex');

      const newTransaction = {
        buyerAddress: person.publicKey,
        sellerAddress: 'null',
        itemBought,
        signature,
      };

      return newTransaction;
    }
  }

  return null; // Return null if no matching private key is found
}

function getaddress(privateKey) {
  for (const key of keys) {
    const person = wallets[key];

    if (privateKey === person.privateKey) {

      //console.log(key.publicKey);
      return key;
    }
  }
}

const newTransaction = getTransaction(buyerPrivateKey);

if (newTransaction !== null) {
  const nameOfAddress = getaddress(buyerPrivateKey);
  const address = getWalletAddressFromName(nameOfAddress);
  const accountBalance = getAddressBalance(address);

  if (accountBalance >= price) {
    transactions.push(newTransaction);
    writeTransactions(transactions);

  }
  else {
    console.log("You don't have enough accountBalance.")
  }
} else {
  console.log('No matching private key found.');
}

image

my logic with buying and selling is correct as per the given rules

  1. buy's only when the account balance is more than the price of the buying item.
  2. sells only when the account contains that item.
  3. writing correct public address to the transaction.json.
  4. one thing wrong in my code is when selling the item, the selling price should be less than the 5 coins.

    i want to know while buying and selling the items, how to append the balance to the wallet address.

ShaunSHamilton commented 12 months ago

Hello there,

Can you see any hints in the Console? Usually, it provides a lot more feedback about why a test is failing.


i want to know while buying and selling the items, how to append the balance to the wallet address.

On this: Remember, there are blockchain-helper.js functions. There are many functions in there to work with the wallets.

If you have further questions for help, I suggest using https://forum.freecodecamp.org/ instead of a GitHub issue as it is slightly easier to follow a conversation.

Hope this helps

Marupillasriram commented 11 months ago

thank you Shaun i will follow the forum.freecodecamp.org