Skip to content
forked from gkany/cocos-go

API for CocosBCX chain - supports websocket RPC & Wallet functions

License

Notifications You must be signed in to change notification settings

Cocos-BCX/cocos-go

 
 

Repository files navigation

cocos-go

A cocos-go API consuming a websocket connection to an active full node or a RPC connection to your cli_wallet. Look for several examples in examples and tests folder. This is work in progress. To mitigate breaking changes, please use tagged branches. New tagged branches will be created for breaking changes. No additional cgo dependencies for transaction signing required. Use it at your own risk.

install

go get -u github.com/Cocos-BCX/cocos-go

Install dev-dependencies with

make init

This API uses ffjson. If you change types or operations you have to regenerate the required static MarshalJSON and UnmarshalJSON functions for the new/changed code.

make generate

If you encounter any errors, try:

make generate_new

to generate ffjson helpers from scratch.

generate operation samples

To generate op samples for testing, go to gen package. Generated operation samples get injected automatically while running operation tests.

code

import (
	sdk "github.com/Cocos-BCX/cocos-go"
)

wsURL := "wss://test.cocosbcx.net"

api := sdk.NewWebsocketAPI(wsURL)
if err := api.Connect(); err != nil {
	log.Println(err)
}

accountID := types.NewAccountID("1.2.16") // nicotest 1.2.16
coreAsset := types.NewAssetID("1.3.0")

balances, err := api.GetAccountBalances(accountID, coreAsset)
if err != nil {
	log.Fatal(err)
}
log.Printf("balances: %v", balances)

If you need wallet functions, use:

import (
	sdk "github.com/Cocos-BCX/cocos-go"
)

walletURL := "http://127.0.0.1:8048"

walletAPI := sdk.NewWalletAPI(walletURL)
if err := walletAPI.Connect(); err != nil {
	log.Println(err)
}

info, err := walletAPI.Info()
if err != nil {
	log.Fatal(err)
}
log.Printf("info: %v", info)

...

For a long application lifecycle, you can use an API instance with latency tester that connects to the most reliable node. Note: Because the tester takes time to unleash its magic, use the above-mentioned constructor for quick in and out.

import (
	sdk "github.com/Cocos-BCX/cocos-go"
)

wsFullApiUrl := "wss://test.cocosbcx.net"

//wsFullApiUrl serves as "quick startup" fallback endpoint here, 
//until the latency tester provides the first results.

api, err := sdk.NewWithAutoEndpoint(wsFullApiUrl)
if err != nil {
	log.Fatal(err)
}

if err := api.Connect(); err != nil {
	log.Fatal(err)
}

api.OnError(func(err error) {
	log.Fatal(err)
})

...

implemented and tested (serialize/unserialize) operations

  • OperationTypeTransfer
  • OperationTypeLimitOrderCreate
  • OperationTypeLimitOrderCancel
  • OperationTypeCallOrderUpdate
  • OperationTypeFillOrder
  • OperationTypeAccountCreate
  • OperationTypeAccountUpdate
  • OperationTypeAccountUpgrade
  • OperationTypeAssetCreate
  • OperationTypeAssetUpdate
  • OperationTypeAssetUpdateRestricted
  • OperationTypeAssetUpdateBitasset
  • OperationTypeAssetUpdateFeedProducers
  • OperationTypeAssetIssue
  • OperationTypeAssetReserve
  • OperationTypeAssetSettle
  • OperationTypeAssetGlobalSettle
  • OperationTypeAssetPublishFeed
  • OperationTypeWitnessCreate
  • OperationTypeWitnessUpdate
  • OperationTypeProposalCreate
  • OperationTypeProposalUpdate
  • OperationTypeProposalDelete
  • OperationTypeCommitteeMemberCreate
  • OperationTypeCommitteeMemberUpdate
  • OperationTypeCommitteeMemberUpdateGlobalParameters
  • OperationTypeVestingBalanceCreate
  • OperationTypeVestingBalanceWithdraw
  • OperationTypeWorkerCreate
  • OperationTypeBalanceClaim
  • OperationTypeAssetSettleCancel
  • OperationTypeAssetClaimFees
  • OperationTypeBidCollateral
  • OperationTypeExecuteBid
  • OperationTypeContractCreate
  • OperationTypeCallContractFunction
  • OperationTypeTemporaryAuthorityChange
  • OperationTypeRegisterNhAssetCreator
  • OperationTypeCreateWorldView
  • OperationTypeRelateWorldView
  • OperationTypeCreateNhAsset
  • OperationTypeDeleteNhAsset
  • OperationTypeTransferNhAsset
  • OperationTypeCreateNhAssetOrder
  • OperationTypeCancelNhAssetOrder
  • OperationTypeFillNhAssetOrder
  • OperationTypeCreateFile
  • OperationTypeAddFileRelateAccount
  • OperationTypeFileSignature
  • OperationTypeRelateParentFile
  • OperationTypeReviseContract
  • OperationTypeCrontabCreate
  • OperationTypeCrontabCancel
  • OperationTypeCrontabRecover
  • OperationTypeUpdateCollateralForGas
  • OperationTypeAccountAuthentication

todo

  • add missing operations
  • add convenience functions

Have fun and feel free to contribute needed operations and tests.

About

API for CocosBCX chain - supports websocket RPC & Wallet functions

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 99.0%
  • Other 1.0%