Skip to content

Commit

Permalink
add side chain gov func
Browse files Browse the repository at this point in the history
  • Loading branch information
unclezoro committed Jul 3, 2020
1 parent 3849561 commit 6911e16
Show file tree
Hide file tree
Showing 10 changed files with 730 additions and 14 deletions.
156 changes: 154 additions & 2 deletions client/rpc/dex_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package rpc

import (
"encoding/binary"
"encoding/json"
"errors"
"fmt"

core_types "github.com/tendermint/tendermint/rpc/core/types"
"time"

"github.com/binance-chain/go-sdk/common"
"github.com/binance-chain/go-sdk/common/types"
Expand All @@ -14,6 +14,7 @@ import (
gtypes "github.com/binance-chain/go-sdk/types"
"github.com/binance-chain/go-sdk/types/msg"
"github.com/binance-chain/go-sdk/types/tx"
core_types "github.com/tendermint/tendermint/rpc/core/types"
)

type SyncType int
Expand Down Expand Up @@ -51,12 +52,15 @@ type DexClient interface {
GetTradingPairs(offset int, limit int) ([]types.TradingPair, error)
GetDepth(tradePair string, level int) (*types.OrderBook, error)
GetProposals(status types.ProposalStatus, numLatest int64) ([]types.Proposal, error)
GetSideChainProposals(status types.ProposalStatus, numLatest int64, sideChainId string) ([]types.Proposal, error)
GetSideChainProposal(proposalId int64, sideChainId string) (types.Proposal, error)
GetProposal(proposalId int64) (types.Proposal, error)
GetTimelocks(addr types.AccAddress) ([]types.TimeLockRecord, error)
GetTimelock(addr types.AccAddress, recordID int64) (*types.TimeLockRecord, error)
GetSwapByID(swapID types.SwapBytes) (types.AtomicSwap, error)
GetSwapByCreator(creatorAddr string, offset int64, limit int64) ([]types.SwapBytes, error)
GetSwapByRecipient(recipientAddr string, offset int64, limit int64) ([]types.SwapBytes, error)
GetSideChainParams(sideChainId string) ([]msg.SCParam, error)

SetKeyManager(k keys.KeyManager)
SendToken(transfers []msg.Transfer, syncType SyncType, options ...tx.Option) (*core_types.ResultBroadcastTx, error)
Expand All @@ -76,6 +80,17 @@ type DexClient interface {
Claim(chainId sdk.IbcChainID, sequence uint64, payload []byte, syncType SyncType, options ...tx.Option) (*core_types.ResultBroadcastTx, error)
GetProphecy(chainId sdk.IbcChainID, sequence int64) (*msg.Prophecy, error)
GetCurrentOracleSequence(chainId sdk.IbcChainID) (int64, error)

SideChainVote(proposalID int64, option msg.VoteOption, sideChainId string, syncType SyncType, options ...tx.Option) (*core_types.ResultBroadcastTx, error)
SideChainDeposit(proposalID int64, amount types.Coins, sideChainId string, syncType SyncType, options ...tx.Option) (*core_types.ResultBroadcastTx, error)
SideChainSubmitSCParamsProposal(title string, scParam msg.SCChangeParams, initialDeposit types.Coins, votingPeriod time.Duration, sideChainId string, syncType SyncType, options ...tx.Option) (*core_types.ResultBroadcastTx, error)
SideChainSubmitCSCParamsProposal(title string, cscParam msg.CSCParamChange, initialDeposit types.Coins, votingPeriod time.Duration, sideChainId string, syncType SyncType, options ...tx.Option) (*core_types.ResultBroadcastTx, error)
SideChainSubmitProposal(title string, description string, proposalType msg.ProposalKind, initialDeposit types.Coins, votingPeriod time.Duration, sideChainId string, syncType SyncType, options ...tx.Option) (*core_types.ResultBroadcastTx, error)
SubmitListProposal(title string, param msg.ListTradingPairParams, proposalType msg.ProposalKind, initialDeposit types.Coins, votingPeriod time.Duration, syncType SyncType, options ...tx.Option) (*core_types.ResultBroadcastTx, error)

SubmitProposal(title string, description string, proposalType msg.ProposalKind, initialDeposit types.Coins, votingPeriod time.Duration, syncType SyncType, options ...tx.Option) (*core_types.ResultBroadcastTx, error)
Deposit(proposalID int64, amount types.Coins, syncType SyncType, options ...tx.Option) (*core_types.ResultBroadcastTx, error)
Vote(proposalID int64, option msg.VoteOption, syncType SyncType, options ...tx.Option) (*core_types.ResultBroadcastTx, error)
}

func (c *HTTP) TxInfoSearch(query string, prove bool, page, perPage int) ([]Info, error) {
Expand Down Expand Up @@ -386,13 +401,22 @@ func (c *HTTP) GetTimelock(addr types.AccAddress, recordID int64) (*types.TimeLo
}

func (c *HTTP) GetProposals(status types.ProposalStatus, numLatest int64) ([]types.Proposal, error) {
return c.getProposals(status, "", numLatest)
}

func (c *HTTP) GetSideChainProposals(status types.ProposalStatus, numLatest int64, sideChainId string) ([]types.Proposal, error) {
return c.getProposals(status, sideChainId, numLatest)
}

func (c *HTTP) getProposals(status types.ProposalStatus, sideChainId string, numLatest int64) ([]types.Proposal, error) {
params := types.QueryProposalsParams{}
if status != types.StatusNil {
params.ProposalStatus = status
}
if numLatest > 0 {
params.NumLatestProposals = numLatest
}
params.SideChainId = sideChainId

bz, err := c.cdc.MarshalJSON(&params)
if err != nil {
Expand All @@ -412,9 +436,18 @@ func (c *HTTP) GetProposals(status types.ProposalStatus, numLatest int64) ([]typ
}

func (c *HTTP) GetProposal(proposalId int64) (types.Proposal, error) {
return c.getProposal(proposalId, "")
}

func (c *HTTP) GetSideChainProposal(proposalId int64, sideChainId string) (types.Proposal, error) {
return c.getProposal(proposalId, sideChainId)
}

func (c *HTTP) getProposal(proposalId int64, sideChainId string) (types.Proposal, error) {
params := types.QueryProposalParams{
ProposalID: proposalId,
}
params.SideChainId = sideChainId
bz, err := c.cdc.MarshalJSON(params)
if err != nil {
return nil, err
Expand All @@ -432,6 +465,23 @@ func (c *HTTP) GetProposal(proposalId int64) (types.Proposal, error) {
return proposal, err
}

func (c *HTTP) GetSideChainParams(sideChainId string) ([]msg.SCParam, error) {
data, err := c.cdc.MarshalJSON(sideChainId)
if err != nil {
return nil, err
}
rawParams, err := c.ABCIQuery("param/sideParams", data)
if err != nil {
return nil, err
}
if !rawParams.Response.IsOK() {
return nil, fmt.Errorf(rawParams.Response.Log)
}
var params []msg.SCParam
err = c.cdc.UnmarshalJSON(rawParams.Response.GetValue(), &params)
return params, err
}

func (c *HTTP) existsCC(symbol string) bool {
resp, err := c.ABCIQuery(fmt.Sprintf("tokens/info/%s", symbol), nil)
if err != nil {
Expand Down Expand Up @@ -641,6 +691,108 @@ func (c *HTTP) RefundHTLT(swapID []byte, syncType SyncType, options ...tx.Option
return c.Broadcast(refundHTLTMsg, syncType, options...)
}

func (c *HTTP) SideChainVote(proposalID int64, option msg.VoteOption, sideChainId string, syncType SyncType, options ...tx.Option) (*core_types.ResultBroadcastTx, error) {
if c.key == nil {
return nil, KeyMissingError
}
fromAddr := c.key.GetAddr()
msg := msg.NewSideChainVoteMsg(fromAddr, proposalID, option, sideChainId)
return c.Broadcast(msg, syncType, options...)
}

func (c *HTTP) SideChainDeposit(proposalID int64, amount types.Coins, sideChainId string, syncType SyncType, options ...tx.Option) (*core_types.ResultBroadcastTx, error) {
if c.key == nil {
return nil, KeyMissingError
}
fromAddr := c.key.GetAddr()
msg := msg.NewSideChainDepositMsg(fromAddr, proposalID, amount, sideChainId)
return c.Broadcast(msg, syncType, options...)
}

func (c *HTTP) SideChainSubmitSCParamsProposal(title string, scParam msg.SCChangeParams, initialDeposit types.Coins, votingPeriod time.Duration, sideChainId string, syncType SyncType, options ...tx.Option) (*core_types.ResultBroadcastTx, error) {
if c.key == nil {
return nil, KeyMissingError
}
err := scParam.Check()
if err != nil {
return nil, err
}
scParamsBz, err := c.cdc.MarshalJSON(scParam)
if err != nil {
return nil, err
}
fromAddr := c.key.GetAddr()
msg := msg.NewSideChainSubmitProposalMsg(title, string(scParamsBz), msg.ProposalTypeSCParamsChange, fromAddr, initialDeposit, votingPeriod, sideChainId)
return c.Broadcast(msg, syncType, options...)
}

func (c *HTTP) SideChainSubmitCSCParamsProposal(title string, cscParam msg.CSCParamChange, initialDeposit types.Coins, votingPeriod time.Duration, sideChainId string, syncType SyncType, options ...tx.Option) (*core_types.ResultBroadcastTx, error) {
if c.key == nil {
return nil, KeyMissingError
}
err := cscParam.Check()
if err != nil {
return nil, err
}
// cscParam get interface field, use amino
cscParamsBz, err := c.cdc.MarshalJSON(cscParam)
if err != nil {
return nil, err
}
fromAddr := c.key.GetAddr()
msg := msg.NewSideChainSubmitProposalMsg(title, string(cscParamsBz), msg.ProposalTypeCSCParamsChange, fromAddr, initialDeposit, votingPeriod, sideChainId)
return c.Broadcast(msg, syncType, options...)
}

func (c *HTTP) SideChainSubmitProposal(title string, description string, proposalType msg.ProposalKind, initialDeposit types.Coins, votingPeriod time.Duration, sideChainId string, syncType SyncType, options ...tx.Option) (*core_types.ResultBroadcastTx, error) {
if c.key == nil {
return nil, KeyMissingError
}
fromAddr := c.key.GetAddr()
msg := msg.NewSideChainSubmitProposalMsg(title, description, proposalType, fromAddr, initialDeposit, votingPeriod, sideChainId)
return c.Broadcast(msg, syncType, options...)
}

func (c *HTTP) SubmitListProposal(title string, param msg.ListTradingPairParams, proposalType msg.ProposalKind, initialDeposit types.Coins, votingPeriod time.Duration, syncType SyncType, options ...tx.Option) (*core_types.ResultBroadcastTx, error) {
if c.key == nil {
return nil, KeyMissingError
}
bz, err := json.Marshal(&param)
if err != nil {
return nil, err
}
fromAddr := c.key.GetAddr()
msg := msg.NewMsgSubmitProposal(title, string(bz), proposalType, fromAddr, initialDeposit, votingPeriod)
return c.Broadcast(msg, syncType, options...)
}

func (c *HTTP) SubmitProposal(title string, description string, proposalType msg.ProposalKind, initialDeposit types.Coins, votingPeriod time.Duration, syncType SyncType, options ...tx.Option) (*core_types.ResultBroadcastTx, error) {
if c.key == nil {
return nil, KeyMissingError
}
fromAddr := c.key.GetAddr()
msg := msg.NewMsgSubmitProposal(title, description, proposalType, fromAddr, initialDeposit, votingPeriod)
return c.Broadcast(msg, syncType, options...)
}

func (c *HTTP) Deposit(proposalID int64, amount types.Coins, syncType SyncType, options ...tx.Option) (*core_types.ResultBroadcastTx, error) {
if c.key == nil {
return nil, KeyMissingError
}
fromAddr := c.key.GetAddr()
msg := msg.NewDepositMsg(fromAddr, proposalID, amount)
return c.Broadcast(msg, syncType, options...)
}

func (c *HTTP) Vote(proposalID int64, option msg.VoteOption, syncType SyncType, options ...tx.Option) (*core_types.ResultBroadcastTx, error) {
if c.key == nil {
return nil, KeyMissingError
}
fromAddr := c.key.GetAddr()
msg := msg.NewMsgVote(fromAddr, proposalID, option)
return c.Broadcast(msg, syncType, options...)
}

func (c *HTTP) CancelOrder(baseAssetSymbol, quoteAssetSymbol, refId string, syncType SyncType, options ...tx.Option) (*core_types.ResultBroadcastTx, error) {
if c.key == nil {
return nil, KeyMissingError
Expand Down
45 changes: 41 additions & 4 deletions common/types/dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@ const (
)

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)
precisionReuse = new(big.Int).Exp(big.NewInt(10), big.NewInt(Precision), nil).Int64()
precisionMultipliers []int64
zeroInt = big.NewInt(0)
oneInt = big.NewInt(1)
tenInt = big.NewInt(10)
)

// Set precision multipliers
func init() {
precisionMultipliers = make([]int64, Precision+1)
for i := 0; i <= Precision; i++ {
precisionMultipliers[i] = calcPrecisionMultiplier(int64(i))
}
}

func precisionInt() int64 {
return precisionReuse
}
Expand Down Expand Up @@ -111,3 +120,31 @@ func (d Dec) Sub(d2 Dec) Dec {
// nolint - common values
func ZeroDec() Dec { return Dec{0} }
func OneDec() Dec { return Dec{precisionInt()} }
func NewDecWithPrec(i, prec int64) Dec {
if i == 0 {
return Dec{0}
}
c := i * precisionMultiplier(prec)
if c/i != precisionMultiplier(prec) {
panic("Int overflow")
}
return Dec{c}
}

// get the precision multiplier, do not mutate result
func precisionMultiplier(prec int64) int64 {
if prec > Precision {
panic(fmt.Sprintf("too much precision, maximum %v, provided %v", Precision, prec))
}
return precisionMultipliers[prec]
}

// calculate the precision multiplier
func calcPrecisionMultiplier(prec int64) int64 {
if prec > Precision {
panic(fmt.Sprintf("too much precision, maximum %v, provided %v", Precision, prec))
}
zerosToAdd := Precision - prec
multiplier := new(big.Int).Exp(tenInt, big.NewInt(zerosToAdd), nil).Int64()
return multiplier
}
6 changes: 6 additions & 0 deletions common/types/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,18 @@ func (tp *TextProposal) SetVotingPeriod(votingPeriod time.Duration) {
tp.VotingPeriod = votingPeriod
}

type BaseParams struct {
SideChainId string
}

type QueryProposalsParams struct {
BaseParams
ProposalStatus ProposalStatus
NumLatestProposals int64
}

// Params for query 'custom/gov/proposal'
type QueryProposalParams struct {
BaseParams
ProposalID int64
}
3 changes: 0 additions & 3 deletions common/types/stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,6 @@ func (ca ConsAddress) Format(s fmt.State, verb rune) {
}
}

type BaseParams struct {
SideChainId string
}

func NewBaseParams(sideChainId string) BaseParams {
return BaseParams{SideChainId:sideChainId}
Expand Down
Loading

0 comments on commit 6911e16

Please sign in to comment.