Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
papa-stiflera committed Apr 16, 2020
0 parents commit 812fc3b
Show file tree
Hide file tree
Showing 14 changed files with 1,542 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# OS
.DS_Store
*.swp
*.swo
.vscode
.idea

# Build
vendor
.vendor-new
build
.build/swagger

# IDE
/.idea/
*.iml

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright © 2020 Wings Stiftung

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
### Warning! ALPHA software!

The dnode-client-go library is designed to make it easier for developers to send transactions and make queries to the dfinance blockchain network.

This library is still under heavy development and is not intended for production use.

The library now contains the minimum methods needed to sign and send transactions to the network.
The full list of available methods will be available later.

### Example
A simple example of use: sending asset prices to the oracle module.
To work with this example, you need to have dnode installed.
The installation process is described in the document at: https://github.com/dfinance/dnode#installation.


After installation, just start the dnode daemon:
```$ dnode start``` and REST-server ```$ dncli rest-server```


```Go
package main

import (
"bufio"
"os"

"github.com/cosmos/cosmos-sdk/x/auth"
sdkcli "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/crypto/keys"

dncl"github.com/dfinance/dnode-client-go"
)

func main() {
accountName := "your-account-name"
// make Keybase from dncli context.
kb, err := keys.NewKeyring("dfinance", keys.BackendOS, os.ExpandEnv("$HOME/.dncli"), bufio.NewReader(os.Stdin))
if err != nil {
panic(err)
}

// make TxBuilder to sign transactions
txb := auth.TxBuilder{}.
WithKeybase(kb).
WithChainID("your-chain-id").
WithFees("your-fees").
WithGas(200000)

// make dnode client
apiCl := dncl.New(
dncl.WithTxBuilder(txb),
dncl.WithAccountName(accountName),
dncl.WithPassphrase("sccount-passphrase"),
)

// get your account information from the network
keyInfo, err := kb.Get(accountName)
if err != nil {
panic(err)
}
acc, err := apiCl.Auth().Account(keyInfo.GetAddress())
if err != nil {
panic(err)
}

result, err := apiCl.WithAccount(acc).Oracle().PostPrices([]MsgPostPrice{
{
From: ki.GetAddress(),
AssetCode: "eth_dfi",
Price: sdk.NewInt(1000000),
ReceivedAt: time.Now(),
},
{
From: ki.GetAddress(),
AssetCode: "eth_dfi",
Price: sdk.NewInt(1200000),
ReceivedAt: time.Now(),
},
})
}
```

### TODO

- [ ] A lot of tests
- [ ] Methods available for calling but not yet implemented in this library
38 changes: 38 additions & 0 deletions auth_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package client

import (
"fmt"

"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/auth"
)

// AuthClient provides a set of methods for the Auth module
type AuthClient struct {
dc DnodeClient
}

// Account returns account information including AccountNumber, Sequence and Address.
// The AccountNumber and Sequence fields are required to sign transactions.
func (c AuthClient) Account(address AccAddress) (auth.BaseAccount, error) {
resp, err := c.dc.httpcl.R().Get(fmt.Sprintf("%s/auth/accounts/%s", c.dc.nodeURL, address))
if err != nil {
return auth.BaseAccount{}, err
}

var rd rest.ResponseWithHeight
err = c.dc.cdc.UnmarshalJSON(resp.Body(), &rd)
if err != nil {
return auth.BaseAccount{}, err
}
var acc = struct {
Type string `json:"type"`
Value auth.BaseAccount `json:"value"`
}{}
err = c.dc.cdc.UnmarshalJSON(rd.Result, &acc)
if err != nil {
return auth.BaseAccount{}, err
}

return acc.Value, err
}
172 changes: 172 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package client

import (
"github.com/cosmos/cosmos-sdk/x/auth"
resty "github.com/go-resty/resty/v2"
)

// DnodeClient implements set of methods for working with dnode blockchain.
// Never create an instance with the DnodeClient{} literal!
// To create a correctly initialized instance, always use the New() function.
type DnodeClient struct {
httpcl *resty.Client
nodeURL string
cdc *Codec
txb TxBuilder
accName string
fromAddress AccAddress
passphrase string
broadcastMode string
}

// New is a constructor function for DnodeClient
func New(opts ...Option) DnodeClient {
cl := DnodeClient{
cdc: DefaultCodec(),
nodeURL: "http://127.0.0.1:1317",
httpcl: resty.New(),
broadcastMode: BroadcastBlock,
txb: auth.TxBuilder{},
}

for _, opt := range opts {
opt(&cl)
}

return cl
}

