Skip to content

Commit 75adda2

Browse files
feat(lang): go Rest (ccxt#23691)
* try new command * fix script * enable debug mode * debug go build * try go build command * improve go build * update command * metada generation * add readme * try again * increase it to 20mins * try with p=1 * add size-large * add medium * remove vm * remove dependency * fix command * test github actions * update * add build steps * remove * fix types * try without go-eth package * fix go-app * update * add step * add export exchanges * remove go from travis commands * remove go * update package.json * restore crypto function * transpile tests * fix tests * fix base tests * fix padEnd * fix mod * fix response tests * go build * fix live-tests * rename * improve generateImplicitAPI * fix import * test without ts build * update proxy * add timeout * try new machine * try js App * missing type * fix bitmart * add workflows * missing package json * py dependencies * run rest and ws tests simultaneously * fix cs live tests * try detaching JS files * fix trigger * add permissions * add types * text new format * add step * add npm i * protect loadMarkets conditions * add parsing method * improve actions * adjust github actions * update * fix build * import orderbooks * fix types * update php version * fix idex test * fix kive tests * export exchanges * fix py * add pip install * msg * add php version * try pow fix * add php version * add npm i * change php version * php log * use php 8.1 * split go builds * add main.go * fix go app * fix currencies * update go * protect against nil * fix generic code * some try fixes * fix path * upload and download go binary * adjust permissions * replace command to use proxy * fix index by * add fetchTime * bump ast-transpiler * split parseTicker call * fix phemex * add parseCurrency * try fix coinbase * fix base. * fix go action * fix gzip encoding * fix optinal arg * some req fixes * fix handleHttStatusCode * parse as text * fix loop with catch * fix parseNumber strikePrice * fix linting * use simplest approach * even simpler * some fixes * change wrappers * fix transpiling * skip createOrder * add stub * adjust createOrder * update readme * error handling * fix panic * remove actions * missing interface methods and types * init CLI * missing methods * Golang CLI * add some examples * some more methods * add example * add optional parameters section * typo * format text * update error and examples * some cli fixes * chore: bump ast-transpiler * fix markets concurrent writes * fix promise-all * add cache mechanism * fix inheritance * fix package-lock * update tests * new error approach * remove logs * fix import * init keccak and ethEncode * eth encoding fixes * remove 0x prefix * response in verbose not showing up fix * fix: fix memory leak by moving http client to shared by exchage * add profiler script to tests * some go required fixes in the ts side * remove most callInternal calls * try optimize safeValue by removing runtime calls * temporarily disable paradex in go * cache fixes * remove dync calls * add rateLimit option to cli * move proxies check * some optimizations * optimize safeValue * more optmizations and fixesssss * more optmizations and fixesssss * revert some changes * fix latency issues * add cli benchmark * add trades to benchmarks in cli * fix cli * get value to benchmark * add more benchmarks * fix deepExtend and other fixes * protect extendMap * AddElementToObject remove reflect * fix: update profile per fetch * some more optimizations * update ast-transpiler * optimize indexBy * improve map * add types tests * types test * some fixes and encodeEth fix * hyperliquid: createOrder/transfer working * hyperliquid: withdrawal working * add fetchAccounts to virtual methods * missing headers extension * update go mod path * remove import * update cli import * optional args fix * fix params * update readme * request static tests working * request tests working * update tests mod path * fix Ymdhms * go app and mod update * update path * add method to interface * pushback go files * update commands and release * add go types * use "v" in version * revert js files * revert * revert generated files --------- Co-authored-by: pablo <[email protected]>
1 parent 729e89a commit 75adda2

File tree

317 files changed

+133586
-2106
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

317 files changed

+133586
-2106
lines changed

.github/workflows/go-app.yml

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
name: Go
2+
on:
3+
pull_request:
4+
branches:
5+
- master
6+
jobs:
7+
build:
8+
runs-on: ubuntu-20.04
9+
if: ${{ !startsWith(github.event.head_commit.message, '[Automated changes]') && !contains(github.event.head_commit.message, 'Merge branch ''master'' of https://github.com/ccxt/ccxt') }}
10+
timeout-minutes: 60
11+
steps:
12+
- uses: actions/checkout@v4
13+
- name: Setup Go
14+
uses: actions/setup-go@v5
15+
with:
16+
go-version: "1.21.x"
17+
- name: Setup Node.js
18+
uses: actions/setup-node@v3
19+
- name: Install npm dependencies
20+
run: npm ci --include=dev
21+
- name: Check tsc version
22+
run: tsc --version
23+
- name: Install tsx
24+
run: npm install -g tsx
25+
- name: export exchanges
26+
run: npm run export-exchanges
27+
- name: Generate implicit API
28+
run: npm run emitAPI -- --go
29+
- name: Transpile go/ccxt
30+
run: tsx build/goTranspiler.ts
31+
- name: Install dependencies
32+
run: go get ./go/ccxt
33+
- name: Build
34+
run: go build ./go/ccxt
35+
- name: Build tests
36+
run: go build ./go/tests/main.go
37+
- name: Test GO Types
38+
run: npm run test-types-go
39+
- name: Run Base Tests
40+
run: npm run test-base-rest-go
41+
- name: Run Id Tests
42+
run: npm run id-tests-go
43+
- name: Request tests
44+
run: npm run request-go
45+
- name: Response Tests
46+
run: npm run response-go
47+
48+
# - name: Upload Go Files
49+
# uses: actions/upload-artifact@v4
50+
# with:
51+
# name: go-files
52+
# path: go/
53+
- name: Upload Go Binary
54+
uses: actions/upload-artifact@v4
55+
with:
56+
name: main
57+
path: .
58+
59+
- name: Push GO changes to master
60+
env:
61+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
62+
if: github.ref == 'refs/heads/master'
63+
run: |
64+
git config --global user.name "github-actions[bot]"
65+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
66+
git add go/
67+
git commit -m "[Automated changes] GO files" || echo "No changes to commit."
68+
git remote set-url origin https://${GITHUB_TOKEN}@github.com/${{ github.repository }}
69+
git config --global pull.rebase false
70+
git pull origin master
71+
git push
72+
73+
live-tests:
74+
needs: build
75+
runs-on: ubuntu-latest
76+
steps:
77+
- uses: actions/checkout@v4
78+
# - name: Download Go Files
79+
# uses: actions/download-artifact@v4
80+
# with:
81+
# name: go-files
82+
# path: go/
83+
# - uses: actions/checkout@v4
84+
- name: Download Go Binary
85+
uses: actions/download-artifact@v4
86+
with:
87+
name: main
88+
path: .
89+
- name: Install npm dependencies
90+
run: npm ci --include=dev
91+
- name: Export exchanges
92+
run: npm run export-exchanges
93+
# - name: Install dependencies
94+
# run: go get ./go/ccxt
95+
# - name: Build
96+
# run: go build ./go/ccxt
97+
# - name: Build tests
98+
# run: go build ./go/tests/main.go
99+
- name: Adjust permissions
100+
run: chmod +x main
101+
- name: Live tests
102+
run: npm run live-tests-rest-go
103+
104+

.github/workflows/release.yml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ jobs:
5151
dotnet-quality: 'preview'
5252
run: |
5353
sudo mkdir -p /usr/share/dotnet
54+
- uses: actions/checkout@v4
55+
- name: Setup Go
56+
uses: actions/setup-go@v5
57+
with:
58+
go-version: "1.21.x"
5459
- name: Install NPM dependencies
5560
run: npm ci
5661
- name: Composer install
@@ -77,6 +82,8 @@ jobs:
7782
run: npm run package-test
7883
- name: Freshness Test
7984
run: npm run test-freshness
85+
- name: Go Types Test
86+
run: npm run test-types-go
8087
# Will start the deployment process
8188
- name: Set new Version
8289
run: |
@@ -100,18 +107,23 @@ jobs:
100107
- name: Create GH Release and git-tag-version and push
101108
env:
102109
COMMIT_MESSAGE: ${{ env.COMMIT_MESSAGE }}
110+
NPM_VERSION: ${{ env.NPM_VERSION }}
103111
GH_TOKEN: ${{ github.token }}
104112
run: |
105113
echo "COMMIT_MESSAGE=$COMMIT_MESSAGE"
106114
git config --global user.name "github-actions[bot]"
107115
git config --global user.email "github-actions[bot]@users.noreply.github.com"
108-
git tag -a "${COMMIT_MESSAGE}" -m "${COMMIT_MESSAGE}"
116+
git tag -a "${NPM_VERSION}" -m "${COMMIT_MESSAGE}"
109117
git commit -m "[Automated Changes] ${COMMIT_MESSAGE}"
110118
git push origin --tags HEAD:master
111119
echo "Creating github release..."
112-
gh release create "${COMMIT_MESSAGE}" --generate-notes --verify-tag
120+
gh release create "${NPM_VERSION}" --generate-notes --verify-tag
113121
git push
114-
122+
- name: Publish Go node_modules
123+
env:
124+
VERSION: ${{ env.NPM_VERSION }}
125+
run: |
126+
GOPROXY=proxy.golang.org go list -m github.com/ccxt/ccxt@$VERSION
115127
- name: C# Release Build
116128
run: npm run buildCSRelease
117129
- name: Deploy C#

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ __pycache__
4040
*.pyc
4141
bin/
4242
obj/
43-
43+
__debug*
44+
*.o
4445
.custom_gitignore

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ python:
44
- 3.8.3
55
git:
66
depth: 2
7+
addons:
8+
artifacts:
9+
debug: true
710
cache:
811
directories:
912
- ".cache"
@@ -44,6 +47,10 @@ before_install:
4447
- sudo apt-get install -y dotnet-runtime-7.0
4548
- dotnet --version
4649
- dotnet --info
50+
- wget https://go.dev/dl/go1.21.6.linux-amd64.tar.gz
51+
- sudo tar -xvf go1.21.6.linux-amd64.tar.gz -C /usr/local
52+
- echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.profile
53+
- go version
4754
- type -p curl >/dev/null || ( apt update && apt install curl -y)
4855
- curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg && sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null && sudo apt update && sudo apt install gh -y
4956

@@ -84,6 +91,7 @@ after_deploy:
8491
- cat ~/.npm/_logs/*-debug.log
8592
env:
8693
global:
94+
- GO111MODULE=on
8795
# PYPI_TOKEN
8896
- secure: QCQa8tn40loCqSx9tR7PFAa7h5Gucl9IaEARiskiPF3zlLoZTst6ThnFl1rQDKx2jIJsN1z8Vwd12I4kYGED+Tgd345kE+J/n2hVEpTwSB+oOUWlwgkTQbCQB0XxPJRPila9lt3Ogpbvrp//L7IkFM06gu+N+qwQEOjn7dkLrVAkg5vl9nouuzqHadaQ1oOMG/v3DgPq5NHaCEWK0285Oqs8dqecVRSFGAtRPTDHzO/nv4stsbnmEWO1pu/SYXyxum1CYv9glfFaRNWoQ9EWjl8ogCzBsH3lkFeNhwLOcSyMFmTpXH1soXcLzJTWoUgfx9hiiC8IqOxx4nGurOK6OyY9QO8aVwyRdYlbpdLFuEiGuadfI6Rbz41TewZ+r3OmFn4gy5oRIM0fE+2Osj3btmsdZYktXdErqhRm18bV/oO/IOteKEXdbC7+PJ5obnpbBKKfcSNOAF8F/en+qouAywghq8UBJ9zHtWSsDXOUeCdvXLGKA01pbdaDpmtHBU9u5iX+KnSbBP+iAb7+i/uymxFTJbseYllPa1IFUf4wNf6AFlfgLnZT064KF1oiIGnvpczqD+UV4fDIukKaBaC0gK2nUQUiSNPs1kFJbs7QiQO5I07RgpAACigDWlp6tKLSUoA4wUXIG1APF5Eh001FAQ01lCjsxhMh3OmtgEIaxkI=
8997
# new new GITHUB_TOKEN

README.md

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Build Status](https://img.shields.io/travis/com/ccxt/ccxt)](https://travis-ci.com/ccxt/ccxt) [![npm](https://img.shields.io/npm/v/ccxt.svg)](https://npmjs.com/package/ccxt) [![PyPI](https://img.shields.io/pypi/v/ccxt.svg)](https://pypi.python.org/pypi/ccxt) [![NPM Downloads](https://img.shields.io/npm/dy/ccxt.svg)](https://www.npmjs.com/package/ccxt) [![Discord](https://img.shields.io/discord/690203284119617602?logo=discord&logoColor=white)](https://discord.gg/ccxt) [![Supported Exchanges](https://img.shields.io/badge/exchanges-112-blue.svg)](https://github.com/ccxt/ccxt/wiki/Exchange-Markets) [![Twitter Follow](https://img.shields.io/twitter/follow/ccxt_official.svg?style=social&label=CCXT)](https://twitter.com/ccxt_official)
44

5-
A JavaScript / Python / PHP / C# library for cryptocurrency trading and e-commerce with support for many bitcoin/ether/altcoin exchange markets and merchant APIs.
5+
A JavaScript / Python / PHP / C# / Go library for cryptocurrency trading and e-commerce with support for many bitcoin/ether/altcoin exchange markets and merchant APIs.
66

77
### [Install](#install) · [Usage](#usage) · [Manual](https://github.com/ccxt/ccxt/wiki) · [FAQ](https://github.com/ccxt/ccxt/wiki/FAQ) · [Examples](https://github.com/ccxt/ccxt/tree/master/examples) · [Contributing](https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md) · [Social](#social)
88

@@ -187,12 +187,15 @@ The easiest way to install the CCXT library is to use a package manager:
187187
- [ccxt in **PyPI**](https://pypi.python.org/pypi/ccxt) (Python 3.7.0+)
188188
- [ccxt in **Packagist/Composer**](https://packagist.org/packages/ccxt/ccxt) (PHP 8.1+)
189189
- [ccxt in **Nuget**](https://www.nuget.org/packages/ccxt) (netstandard 2.0)
190+
- [ccxt in **GO**](https://pkg.go.dev/github.com/ccxt/ccxt)
190191

191192
This library is shipped as an all-in-one module implementation with minimalistic dependencies and requirements:
192193

193194
- [js/](https://github.com/ccxt/ccxt/blob/master/js/) in JavaScript
194-
- [python/](https://github.com/ccxt/ccxt/blob/master/python/) in Python (generated from JS)
195-
- [php/](https://github.com/ccxt/ccxt/blob/master/php/) in PHP (generated from JS)
195+
- [python/](https://github.com/ccxt/ccxt/blob/master/python/) in Python (generated from TS)
196+
- [php/](https://github.com/ccxt/ccxt/blob/master/php/) in PHP (generated from TS)
197+
- [cs/](https://github.com/ccxt/ccxt/blob/master/cs/) in C# (generated from TS)
198+
- [go/](https://github.com/ccxt/ccxt/blob/master/go/) in Go (generated from TS)
196199

197200
You can also clone it into your project directory from [ccxt GitHub repository](https://github.com/ccxt/ccxt):
198201

@@ -296,6 +299,19 @@ using ccxt;
296299
Console.WriteLine(ccxt.Exchanges) // check this later
297300
```
298301

302+
### Go
303+
304+
[ccxt in GO with] (https://pkg.go.dev/github.com/ccxt/ccxt)
305+
306+
```shell
307+
go get github.com/ccxt/ccxt
308+
```
309+
310+
```Go
311+
import "ccxt"
312+
fmt.Println(ccxt.Exchanges)
313+
```
314+
299315
### Docker
300316

301317
You can get CCXT installed in a container along with all the supported languages and dependencies. This may be useful if you want to contribute to CCXT (e.g. run the build scripts and tests — please see the [Contributing](https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md) document for the details on that).
@@ -529,6 +545,73 @@ class Project {
529545
}
530546
```
531547

548+
### Go
549+
550+
```Go
551+
package main
552+
import (
553+
"github.com/ccxt/ccxt/go/ccxt"
554+
"fmt"
555+
)
556+
557+
func main() {
558+
exchange := ccxt.NewBinance(map[string]interface{}{
559+
"apiKey": "MY KEY",
560+
"secret": "MY SECRET",
561+
})
562+
orderParams := map[string]interface{}{
563+
"clientOrderId": "myOrderId68768678",
564+
}
565+
566+
<-exchange.LoadMarkets()
567+
568+
order, err := exchange.CreateOrder("BTC/USDT", "limit", "buy", 0.001, ccxt.WithCreateOrderPrice(6000), ccxt.WithCreateOrderParams(orderParams))
569+
if err != nil {
570+
if ccxtError, ok := err.(*ccxt.Error); ok {
571+
if ccxtError.Type == "InvalidOrder" {
572+
fmt.Println("Invalid order")
573+
} else {
574+
fmt.Println("Some other error")
575+
}
576+
}
577+
} else {
578+
fmt.Println(*order.Id)
579+
}
580+
581+
582+
// fetching OHLCV
583+
ohlcv, err := exchange.FetchOHLCV("BTC/USDT", ccxt.WithFetchOHLCVTimeframe("5m"), ccxt.WithFetchOHLCVLimit(100))
584+
585+
if err != nil {
586+
fmt.Println("Error: ", err)
587+
} else {
588+
fmt.Println("Got OHLCV!")
589+
}
590+
}
591+
```
592+
593+
#### Optional parameters
594+
595+
Unlike Javascript/Python/PHP/C# Go does not support "traditional" optional parameters like `function a(optional = false)`. However, the CCXT language and structure have some methods with optional params, and since the Go language is transpiled from the Typescript source, we had to find a way of representing them.
596+
597+
We have decided to "go" (pun intended) with Option structs and the `WithX` methods.
598+
599+
For example, this function `FetchMyTrades` supports 4 different "optional" parameters, symbol, since, limit, and params.
600+
601+
```Golang
602+
func (this *Binance) FetchMyTrades(options ...FetchMyTradesOptions) ([]Trade, error)
603+
```
604+
605+
And we can provide them by doing
606+
607+
```Golang
608+
trades, error := exchange.FetchMyTrades(ccxt.withFetchMyTradesSymbol("BTC/USDT"), ccxt.WithFetchOHLCVLimit(5), ccxt.WithFetchMyTradesParams(orderParams))
609+
```
610+
611+
Lastly, just because the signature dictates that some argument like `symbol` is optional, it will depend from exchange to exchange and you might need to provide it to avoid getting a `SymbolRequired` error.
612+
613+
You can check different examples in the `examples/go` folder.
614+
532615
## Contributing
533616

534617
Please read the [CONTRIBUTING](https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md) document before making changes that you would like adopted in the code. Also, read the [Manual](https://github.com/ccxt/ccxt/wiki) for more details.

build-go.sh

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/bin/bash
2+
3+
# Function to print a message with elapsed time
4+
print_message() {
5+
local start_time=$1
6+
while true; do
7+
elapsed=$((SECONDS - start_time))
8+
echo "GO Build is still running... Elapsed time: ${elapsed}s"
9+
sleep 30
10+
done
11+
}
12+
13+
14+
timeout_kill() {
15+
local pid=$1
16+
local timeout=$((60 * 15)) # 10 minutes in seconds
17+
local elapsed=0
18+
19+
echo "Monitoring process $pid for timeout..."
20+
21+
while kill -0 "$pid" 2>/dev/null; do
22+
if (( elapsed >= timeout )); then
23+
cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}') # CPU usage as a percentage
24+
mem_usage=$(free -m | awk '/Mem:/ { printf "%.2f%%", $3/$2 * 100 }') # Memory usage as a percentage
25+
echo "Stats before killing | CPU Usage: ${cpu_usage}% | RAM Usage: ${mem_usage}"
26+
echo "Timeout reached: Killing the go build process (PID: $pid)"
27+
kill -6 "$pid"
28+
return
29+
fi
30+
sleep 1
31+
((elapsed++))
32+
done
33+
34+
echo "Process $pid has completed within the timeout."
35+
}
36+
37+
38+
# Capture the start time
39+
start_time=$SECONDS
40+
41+
# Start the message-printing function in the background, passing the start time
42+
print_message $start_time &
43+
message_pid=$!
44+
45+
# Trap SIGINT (Ctrl+C) and SIGTERM to clean up background processes
46+
cleanup() {
47+
echo "Stopping background process..."
48+
kill $message_pid 2>/dev/null
49+
wait $message_pid 2>/dev/null
50+
exit 1
51+
}
52+
trap cleanup SIGINT SIGTERM
53+
54+
# Run the main command and capture its exit code
55+
export GOMAXPROCS=1
56+
57+
# Command to run
58+
echo "Will download modules"
59+
# go mod download
60+
echo "Will build the project"
61+
go build -p=1 -x -trimpath -ldflags="-s -w" -o ccxt ./go/ccxt &
62+
pid_go_build=$!
63+
64+
if [[ -z "$pid_go_build" ]]; then
65+
echo "Error: Failed to capture PID for go build."
66+
exit 1
67+
fi
68+
69+
# Start the timeout monitoring in the background
70+
timeout_kill "$pid_go_build" &
71+
wait $pid_go_build
72+
command_exit_code=$?
73+
74+
# Kill the background message-printing process
75+
kill $message_pid 2>/dev/null
76+
wait $message_pid 2>/dev/null
77+
78+
# Exit with the same code as the main command
79+
exit $command_exit_code

0 commit comments

Comments
 (0)