0

I create my own erc20 token and transfer into the metamask. Now I want to use for purchase things which i am listing on the website .For example ,i made a shop where different things are avaliable . so how i would purchase with my own erc20 token

1
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.
    – Community Bot
    Sep 23, 2023 at 13:13

1 Answer 1

0

Create a Solidity smart contract for your shop:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract Shop {
    address public owner;
    IERC20 public erc20Token;

    struct Item {
        uint256 price;
        uint256 stock;
    }

    mapping(uint256 => Item) public items;

    event Purchase(address indexed buyer, uint256 itemId, uint256 quantity);

    constructor(address _tokenAddress) {
        owner = msg.sender;
        erc20Token = IERC20(_tokenAddress);
        // Initialize items with prices and stock
        items[1] = Item(10 ether, 10);
        items[2] = Item(5 ether, 20);
    }

    function purchase(uint256 itemId, uint256 quantity) external {
        require(quantity > 0, "Quantity must be greater than zero");
        require(items[itemId].stock >= quantity, "Not enough stock");
        uint256 totalPrice = items[itemId].price * quantity;
        require(erc20Token.allowance(msg.sender, address(this)) >= totalPrice, "Not enough allowance");
        require(erc20Token.transferFrom(msg.sender, owner, totalPrice), "Transfer failed");
        items[itemId].stock -= quantity;
        emit Purchase(msg.sender, itemId, quantity);
    }
}

Frontend Integration: In your website's JavaScript code, you can interact with the smart contract using Web3.js. You'll need to initialize a Web3 provider (like MetaMask) and use it to send transactions. Below is a simplified example:

// Initialize Web3.js with MetaMask or another provider
const web3 = new Web3(window.ethereum);

// Contract address and ABI
const contractAddress = 'YOUR_CONTRACT_ADDRESS';
const contractABI = [...]; // Insert your contract's ABI here

// Create a contract instance
const shopContract = new web3.eth.Contract(contractABI, contractAddress);

// Function to handle a purchase
async function purchaseItem(itemId, quantity) {
    try {
        const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
        const account = accounts[0];
        const gas = await shopContract.methods.purchase(itemId, quantity).estimateGas({ from: account });
        const result = await shopContract.methods.purchase(itemId, quantity).send({
            from: account,
            gas,
        });
        console.log('Purchase successful:', result);
    } catch (error) {
        console.error('Purchase error:', error);
    }
}
2
  • Can it deploy on sepolia or localhost? To use own Erc20 token
    – Rizwan Ali
    Sep 20, 2023 at 7:48
  • Yes, you can deploy and test on any network, mainnet or local
    – Mehran Ra
    Sep 20, 2023 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.