Skip to content
This repository has been archived by the owner on Oct 27, 2024. It is now read-only.

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
trading-peter committed Apr 13, 2021
1 parent d0709ae commit fe2737c
Show file tree
Hide file tree
Showing 119 changed files with 14,505 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

__debug_bin
20 changes: 20 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}",
"args": [
"-k", "xxx",
"-s", "xxx",
"-a", "xxx"
]
}
]
}
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# FTX AUTO LENDING

This is a CLI tool that allows to automatically compound payouts earned from lending coins.
It will check for newly available funds every hour (5 min after hour elapsed to be precise) and automatically update the lending offer to the max. size that can be lend out on the account.

## Example
ftx-auto-lend --key xxxxxxx --secret yyyyyyyy --subaccount mylendingsubacc --coin USD

14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/trading-peter/ftx-auto-lend

go 1.16

require (
github.com/akamensky/argparse v1.2.2
github.com/avast/retry-go v3.0.0+incompatible
github.com/grishinsana/goftx v1.2.0
github.com/robfig/cron/v3 v3.0.0
github.com/shopspring/decimal v1.2.0
go.uber.org/ratelimit v0.2.0
)

replace github.com/grishinsana/goftx => ../goftx
51 changes: 51 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
github.com/akamensky/argparse v1.2.2 h1:P17T0ZjlUNJuWTPPJ2A5dM1wxarHgHqfYH+AZTo2xQA=
github.com/akamensky/argparse v1.2.2/go.mod h1:S5kwC7IuDcEr5VeXtGPRVZ5o/FdhcMlQz4IZQuw64xA=
github.com/andres-erbsen/clock v0.0.0-20160526145045-9e14626cd129 h1:MzBOUgng9orim59UnfUTLRjMpd09C5uEVQ6RPGeCaVI=
github.com/andres-erbsen/clock v0.0.0-20160526145045-9e14626cd129/go.mod h1:rFgpPQZYZ8vdbc+48xibu8ALc3yeyd64IhHS+PU6Yyg=
github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/avast/retry-go v3.0.0+incompatible h1:4SOWQ7Qs+oroOTQOYnAHqelpCO0biHSxpiH9JdtuBj0=
github.com/avast/retry-go v3.0.0+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/go-numb/go-ftx v0.0.0-20200829181514-3144aa68f505/go.mod h1:rjG/Mg/la6U9w0NN/oaMZkgCpEQgseKPOl6EkvYkjCw=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/klauspost/compress v1.10.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E=
github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.16.0/go.mod h1:YOKImeEosDdBPnxc0gy7INqi3m1zK6A+xl6TwOBhHCA=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/ratelimit v0.2.0 h1:UQE2Bgi7p2B85uP5dC2bbRtig0C+OeNRnNEafLjsLPA=
go.uber.org/ratelimit v0.2.0/go.mod h1:YYBV4e4naJvhpitQrWJu1vCpgB7CboMe0qhltKt6mUg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
32 changes: 32 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"io/ioutil"
"log"
"os"
)

var (
Trace *log.Logger
Info *log.Logger
Warning *log.Logger
Error *log.Logger
)

func init() {
Trace = log.New(ioutil.Discard,
"TRACE: ",
log.LUTC|log.Ldate|log.Ltime|log.Lshortfile)

Info = log.New(os.Stdout,
"INFO: ",
log.LUTC|log.Ldate|log.Ltime|log.Lshortfile)

Warning = log.New(os.Stdout,
"WARNING: ",
log.LUTC|log.Ldate|log.Ltime|log.Lshortfile)

Error = log.New(os.Stderr,
"ERROR: ",
log.LUTC|log.Ldate|log.Ltime|log.Lshortfile)
}
133 changes: 133 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package main

import (
"fmt"
"os"
"strings"
"time"

"github.com/akamensky/argparse"
"github.com/avast/retry-go"
"github.com/grishinsana/goftx"
"github.com/robfig/cron/v3"
"github.com/shopspring/decimal"
"go.uber.org/ratelimit"
)

var (
limiter ratelimit.Limiter = ratelimit.New(30)
client *goftx.Client
)

func main() {
job := cron.New()
parser := argparse.NewParser("ftx-auto-lend", "Automatically compounds lending payouts.")
apiKey := parser.String("k", "key", &argparse.Options{Required: true, Help: "API key"})
apiSecret := parser.String("s", "secret", &argparse.Options{Required: true, Help: "API secret"})
subAcc := parser.String("a", "subaccount", &argparse.Options{Required: false, Help: "Subaccount"})
coin := strings.ToUpper(*parser.String("c", "coin", &argparse.Options{Required: false, Default: "USD", Help: "Coin to lend"}))
strRate := strings.ToUpper(*parser.String("r", "min-rate", &argparse.Options{Required: false, Default: "0.000001", Help: "Coin to lend"}))
err := parser.Parse(os.Args)

if err != nil {
fmt.Print(parser.Usage(err))
return
}

if coin == "" {
coin = "USD"
}

if strRate == "" {
strRate = "0.000001"
}

minRate, err := decimal.NewFromString(strRate)

if err != nil {
Error.Fatal("Min Rate: Invalid number")
}

client = goftx.New(
goftx.WithAuth(*apiKey, *apiSecret),
goftx.WithSubaccount(*subAcc),
)

_, err = client.GetAccountInformation()

if err != nil {
Error.Fatalln("It seems like the supplied API key is wrong. Please check and try again")
}

job.Start()

job.AddFunc("5 * * * *", func() {
lendable, delta, err := getMaxLendingAmount(coin)

if err != nil {
Error.Println(err)
}

if delta.Equal(decimal.Zero) || delta.LessThan(decimal.Zero) {
Info.Println("No increase in funds to update lending offer.")
return
}

Info.Printf("New lendable amount of %s is %s (+%s).", coin, lendable, delta)
Info.Printf("Updating lending offer with a minimum rate of %s.", minRate)

updateLendingOffer(coin, lendable, minRate)
})

fmt.Printf("Will attempt to update your lending offer for %s each hour.\nPress any key if you want to stop and exit the program.", coin)
fmt.Scanln()
fmt.Println("Bye!")
}

func updateLendingOffer(coin string, amount decimal.Decimal, minRate decimal.Decimal) (err error) {
err = retry.Do(
func() error {
limiter.Take()
err := client.SubmitLendingOffer(coin, amount, minRate)

if err != nil {
fmt.Printf("%+v\n", err)
return err
}

return nil
},
retry.Delay(time.Minute),
retry.Attempts(10),
retry.DelayType(retry.FixedDelay),
)

return
}

func getMaxLendingAmount(coin string) (lendable decimal.Decimal, delta decimal.Decimal, err error) {
err = retry.Do(
func() error {
limiter.Take()
resp, err := client.GetLendingInfo()

if err != nil {
return err
}

for i := range resp {
if resp[i].Coin == "USD" {
lendable = resp[i].Lendable
delta = resp[i].Lendable.Sub(resp[i].Offered)
}
}

return nil
},
retry.Delay(time.Minute),
retry.Attempts(10),
retry.DelayType(retry.FixedDelay),
)

return
}
16 changes: 16 additions & 0 deletions vendor/github.com/akamensky/argparse/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions vendor/github.com/akamensky/argparse/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/akamensky/argparse/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fe2737c

Please sign in to comment.