Skip to content

Commit

Permalink
[R4R]add Timelock api; add stake message types (#40)
Browse files Browse the repository at this point in the history
* add stake message;support timelock message

* add timelock testcase

* format goimports
  • Loading branch information
unclezoro authored Jun 17, 2019
1 parent 6442ea1 commit f23da44
Show file tree
Hide file tree
Showing 11 changed files with 768 additions and 12 deletions.
90 changes: 90 additions & 0 deletions client/transaction/time_lock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package transaction

import (
"strconv"

"github.com/binance-chain/go-sdk/common/types"
"github.com/binance-chain/go-sdk/types/msg"
"github.com/binance-chain/go-sdk/types/tx"
)

type TimeLockResult struct {
tx.TxCommitResult
LockId int64 `json:"lock_id"`
}

func (c *client) TimeLock(description string, amount types.Coins, lockTime int64, sync bool, options ...Option) (*TimeLockResult, error) {
fromAddr := c.keyManager.GetAddr()

lockMsg := msg.NewTimeLockMsg(fromAddr, description, amount, lockTime)
err := lockMsg.ValidateBasic()
if err != nil {
return nil, err
}
commit, err := c.broadcastMsg(lockMsg, sync, options...)
if err != nil {
return nil, err
}
var lockId int64
if commit.Ok && sync {
lockId, err = strconv.ParseInt(string(commit.Data), 10, 64)
if err != nil {
return nil, err
}
}
return &TimeLockResult{*commit, lockId}, err
}

type TimeUnLockResult struct {
tx.TxCommitResult
LockId int64 `json:"lock_id"`
}

func (c *client) TimeUnLock(id int64, sync bool, options ...Option) (*TimeUnLockResult, error) {
fromAddr := c.keyManager.GetAddr()

unlockMsg := msg.NewTimeUnlockMsg(fromAddr, id)
err := unlockMsg.ValidateBasic()
if err != nil {
return nil, err
}
commit, err := c.broadcastMsg(unlockMsg, sync, options...)
if err != nil {
return nil, err
}
var lockId int64
if commit.Ok && sync {
lockId, err = strconv.ParseInt(string(commit.Data), 10, 64)
if err != nil {
return nil, err
}
}
return &TimeUnLockResult{*commit, lockId}, err
}

type TimeReLockResult struct {
tx.TxCommitResult
LockId int64 `json:"lock_id"`
}

func (c *client) TimeReLock(id int64, description string, amount types.Coins, lockTime int64, sync bool, options ...Option) (*TimeReLockResult, error) {
fromAddr := c.keyManager.GetAddr()

relockMsg := msg.NewTimeRelockMsg(fromAddr, id, description, amount, lockTime)
err := relockMsg.ValidateBasic()
if err != nil {
return nil, err
}
commit, err := c.broadcastMsg(relockMsg, sync, options...)
if err != nil {
return nil, err
}
var lockId int64
if commit.Ok && sync {
lockId, err = strconv.ParseInt(string(commit.Data), 10, 64)
if err != nil {
return nil, err
}
}
return &TimeReLockResult{*commit, lockId}, err
}
4 changes: 4 additions & 0 deletions client/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/binance-chain/go-sdk/client/basic"
"github.com/binance-chain/go-sdk/client/query"
"github.com/binance-chain/go-sdk/common/types"
"github.com/binance-chain/go-sdk/keys"
"github.com/binance-chain/go-sdk/types/msg"
"github.com/binance-chain/go-sdk/types/tx"
Expand All @@ -21,6 +22,9 @@ type TransactionClient interface {
IssueToken(name, symbol string, supply int64, sync bool, mintable bool, options ...Option) (*IssueTokenResult, error)
SendToken(transfers []msg.Transfer, sync bool, options ...Option) (*SendTokenResult, error)
MintToken(symbol string, amount int64, sync bool, options ...Option) (*MintTokenResult, error)
TimeLock(description string, amount types.Coins, lockTime int64, sync bool, options ...Option) (*TimeLockResult, error)
TimeUnLock(id int64, sync bool, options ...Option) (*TimeUnLockResult, error)
TimeReLock(id int64, description string, amount types.Coins, lockTime int64, sync bool, options ...Option) (*TimeReLockResult, error)

SubmitListPairProposal(title string, param msg.ListTradingPairParams, initialDeposit int64, votingPeriod time.Duration, sync bool, options ...Option) (*SubmitProposalResult, error)
SubmitProposal(title string, description string, proposalType msg.ProposalKind, initialDeposit int64, votingPeriod time.Duration, sync bool, options ...Option) (*SubmitProposalResult, error)
Expand Down
21 changes: 21 additions & 0 deletions common/types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"

"github.com/tendermint/tendermint/crypto"
"github.com/binance-chain/go-sdk/common/bech32"
)

Expand All @@ -16,6 +17,11 @@ type AccAddress []byte
type ChainNetwork uint8

const (
AddrLen = 20

bech32PrefixConsPub = "bcap"
bech32PrefixConsAddr = "bca"

TestNetwork ChainNetwork = iota
ProdNetwork
)
Expand Down Expand Up @@ -119,3 +125,18 @@ func (bz AccAddress) String() string {
}
return bech32Addr
}

