Skip to content

Commit

Permalink
Merge pull request #8 from sugarshop/develop
Browse files Browse the repository at this point in the history
[feat]: replace pakcage name from eth-tx-server to token-gateway
  • Loading branch information
mengmengmengqiang authored May 21, 2024
2 parents 46a7558 + 8a3fa17 commit 4e27f68
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.DS_Store
*~

eth-tx-parser
token-gateway

# Output of tools: coverage tool, editor ...
*.swp
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# eth-tx-parser
# token-gateway
[Design Doc](https://renaissancelabs101.notion.site/Ethereum-Homework-62f3463b94ad413b8a445fb5d3bcbb9e?pvs=4)

![infra.png](infra.png)
4 changes: 4 additions & 0 deletions conf/prod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"ENV": "prod",
"ETHJSONRPCURL": "${ETHJSONRPCURL}"
}
4 changes: 4 additions & 0 deletions conf/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"ENV": "test",
"ETHJSONRPCURL": "${ETHJSONRPCURL}"
}
4 changes: 4 additions & 0 deletions conf/ut.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"ENV": "test",
"ETHJSONRPCURL": "${ETHJSONRPCURL}"
}
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
module github.com/sugarshop/eth-tx-parser
module github.com/sugarshop/token-gateway

go 1.18

require (
github.com/gin-gonic/gin v1.10.0
github.com/sugarshop/env v1.0.1
github.com/tj/assert v0.0.3
)

