Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Marupillasriram opened this issue Sep 24, 2023 · 2 comments
Open

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

Marupillasriram opened this issue Sep 24, 2023 · 2 comments

Comments

@Marupillasriram
Copy link

Marupillasriram commented Sep 24, 2023

Project

build-a-video-game-marketplace-blockchain

Lesson Number

open project(single lesson)

Question

  1. ✗ 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
  2. ✗ Running node buy-item.js should not add a transaction if that address doesn't have enough funds to buy the item
  3. ✗ 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
  4. ✓ 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
Copy link
Member

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants