api.go


Overview

The [api.go](/projects/291/69232) file defines a foundational API layer for the software project’s blockchain-related modules, referred to as *coinstacks*. It provides standardized data types, interfaces, and error models for representing blockchain network information, account details, transactions, and pagination in API responses. Additionally, it declares common request parameter structures and a base interface (`BaseAPI`) that all coinstack implementations must follow.

This file serves as a shared contract and utility set enabling consistent API behavior and data exchange formats across different blockchain implementations within the system. It also includes Swagger annotations to support automatic API documentation generation.


Contents and Detailed Explanation

Package Declaration

package api

Defines the `api` package which groups core API types and functionality used throughout all coinstacks.


Error Models

These structs represent standard HTTP error responses, useful for API clients to handle errors consistently.


Core Interfaces and Structs

These define the basic data contracts for blockchain data entities.

Info Interface and BaseInfo Struct

type Info interface {
    network() string
}

type BaseInfo struct {
    Network string `json:"network"`
}

func (b BaseInfo) network() string {
    return b.Network
}

Account Interface and BaseAccount Struct

type Account interface {
    balance() string
    pubkey() string
}

type BaseAccount struct {
    Balance           string `json:"balance"`
    UnconfirmedBalance string `json:"unconfirmedBalance"`
    Pubkey            string `json:"pubkey"`
}

func (b BaseAccount) balance() string {
    return b.Balance
}

func (b BaseAccount) pubkey() string {
    return b.Pubkey
}

Pagination Struct

type Pagination struct {
    Cursor string `json:"cursor,omitempty"`
}

Tx Interface and BaseTx Struct

type Tx interface {
    txid() string
    blockHash() *string
    blockHeight() int
    timestamp() int
}

type BaseTx struct {
    TxID        string  `json:"txid"`
    BlockHash   *string `json:"blockHash,omitempty"`
    BlockHeight int     `json:"blockHeight"`
    Timestamp   int     `json:"timestamp"`
}

func (b *BaseTx) txid() string {
    return b.TxID
}

func (b *BaseTx) blockHash() *string {
    return b.BlockHash
}

func (b *BaseTx) blockHeight() int {
    return b.BlockHeight
}

func (b *BaseTx) timestamp() int {
    return b.Timestamp
}

TxHistory Interface and BaseTxHistory Struct

type TxHistory interface {
    pubkey() string
    txs() []Tx
}

type BaseTxHistory struct {
    Pagination
    Pubkey string `json:"pubkey"`
    Txs    []Tx   `json:"txs"`
}

func (b BaseTxHistory) pubkey() string {
    return b.Pubkey
}

func (b BaseTxHistory) txs() []Tx {
    return b.Txs
}

TxBody Struct

type TxBody struct {
    RawTx string `json:"rawTx"`
}

Request Parameter Structs

These structs are used to bind and validate HTTP request parameters.


Other Models


BaseAPI Interface

type BaseAPI interface {
    GetInfo() (Info, error)
    GetAccount(pubkey string) (Account, error)
    GetTxHistory(pubkey string, cursor string, pageSize int) (TxHistory, error)
    SendTx(hex string) (string, error)
}

Important Implementation Details and Algorithms


Interaction with Other System Components


Usage Examples

Implementing BaseAPI in a Coinstack

type MyCoinstackAPI struct {
    // internal fields like client or config
}

func (api *MyCoinstackAPI) GetInfo() (api.Info, error) {
    return api.BaseInfo{Network: "mainnet"}, nil
}

func (api *MyCoinstackAPI) GetAccount(pubkey string) (api.Account, error) {
    // fetch account data ...
    return &api.BaseAccount{
        Balance: "1000000",
        UnconfirmedBalance: "0",
        Pubkey: pubkey,
    }, nil
}

// Implement other interface methods...

Handling a Paginated Request

func handleTxHistory(c *gin.Context, api api.BaseAPI) {
    var params api.PaginatedPubkeyParam
    if err := c.BindQuery(&params); err != nil {
        c.JSON(400, api.BadRequestError{Error: "Invalid parameters"})
        return
    }

    history, err := api.GetTxHistory(params.Pubkey, params.Cursor, params.PageSize)
    if err != nil {
        c.JSON(500, api.InternalServerError{Message: err.Error()})
        return
    }

    c.JSON(200, history)
}

Visual Diagram

classDiagram
    class BaseAPI {
        +GetInfo() Info
        +GetAccount(pubkey string) Account
        +GetTxHistory(pubkey string, cursor string, pageSize int) TxHistory
        +SendTx(hex string) string
    }

    class Info {
        <<interface>>
        +network() string
    }

    class BaseInfo {
        +Network: string
        +network() string
    }

    class Account {
        <<interface>>
        +balance() string
        +pubkey() string
    }

    class BaseAccount {
        +Balance: string
        +UnconfirmedBalance: string
        +Pubkey: string
        +balance() string
        +pubkey() string
    }

    class Tx {
        <<interface>>
        +txid() string
        +blockHash() *string
        +blockHeight() int
        +timestamp() int
    }

    class BaseTx {
        +TxID: string
        +BlockHash: *string
        +BlockHeight: int
        +Timestamp: int
        +txid() string
        +blockHash() *string
        +blockHeight() int
        +timestamp() int
    }

    class TxHistory {
        <<interface>>
        +pubkey() string
        +txs() []Tx
    }

    class BaseTxHistory {
        +Cursor: string
        +Pubkey: string
        +Txs: []Tx
        +pubkey() string
        +txs() []Tx
    }

    BaseAPI ..> Info : returns
    BaseAPI ..> Account : returns
    BaseAPI ..> TxHistory : returns

    BaseInfo ..|> Info
    BaseAccount ..|> Account
    BaseTx ..|> Tx
    BaseTxHistory ..|> TxHistory

Summary

The [api.go](/projects/291/69232) file is a critical foundational component defining standard data types, interfaces, and error models to support consistent API design and implementation across multiple blockchain stacks in the system. It enables polymorphic behavior through interfaces, supports pagination, and integrates with Swagger for API documentation. This design promotes modularity, reusability, and clarity in the project's blockchain API layer.