Skip to content

Commit

Permalink
Merge pull request #6 from rddl-network/jmastr/more-mock-endpoints
Browse files Browse the repository at this point in the history
Add remaining mock endpoints
  • Loading branch information
eckelj authored Dec 13, 2023
2 parents 2eccf3b + 314eab0 commit 0e515e5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 17 deletions.
10 changes: 5 additions & 5 deletions rawtransactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// CreateRawTransaction creates a transaction spending the given inputs and
// creating new outputs.
func CreateRawTransaction(url string, params []string) (hex string, err error) {
result, err := SendRequest(url, "createrawtransaction", params)
result, err := SendRequest(url, types.MethodCreateRawTransaction, params)
if err != nil {
return
}
Expand All @@ -20,7 +20,7 @@ func CreateRawTransaction(url string, params []string) (hex string, err error) {

// FundRawTransaction funds a raw transaction.
func FundRawTransaction(url string, params []string) (transactionResult types.FundRawTransactionResult, err error) {
result, err := SendRequest(url, "fundrawtransaction", params)
result, err := SendRequest(url, types.MethodFundRawTransaction, params)
if err != nil {
return
}
Expand All @@ -33,7 +33,7 @@ func FundRawTransaction(url string, params []string) (transactionResult types.Fu

// RawIssueAsset creates an asset by attaching issuances to transaction inputs.
func RawIssueAsset(url string, params []string) (transactionResults []types.RawIssueAssetResult, err error) {
result, err := SendRequest(url, "rawissueasset", params)
result, err := SendRequest(url, types.MethodRawIssueAsset, params)
if err != nil {
return
}
Expand All @@ -47,7 +47,7 @@ func RawIssueAsset(url string, params []string) (transactionResults []types.RawI
// SendRawTransaction submits a raw transaction (serialized, hex-encoded) to
// local node and network.
func SendRawTransaction(url string, params []string) (hex string, err error) {
result, err := SendRequest(url, "sendrawtransaction", params)
result, err := SendRequest(url, types.MethodSendRawTransaction, params)
if err != nil {
return
}
Expand All @@ -58,7 +58,7 @@ func SendRawTransaction(url string, params []string) (hex string, err error) {
// TestMempoolAccept returns result of mempool acceptance tests indicating if
// raw transaction(s) (serialized, hex-encoded) would be accepted by mempool.
func TestMempoolAccept(url string, params []string) (transactionResults []types.TestMempoolAcceptResult, err error) {
result, err := SendRequest(url, "testmempoolaccept", params)
result, err := SendRequest(url, types.MethodTestMempoolAccept, params)
if err != nil {
return
}
Expand Down
14 changes: 14 additions & 0 deletions types/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,18 @@ import (

var (
ErrMissingSignatures = errors.New("transaction does not have the complete set of signatures")

// elements RPC methods
MethodBlindRawTransaction = "blindrawtransaction"
MethodCreateRawTransaction = "createrawtransaction"
MethodFundRawTransaction = "fundrawtransaction"
MethodGetAddressInfo = "getaddressinfo"
MethodGetNewAddress = "getnewaddress"
MethodGetTransaction = "gettransaction"
MethodRawIssueAsset = "rawissueasset"
MethodReissueAsset = "reissueasset"
MethodSendRawTransaction = "sendrawtransaction"
MethodSendToAddress = "sendtoaddress"
MethodSignRawTransactionWithWallet = "signrawtransactionwithwallet"
MethodTestMempoolAccept = "testmempoolaccept"
)
45 changes: 40 additions & 5 deletions utils/mocks/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,20 @@ type MockClient struct {
DoFunc func(req *http.Request) (*http.Response, error)
}

// Note: not all fields in the results are set
var (
txID = "0000000000000000000000000000000000000000000000000000000000000000"
address = "tlq1qqdhf9taz5ftpxcl0qphdurs9csneacznlazmqqdjs9te0jh7nld0w3rmvzfuggr7l508ahclr69te88exl4rjp8z8etu5d35t"
addressInfoResult = types.GetAddressInfoResult{Address: address, Confidential: address}
fundRawTransactionResult = types.FundRawTransactionResult{Hex: zeros, Fee: 0.0, Changepos: 0}
rawIssueAssetResult = types.RawIssueAssetResult{Hex: zeros, Vin: 0, Entropy: "", Asset: "", Token: ""}
rawIssueAssetResults = []types.RawIssueAssetResult{rawIssueAssetResult}
reissueAssetResult = types.ReissueAssetResult{TxID: zeros, Vin: 0}
signRawTransactionWithWalletResult = types.SignRawTransactionWithWalletResult{Hex: zeros, Complete: true}
testMempoolAcceptResult = types.TestMempoolAcceptResult{Allowed: true, Txid: zeros, Vsize: 0, Wtxid: ""}
testMempoolAcceptResults = []types.TestMempoolAcceptResult{testMempoolAcceptResult}
transactionResult = types.GetTransactionResult{Hex: zeros, TxID: zeros}
zeros = "0000000000000000000000000000000000000000000000000000000000000000"
zerosWithQuotes = `"` + zeros + `"`
)

// GetDoFunc fetches the mock client's `Do` func
Expand All @@ -36,13 +48,36 @@ func GetDoFunc(req *http.Request) (*http.Response, error) {
if err != nil {
return nil, err
}

var response types.Response
response.Error.Code = 0
response.Error.Message = ""

switch body.Method {
case "reissueasset":
reissueAssetResult := types.ReissueAssetResult{TxID: txID, Vin: 0}
case types.MethodBlindRawTransaction:
response.Result = zerosWithQuotes
case types.MethodCreateRawTransaction:
response.Result = zerosWithQuotes
case types.MethodFundRawTransaction:
response.Result = fundRawTransactionResult
case types.MethodGetAddressInfo:
response.Result = addressInfoResult
case types.MethodGetNewAddress:
response.Result = address
case types.MethodGetTransaction:
response.Result = transactionResult
case types.MethodRawIssueAsset:
response.Result = rawIssueAssetResults
case types.MethodReissueAsset:
response.Result = reissueAssetResult
response.Error.Code = 0
response.Error.Message = ""
case types.MethodSendRawTransaction:
response.Result = zerosWithQuotes
case types.MethodSendToAddress:
response.Result = zerosWithQuotes
case types.MethodSignRawTransactionWithWallet:
response.Result = signRawTransactionWithWalletResult
case types.MethodTestMempoolAccept:
response.Result = testMempoolAcceptResults
default:
response.Result = nil
response.Error.Code = -1337
Expand Down
14 changes: 7 additions & 7 deletions wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// BlindRawTransaction converts one or more outputs of a raw transaction into
// confidential ones using only wallet inputs.
func BlindRawTransaction(url string, params []string) (hex string, err error) {
result, err := SendRequest(url, "blindrawtransaction", params)
result, err := SendRequest(url, types.MethodBlindRawTransaction, params)
if err != nil {
return
}
Expand All @@ -20,7 +20,7 @@ func BlindRawTransaction(url string, params []string) (hex string, err error) {

// GetAddressInfo returns information about the given address.
func GetAddressInfo(url string, params []string) (transactionResult types.GetAddressInfoResult, err error) {
result, err := SendRequest(url, "getaddressinfo", params)
result, err := SendRequest(url, types.MethodGetAddressInfo, params)
if err != nil {
return
}
Expand All @@ -33,7 +33,7 @@ func GetAddressInfo(url string, params []string) (transactionResult types.GetAdd

// GetNewAddress returns a new address for receiving payments.
func GetNewAddress(url string, params []string) (address string, err error) {
result, err := SendRequest(url, "getnewaddress", params)
result, err := SendRequest(url, types.MethodGetNewAddress, params)
if err != nil {
return
}
Expand All @@ -43,7 +43,7 @@ func GetNewAddress(url string, params []string) (address string, err error) {

// GetTransaction retrieves a transaction from the chain.
func GetTransaction(url string, params []string) (transactionResult types.GetTransactionResult, err error) {
result, err := SendRequest(url, "gettransaction", params)
result, err := SendRequest(url, types.MethodGetTransaction, params)
if err != nil {
return
}
Expand All @@ -57,7 +57,7 @@ func GetTransaction(url string, params []string) (transactionResult types.GetTra
// ReissueAsset creates more of an already issued asset. Must have reissuance
// token in wallet to do so.
func ReissueAsset(url string, params []string) (transactionResult types.ReissueAssetResult, err error) {
result, err := SendRequest(url, "reissueasset", params)
result, err := SendRequest(url, types.MethodReissueAsset, params)
if err != nil {
return
}
Expand All @@ -70,7 +70,7 @@ func ReissueAsset(url string, params []string) (transactionResult types.ReissueA

// SendToAddress sends an amount to a given address.
func SendToAddress(url string, params []string) (hex string, err error) {
result, err := SendRequest(url, "sendtoaddress", params)
result, err := SendRequest(url, types.MethodSendToAddress, params)
if err != nil {
return
}
Expand All @@ -81,7 +81,7 @@ func SendToAddress(url string, params []string) (hex string, err error) {
// SignRawTransactionWithWallet signs inputs for raw transaction (serialized,
// hex-encoded).
func SignRawTransactionWithWallet(url string, params []string) (transactionResult types.SignRawTransactionWithWalletResult, err error) {
result, err := SendRequest(url, "signrawtransactionwithwallet", params)
result, err := SendRequest(url, types.MethodSignRawTransactionWithWallet, params)
if err != nil {
return
}
Expand Down

0 comments on commit 0e515e5

Please sign in to comment.