Skip to content

Instantly share code, notes, and snippets.

@abdullah-web-dev
Created February 17, 2023 05:08
Show Gist options
  • Save abdullah-web-dev/b94c92b6cb3e16088f778debcb4e1725 to your computer and use it in GitHub Desktop.
Save abdullah-web-dev/b94c92b6cb3e16088f778debcb4e1725 to your computer and use it in GitHub Desktop.

Revisions

  1. abdullah-web-dev created this gist Feb 17, 2023.
    158 changes: 158 additions & 0 deletions BEP-20 Transaction
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,158 @@
    const contractAddress = "0x70f05CA210B8322ED343bC0AA00EDA55E2ACFE0C";
    const abi = [
    { inputs: [], stateMutability: "nonpayable", type: "constructor" },
    {
    anonymous: false,
    inputs: [
    {
    indexed: true,
    internalType: "address",
    name: "_owner",
    type: "address",
    },
    {
    indexed: true,
    internalType: "address",
    name: "_spender",
    type: "address",
    },
    {
    indexed: false,
    internalType: "uint256",
    name: "_value",
    type: "uint256",
    },
    ],
    name: "Approval",
    type: "event",
    },
    {
    anonymous: false,
    inputs: [
    {
    indexed: true,
    internalType: "address",
    name: "_from",
    type: "address",
    },
    { indexed: true, internalType: "address", name: "_to", type: "address" },
    {
    indexed: false,
    internalType: "uint256",
    name: "_value",
    type: "uint256",
    },
    ],
    name: "Transfer",
    type: "event",
    },
    {
    inputs: [
    { internalType: "address", name: "", type: "address" },
    { internalType: "address", name: "", type: "address" },
    ],
    name: "allowance",
    outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
    stateMutability: "view",
    type: "function",
    },
    {
    inputs: [
    { internalType: "address", name: "_spender", type: "address" },
    { internalType: "uint256", name: "_value", type: "uint256" },
    ],
    name: "approve",
    outputs: [{ internalType: "bool", name: "success", type: "bool" }],
    stateMutability: "nonpayable",
    type: "function",
    },
    {
    inputs: [{ internalType: "address", name: "", type: "address" }],
    name: "balanceOf",
    outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
    stateMutability: "view",
    type: "function",
    },
    {
    inputs: [],
    name: "decimals",
    outputs: [{ internalType: "uint8", name: "", type: "uint8" }],
    stateMutability: "view",
    type: "function",
    },
    {
    inputs: [],
    name: "name",
    outputs: [{ internalType: "string", name: "", type: "string" }],
    stateMutability: "view",
    type: "function",
    },
    {
    inputs: [],
    name: "symbol",
    outputs: [{ internalType: "string", name: "", type: "string" }],
    stateMutability: "view",
    type: "function",
    },
    {
    inputs: [],
    name: "totalSupply",
    outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
    stateMutability: "view",
    type: "function",
    },
    {
    inputs: [
    { internalType: "address", name: "_to", type: "address" },
    { internalType: "uint256", name: "_value", type: "uint256" },
    ],
    name: "transfer",
    outputs: [{ internalType: "bool", name: "success", type: "bool" }],
    stateMutability: "nonpayable",
    type: "function",
    },
    {
    inputs: [
    { internalType: "address", name: "_from", type: "address" },
    { internalType: "address", name: "_to", type: "address" },
    { internalType: "uint256", name: "_value", type: "uint256" },
    ],
    name: "transferFrom",
    outputs: [{ internalType: "bool", name: "success", type: "bool" }],
    stateMutability: "nonpayable",
    type: "function",
    },
    ];

    const Web3 = require("web3");
    const web3 = new Web3("https://data-seed-prebsc-1-s1.binance.org:8545/");

    const privateKey =
    "CAN BE FOUND IN METAMASK WALLET ID -> ACCOUNT DETAILS -> EXPORT PRIVATE KEY";

    const recipient = "RECEIVED WALLET ADDRESS";

    const account = web3.eth.accounts.privateKeyToAccount(privateKey);

    (async () => {
    web3.eth.defaultAccount = account.address;
    const contract = new web3.eth.Contract(abi, contractAddress);

    const tx = contract.methods.transfer(recipient, "1000");
    const gas = await tx.estimateGas({ from: account.address });
    const signedTx = await web3.eth.accounts.signTransaction(
    {
    to: contractAddress,
    data: tx.encodeABI(),
    gas: gas,
    gasPrice: web3.utils.toWei("10", "gwei"),
    nonce: await web3.eth.getTransactionCount(account.address, "pending"),
    },
    privateKey
    );

    const txReceipt = await web3.eth.sendSignedTransaction(
    signedTx.rawTransaction
    );
    console.log("Transaction hash:", txReceipt.transactionHash);
    })();