Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
chain33_sdk
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
harrylee2015
chain33_sdk
Commits
80aedb4e
Commit
80aedb4e
authored
May 26, 2018
by
lihailei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
封装jsonClient,方便调用
parent
c031c95f
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
413 additions
and
20 deletions
+413
-20
common.go
src/common/common.go
+1
-1
jsonclient.go
src/common/rpc/jsonclient.go
+77
-0
types.go
src/common/rpc/types.go
+309
-0
account.go
src/controllers/account.go
+1
-1
block.go
src/controllers/block.go
+1
-1
contract.go
src/controllers/contract.go
+16
-3
peer.go
src/controllers/peer.go
+1
-1
tx.go
src/controllers/tx.go
+6
-6
wallet.go
src/controllers/wallet.go
+1
-1
models.go
src/models/models.go
+0
-6
No files found.
src/common/common.go
View file @
80aedb4e
...
...
@@ -9,8 +9,8 @@ import (
"gitlab.33.cn/chain33/chain33/account"
common
"gitlab.33.cn/chain33/chain33/common"
crypto
"gitlab.33.cn/chain33/chain33/common/crypto"
jsonrpc
"gitlab.33.cn/chain33/chain33/rpc"
"gitlab.33.cn/chain33/chain33/types"
jsonrpc
"gitlab.33.cn/lihailei/chain33_sdk/src/common/rpc"
"io/ioutil"
"net/http"
"os"
...
...
src/common/rpc/jsonclient.go
0 → 100644
View file @
80aedb4e
package
rpc
import
(
"bytes"
"encoding/json"
"fmt"
log
"github.com/inconshreveable/log15"
"io/ioutil"
"net/http"
)
var
(
jlog
=
log
.
New
(
"module"
,
"common"
)
)
const
JSON_RPC_VERSION
=
"2.0"
type
JSONClient
struct
{
url
string
}
func
NewJSONClient
(
url
string
)
(
*
JSONClient
,
error
)
{
return
&
JSONClient
{
url
},
nil
}
type
clientRequest
struct
{
Jsonrpc
string
`json:"jsonrpc"`
Method
string
`json:"method"`
Params
[
1
]
interface
{}
`json:"params"`
Id
uint64
`json:"id"`
}
type
clientResponse
struct
{
Id
uint64
`json:"id"`
Result
*
json
.
RawMessage
`json:"result"`
Error
interface
{}
`json:"error"`
}
func
(
client
*
JSONClient
)
Call
(
method
string
,
params
,
resp
interface
{})
error
{
req
:=
&
clientRequest
{}
req
.
Jsonrpc
=
JSON_RPC_VERSION
req
.
Method
=
method
req
.
Params
[
0
]
=
params
data
,
err
:=
json
.
Marshal
(
req
)
if
err
!=
nil
{
return
err
}
//poststr := fmt.Sprintf(`{"jsonrpc":"2.0","id":2,"method":"Chain33.SendTransaction","params":[{"data":"%v"}]}`,
// common.ToHex(types.Encode(tx)))
jlog
.
Debug
(
"request JsonStr"
,
string
(
data
),
""
)
postresp
,
err
:=
http
.
Post
(
client
.
url
,
"application/json"
,
bytes
.
NewBuffer
(
data
))
if
err
!=
nil
{
return
err
}
defer
postresp
.
Body
.
Close
()
b
,
err
:=
ioutil
.
ReadAll
(
postresp
.
Body
)
if
err
!=
nil
{
return
err
}
log
.
Debug
(
"response"
,
string
(
b
),
""
)
cresp
:=
&
clientResponse
{}
err
=
json
.
Unmarshal
(
b
,
&
cresp
)
if
err
!=
nil
{
return
err
}
if
cresp
.
Error
!=
nil
||
cresp
.
Result
==
nil
{
x
,
ok
:=
cresp
.
Error
.
(
string
)
if
!
ok
{
return
fmt
.
Errorf
(
"invalid error %v"
,
cresp
.
Error
)
}
if
x
==
""
{
x
=
"unspecified error"
}
return
fmt
.
Errorf
(
x
)
}
return
json
.
Unmarshal
(
*
cresp
.
Result
,
resp
)
}
src/common/rpc/types.go
0 → 100644
View file @
80aedb4e
package
rpc
import
(
"encoding/json"
)
type
userWrite
struct
{
Topic
string
`json:"topic"`
Content
string
`json:"content"`
}
type
TransParm
struct
{
Execer
string
`json:"execer"`
Payload
string
`json:"payload"`
Signature
*
Signature
`json:"signature"`
Fee
int64
`json:"fee"`
}
type
SignedTx
struct
{
Unsign
string
`json:"unsignTx"`
Sign
string
`json:"sign"`
Pubkey
string
`json:"pubkey"`
Ty
int32
`json:"ty"`
}
type
RawParm
struct
{
Data
string
`json:"data"`
}
type
QueryParm
struct
{
Hash
string
`json:"hash"`
}
type
BlockParam
struct
{
Start
int64
`json:"start"`
End
int64
`json:"end"`
Isdetail
bool
`json:"isDetail"`
}
type
Header
struct
{
Version
int64
`json:"version"`
ParentHash
string
`json:"parentHash"`
TxHash
string
`json:"txHash"`
StateHash
string
`json:"stateHash"`
Height
int64
`json:"height"`
BlockTime
int64
`json:"blockTime"`
TxCount
int64
`json:"txCount"`
Hash
string
`json:"hash"`
}
type
Signature
struct
{
Ty
int32
`json:"ty"`
Pubkey
string
`json:"pubkey"`
Signature
string
`json:"signature"`
}
type
Transaction
struct
{
Execer
string
`json:"execer"`
Payload
interface
{}
`json:"payload"`
RawPayload
string
`json:"rawPayload"`
Signature
*
Signature
`json:"signature"`
Fee
int64
`json:"fee"`
Expire
int64
`json:"expire"`
Nonce
int64
`json:"nonce"`
From
string
`json:"from,omitempty"`
To
string
`json:"to"`
Amount
int64
`json:"amount,omitempty"`
}
type
ReceiptLog
struct
{
Ty
int32
`json:"ty"`
Log
string
`json:"log"`
}
type
ReceiptData
struct
{
Ty
int32
`json:"ty"`
Logs
[]
*
ReceiptLog
`json:"logs"`
}
type
ReceiptDataResult
struct
{
Ty
int32
`json:"ty"`
TyName
string
`json:"tyName"`
Logs
[]
*
ReceiptLogResult
`json:"logs"`
}
type
ReceiptLogResult
struct
{
Ty
int32
`json:"ty"`
TyName
string
`json:"tyName"`
Log
interface
{}
`json:"log"`
RawLog
string
`json:"rawLog"`
}
type
Block
struct
{
Version
int64
`json:"version"`
ParentHash
string
`json:"parentHash"`
TxHash
string
`json:"txHash"`
StateHash
string
`json:"stateHash"`
Height
int64
`json:"height"`
BlockTime
int64
`json:"blockTime"`
Txs
[]
*
Transaction
`json:"txs"`
}
type
BlockDetail
struct
{
Block
*
Block
`json:"block"`
Receipts
[]
*
ReceiptDataResult
`json:"recipts"`
}
type
BlockDetails
struct
{
Items
[]
*
BlockDetail
`json:"items"`
}
type
TransactionDetail
struct
{
Tx
*
Transaction
`json:"tx"`
Receipt
*
ReceiptDataResult
`json:"receipt"`
Proofs
[]
string
`json:"proofs"`
Height
int64
`json:"height"`
Index
int64
`json:"index"`
Blocktime
int64
`json:"blockTime"`
Amount
int64
`json:"amount"`
Fromaddr
string
`json:"fromAddr"`
ActionName
string
`json:"actionName"`
}
type
ReplyTxInfos
struct
{
TxInfos
[]
*
ReplyTxInfo
`protobuf:"bytes,1,rep,name=txInfos" json:"txInfos"`
}
type
ReplyTxInfo
struct
{
Hash
string
`json:"hash"`
Height
int64
`json:"height"`
Index
int64
`json:"index"`
}
type
TransactionDetails
struct
{
//Txs []*Transaction `json:"txs"`
Txs
[]
*
TransactionDetail
`protobuf:"bytes,1,rep,name=txs" json:"txs"`
}
type
ReplyTxList
struct
{
Txs
[]
*
Transaction
`json:"txs"`
}
type
ReplyHash
struct
{
Hash
string
`json:"hash"`
}
type
ReplyHashes
struct
{
Hashes
[]
string
`json:"hashes"`
}
type
PeerList
struct
{
Peers
[]
*
Peer
`json:"peers"`
}
type
Peer
struct
{
Addr
string
`json:"addr"`
Port
int32
`json:"port"`
Name
string
`json:"name"`
MempoolSize
int32
`json:"mempoolSize"`
Self
bool
`json:"self"`
Header
*
Header
`json:"header"`
}
// Wallet Module
type
WalletAccounts
struct
{
Wallets
[]
*
WalletAccount
`protobuf:"bytes,1,rep,name=wallets" json:"wallets"`
}
type
WalletAccount
struct
{
Acc
*
Account
`protobuf:"bytes,1,opt,name=acc" json:"acc"`
Label
string
`protobuf:"bytes,2,opt,name=label" json:"label"`
}
type
Account
struct
{
Currency
int32
`protobuf:"varint,1,opt,name=currency" json:"currency"`
Balance
int64
`protobuf:"varint,2,opt,name=balance" json:"balance"`
Frozen
int64
`protobuf:"varint,3,opt,name=frozen" json:"frozen"`
Addr
string
`protobuf:"bytes,4,opt,name=addr" json:"addr"`
}
type
Reply
struct
{
IsOk
bool
`protobuf:"varint,1,opt,name=isOk" json:"isOK"`
Msg
string
`protobuf:"bytes,2,opt,name=msg,proto3" json:"msg"`
}
type
Headers
struct
{
Items
[]
*
Header
`protobuf:"bytes,1,rep,name=items" json:"items"`
}
type
ReqAddr
struct
{
Addr
string
`json:"addr"`
}
type
ReqHashes
struct
{
Hashes
[]
string
`json:"hashes"`
DisableDetail
bool
`json:"disableDetail"`
}
type
ReqWalletTransactionList
struct
{
FromTx
string
`json:"fromTx"`
Count
int32
`json:"count"`
Direction
int32
`json:"direction"`
}
type
WalletTxDetails
struct
{
TxDetails
[]
*
WalletTxDetail
`protobuf:"bytes,1,rep,name=txDetails" json:"txDetails"`
}
type
WalletTxDetail
struct
{
Tx
*
Transaction
`protobuf:"bytes,1,opt,name=tx" json:"tx"`
Receipt
*
ReceiptDataResult
`protobuf:"bytes,2,opt,name=receipt" json:"receipt"`
Height
int64
`protobuf:"varint,3,opt,name=height" json:"height"`
Index
int64
`protobuf:"varint,4,opt,name=index" json:"index"`
BlockTime
int64
`json:"blockTime"`
Amount
int64
`json:"amount"`
FromAddr
string
`json:"fromAddr"`
TxHash
string
`json:"txHash"`
ActionName
string
`json:"actionName"`
}
type
BlockOverview
struct
{
Head
*
Header
`protobuf:"bytes,1,opt,name=head" json:"head"`
TxCount
int64
`protobuf:"varint,2,opt,name=txCount" json:"txCount"`
TxHashes
[]
string
`protobuf:"bytes,3,rep,name=txHashes,proto3" json:"txHashes"`
}
type
Query4Cli
struct
{
Execer
string
`protobuf:"bytes,1,opt,name=execer,proto3" json:"execer"`
FuncName
string
`protobuf:"bytes,2,opt,name=funcName" json:"funcName"`
Payload
interface
{}
`protobuf:"bytes,3,opt,name=payload" json:"payload"`
}
type
Query4Jrpc
struct
{
Execer
string
`protobuf:"bytes,1,opt,name=execer,proto3" json:"execer"`
FuncName
string
`protobuf:"bytes,2,opt,name=funcName" json:"funcName"`
Payload
json
.
RawMessage
`protobuf:"bytes,3,opt,name=payload" json:"payload"`
}
type
WalletStatus
struct
{
IsWalletLock
bool
`json:"isWalletLock"`
IsAutoMining
bool
`json:"isAutoMining"`
IsHasSeed
bool
`json:"isHasSeed"`
IsTicketLock
bool
`json:"isTicketLock"`
}
// Token Transaction
type
TokenPreCreateTx
struct
{
Price
int64
`json:"price"`
Name
string
`json:"name"`
Symbol
string
`json:"symbol"`
Introduction
string
`json:"introduction"`
OwnerAddr
string
`json:"ownerAddr"`
Total
int64
`json:"total"`
Fee
int64
`json:"fee"`
}
type
TokenFinishTx
struct
{
OwnerAddr
string
`json:"ownerAddr"`
Symbol
string
`json:"symbol"`
Fee
int64
`json:"fee"`
}
type
TokenRevokeTx
struct
{
OwnerAddr
string
`json:"ownerAddr"`
Symbol
string
`json:"symbol"`
Fee
int64
`json:"fee"`
}
// Trade Transaction
type
TradeSellTx
struct
{
TokenSymbol
string
`json:"tokenSymbol"`
AmountPerBoardlot
int64
`json:"amountPerBoardlot"`
MinBoardlot
int64
`json:"minBoardlot"`
PricePerBoardlot
int64
`json:"pricePerBoardlot"`
TotalBoardlot
int64
`json:"totalBoardlot"`
Fee
int64
`json:"fee"`
}
type
TradeBuyTx
struct
{
SellID
string
`json:"sellID"`
BoardlotCnt
int64
`json:"boardlotCnt"`
Fee
int64
`json:"fee"`
}
type
TradeRevokeTx
struct
{
SellID
string
`json:"sellID,"`
Fee
int64
`json:"fee"`
}
type
TradeBuyLimitTx
struct
{
TokenSymbol
string
`json:"tokenSymbol"`
AmountPerBoardlot
int64
`json:"amountPerBoardlot"`
MinBoardlot
int64
`json:"minBoardlot"`
PricePerBoardlot
int64
`json:"pricePerBoardlot"`
TotalBoardlot
int64
`json:"totalBoardlot"`
Fee
int64
`json:"fee"`
}
type
TradeSellMarketTx
struct
{
BuyID
string
`json:"buyID"`
BoardlotCnt
int64
`json:"boardlotCnt"`
Fee
int64
`json:"fee"`
}
type
TradeRevokeBuyTx
struct
{
BuyID
string
`json:"buyID,"`
Fee
int64
`json:"fee"`
}
type
NodeNetinfo
struct
{
Externaladdr
string
`json:"externalAddr"`
Localaddr
string
`json:"localAddr"`
Service
bool
`json:"service"`
Outbounds
int32
`json:"outbounds"`
Inbounds
int32
`json:"inbounds"`
}
src/controllers/account.go
View file @
80aedb4e
...
...
@@ -4,9 +4,9 @@ import (
"fmt"
"github.com/astaxie/beego"
log
"github.com/inconshreveable/log15"
jsonrpc
"gitlab.33.cn/chain33/chain33/rpc"
"gitlab.33.cn/chain33/chain33/types"
.
"gitlab.33.cn/lihailei/chain33_sdk/src/common"
jsonrpc
"gitlab.33.cn/lihailei/chain33_sdk/src/common/rpc"
.
"gitlab.33.cn/lihailei/chain33_sdk/src/models"
)
...
...
src/controllers/block.go
View file @
80aedb4e
...
...
@@ -2,9 +2,9 @@ package controllers
import
(
"github.com/astaxie/beego"
jsonrpc
"gitlab.33.cn/chain33/chain33/rpc"
"gitlab.33.cn/chain33/chain33/types"
.
"gitlab.33.cn/lihailei/chain33_sdk/src/common"
jsonrpc
"gitlab.33.cn/lihailei/chain33_sdk/src/common/rpc"
.
"gitlab.33.cn/lihailei/chain33_sdk/src/models"
)
...
...
src/controllers/contract.go
View file @
80aedb4e
...
...
@@ -3,10 +3,11 @@ package controllers
import
(
"github.com/astaxie/beego"
jsonrpc
"gitlab.33.cn/chain33/chain33/rpc
"
.
"gitlab.33.cn/chain33/chain33/common
"
"gitlab.33.cn/chain33/chain33/types"
.
"gitlab.33.cn/lihailei/chain33_sdk/src/common"
.
"gitlab.33.cn/lihailei/chain33_sdk/src/common/crypto"
jsonrpc
"gitlab.33.cn/lihailei/chain33_sdk/src/common/rpc"
"encoding/json"
...
...
@@ -57,7 +58,13 @@ func (c *ContractController) Invoke() {
addr
,
priv
:=
Genaddress
()
tx
:=
&
types
.
Transaction
{
Execer
:
[]
byte
(
"user.blacklist"
),
Payload
:
types
.
Encode
(
action
),
Fee
:
1e6
,
To
:
addr
}
tx
.
Sign
(
types
.
SECP256K1
,
priv
)
res
,
err
:=
SendTransaction
(
tx
)
//res, err := SendTransaction(tx)
data
:=
ToHex
(
types
.
Encode
(
tx
))
params
:=
jsonrpc
.
RawParm
{
Data
:
data
,
}
ctx
:=
NewRpcCtx
(
GetJrpcURL
(),
"Chain33.SendTransaction"
,
params
,
nil
)
res
,
err
:=
ctx
.
RunWithoutMarshal
()
if
err
!=
nil
{
c
.
Data
[
"json"
]
=
err
.
Error
()
c
.
ServeJSON
()
...
...
@@ -103,7 +110,13 @@ func (c *ContractController) Invoke() {
addr
,
priv
:=
Genaddress
()
tx
:=
&
types
.
Transaction
{
Execer
:
[]
byte
(
"user.blacklist"
),
Payload
:
types
.
Encode
(
action
),
Fee
:
1e6
,
To
:
addr
}
tx
.
Sign
(
types
.
SECP256K1
,
priv
)
res
,
err
:=
SendTransaction
(
tx
)
//res, err := SendTransaction(tx)
data
:=
ToHex
(
types
.
Encode
(
tx
))
params
:=
jsonrpc
.
RawParm
{
Data
:
data
,
}
ctx
:=
NewRpcCtx
(
GetJrpcURL
(),
"Chain33.SendTransaction"
,
params
,
nil
)
res
,
err
:=
ctx
.
RunWithoutMarshal
()
if
err
!=
nil
{
c
.
Data
[
"json"
]
=
err
.
Error
()
c
.
ServeJSON
()
...
...
src/controllers/peer.go
View file @
80aedb4e
...
...
@@ -2,8 +2,8 @@ package controllers
import
(
"github.com/astaxie/beego"
jsonrpc
"gitlab.33.cn/chain33/chain33/rpc"
.
"gitlab.33.cn/lihailei/chain33_sdk/src/common"
jsonrpc
"gitlab.33.cn/lihailei/chain33_sdk/src/common/rpc"
)
// PeerController operations for Peer
...
...
src/controllers/tx.go
View file @
80aedb4e
...
...
@@ -2,11 +2,10 @@ package controllers
import
(
"github.com/astaxie/beego"
"gitlab.33.cn/chain33/chain33/account"
.
"gitlab.33.cn/chain33/chain33/common"
jsonrpc
"gitlab.33.cn/chain33/chain33/rpc"
"gitlab.33.cn/chain33/chain33/types"
.
"gitlab.33.cn/lihailei/chain33_sdk/src/common"
jsonrpc
"gitlab.33.cn/lihailei/chain33_sdk/src/common/rpc"
.
"gitlab.33.cn/lihailei/chain33_sdk/src/models"
r
"math/rand"
)
...
...
@@ -125,16 +124,17 @@ func queryByHash(rpcAddr, txHash string) ([]byte, error) {
// @router /sendTX [post]
func
(
c
*
TxController
)
SendTx
()
{
execer
:=
c
.
GetString
(
"execerName"
)
privKey
:=
c
.
GetString
(
"privateKey"
)
//
privKey := c.GetString("privateKey")
body
:=
c
.
Ctx
.
Input
.
RequestBody
if
execer
==
""
||
privKey
==
""
||
len
(
body
)
==
0
{
if
execer
==
""
||
len
(
body
)
==
0
{
c
.
Ctx
.
ResponseWriter
.
WriteHeader
(
400
)
return
}
addr
,
priv
:=
Genaddress
()
tx
:=
&
types
.
Transaction
{
Execer
:
[]
byte
(
execer
),
Payload
:
body
,
Fee
:
1e6
}
tx
.
Nonce
=
r
.
Int63
()
tx
.
Sign
(
types
.
SECP256K1
,
Getprivkey
(
privKey
))
tx
.
To
=
account
.
ExecAddress
(
execer
)
.
String
(
)
tx
.
To
=
addr
tx
.
Sign
(
types
.
SECP256K1
,
priv
)
data
:=
ToHex
(
types
.
Encode
(
tx
))
params
:=
jsonrpc
.
RawParm
{
Data
:
data
,
...
...
src/controllers/wallet.go
View file @
80aedb4e
...
...
@@ -3,9 +3,9 @@ package controllers
import
(
"fmt"
"github.com/astaxie/beego"
jsonrpc
"gitlab.33.cn/chain33/chain33/rpc"
"gitlab.33.cn/chain33/chain33/types"
.
"gitlab.33.cn/lihailei/chain33_sdk/src/common"
jsonrpc
"gitlab.33.cn/lihailei/chain33_sdk/src/common/rpc"
.
"gitlab.33.cn/lihailei/chain33_sdk/src/models"
"strconv"
)
...
...
src/models/models.go
View file @
80aedb4e
...
...
@@ -5,7 +5,6 @@ import (
"strconv"
log
"github.com/inconshreveable/log15"
bl
"gitlab.33.cn/chain33/chain33/executor/drivers/blacklist/types"
jsonrpc
"gitlab.33.cn/chain33/chain33/rpc"
"gitlab.33.cn/chain33/chain33/types"
)
...
...
@@ -384,8 +383,3 @@ func ParseGetBalanceRes(arg interface{}) (interface{}, error) {
}
return
result
,
nil
}
func
ParseBlacklistOrg
(
res
interface
{})
(
interface
{},
error
)
{
var
result
bl
.
Org
return
result
,
nil
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment