-
Notifications
You must be signed in to change notification settings - Fork 13
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
Add support for Binance Chain #17
Open
susruth
wants to merge
28
commits into
master
Choose a base branch
from
feat/binance-chain
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 26 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
9ff8063
types/bnctypes: add BinanceChain tx support
susruth 98dd008
Add BinanceChain and Tendermint dependencies
susruth 1346d93
types: add Binance Chain
susruth 0afe4dc
types/bnctypes: add Address
susruth 04b7864
types/bnctypes: add binance chain networks
susruth d09cb3c
sdk/client/bncclient: add basic binance client
susruth 33e9bc9
updated dependencies
susruth 43baab7
sdk/client/bncclient: add basic test for bncclient
susruth a091ed7
Merge branch 'master' into feat/binance-chain
susruth 63f5299
types/bnctypes: updated AddressFromBech32
susruth 54638e6
sdk/client/bncclient: add OpenOrder
susruth 3250974
updated go modules
susruth 90d7f87
sdk/client/bncclient: fixed binance base urls
susruth 47961aa
updated go modules
susruth 6646f65
resolved conflicts with master
susruth 34cbaa8
resolved conflicts with master
susruth 8f47640
resolved merge conflicts with master
susruth 08637f6
types/bnctypes: updated to use binance sdk for address en/decoding
susruth d23c66e
sdk/client/bncclient: fixed to use the correct ChainID
susruth 7c5bd37
updated to use the latest Ginkgo
susruth 32679e9
types/bnctypes: add NewCoins
susruth 564887e
sdk/client/bncclient: fixed balances function
susruth 76f8cc4
Merge branch 'master' into feat/binance-chain
susruth 2f4add5
testutil/btcaccount: updated to use fixed fee
susruth ec2ec32
sdk/client/bncclient: add mint and burn requests
susruth a9fce1b
types: add stringer impl for Binance
susruth 8e1f6ed
types/bnctypes: updated to use local address string conv
susruth 16227a7
go mod tidy
susruth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package bncclient | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"github.com/binance-chain/go-sdk/client/basic" | ||
"github.com/binance-chain/go-sdk/client/query" | ||
"github.com/binance-chain/go-sdk/client/websocket" | ||
"github.com/binance-chain/go-sdk/types/msg" | ||
"github.com/binance-chain/go-sdk/types/tx" | ||
"github.com/renproject/mercury/types" | ||
"github.com/renproject/mercury/types/bnctypes" | ||
) | ||
|
||
type Client interface { | ||
PrintTime() | ||
Balances(from bnctypes.Address) (bnctypes.Coins, error) | ||
Mint(from bnctypes.Address, coin bnctypes.Coin) (types.Tx, error) | ||
Burn(from bnctypes.Address, coin bnctypes.Coin) (types.Tx, error) | ||
Send(from bnctypes.Address, recipients bnctypes.Recipients) (types.Tx, error) | ||
SubmitTx(tx types.Tx) error | ||
} | ||
|
||
type client struct { | ||
network bnctypes.Network | ||
queryClient query.QueryClient | ||
basicClient basic.BasicClient | ||
wsClient websocket.WSClient | ||
} | ||
|
||
func New(network bnctypes.Network) Client { | ||
var baseURL string | ||
switch network { | ||
case bnctypes.Mainnet: | ||
baseURL = "dex.binance.org" | ||
case bnctypes.Testnet: | ||
baseURL = "testnet-dex.binance.org" | ||
default: | ||
panic(types.ErrUnknownNetwork) | ||
} | ||
|
||
c := basic.NewClient(baseURL) | ||
return &client{ | ||
network: network, | ||
basicClient: c, | ||
queryClient: query.NewClient(c), | ||
wsClient: websocket.NewClient(c), | ||
} | ||
} | ||
|
||
func (client *client) PrintTime() { | ||
t, err := client.queryClient.GetTime() | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
fmt.Println(t.ApTime, t.BlockTime) | ||
} | ||
|
||
func (client *client) Mint(from bnctypes.Address, coin bnctypes.Coin) (types.Tx, error) { | ||
return client.BuildTx(from, msg.NewMintMsg( | ||
from.AccAddress(), | ||
coin.Denom, | ||
coin.Amount, | ||
)) | ||
} | ||
|
||
func (client *client) Burn(from bnctypes.Address, coin bnctypes.Coin) (types.Tx, error) { | ||
return client.BuildTx(from, msg.NewTokenBurnMsg( | ||
from.AccAddress(), | ||
coin.Denom, | ||
coin.Amount, | ||
)) | ||
} | ||
|
||
func (client *client) Balances(from bnctypes.Address) (bnctypes.Coins, error) { | ||
acc, err := client.queryClient.GetAccount(from.String()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
balances := make([]bnctypes.Coin, len(acc.Balances)) | ||
for i := range balances { | ||
balances[i] = bnctypes.Coin{ | ||
Denom: acc.Balances[i].Symbol, | ||
Amount: acc.Balances[i].Free.ToInt64(), | ||
} | ||
} | ||
return bnctypes.NewCoins(balances...), nil | ||
} | ||
|
||
func (client *client) Send(from bnctypes.Address, recipients bnctypes.Recipients) (types.Tx, error) { | ||
return client.BuildTx(from, recipients.SendTx(from)) | ||
} | ||
|
||
func (client *client) BuildTx(from bnctypes.Address, m msg.Msg) (types.Tx, error) { | ||
acc, err := client.queryClient.GetAccount(from.String()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// prepare message to sign | ||
signMsg := tx.StdSignMsg{ | ||
ChainID: client.network.ChainID(), | ||
AccountNumber: acc.Number, | ||
Sequence: acc.Sequence, | ||
Memo: "", | ||
Msgs: []msg.Msg{m}, | ||
Source: tx.Source, | ||
} | ||
|
||
// special logic for createOrder, to save account query | ||
if orderMsg, ok := m.(msg.CreateOrderMsg); ok { | ||
orderMsg.ID = msg.GenerateOrderID(signMsg.Sequence+1, from.AccAddress()) | ||
signMsg.Msgs[0] = orderMsg | ||
} | ||
|
||
// validate messages | ||
for _, m := range signMsg.Msgs { | ||
if err := m.ValidateBasic(); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return bnctypes.NewTx(signMsg), nil | ||
} | ||
|
||
func (client *client) SubmitTx(tx types.Tx) error { | ||
stx, err := tx.Serialize() | ||
if err != nil { | ||
return err | ||
} | ||
params := map[string]string{} | ||
params["sync"] = "true" | ||
if _, err := client.basicClient.PostTx([]byte(hex.EncodeToString(stx)), params); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package bncclient_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestBncclient(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Bncclient Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package bncclient_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/ethereum/go-ethereum/crypto" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
. "github.com/renproject/mercury/sdk/client/bncclient" | ||
"github.com/renproject/mercury/types/bnctypes" | ||
) | ||
|
||
var _ = Describe("bnc client", func() { | ||
Context("when communicating with the testnet", func() { | ||
It("should print the current time", func() { | ||
client := New(bnctypes.Testnet) | ||
client.PrintTime() | ||
}) | ||
|
||
It("should get the correct balance of an address", func() { | ||
privKey, err := crypto.HexToECDSA(os.Getenv("BNB_PRIVATE_KEY")) | ||
Expect(err).Should(BeNil()) | ||
address := bnctypes.AddressFromPubKey(privKey.PublicKey, bnctypes.Testnet) | ||
Expect(err).Should(BeNil()) | ||
fmt.Println(address) | ||
client := New(bnctypes.Testnet) | ||
client.Balances(bnctypes.Address(address)) | ||
}) | ||
|
||
It("should transfer 0.001", func() { | ||
privKey, err := crypto.HexToECDSA(os.Getenv("BNB_PRIVATE_KEY")) | ||
Expect(err).Should(BeNil()) | ||
address := bnctypes.AddressFromPubKey(privKey.PublicKey, bnctypes.Testnet) | ||
Expect(err).Should(BeNil()) | ||
client := New(bnctypes.Testnet) | ||
tx, err := client.Send(address, bnctypes.Recipients{bnctypes.NewRecipent(address, bnctypes.NewBNBCoin(1000000))}) | ||
Expect(err).Should(BeNil()) | ||
tx.Sign(privKey) | ||
Expect(client.SubmitTx(tx)).Should(BeNil()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package bnctypes | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"encoding/hex" | ||
|
||
"github.com/binance-chain/go-sdk/common/types" | ||
"github.com/binance-chain/go-sdk/types/msg" | ||
"github.com/btcsuite/btcd/btcec" | ||
"github.com/btcsuite/btcutil" | ||
) | ||
|
||
type Recipient struct { | ||
Address Address | ||
Coins Coins | ||
} | ||
|
||
type Recipients []Recipient | ||
|
||
type Address struct { | ||
Network Network | ||
PubKeyHash []byte | ||
} | ||
|
||
func (recipients Recipients) SendTx(from Address) msg.SendMsg { | ||
types.Network = from.Network.ChainNetwork() | ||
var inputCoins types.Coins | ||
ops := make([]msg.Output, len(recipients)) | ||
for i, recipient := range recipients { | ||
ops[i] = msg.Output{ | ||
Address: recipient.Address.AccAddress(), | ||
Coins: types.Coins(recipient.Coins), | ||
} | ||
ops[i].Coins = ops[i].Coins.Sort() | ||
inputCoins = inputCoins.Plus(ops[i].Coins) | ||
} | ||
ips := []msg.Input{{Address: from.AccAddress(), Coins: inputCoins}} | ||
return msg.SendMsg{Inputs: ips, Outputs: ops} | ||
} | ||
|
||
func AddressFromBech32(address string, network Network) (Address, error) { | ||
types.Network = network.ChainNetwork() | ||
accAddr, err := types.AccAddressFromBech32(address) | ||
if err != nil { | ||
return Address{}, err | ||
} | ||
return Address{PubKeyHash: accAddr, Network: network}, nil | ||
} | ||
|
||
func AddressFromHex(address string, network Network) (Address, error) { | ||
pkh, err := hex.DecodeString(address) | ||
if err != nil { | ||
return Address{}, err | ||
} | ||
return Address{PubKeyHash: pkh, Network: network}, nil | ||
} | ||
|
||
func AddressFromPubKey(pubKey ecdsa.PublicKey, network Network) Address { | ||
pkHash := btcutil.Hash160((*btcec.PublicKey)(&pubKey).SerializeCompressed()) | ||
return Address{network, pkHash} | ||
} | ||
|
||
func (addr Address) AccAddress() types.AccAddress { | ||
types.Network = addr.Network.ChainNetwork() | ||
return types.AccAddress(addr.PubKeyHash) | ||
} | ||
|
||
func (addr Address) String() string { | ||
types.Network = addr.Network.ChainNetwork() | ||
return types.AccAddress(addr.PubKeyHash).String() | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is dangerous to change the global variable of the package. Maybe reimplement the function here along with the type you need.