// WithAccount returns DnodeClient with AccountNumber, Sequence and FromAddress fields set.
// This is a short record of the call of the following methods WithAccountNumber(...).WithSequence(...).WithFromAddress(...).
func (c DnodeClient) WithAccount(account BaseAccount) DnodeClient {
c.txb = c.txb.WithAccountNumber(account.GetAccountNumber()).WithSequence(account.GetSequence())
c.fromAddress = account.GetAddress()
return c
}

// WithAccountNumber returns DnodeClient with the specified AccountNumber
func (c DnodeClient) WithAccountNumber(number uint64) DnodeClient {
c.txb = c.txb.WithAccountNumber(number)
return c
}

// WithChainID returns DnodeClient with the specified ChainID
func (c DnodeClient) WithChainID(chainID string) DnodeClient {
c.txb = c.txb.WithChainID(chainID)
return c
}

// WithFees returns DnodeClient with the specified Fees
func (c DnodeClient) WithFees(fees string) DnodeClient {
c.txb = c.txb.WithFees(fees)
return c
}

// WithGas returns DnodeClient with the specified gas
func (c DnodeClient) WithGas(gas uint64) DnodeClient {
c.txb = c.txb.WithGas(gas)
return c
}

// WithGasPrices returns DnodeClient with the specified gas prices
func (c DnodeClient) WithGasPrices(gasPrices string) DnodeClient {
c.txb = c.txb.WithGasPrices(gasPrices)
return c
}

// WithKeybase returns DnodeClient with the specified KeyBase
func (c DnodeClient) WithKeybase(kb Keybase) DnodeClient {
c.txb = c.txb.WithKeybase(kb)
return c
}

// WithMemo returns DnodeClient with the specified memo
func (c DnodeClient) WithMemo(memo string) DnodeClient {
c.txb = c.txb.WithMemo(memo)
return c
}

// WithSequence returns DnodeClient with the specified account sequence
func (c DnodeClient) WithSequence(seq uint64) DnodeClient {
c.txb = c.txb.WithSequence(seq)
return c
}

// WithAccountName returns DnodeClient with the specified account name
func (c DnodeClient) WithAccountName(name string) DnodeClient {
c.accName = name
return c
}

// WithPassphrase returns DnodeClient with the specified passphrase
func (c DnodeClient) WithPassphrase(phrase string) DnodeClient {
c.passphrase = phrase
return c
}

// WithBroadcastMode returns DnodeClient with the specified broadcast mode
func (c DnodeClient) WithBroadcastMode(mode TxBroadcastMode) DnodeClient {
c.broadcastMode = mode.String()
return c
}

// WithFromAddress returns DnodeClient with the specified transaction sender address
func (c DnodeClient) WithFromAddress(addr AccAddress) DnodeClient {
c.fromAddress = addr
return c
}

// Auth returns initialized AuthClient
func (c DnodeClient) Auth() AuthClient {
return AuthClient{dc: c}
}

// Currencies returns initialized CurrenciesClient
func (c DnodeClient) Currencies() CurrenciesClient {
return CurrenciesClient{dc: c}
}

// Oracle returns initialized OracleClient
func (c DnodeClient) Oracle() OracleClient {
return OracleClient{dc: c}
}

// Tx returns initialized TxClient
func (c DnodeClient) Tx() TxClient {
return TxClient{dc: c}
}

// BroadcastMode returns the current broadcast mode
func (c DnodeClient) BroadcastMode() string {
return c.broadcastMode
}

// Passphrase returns the current passphrase
func (c DnodeClient) Passphrase() string {
return c.passphrase
}

// FromAddress returns the current transaction sender address
func (c DnodeClient) FromAddress() AccAddress {
return c.fromAddress
}

// AccountName returns the current transaction sender account name
func (c DnodeClient) AccountName() string {
return c.accName
}

// NodeURL returns the current node URL
func (c DnodeClient) NodeURL() string {
return c.nodeURL
}

// Codec returns the current codec
func (c DnodeClient) Codec() *Codec {
return c.cdc
}

// TxBuilder returns the current transactions bulder
func (c DnodeClient) TxBuilder() TxBuilder {
return c.txb
}
Loading

0 comments on commit 812fc3b

Please sign in to comment.