Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/chaincode #87

Merged
merged 9 commits into from
Dec 6, 2023
Merged
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
26 changes: 14 additions & 12 deletions blockchain/chain/gw.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,44 @@ import (
"github.com/0xcregis/easynode/blockchain/chain/polygonpos"
"github.com/0xcregis/easynode/blockchain/chain/tron"
"github.com/0xcregis/easynode/blockchain/chain/xrp"
"github.com/0xcregis/easynode/common/chain"
"github.com/sunjiangjun/xlog"
)

func NewChain(blockchain int64) blockchain.ChainConn {
if blockchain == 200 {
func NewChain(blockchain int64, log *xlog.XLog) blockchain.ChainConn {
if chain.GetChainCode(blockchain, "ETH", log) {
//eth
return ether.NewChainClient()
} else if blockchain == 205 {
} else if chain.GetChainCode(blockchain, "TRON", log) {
//tron
return tron.NewChainClient()
} else if blockchain == 201 {
} else if chain.GetChainCode(blockchain, "POLYGON", log) {
//polygon-pos
return polygonpos.NewChainClient()
} else if blockchain == 202 {
} else if chain.GetChainCode(blockchain, "BSC", log) {
//bnb
return bnb.NewChainClient()
} else if blockchain == 301 {
} else if chain.GetChainCode(blockchain, "FIL", log) {
//file-coin
return filecoin.NewChainClient()
} else if blockchain == 300 {
} else if chain.GetChainCode(blockchain, "BTC", log) {
//btc
return btc.NewChainClient()
} else if blockchain == 310 {
} else if chain.GetChainCode(blockchain, "XRP", log) {
//xrp
return xrp.NewChainClient()
} else {
return nil
}
}

func NewNFT(blockchain int64) blockchain.NFT {
if blockchain == 200 {
func NewNFT(blockchain int64, log *xlog.XLog) blockchain.NFT {
if chain.GetChainCode(blockchain, "ETH", log) {
//eth
return ether.NewNFTClient()
} else if blockchain == 201 {
} else if chain.GetChainCode(blockchain, "POLYGON", log) {
return polygonpos.NewNFTClient()
} else if blockchain == 202 {
} else if chain.GetChainCode(blockchain, "BSC", log) {
return bnb.NewNFTClient()
} else {
return nil
Expand Down
27 changes: 26 additions & 1 deletion blockchain/config/configutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"io"
"os"

"github.com/tidwall/gjson"
)

func LoadConfig(path string) Config {
Expand All @@ -19,6 +21,29 @@ func LoadConfig(path string) Config {
panic(err)
}
cfg := Config{}
_ = json.Unmarshal(b, &cfg)
err = json.Unmarshal(b, &cfg)
if err != nil {
panic(err)
}

list := gjson.ParseBytes(b).Get("Nodes").Array()
cfg.Cluster = make(map[int64][]*NodeCluster)
for _, v := range list {
chainCode := v.Get("BlockChain").Int()
var node NodeCluster
err := json.Unmarshal([]byte(v.String()), &node)
if err != nil {
panic(err)
}

if m, ok := cfg.Cluster[chainCode]; ok {
m = append(m, &node)
cfg.Cluster[chainCode] = m
} else {
nodeList := []*NodeCluster{&node}
cfg.Cluster[chainCode] = nodeList
}
}

return cfg
}
8 changes: 5 additions & 3 deletions blockchain/service/bnb/bnb.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/0xcregis/easynode/blockchain"
"github.com/0xcregis/easynode/blockchain/chain"
"github.com/0xcregis/easynode/blockchain/config"
"github.com/0xcregis/easynode/common/util"
"github.com/sunjiangjun/xlog"
"github.com/tidwall/gjson"
)
Expand Down Expand Up @@ -256,6 +257,7 @@ func (e *Bnb) GetBlockByNumber(chainCode int64, number string, flag bool) (strin
]
}
`
number, _ = util.Int2Hex(number)
req = fmt.Sprintf(req, number, flag)
return e.SendReq(chainCode, req)
}
Expand Down Expand Up @@ -284,12 +286,12 @@ func (e *Bnb) SendJsonRpc(chainCode int64, req string) (string, error) {
}

func NewNftBnb(cluster []*config.NodeCluster, blockchain int64, xlog *xlog.XLog) blockchain.NftApi {
nftClient := chain.NewNFT(blockchain)
nftClient := chain.NewNFT(blockchain, xlog)
if nftClient == nil {
return nil
}

chain.NewNFT(blockchain)
chain.NewNFT(blockchain, xlog)
e := &Bnb{
log: xlog,
nodeCluster: cluster,
Expand All @@ -300,7 +302,7 @@ func NewNftBnb(cluster []*config.NodeCluster, blockchain int64, xlog *xlog.XLog)
}

func NewBnb(cluster []*config.NodeCluster, blockchain int64, xlog *xlog.XLog) blockchain.API {
blockChainClient := chain.NewChain(blockchain)
blockChainClient := chain.NewChain(blockchain, xlog)
if blockChainClient == nil {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion blockchain/service/btc/btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (e *Btc) SendJsonRpc(chainCode int64, req string) (string, error) {
}

func NewBtc(cluster []*config.NodeCluster, blockchain int64, xlog *xlog.XLog) blockchain.API {
blockChainClient := chain.NewChain(blockchain)
blockChainClient := chain.NewChain(blockchain, xlog)
if blockChainClient == nil {
return nil
}
Expand Down
9 changes: 6 additions & 3 deletions blockchain/service/ether/ether.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/0xcregis/easynode/blockchain"
"github.com/0xcregis/easynode/blockchain/chain"
"github.com/0xcregis/easynode/blockchain/config"
"github.com/0xcregis/easynode/common/util"
"github.com/gorilla/websocket"
"github.com/sunjiangjun/xlog"
"github.com/tidwall/gjson"
Expand Down Expand Up @@ -275,6 +276,8 @@ func (e *Ether) GetBlockByNumber(chainCode int64, number string, flag bool) (str
]
}
`

number, _ = util.Int2Hex(number)
req = fmt.Sprintf(req, number, flag)
return e.SendReq(chainCode, req)
}
Expand Down Expand Up @@ -303,12 +306,12 @@ func (e *Ether) SendJsonRpc(chainCode int64, req string) (string, error) {
}

func NewNftEth(cluster []*config.NodeCluster, blockchain int64, xlog *xlog.XLog) blockchain.NftApi {
nftClient := chain.NewNFT(blockchain)
nftClient := chain.NewNFT(blockchain, xlog)
if nftClient == nil {
return nil
}

chain.NewNFT(blockchain)
chain.NewNFT(blockchain, xlog)
e := &Ether{
log: xlog,
nodeCluster: cluster,
Expand All @@ -319,7 +322,7 @@ func NewNftEth(cluster []*config.NodeCluster, blockchain int64, xlog *xlog.XLog)
}

func NewEth(cluster []*config.NodeCluster, blockchain int64, xlog *xlog.XLog) blockchain.API {
blockChainClient := chain.NewChain(blockchain)
blockChainClient := chain.NewChain(blockchain, xlog)
if blockChainClient == nil {
return nil
}
Expand Down
10 changes: 10 additions & 0 deletions blockchain/service/ether/ether_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ func TestEther_TokenBalance(t *testing.T) {
}
}

func TestEther_Nonce(t *testing.T) {
s := Init()
resp, err := s.Nonce(200, "0xae2Fc483527B8EF99EB5D9B44875F005ba1FaE13", "latest")
if err != nil {
t.Error(err)
} else {
t.Log(resp)
}
}

func TestEther_GetLatestBlock(t *testing.T) {
s := Init()
resp, err := s.LatestBlock(200)
Expand Down
2 changes: 1 addition & 1 deletion blockchain/service/filecoin/filecoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (e *FileCoin) Token(chainCode int64, contractAddr string, abi string, eip s
}

func NewFileCoin(cluster []*config.NodeCluster, blockchain int64, xlog *xlog.XLog) blockchain.API {
blockChainClient := chain.NewChain(blockchain)
blockChainClient := chain.NewChain(blockchain, xlog)
if blockChainClient == nil {
return nil
}
Expand Down
21 changes: 11 additions & 10 deletions blockchain/service/gw.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,35 @@ import (
"github.com/0xcregis/easynode/blockchain/service/polygon"
"github.com/0xcregis/easynode/blockchain/service/tron"
"github.com/0xcregis/easynode/blockchain/service/xrp"
"github.com/0xcregis/easynode/common/chain"
"github.com/sunjiangjun/xlog"
)

func NewApi(blockchain int64, cluster []*config.NodeCluster, xlog *xlog.XLog) blockchain.API {
if blockchain == 200 {
if chain.GetChainCode(blockchain, "ETH", xlog) {
return ether.NewEth(cluster, blockchain, xlog)
} else if blockchain == 205 {
} else if chain.GetChainCode(blockchain, "TRON", xlog) {
return tron.NewTron(cluster, blockchain, xlog)
} else if blockchain == 201 {
} else if chain.GetChainCode(blockchain, "POLYGON", xlog) {
return polygon.NewPolygonPos(cluster, blockchain, xlog)
} else if blockchain == 202 {
} else if chain.GetChainCode(blockchain, "BSC", xlog) {
return bnb.NewBnb(cluster, blockchain, xlog)
} else if blockchain == 300 {
} else if chain.GetChainCode(blockchain, "BTC", xlog) {
return btc.NewBtc(cluster, blockchain, xlog)
} else if blockchain == 301 {
} else if chain.GetChainCode(blockchain, "FIL", xlog) {
return filecoin.NewFileCoin(cluster, blockchain, xlog)
} else if blockchain == 310 {
} else if chain.GetChainCode(blockchain, "XRP", xlog) {
return xrp.NewXRP(cluster, blockchain, xlog)
}
return nil
}

func NewNftApi(blockchain int64, cluster []*config.NodeCluster, xlog *xlog.XLog) blockchain.NftApi {
if blockchain == 200 {
if chain.GetChainCode(blockchain, "ETH", xlog) {
return ether.NewNftEth(cluster, blockchain, xlog)
} else if blockchain == 201 {
} else if chain.GetChainCode(blockchain, "POLYGON", xlog) {
return polygon.NewNftPolygonPos(cluster, blockchain, xlog)
} else if blockchain == 202 {
} else if chain.GetChainCode(blockchain, "BSC", xlog) {
return bnb.NewNftBnb(cluster, blockchain, xlog)
}
return nil
Expand Down
Loading
Loading