Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0afc72b
Initial Server setup and ReadMe Updates
May 9, 2023
2c2f864
added read end-point. add SDK instantiation. Brought back Analytics s…
May 10, 2023
36c40e0
Deployed NFT contract on Dashboard. updated Read to call function on…
May 10, 2023
6728ee2
Added winston. updated sharedAPISchema to have all schemas. Updated R…
May 10, 2023
d7da05c
Added write end-point. updated scehma details for args
May 11, 2023
010443c
update write end-point
May 11, 2023
13c0cdd
1st version of OpenAPI/Swagger imeplemented
May 11, 2023
acf441a
added Note on README
farhanW3 May 11, 2023
ccb53c1
updated param name & Dockerfile commented
farhanW3 May 12, 2023
4801cfc
commented out Analytics Service
farhanW3 May 12, 2023
e803eb4
Updated the route to show documentation on base url. Added descriptio…
farhanW3 May 12, 2023
11f3e3a
Fixed header issue. Updates for quotes. removed use & revoke.
farhanW3 May 12, 2023
fd98be8
Added API_KEY as ENV
farhanW3 May 12, 2023
c3d6c06
fixed error messages
farhanW3 May 12, 2023
50c55b1
fixed build error
farhanW3 May 12, 2023
d9395e3
fixed parameter name
farhanW3 May 12, 2023
06157e6
updated README
farhanW3 May 13, 2023
88bce22
Added ERC20 Standard Extensions. Re-organized Schemas, Routes etc
farhanW3 May 16, 2023
b596e98
updated transfer response
farhanW3 May 16, 2023
b9d3031
commented ERC721 & ERC1155 extensions to avoid build error
farhanW3 May 16, 2023
8d33b12
Updated READ GET to POST. Added BigNumber conversion
farhanW3 May 17, 2023
c9ff9eb
Added env example file
farhanW3 May 17, 2023
ab62436
Added POSTMAN collection into scripts folder
farhanW3 May 17, 2023
a143634
updated read (POST) to GET to keep it RESTful
farhanW3 May 17, 2023
427265e
Merge branch 'development' into farhan/dev-upds
farhanW3 May 17, 2023
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
Binary file added .DS_Store
Binary file not shown.
17 changes: 17 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"rules": {
"no-console": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
}
}
5 changes: 5 additions & 0 deletions .example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PORT=3005
OPENAPI_BASE_ORIGIN="http://localhost:3005"
HOST="0.0.0.0"
WALLET_PRIVATE_KEY="<ppk>"
API_KEY="TEST"
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"arrowParens": "always"
}
37 changes: 37 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
FROM node:18.15.0-alpine AS base

WORKDIR /app
RUN apk --no-cache --virtual build-dependencies add g++ make py3-pip

COPY . .
COPY package*.json .
COPY yarn*.json .

EXPOSE 3005

FROM base AS local

ENV NODE_ENV="local"

RUN yarn install
RUN apk del build-dependencies
ENV PATH /app/node_modules/.bin:$PATH

CMD [ "yarn", "dev" ]

# FROM base AS dev

# ENV NODE_ENV="development"
# RUN yarn install

# CMD [ "yarn", "dev" ]

# FROM base AS prod

# ENV NODE_ENV="production"

# RUN yarn install
# RUN yarn build

# RUN yarn install --production
# CMD [ "yarn", "start" ]
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# 🔑 web3-api

Thirdweb's Web3-API server.

## ENV Variables

```
PORT=3005
OPENAPI_BASE_ORIGIN="http://localhost:3005"
HOST="0.0.0.0"
WALLET_PRIVATE_KEY="<ppk>"
API_KEY="TEST"
```

## Getting Started

1. Install packages: `yarn`
2. Start local Docker containers: `yarn infra`
3. Start server for local development with hot reloading: `yarn dev`

## ToDo

- [X] Fastify Server Up & Running
- [X] Dockerize the Server
- [X] Add logging capabilty with winston
- [ ] Add OpenAPI/Swagger Document Generation
- [X] Add API-Key validation as middleware
- [ ] Make API-Key Validation work with ThirdWeb Access check
- [ ] Add wallet-id validation as middleware
- [X] Add Read End-point
- [X] Add Write End-point
- [ ] Add Deployer End-point


## Note:

`docker-compose.yaml` has commented out env variables value. You will need to add a PPK of your test wallet and then you should be able to intereact with the APIs

## Example

```
Example 1:
- function_name: balanceOf
- args: <eth_address> // My Wallet: 0x1946267d81fb8adeeea28e6b98bcd446c8248473
- chain_or_rpc: mumbai
- contract_address: 0xc8be6265C06aC376876b4F62670adB3c4d72EABA
- x-api-key : <pass_any_random_string_as_we_are_not_doing_any_checks_in_this_implementation> [future iterations will have key checking too]
- x-wallet_id : <pass_any_random_string_as_we_are_not_doing_any_checks_in_this_implementation> [future iterations will have wallet checking/retrieval too]

Response:

{
"result": {
"data": "1" //Since my wallet has 1 NFT
},
"error": null
}

----------

Example 2:
- function_name: name
- args: // Leave Empty
- chain_or_rpc: mumbai
- contract_address: 0xc8be6265C06aC376876b4F62670adB3c4d72EABA
- x-api-key : <pass_any_random_string_as_we_are_not_doing_any_checks_in_this_implementation> [future iterations will have key checking too]
- x-wallet_id : <pass_any_random_string_as_we_are_not_doing_any_checks_in_this_implementation> [future iterations will have wallet checking/retrieval too]

Response:

{
"result": {
"data": "Mumba"
},
"error": null
}
```
21 changes: 21 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: '3.8'

