Skip to content
Snippets Groups Projects
  • jkrvivian's avatar
    4a47fc7c
    feat: Implement value web api & client library (#438) · 4a47fc7c
    jkrvivian authored
    * feat: Implement value api: Attachment
    
    * feat: Implement value api: UnspentOutputs
    
    * feat: Implement value api: transactionByID
    
    * feat: Implement client lib & value api: sendTransaction
    
    * fix: minor tweak
    
    * fix: minor fix
    
    * refactor: Refactor sendTransaction api
    
    * Refactor: Fix :dog:
    
    * refactor: Fix :dog:
    
    * refactor: Rename api route of testSendTxn to camel case
    feat: Implement value web api & client library (#438)
    jkrvivian authored
    * feat: Implement value api: Attachment
    
    * feat: Implement value api: UnspentOutputs
    
    * feat: Implement value api: transactionByID
    
    * feat: Implement client lib & value api: sendTransaction
    
    * fix: minor tweak
    
    * fix: minor fix
    
    * refactor: Refactor sendTransaction api
    
    * Refactor: Fix :dog:
    
    * refactor: Fix :dog:
    
    * refactor: Rename api route of testSendTxn to camel case
value.go 2.10 KiB
package client

import (
	"fmt"
	"net/http"

	webapi_attachments "github.com/iotaledger/goshimmer/plugins/webapi/value/attachments"
	webapi_gettxn "github.com/iotaledger/goshimmer/plugins/webapi/value/gettransactionbyid"
	webapi_sendtxn "github.com/iotaledger/goshimmer/plugins/webapi/value/sendtransaction"
	webapi_unspentoutputs "github.com/iotaledger/goshimmer/plugins/webapi/value/unspentoutputs"
)

const (
	routeAttachments    = "value/attachments"
	routeGetTxnByID     = "value/transactionByID"
	routeSendTxn        = "value/sendTransaction"
	routeUnspentOutputs = "value/unspentOutputs"
)

// GetAttachments gets the attachments of a transaction ID
func (api *GoShimmerAPI) GetAttachments(base58EncodedTxnID string) (*webapi_attachments.Response, error) {
	res := &webapi_attachments.Response{}
	if err := api.do(http.MethodGet, func() string {
		return fmt.Sprintf("%s?txnID=%s", routeAttachments, base58EncodedTxnID)
	}(), nil, res); err != nil {
		return nil, err
	}

	return res, nil
}

// GetTransactionByID gets the transaction of a transaction ID
func (api *GoShimmerAPI) GetTransactionByID(base58EncodedTxnID string) (*webapi_gettxn.Response, error) {
	res := &webapi_gettxn.Response{}
	if err := api.do(http.MethodGet, func() string {
		return fmt.Sprintf("%s?txnID=%s", routeGetTxnByID, base58EncodedTxnID)
	}(), nil, res); err != nil {
		return nil, err
	}

	return res, nil
}

// GetUnspentOutputs return unspent output IDs of addresses
func (api *GoShimmerAPI) GetUnspentOutputs(addresses []string) (*webapi_unspentoutputs.Response, error) {
	res := &webapi_unspentoutputs.Response{}
	if err := api.do(http.MethodPost, routeUnspentOutputs,
		&webapi_unspentoutputs.Request{Addresses: addresses}, res); err != nil {
		return nil, err
	}

	return res, nil
}

// SendTransaction sends the transaction(bytes) to Tangle and returns transaction ID
func (api *GoShimmerAPI) SendTransaction(txnBytes []byte) (string, error) {
	res := &webapi_sendtxn.Response{}
	if err := api.do(http.MethodPost, routeSendTxn,
		&webapi_sendtxn.Request{TransactionBytes: txnBytes}, res); err != nil {
		return "", err
	}

	return res.TransactionID, nil
}