Skip to content

Commit

Permalink
update api
Browse files Browse the repository at this point in the history
  • Loading branch information
klim0v committed Oct 13, 2020
1 parent 91edcd3 commit f43322a
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 53 deletions.
13 changes: 9 additions & 4 deletions api/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ type AddressResponse struct {
Result *AddressResult `json:"result,omitempty"`
Error *Error `json:"error,omitempty"`
}
type Balance struct {
ID string `json:"coin_id"`
Symbol string `json:"symbol"`
Value string `json:"value"`
}

type AddressResult struct {
Balance map[string]string `json:"balance"`
TransactionCount string `json:"transaction_count"`
Balance []Balance `json:"balances"`
TransactionCount string `json:"transaction_count"`
}

// Returns coins list, balance and transaction count (for nonce) of an address.
Expand Down Expand Up @@ -53,12 +58,12 @@ func (a *Api) AddressAtHeight(address string, height int) (*AddressResult, error
}

// Returns balance of an address.
func (a *Api) Balance(address string) (map[string]string, error) {
func (a *Api) Balance(address string) ([]Balance, error) {
return a.BalanceAtHeight(address, LatestBlockHeight)
}

// Returns balance of an address.
func (a *Api) BalanceAtHeight(address string, height int) (map[string]string, error) {
func (a *Api) BalanceAtHeight(address string, height int) ([]Balance, error) {
response, err := a.AddressAtHeight(address, height)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions api/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ type AddressesResponse struct {
}

type AddressesResult struct {
Address string `json:"address"`
Balance map[string]string `json:"balance"`
TransactionCount string `json:"transaction_count"`
Address string `json:"address"`
Balance []Balance `json:"balance"`
TransactionCount string `json:"transaction_count"`
}

func (a *Api) Addresses(addresses []string, height int) ([]*AddressesResult, error) {
Expand Down
2 changes: 1 addition & 1 deletion api/candidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type CandidateResult struct {
Commission string `json:"commission"`
Stakes []struct {
Owner string `json:"owner"`
Coin string `json:"coin"`
Coin Coin `json:"coin"`
Value string `json:"value"`
BipValue string `json:"bip_value"`
} `json:"stakes"`
Expand Down
4 changes: 3 additions & 1 deletion api/coin_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ type CoinInfoResult struct {
Name string `json:"name"`
Symbol string `json:"symbol"`
Volume string `json:"volume"`
Crr string `json:"crr"`
Crr int `json:"crr"`
ReserveBalance string `json:"reserve_balance"`
MaxSupply string `json:"max_supply"`
OwnerAddress string `json:"owner_address,omitempty"`
}

// Returns information about coin. Note: this method does not return information about base coins (MNT and BIP).
Expand Down
3 changes: 1 addition & 2 deletions api/estimate_tx_commission.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package api
import (
"encoding/json"
"fmt"
"github.com/MinterTeam/minter-go-sdk/transaction"
)

type EstimateTxCommissionResponse struct {
Expand All @@ -18,7 +17,7 @@ type EstimateTxCommissionResult struct {
}

// Return estimate of transaction.
func (a *Api) EstimateTxCommission(transaction transaction.EncodeInterface) (*EstimateTxCommissionResult, error) {
func (a *Api) EstimateTxCommission(transaction interface{ Encode() (string, error) }) (*EstimateTxCommissionResult, error) {
bytes, err := transaction.Encode()
if err != nil {
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions api/send_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package api
import (
"encoding/json"
"fmt"
"github.com/MinterTeam/minter-go-sdk/transaction"
)

type SendTransactionResponse struct {
Expand Down Expand Up @@ -60,7 +59,7 @@ func (a *Api) SendRawTransaction(tx string) (*SendTransactionResult, error) {
}

// Returns the result of sending signed tx.
func (a *Api) SendTransaction(transaction transaction.EncodeInterface) (*SendTransactionResult, error) {
func (a *Api) SendTransaction(transaction interface{ Encode() (string, error) }) (*SendTransactionResult, error) {
tx, err := transaction.Encode()
if err != nil {
return nil, err
Expand Down
63 changes: 35 additions & 28 deletions api/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package api
import (
"encoding/json"
"errors"
"github.com/MinterTeam/minter-go-sdk/transaction"
)

type TransactionResponse struct {
Expand All @@ -13,6 +12,11 @@ type TransactionResponse struct {
Error *Error `json:"error,omitempty"`
}

type Coin struct {
ID string `json:"id"`
Symbol string `json:"symbol"`
}

type TransactionResult struct {
Hash string `json:"hash"`
RawTx string `json:"raw_tx"`
Expand All @@ -22,7 +26,7 @@ type TransactionResult struct {
Nonce string `json:"nonce"`
Gas string `json:"gas"`
GasPrice int `json:"gas_price"`
GasCoin string `json:"gas_coin"`
GasCoin Coin `json:"gas_coin"`
Type int `json:"type"`
Data transactionData `json:"data"`
Payload []byte `json:"payload"`
Expand All @@ -34,7 +38,8 @@ type TransactionResult struct {
TxType string `json:"tx.type,omitempty"`
TxFrom string `json:"tx.from,omitempty"`
TxTo string `json:"tx.to,omitempty"`
TxCoin string `json:"tx.coin,omitempty"`
TxCoinID string `json:"tx.coin_id,omitempty"`
TxCoinSymbol string `json:"tx.coin_symbol,omitempty"`
TxSellAmount string `json:"tx.sell_amount,omitempty"`
TxCreatedMultisig string `json:"tx.created_multisig,omitempty"`
} `json:"tags,omitempty"`
Expand Down Expand Up @@ -68,34 +73,34 @@ func (t *TransactionResult) ErrorLog() error {
func (t *TransactionResult) DataStruct() (tdi, error) {

var data tdi
switch transaction.Type(t.Type) {
case transaction.TypeSend:
switch t.Type {
case 1:
data = &SendData{}
case transaction.TypeSellCoin:
case 2:
data = &SellCoinData{}
case transaction.TypeSellAllCoin:
case 3:
data = &SellAllCoinData{}
case transaction.TypeBuyCoin:
case 4:
data = &BuyCoinData{}
case transaction.TypeCreateCoin:
case 5:
data = &CreateCoinData{}
case transaction.TypeDeclareCandidacy:
case 6:
data = &DeclareCandidacyData{}
case transaction.TypeDelegate:
case 7:
data = &DelegateData{}
case transaction.TypeUnbond:
case 8:
data = &UnbondData{}
case transaction.TypeRedeemCheck:
case 9:
data = &RedeemCheckData{}
case transaction.TypeSetCandidateOnline:
case 10:
data = &SetCandidateOnData{}
case transaction.TypeSetCandidateOffline:
case 11:
data = &SetCandidateOffData{}
case transaction.TypeCreateMultisig:
case 12:
data = &CreateMultisigData{}
case transaction.TypeMultisend:
case 13:
data = &MultisendData{}
case transaction.TypeEditCandidate:
case 14:
data = &EditCandidateData{}
default:
return nil, errors.New("unknown transaction type")
Expand All @@ -109,7 +114,7 @@ type tdi interface {
}

type SendData struct {
Coin string `json:"coin"`
Coin Coin `json:"coin"`
To string `json:"to"`
Value string `json:"value"`
}
Expand All @@ -119,9 +124,9 @@ func (s *SendData) fill(b []byte) error {
}

type SellCoinData struct {
CoinToSell string `json:"coin_to_sell"`
CoinToSell Coin `json:"coin_to_sell"`
ValueToSell string `json:"value_to_sell"`
CoinToBuy string `json:"coin_to_buy"`
CoinToBuy Coin `json:"coin_to_buy"`
MinimumValueToBuy string `json:"minimum_value_to_buy"`
}

Expand All @@ -130,8 +135,8 @@ func (s *SellCoinData) fill(b []byte) error {
}

type SellAllCoinData struct {
CoinToSell string `json:"coin_to_sell"`
CoinToBuy string `json:"coin_to_buy"`
CoinToSell Coin `json:"coin_to_sell"`
CoinToBuy Coin `json:"coin_to_buy"`
MinimumValueToBuy string `json:"minimum_value_to_buy"`
}

Expand All @@ -140,9 +145,9 @@ func (s *SellAllCoinData) fill(b []byte) error {
}

type BuyCoinData struct {
CoinToBuy string `json:"coin_to_buy"`
CoinToBuy Coin `json:"coin_to_buy"`
ValueToBuy string `json:"value_to_buy"`
CoinToSell string `json:"coin_to_sell"`
CoinToSell Coin `json:"coin_to_sell"`
MaximumValueToSell string `json:"maximum_value_to_sell"`
}

Expand All @@ -167,7 +172,7 @@ type DeclareCandidacyData struct {
Address string `json:"address"`
PubKey string `json:"pub_key"`
Commission string `json:"commission"`
Coin string `json:"coin"`
Coin Coin `json:"coin"`
Stake string `json:"stake"`
}

Expand All @@ -177,7 +182,7 @@ func (s *DeclareCandidacyData) fill(b []byte) error {

type DelegateData struct {
PubKey string `json:"pub_key"`
Coin string `json:"coin"`
Coin Coin `json:"coin"`
Value string `json:"value"`
}

Expand All @@ -187,10 +192,12 @@ func (s *DelegateData) fill(b []byte) error {

type UnbondData struct {
PubKey string `json:"pub_key"`
Coin string `json:"coin"`
Coin Coin `json:"coin"`
Value string `json:"value"`
}

// todo add more types

func (s *UnbondData) fill(b []byte) error {
return json.Unmarshal(b, s)
}
Expand Down
20 changes: 9 additions & 11 deletions transaction/migrate_v12.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ package transaction

import (
"encoding/json"
"github.com/MinterTeam/minter-go-sdk/v2/api/http_client"
"github.com/MinterTeam/minter-go-sdk/api"
"github.com/MinterTeam/minter-go-sdk/v2/transaction"
"github.com/ethereum/go-ethereum/rlp"
"github.com/go-resty/resty/v2"
"os"
"reflect"
"time"
)

var httpClient *http_client.Client
var httpClient *api.Api

func coinID(symbol string) (transaction.CoinID, error) {
coinID, err := httpClient.CoinID(symbol)
coinID, err := httpClient.CoinInfo(symbol)
if err != nil {
return 0, err
}
return transaction.CoinID(coinID), nil
return transaction.CoinID(coinID.ID), nil
}

func (o *object) sign(key string, multisigPrKeys ...string) (SignedTransaction, error) {
Expand Down Expand Up @@ -108,13 +110,9 @@ func (o *object) EncodeDataReflect() (bytes []byte, err error) {
}

func init() {
nodeApiUrl := os.Getenv("NODE_API_V2")
nodeApiUrl := os.Getenv("NODE_API")
if nodeApiUrl == "" {
panic("set env 'NODE_API_V2'")
panic("set env 'NODE_API'")
}
client, err := http_client.New(nodeApiUrl)
if err != nil {
panic(err)
}
httpClient = client
httpClient = api.NewApiWithClient(nodeApiUrl, resty.New().SetTimeout(time.Minute).EnableTrace().SetDebug(true))
}
1 change: 1 addition & 0 deletions transaction/migrate_v12_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package transaction
2 changes: 1 addition & 1 deletion transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ type SignedTransaction interface {
Signature() (signatureInterface, error)
AddSignature(signatures ...[]byte) (SignedTransaction, error)
SignatureData() []byte
// SimpleSignatureData() ([]byte, error)
SimpleSignatureData() ([]byte, error)
SenderAddress() (string, error)
Sign(prKey string, multisigPrKeys ...string) (SignedTransaction, error)
}
Expand Down

0 comments on commit f43322a

Please sign in to comment.