Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
# Smart Contract API Example

Example code for building your own Ethereum smart contract API in [Node.js](https://nodejs.org/en/download/).

## Setup

```
A minimal example showing how to build an Ethereum smart contract HTTP API with Node.js, Express and Infura.

Relevant files
- Main server: [/server.js](server.js)
- Runtime config: [/config.json](config.json)
- Project metadata: [/package.json](package.json)
- License: [/LICENSE](LICENSE)
- Git ignore: [/.gitignore](.gitignore)

Prerequisites
- Node.js (recommended LTS)
- An Ethereum provider URL (e.g. Infura project endpoint)
- A wallet private key set in the environment

Security notice
- Do NOT commit or store private keys in the repo. The server reads the private key from the environment variable `walletPrivateKey` (see [`walletPrivateKey`](server.js)).
- Use a dedicated wallet with limited funds for testing.
- Ensure your provider URL and keys are stored securely.

Quick setup
```bash
git clone git@github.com:compound-developers/api-guide-example.git
cd api-guide-example
npm install
```

**config.json**

The listed contract addresses and ABIs might be out of date. They can be confirmed here https://compound.finance/docs#networks.

**server.js**

1. Be sure to set an environment variable for the Ethereum wallet private
key.
2. Replace the HTTP Provider URL with your own. [Infura provides free API keys](https://infura.io/).

```js
const walletPrivateKey = process.env.walletPrivateKey;
const web3 = new Web3('https://mainnet.infura.io/v3/_your_api_key_here_');
```

## Running

```
Expand Down Expand Up @@ -75,5 +76,4 @@ curl localhost:3000/wallet-balance/ceth/

curl localhost:3000/wallet-balance/eth/
> 99.995909700269046879

```
Loading