package main
import (
"fmt"
"./api"
"./utils"
//"time"
)
var idCount int = 1
func main() {
fmt.Printf("----\n")
JsonParse := utils.NewJsonStruct()
//v := utils.JsonConfig{}
utils.JsonCfg = new(utils.JsonConfig)
JsonParse.Load("config.json", utils.JsonCfg)
api.BaseUrl = utils.JsonCfg.BaseUrl
//fmt.Printf("Base Url: %s\n",api.BaseUrl)
//fmt.Println(utils.JsonCfg)
//fmt.Println(utils.JsonCfg.Sql)
engine := utils.GetEngine(utils.JsonCfg.Sql)
a,e := utils.GetUserInfo(engine)
if e == nil {
for _, v := range a {
fmt.Println(v)
}
} else {
fmt.Printf("user info get error\n")
return
}
apiCmd := new(api.ApiCommand)
meCfg,e := apiCmd.CmdGet(utils.JsonCfg.MeConfigUrl)
if e != nil {
fmt.Printf("meCfg get error\n")
return
}
utils.MeCfg = new(utils.MeConfig)
JsonParse.LoadFromString(meCfg,utils.MeCfg)
//////////////////////////////// trade /////////////////////////////////////
tradeRes := new(api.TradeRes)
Type := "buy"
price := "100"
amount := "10"
for _,market := range utils.MeCfg.Con.Markets {
for _,v := range a {
//(apikey string,market string,Type string,price string,amount string)
params := api.BaseTradeUrl(v.Fkey,market.Name,Type,price,amount)
if Type == "buy"{
Type = "sell"
} else {
Type = "buy"
}
str := api.BaseUrl + params["method?"]
delete(params,"method?")
ps := utils.CreatParams(v.Fsecret,params)
str += ps
fmt.Printf("url: %s \n",str)
bu,e := apiCmd.CmdPost(str)
if e != nil {
fmt.Printf("user: %d ,trade error\n",v.Fuser)
continue
}
//fmt.Printf("bu: %s \n",bu)
ecode,err := apiCmd.ApiResParse(bu,tradeRes)
if err != nil {
fmt.Printf("parse error %s\n",err.Error())
continue
}
if ecode != 0 {
fmt.Printf("user get info err: %d\n",ecode)
continue
}
//fmt.Printf("user info[%s] = \n%s\n\n",v.Fuser,bu)
}
}
////////////////////////////// balance ////////////////////////////////////
/*
userinfoRes := new(api.UserinfoRes)
for _, v := range a {
params := make(map[string]string)
params["apikey"]=v.Fkey
secKey := v.Fsecret
fmt.Printf("user: %d \n",v.Fuser)
//sign := utils.CreatSign(secKey,params)
ps := utils.CreatParams(secKey,params)
str := api.BaseUrl + "balance.do?" + ps//api.Build_userinfo_cmd(v.Fkey,sign)
fmt.Printf("url: %s \n",str)
bu,e := apiCmd.CmdPost(str)
if e != nil {
fmt.Printf("user: %d ,info get error\n",v.Fuser)
continue
}
//fmt.Printf("bu: %s \n",bu)
ecode,err := apiCmd.ApiResParse(bu,userinfoRes)
if err != nil {
fmt.Printf("parse error %s\n",err.Error())
continue
}
if ecode != 0 {
fmt.Printf("user get info err: %d\n",ecode)
continue
}
//fmt.Printf("user info[%s] = \n%s\n\n",v.Fuser,bu)
}
//fmt.Println(utils.MeCfg.Con.Assets[0] )
//fmt.Println(utils.MeCfg.Con.Markets[0].Money )
//////////////////////////////// ticker test ///////////////////////////////////
ticker := new(api.Ticker)
for _,market := range utils.MeCfg.Con.Markets {
str := api.Build_ticker_cmd(market.Name)
fmt.Printf("url: %s\n",str)
bti,et := apiCmd.CmdGet(str)
if et != nil {
fmt.Printf("market %s ,ticker get error\n",market.Name)
continue
}
//fmt.Println(bti)
ecode,err := apiCmd.ApiResParse(bti,ticker)
if err != nil {
fmt.Printf("parse error %s\n",err.Error())
continue
}
if ecode != 0 {
fmt.Printf("api get res err: %d\n",ecode)
continue
}
fmt.Printf("ticker[%s] = \n%s\n\n",market.Name,bti)
}
/////////////////////////////////// depth test /////////////////////////
depth := new(api.DepthRes)
for _,market := range utils.MeCfg.Con.Markets {
str := api.Build_depth_cmd(market.Name)
fmt.Printf("url: %s\n",str)
bdp,et := apiCmd.CmdGet(str)
if et != nil {
fmt.Printf("market %s ,depth get error\n",market.Name)
continue
}
//fmt.Println(bti)
ecode,err := apiCmd.ApiResParse(bdp,depth)
if err != nil {
fmt.Printf("parse error %s\n",err.Error())
continue
}
if ecode != 0 {
fmt.Printf("api get res err: %d\n",ecode)
continue
}
fmt.Printf("depth[%s] = \n%s\n\n",market.Name,bdp)
}
////////////////////////////////////////////////////////////////////
trades := new(api.TradesRes)
t:=time.Now().Unix()
ts := t - 86400
tss := fmt.Sprintf("%d",ts)
for _,market := range utils.MeCfg.Con.Markets {
str := api.Build_trades_cmd(market.Name,tss)
fmt.Printf("url: %s\n",str)
bdp,et := apiCmd.CmdGet(str)
if et != nil {
fmt.Printf("market %s ,trades get error\n",market.Name)
continue
}
//fmt.Println(bti)
ecode,err := apiCmd.ApiResParse(bdp,trades)
if err != nil {
fmt.Printf("parse error %s\n",err.Error())
continue
}
if ecode != 0 {
fmt.Printf("api get res err: %d\n",ecode)
continue
}
fmt.Printf("trades[%s] = \n%s\n\n",market.Name,bdp)
}*/
}