Code Monkey home page Code Monkey logo

web3-example's Introduction

WEB3 example

Setup

Copy env file

cp .env_example .env

# Set your NETWORK_ENDPOINT if necessary

Setting your wallet information

echo '[
  {
    "address": "0xYOUR ADDRESS",
    "privateKey": "YOUR PRIVATE KEY"
  },
  {
    "address": "0xYOUR ADDRESS",
    "privateKey": "YOUR PRIVATE KEY"
  }
]' | base64

# COPY above output to your .env file, at MY_WALLET=

Install dependencies and run the wallet

yarn
yarn dev

Actions

Action Description
GET /balance Get wallet balance
GET /usdc-balance Get usdc balance via SmartContract
POST /transfer Transfer token from address in walet to another address

Example

Get balance

curl --location 'http://localhost:3000/balance'

Output:

{
  "data": {
    "balances": [
      {
        "address": "0x778E72B05320A00cd014Cd7aB842BE866022634b",
        "balance": 17833599993013584
      },
      {
        "address": "0xC969D4d09D431CF43861701EB0C0BdD15C838ECB",
        "balance": 6986415
      }
    ],
    "totalBalance": 17833600000000000
  }
}

Get USDC balance via SmartContract

curl --location 'http://localhost:3000/usdc-balance?address=0x778E72B05320A00cd014Cd7aB842BE866022634b'

Output:

{
    "data": {
        "balance": 0
    }
}

Transfer token

curl --location 'http://localhost:3000/transfer' \
--header 'Content-Type: application/json' \
--data '{
    "from": "0x778E72B05320A00cd014Cd7aB842BE866022634b",
    "to": "0xC969D4d09D431CF43861701EB0C0BdD15C838ECB",
    "amount": 912345
}'

Ouput:

{
  "data": {
    "confirmations": 1,
    "receipt": {
      "blockHash": "0x9b47f6130ffcfbbc4fe5f6c57f5d98c524d1cb0371c9859dc47603f9a1a1e0ee",
      "blockNumber": 30896414,
      "cumulativeGasUsed": 902212,
      "effectiveGasPrice": 10000000000,
      "from": "0x778e72b05320a00cd014cd7ab842be866022634b",
      "gasUsed": 21664,
      "logs": [],
      "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
      "status": 1,
      "to": "0xc969d4d09d431cf43861701eb0c0bdd15c838ecb",
      "transactionHash": "0x57e1c2838bc8b674590913058c092c56f82d2461ea7a2df187f70a6535d6ea4d",
      "transactionIndex": 7,
      "type": 0
    }
  }
}

Console log:

Transaction is sending
Transaction was sent
Transaction hash received
Receipt received
Transaction is confirmed

Thanks.

web3-example's People

Contributors

phongkt-dev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.