Skip to content

API for Cocos-BCX chain - supports websocket RPC & Wallet functions

License

Notifications You must be signed in to change notification settings

gkany/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/gkany/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.

testing

To test this stuff I use a combined docker based MainNet/TestNet wallet, you can find here. Operations testing uses generated real blockchain sample code by gen package. To test run:

make test_operations
make test_api

or a long running block (deserialize/serialize/compare) range test.

make test_blocks

code

// wsURL := "ws://127.0.0.1:8049"
wsURL := "ws://test.cocosbcx.net"

api := cocos-go.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:

// local cli_wallet, rpc_port: 8048
walletURL := "http://127.0.0.1:8048"

walletAPI := cocos-go.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.

wsFullApiUrl := "wss://cocos-go.openledger.info/ws"

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

api, err := cocos-go.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 Cocos-BCX chain - supports websocket RPC & Wallet functions

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 99.4%
  • Other 0.6%