require (
github.com/bitly/go-simplejson v0.5.0 // indirect
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
Expand All @@ -21,12 +23,14 @@ require (
github.com/goccy/go-json v0.10.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
github.com/bitly/go-simplejson v0.5.0 h1:6IH+V8/tVMab511d5bn4M7EwGXZf9Hj6i2xSwkNEM+Y=
github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngEKAMDJEczWVA=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY=
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
Expand All @@ -6,6 +9,7 @@ github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -32,6 +36,9 @@ github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa02
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
Expand All @@ -45,6 +52,8 @@ github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand All @@ -58,6 +67,8 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/sugarshop/env v1.0.1 h1:ToyeNDj0/DrTVrdpK+pM6l4rF5+HEzq9HEM9+PISOqI=
github.com/sugarshop/env v1.0.1/go.mod h1:TVOObQ3eH+TE0m1v85oFzcbi2rE8JPC1irxK+4tz2JE=
github.com/tj/assert v0.0.3 h1:Df/BlaZ20mq6kuai7f5z2TvPFiwC3xaWJSDQNiIS3Rk=
github.com/tj/assert v0.0.3/go.mod h1:Ne6X72Q+TB1AteidzQncjw9PabbMp4PBMZ1k+vd1Pvk=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
Expand Down
4 changes: 2 additions & 2 deletions handler/eth_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package handler
import (
"errors"
"github.com/gin-gonic/gin"
"github.com/sugarshop/eth-tx-parser/service"
"github.com/sugarshop/eth-tx-parser/util"
"github.com/sugarshop/token-gateway/service"
"github.com/sugarshop/token-gateway/util"
"log"
"strings"
)
Expand Down
2 changes: 1 addition & 1 deletion handler/json_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package handler

import (
"github.com/gin-gonic/gin"
"github.com/sugarshop/eth-tx-parser/model"
"github.com/sugarshop/token-gateway/model"
"net/http"
)

Expand Down
20 changes: 15 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
package main

import (
"flag"
"net/http"
"os"
"os/signal"
"syscall"

"github.com/gin-gonic/gin"
"github.com/sugarshop/eth-tx-parser/handler"
"github.com/sugarshop/eth-tx-parser/mw"
"github.com/sugarshop/eth-tx-parser/remote"
"github.com/sugarshop/eth-tx-parser/service"
"github.com/sugarshop/env"
"github.com/sugarshop/token-gateway/handler"
"github.com/sugarshop/token-gateway/mw"
"github.com/sugarshop/token-gateway/remote"
"github.com/sugarshop/token-gateway/service"
)

func main() {
func main() {
// start config
var conf string
flag.StringVar(&conf, "conf", "conf/test.json", "specify the load config file")
flag.Parse()

// load env configuration
env.LoadGlobalEnv(conf)

engine := gin.New()
engine.Use(mw.ParseFormMiddleware)
engine.GET("/ping", func(c *gin.Context) {
Expand Down
3 changes: 0 additions & 3 deletions model/eth_json_rpc.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package model

// ETHJsonRpcUrl todo: fill in your API KEY
const ETHJsonRpcUrl = "https://mainnet.infura.io/v3/YOUR-API-KEY"

// JSONRPCRequest represents the structure of the JSON-RPC request
type JSONRPCRequest struct {
JSONRPC string `json:"jsonrpc"`
Expand Down
2 changes: 1 addition & 1 deletion mw/parse_from_mw.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"bytes"
"github.com/gin-gonic/gin"
"github.com/sugarshop/eth-tx-parser/util"
"github.com/sugarshop/token-gateway/util"
)

// ParseFormMiddleware parse form, such as device
Expand Down
2 changes: 1 addition & 1 deletion remote/remote_init.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package remote

func Init() {
RPCServiceInstance()
ETHRPCServiceInstance()
}
41 changes: 25 additions & 16 deletions remote/rpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,38 @@ import (
"strconv"
"sync"

"github.com/sugarshop/eth-tx-parser/model"
"github.com/sugarshop/env"
"github.com/sugarshop/token-gateway/model"
)

// RPCService ETH RPC service.
type RPCService struct {}
// ETHRPCService ETH RPC service.
type ETHRPCService struct {
ethJsonRPCURL string
}

var (
rPCServiceInstance *RPCService
rPCServiceOnce sync.Once
ethRPCServiceInstance *ETHRPCService
ethRPCServiceOnce sync.Once
)

// RPCServiceInstance RPCService singleton
func RPCServiceInstance() *RPCService {
rPCServiceOnce.Do(func() {
rPCServiceInstance = &RPCService{}
// ETHRPCServiceInstance ETHRPCService singleton
func ETHRPCServiceInstance() *ETHRPCService {
url, ok := env.GlobalEnv().Get("ETHJSONRPCURL")
if !ok {
panic("no ETHJSONRPCURL env set")
}

ethRPCServiceOnce.Do(func() {
ethRPCServiceInstance = &ETHRPCService{
ethJsonRPCURL: url,
}
})

return rPCServiceInstance
return ethRPCServiceInstance
}

// ETHBlockDecimalNumber return the decimal number of the most recent block.
func (s *RPCService) ETHBlockDecimalNumber(ctx context.Context) (int64, error) {
func (s *ETHRPCService) ETHBlockDecimalNumber(ctx context.Context) (int64, error) {
hexStr, err := s.EthBlockNumber(ctx)
if err != nil {
log.Println(ctx, "[ETHBlockDecimalNumber]: Error EthBlockNumber request:", err)
Expand All @@ -52,7 +62,7 @@ func (s *RPCService) ETHBlockDecimalNumber(ctx context.Context) (int64, error) {
}

// EthBlockNumber returns the number of the most recent block.
func (s *RPCService) EthBlockNumber(ctx context.Context) (string, error) {
func (s *ETHRPCService) EthBlockNumber(ctx context.Context) (string, error) {
request := &model.JSONRPCRequest{
JSONRPC: "2.0",
Method: "eth_blockNumber",
Expand All @@ -78,7 +88,7 @@ func (s *RPCService) EthBlockNumber(ctx context.Context) (string, error) {
}

// EthGetBlockByNumber returns information about a block by number.
func (s *RPCService) EthGetBlockByNumber(ctx context.Context, number string) (*model.ETHBlockInfo, error) {
func (s *ETHRPCService) EthGetBlockByNumber(ctx context.Context, number string) (*model.ETHBlockInfo, error) {
request := &model.JSONRPCRequest{
JSONRPC: "2.0",
Method: "eth_getBlockByNumber",
Expand Down Expand Up @@ -106,16 +116,15 @@ func (s *RPCService) EthGetBlockByNumber(ctx context.Context, number string) (*m
return blockInfo, nil
}

func (s *RPCService) httpJsonRPCPOST(ctx context.Context, request *model.JSONRPCRequest) ([]byte, error) {
func (s *ETHRPCService) httpJsonRPCPOST(ctx context.Context, request *model.JSONRPCRequest) ([]byte, error) {
jsonData, err := json.Marshal(request)
if err != nil {
log.Println(ctx, "[httpJsonRPCPOST]: Error marshaling request:", err)
return nil, err
}

// create HTTP POST request
url := model.ETHJsonRpcUrl
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
req, err := http.NewRequest("POST", s.ethJsonRPCURL, bytes.NewBuffer(jsonData))
if err != nil {
log.Println(ctx, "[httpJsonRPCPOST]: Error creating request:", err)
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions remote/rpc_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestRPCService_EthBlockNumber(t *testing.T) {
ctx := context.Background()
hexStr, err := RPCServiceInstance().EthBlockNumber(ctx)
hexStr, err := ETHRPCServiceInstance().EthBlockNumber(ctx)
assert.Nil(t, err)
assert.NotEqual(t, hexStr, "")
assert.Condition(t, func() (success bool) {
Expand All @@ -19,18 +19,18 @@ func TestRPCService_EthBlockNumber(t *testing.T) {

func TestRPCService_EthGetBlockByNumber(t *testing.T) {
ctx := context.Background()
hexStr, err := RPCServiceInstance().EthBlockNumber(ctx)
hexStr, err := ETHRPCServiceInstance().EthBlockNumber(ctx)
assert.Nil(t, err)
assert.NotEqual(t, hexStr, "")
resp, err := RPCServiceInstance().EthGetBlockByNumber(ctx, hexStr)
resp, err := ETHRPCServiceInstance().EthGetBlockByNumber(ctx, hexStr)
assert.Nil(t, err)
assert.NotNil(t, resp)
assert.Equal(t, resp.Number, hexStr)
}

func TestRPCService_ETHBlockDecimalNumber(t *testing.T) {
ctx := context.Background()
hexStr, err := RPCServiceInstance().ETHBlockDecimalNumber(ctx)
hexStr, err := ETHRPCServiceInstance().ETHBlockDecimalNumber(ctx)
assert.Nil(t, err)
assert.NotNil(t, hexStr, 0)
}
14 changes: 7 additions & 7 deletions service/eth_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"sync"
"time"

"github.com/sugarshop/eth-tx-parser/model"
"github.com/sugarshop/eth-tx-parser/remote"
"github.com/sugarshop/token-gateway/model"
"github.com/sugarshop/token-gateway/remote"
)

// ETHService ETH Transactions data parser service.
Expand All @@ -34,7 +34,7 @@ func ETHServiceInstance() *ETHService {
transactions: map[string][]*model.ETHTransaction{},
}
ctx := context.Background()
dec, err := remote.RPCServiceInstance().ETHBlockDecimalNumber(ctx)
dec, err := remote.ETHRPCServiceInstance().ETHBlockDecimalNumber(ctx)
if err != nil {
log.Panicln(ctx, "[ETHServiceInstance]: Panic, Error ETHBlockDecimalNumber, err: ", err)
}
Expand All @@ -57,12 +57,12 @@ func ETHServiceInstance() *ETHService {

// GetCurrentBlock get current block.
func (s *ETHService) GetCurrentBlock(ctx context.Context) (*model.ETHBlockInfo, error) {
num, err := remote.RPCServiceInstance().EthBlockNumber(ctx)
num, err := remote.ETHRPCServiceInstance().EthBlockNumber(ctx)
if err != nil {
log.Println(ctx, "[GetCurrentBlock]: Error EthBlockNumber, err: ", err)
return nil, err
}
blockInfo, err := remote.RPCServiceInstance().EthGetBlockByNumber(ctx, num)
blockInfo, err := remote.ETHRPCServiceInstance().EthGetBlockByNumber(ctx, num)
if err != nil {
log.Println(ctx, "[GetCurrentBlock]: Error EthGetBlockByNumber, err: ", err)
return nil, err
Expand Down Expand Up @@ -94,7 +94,7 @@ func (s *ETHService) GetTransactions(ctx context.Context, address string) ([]*mo
// load load transactions via address.
func (s *ETHService) load(ctx context.Context) error {
// 1. query new block number.
num, err := remote.RPCServiceInstance().ETHBlockDecimalNumber(ctx)
num, err := remote.ETHRPCServiceInstance().ETHBlockDecimalNumber(ctx)
if err != nil {
log.Println(ctx, "[load]: Error EthBlockNumber request:", err)
return err
Expand All @@ -119,7 +119,7 @@ func (s *ETHService) load(ctx context.Context) error {
// ParseTransactions parse block transactions.
func (s *ETHService) ParseTransactions(ctx context.Context, number int64) error {
hexStr := fmt.Sprintf("0x%x", number)
blockInfo, err := remote.RPCServiceInstance().EthGetBlockByNumber(ctx, hexStr)
blockInfo, err := remote.ETHRPCServiceInstance().EthGetBlockByNumber(ctx, hexStr)
if err != nil {
log.Println(ctx, "[ParseTransactions]: Error EthGetBlockByNumber request:", err)
return err
Expand Down

0 comments on commit 4e27f68

Please sign in to comment.