func MustBech32ifyConsPub(pub crypto.PubKey) string {
enc, err := Bech32ifyConsPub(pub)
if err != nil {
panic(err)
}

return enc
}

// Bech32ifyConsPub returns a Bech32 encoded string containing the
// Bech32PrefixConsPub prefixfor a given consensus node's PubKey.
func Bech32ifyConsPub(pub crypto.PubKey) (string, error) {
return bech32.ConvertAndEncode(bech32PrefixConsPub, pub.Bytes())
}
9 changes: 9 additions & 0 deletions common/types/coins.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ func (coins Coins) IsEqual(coinsB Coins) bool {
return true
}

func (coins Coins) IsZero() bool {
for _, coin := range coins {
if !coin.IsZero() {
return false
}
}
return true
}

func (coins Coins) IsNotNegative() bool {
if len(coins) == 0 {
return true
Expand Down
50 changes: 50 additions & 0 deletions common/types/dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,30 @@ package types
import (
"encoding/json"
"fmt"
"math/big"
"strconv"
)

// number of decimal places
const (
Precision = 8

// bytes required to represent the above precision
// ceil(log2(9999999999))
DecimalPrecisionBits = 34
)

var (
precisionReuse = new(big.Int).Exp(big.NewInt(10), big.NewInt(Precision), nil).Int64()
zeroInt = big.NewInt(0)
oneInt = big.NewInt(1)
tenInt = big.NewInt(10)
)

func precisionInt() int64 {
return precisionReuse
}

type Dec struct {
int64 `json:"int"`
}
Expand Down Expand Up @@ -61,3 +82,32 @@ func NewDecFromStr(str string) (d Dec, err error) {
}
return Dec{value}, nil
}

//nolint
func (d Dec) IsNil() bool { return false } // is decimal nil
func (d Dec) IsZero() bool { return d.int64 == 0 } // is equal to zero
func (d Dec) Equal(d2 Dec) bool { return d.int64 == d2.int64 } // equal decimals
func (d Dec) GT(d2 Dec) bool { return d.int64 > d2.int64 } // greater than
func (d Dec) GTE(d2 Dec) bool { return d.int64 >= d2.int64 } // greater than or equal
func (d Dec) LT(d2 Dec) bool { return d.int64 < d2.int64 } // less than
func (d Dec) LTE(d2 Dec) bool { return d.int64 <= d2.int64 } // less than or equal
func (d Dec) Neg() Dec { return Dec{-d.int64} } // reverse the decimal sign
func (d Dec) Abs() Dec {
if d.int64 < 0 {
return d.Neg()
}
return d
}

// subtraction
func (d Dec) Sub(d2 Dec) Dec {
c := d.int64 - d2.int64
if (c < d.int64) != (d2.int64 > 0) {
panic("Int overflow")
}
return Dec{c}
}

// nolint - common values
func ZeroDec() Dec { return Dec{0} }
func OneDec() Dec { return Dec{precisionInt()} }
1 change: 0 additions & 1 deletion common/types/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func (pt ProposalKind) String() string {
}
}


type ProposalStatus byte

//nolint
Expand Down
Loading

0 comments on commit f23da44

Please sign in to comment.