Skip to content
This repository has been archived by the owner on Mar 16, 2022. It is now read-only.

Commit

Permalink
[Smart Chain] Add config (#1206)
Browse files Browse the repository at this point in the history
* Add smart chain config

* update bsc sample config
  • Loading branch information
hewigovens authored Aug 31, 2020
1 parent e90b7df commit b573ace
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 7 deletions.
18 changes: 17 additions & 1 deletion coin/coins.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions coin/coins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,12 @@
decimals: 18
blockTime: 6000
sampleAddress: 'erd12tqtt5zcg6vpw65y4hkanvt49kzq695sr3ctuszjy92xw0ppzcssy2xd5r'

- id: 10000714
symbol: BNB
preferedSymbol: BSC
handle: bsc
name: 'Binance Smart Chain'
decimals: 18
blockTime: 3000
sampleAddress: '0x3efb67b34a9b69b54d4027e044954f483dc31678'
24 changes: 21 additions & 3 deletions coin/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
package main

import (
"gopkg.in/yaml.v2"
"html/template"
"log"
"os"
"strings"
"time"

"gopkg.in/yaml.v2"
)

const (
Expand All @@ -32,6 +33,7 @@ type Coin struct {
ID uint
Handle string
Symbol string
PreferedSymbol string
Name string
Decimals uint
BlockTime int
Expand All @@ -45,16 +47,27 @@ func (c *Coin) String() string {
const (
{{- range .Coins }}
{{- if .PreferedSymbol}}
{{ .PreferedSymbol }} = {{ .ID }}
{{- else}}
{{ .Symbol }} = {{ .ID }}
{{- end}}
{{- end }}
)
var Coins = map[uint]Coin{
{{- range .Coins }}
{{- if .PreferedSymbol }}
{{ .PreferedSymbol }}: {
{{- else }}
{{ .Symbol }}: {
{{- end }}
ID: {{.ID}},
Handle: "{{.Handle}}",
Symbol: "{{.Symbol}}",
{{- if .PreferedSymbol }}
PreferedSymbol: "{{.PreferedSymbol}}",
{{- end }}
Name: "{{.Name}}",
Decimals: {{.Decimals}},
BlockTime: {{.BlockTime}},
Expand All @@ -65,8 +78,12 @@ var Coins = map[uint]Coin{
}
{{- range .Coins }}
func {{ .Handle.Upper }}() Coin {
func {{ .Handle.Capitalize }}() Coin {
{{- if .PreferedSymbol }}
return Coins[{{ .PreferedSymbol }}]
{{- else }}
return Coins[{{ .Symbol }}]
{{- end}}
}
{{- end }}
Expand All @@ -76,14 +93,15 @@ func {{ .Handle.Upper }}() Coin {

type Handle string

func (h Handle) Upper() string {
func (h Handle) Capitalize() string {
return strings.Title(string(h))
}

type Coin struct {
ID uint `yaml:"id"`
Handle Handle `yaml:"handle"`
Symbol string `yaml:"symbol"`
PreferedSymbol string `yaml:"preferedSymbol,omitempty"`
Name string `yaml:"name"`
Decimals uint `yaml:"decimals"`
BlockTime int `yaml:"blockTime"`
Expand Down
14 changes: 11 additions & 3 deletions coin/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package coin

import (
"fmt"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)

const (
Expand All @@ -19,6 +20,7 @@ type TestCoin struct {
ID uint `yaml:"id"`
Handle string `yaml:"handle"`
Symbol string `yaml:"symbol"`
PreferedSymbol string `yaml:"preferedSymbol,omitempty"`
Name string `yaml:"name"`
Decimals uint `yaml:"decimals"`
BlockTime int `yaml:"blockTime"`
Expand Down Expand Up @@ -69,8 +71,14 @@ func TestCoinFile(t *testing.T) {
s := strings.Title(want.Handle)
method := fmt.Sprintf("func %s() Coin", s)
assert.True(t, strings.Contains(code, method), "Coin method not found")
enum := fmt.Sprintf("%s = %d", want.Symbol, want.ID)
var enum string
if want.PreferedSymbol != "" {
enum = fmt.Sprintf("%s = %d", want.PreferedSymbol, want.ID)
} else {
enum = fmt.Sprintf("%s = %d", want.Symbol, want.ID)
}
assert.True(t, strings.Contains(code, enum), "Coin enum not found")

}
}

Expand Down
4 changes: 4 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,7 @@ near:

elrond:
api: https://api.elrond.com

# bsc:
# api: <blockbook api>
# rpc: <rpc>
2 changes: 2 additions & 0 deletions platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package platform

import (
"fmt"

"github.com/trustwallet/blockatlas/platform/kava"

"github.com/spf13/viper"
Expand Down Expand Up @@ -107,6 +108,7 @@ func getAllHandlers() blockatlas.Platforms {
coin.Callisto().Handle: ethereum.Init(coin.CLO, GetApiVar(coin.CLO), GetRpcVar(coin.CLO)),
coin.Wanchain().Handle: ethereum.Init(coin.WAN, GetApiVar(coin.WAN), GetRpcVar(coin.WAN)),
coin.Tomochain().Handle: ethereum.Init(coin.TOMO, GetApiVar(coin.TOMO), GetRpcVar(coin.TOMO)),
coin.Bsc().Handle: ethereum.InitWithBlockbook(coin.BSC, GetApiVar(coin.BSC), GetRpcVar(coin.BSC)),
coin.Ethereum().Handle: ethereum.InitWitCollection(coin.ETH, GetApiVar(coin.ETH), GetRpcVar(coin.ETH), GetVar("ethereum.blockbook_api"), GetVar("ethereum.collections_api"), GetVar("ethereum.collections_api_key")),
coin.Near().Handle: near.Init(GetApiVar(coin.NEAR)),
coin.Elrond().Handle: elrond.Init(coin.ERD, GetApiVar(coin.ERD)),
Expand Down

0 comments on commit b573ace

Please sign in to comment.