services:
web3-api:
# environment:
# - PORT=3005
# - OPENAPI_BASE_ORIGIN="http://localhost:3005"
# - HOST="0.0.0.0"
# - WALLET_PRIVATE_KEY=""
build:
dockerfile: Dockerfile
context: .
target: local
ports:
- 3005:3005
volumes:
- ./:/app
- node_modules:/app/node_modules

volumes:
node_modules:
62 changes: 62 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"name": "web3-api",
"version": "1.0.0",
"description": "Thirdweb's web3-api server",
"main": "src/index.ts",
"author": "Farhan Khwaja <farhan@thirdweb.com>",
"type": "module",
"scripts": {
"build": "yarn && rimraf dist && tsc -p ./tsconfig.json --outDir dist",
"start": "node --experimental-specifier-resolution=node dist/index.js",
"dev": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' --files src/index.ts",
"infra": "docker compose --env-file ./.env up",
"lint": "eslint 'src/**/*.ts'",
"lint:fix": "eslint --fix 'src/**/*.ts'",
"prepare": "husky install"
},
"dependencies": {
"@fastify/cookie": "^8.3.0",
"@fastify/cors": "^8.2.1",
"@fastify/express": "^2.3.0",
"@fastify/swagger": "^8.3.1",
"@fastify/swagger-ui": "^1.8.1",
"@fastify/type-provider-typebox": "^3.0.0",
"@sinclair/typebox": "^0.27.8",
"@thirdweb-dev/auth": "3.0.16-nightly-72b8b982-20230503002100",
"body-parser": "^1.20.2",
"cookie": "^0.5.0",
"dotenv": "^16.0.3",
"ethers": "5",
"express": "^4.18.2",
"fastify": "^4.15.0",
"fastify-plugin": "^4.5.0",
"http-status-codes": "^2.2.0",
"winston": "^3.8.2"
},
"devDependencies": {
"@swc/core": "^1.3.41",
"@types/cookie": "^0.5.1",
"@types/express": "^4.17.17",
"@types/node": "^18.15.4",
"@types/ws": "^8.5.4",
"@typescript-eslint/eslint-plugin": "^5.55.0",
"@typescript-eslint/parser": "^5.55.0",
"eslint": "^8.36.0",
"eslint-config-prettier": "^8.7.0",
"husky": "^8.0.3",
"nodemon": "^2.0.21",
"pino-pretty": "^10.0.0",
"prettier": "^2.8.7",
"rimraf": "^5.0.0",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
},
"volta": {
"node": "18.15.0",
"yarn": "3.5.0"
},
"lint-staged": {
"*.{js,ts}": "eslint --cache --fix",
"*.{js,ts,md}": "prettier --write"
}
}
79 changes: 79 additions & 0 deletions scripts/Web3-API.postman_collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"info": {
"_postman_id": "e1257f8e-1174-4235-815e-0b73f7383fe9",
"name": "Web3-API",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "27458986"
},
"item": [
{
"name": "Read - Request",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"function_name\": \"balanceOf\",\n \"args\": [\"0x3EcDBF3B911d0e9052b64850693888b008e18373\"]\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:3005/contract/mumbai/0x365b83D67D5539C6583b9c0266A548926Bf216F4/read",
"protocol": "http",
"host": [
"localhost"
],
"port": "3005",
"path": [
"contract",
"mumbai",
"0x365b83D67D5539C6583b9c0266A548926Bf216F4",
"read"
],
"query": [
{
"key": "args",
"value": "\"\"",
"disabled": true
}
]
}
},
"response": []
},
{
"name": "Write - Request",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"function_name\": \"transfer\",\n \"args\": [\"0x3EcDBF3B911d0e9052b64850693888b008e18373\", 1]\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:3005/contract/mumbai/0x365b83D67D5539C6583b9c0266A548926Bf216F4/write",
"protocol": "http",
"host": [
"localhost"
],
"port": "3005",
"path": [
"contract",
"mumbai",
"0x365b83D67D5539C6583b9c0266A548926Bf216F4",
"write"
]
}
},
"response": []
}
]
}
42 changes: 42 additions & 0 deletions src/api/contract/extensions/erc1155/read.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// import { FastifyInstance, RouteGenericInterface } from 'fastify';
// import { StatusCodes } from 'http-status-codes';
// import { getSDK } from '../../../../helpers/sdk';
// import { Static } from '@sinclair/typebox';
// import { fullRouteSchema, schemaTypes } from '../../../../sharedApiSchemas';
// import { logger } from '../../../../utilities/logger';

// export async function erc1155ReadContract(fastify: FastifyInstance) {
// fastify.route<schemaTypes>({
// method: 'GET',
// url: '/contract/:chain_name_or_id/:contract_address/erc1155/read',
// schema: {
// description: 'Read From ERC1155 Contract',
// tags: ['extensions'],
// operationId: 'erc1155Read',
// ...fullRouteSchema,
// },
// handler: async (request, reply) => {
// const { chain_name_or_id, contract_address } = request.params;
// const { function_name, args } = request.query;
// logger.info('Inside Read Function');
// logger.silly(`Chain : ${chain_name_or_id}`)
// logger.silly(`Contract Address : ${contract_address}`);

// logger.silly(`Function Name : ${function_name}`)
// logger.silly(`Args : ${args}`);

// const sdk = await getSDK(chain_name_or_id);
// const contract = await sdk.getContract(contract_address);

// const returnData: any = "adad";
// // await contract.erc1155.call(function_name, args ? args.split(',') : []);

// reply.status(StatusCodes.OK).send({
// result: {
// data: returnData
// },
// error: null,
// });
// },
// });
// }
Loading