Skip to content

Commit

Permalink
feat: add price rate api (bittorrent#273)
Browse files Browse the repository at this point in the history
Co-authored-by: fish <[email protected]>
  • Loading branch information
2 people authored and daniel-tron committed Dec 13, 2022
1 parent db1511a commit d6c7622
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 12 deletions.
78 changes: 68 additions & 10 deletions core/commands/cheque/cheque.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Vault services include issue cheque to peer, receive cheque and store operations
"cashstatus": ChequeCashStatusCmd,
"cashlist": ChequeCashListCmd,
"price": StorePriceCmd,
"price-all": StorePriceAllCmd,

"send": SendChequeCmd,
"sendlist": ListSendChequesCmd,
Expand Down Expand Up @@ -96,9 +97,17 @@ Vault services include issue cheque to peer, receive cheque and store operations
},
}

type PriceInfo struct {
Token string `json:"token"`
TokenStr string `json:"token_str"`
Price string `json:"price"`
Rate string `json:"rate"`
TotalPrice string `json:"total_price"`
}

var StorePriceCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Get btfs store price.",
Tagline: "Get btfs token price.",
},
Options: []cmds.Option{
cmds.StringOption(tokencfg.TokenTypeName, "tk", "file storage with token type,default WBTT, other TRX/USDD/USDT.").WithDefault("WBTT"),
Expand All @@ -113,21 +122,70 @@ var StorePriceCmd = &cmds.Command{
return errors.New("your input token is none. ")
}

price, err := chain.SettleObject.OracleService.CurrentPrice(token)
if err != nil {
return err
}

rate, err := chain.SettleObject.OracleService.CurrentRate(token)
if err != nil {
return err
}

totalPrice, err := chain.SettleObject.OracleService.CurrentTotalPrice(token)
if err != nil {
return err
}

return cmds.EmitOnce(res, &StorePriceRet{
Price: totalPrice,
})
priceInfo := PriceInfo{
Token: token.String(),
TokenStr: tokenStr,
Price: price.String(),
Rate: rate.String(),
TotalPrice: totalPrice.String(),
}

return cmds.EmitOnce(res, &priceInfo)
},
Type: StorePriceRet{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *StorePriceRet) error {
_, err := fmt.Fprintf(w, "the btfs store price: %v\n", out.Price)
return err
}),
}

var StorePriceAllCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Get btfs all price.",
},
RunTimeout: 5 * time.Minute,
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {

mp := make(map[string]*PriceInfo, 0)
for k, token := range tokencfg.MpTokenAddr {

price, err := chain.SettleObject.OracleService.CurrentPrice(token)
if err != nil {
return err
}

rate, err := chain.SettleObject.OracleService.CurrentRate(token)
if err != nil {
return err
}

totalPrice, err := chain.SettleObject.OracleService.CurrentTotalPrice(token)
if err != nil {
return err
}

priceInfo := PriceInfo{
Token: token.String(),
TokenStr: k,
Price: price.String(),
Rate: rate.String(),
TotalPrice: totalPrice.String(),
}

mp[k] = &priceInfo
}

return cmds.EmitOnce(res, &mp)
},
}

Expand Down
1 change: 1 addition & 0 deletions core/commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ func TestCommands(t *testing.T) {
"/cheque/cashstatus",
"/cheque/chaininfo",
"/cheque/price",
"/cheque/price-all",
"/cheque/receive",
"/cheque/receive-history-list",
"/cheque/receive-history-peer",
Expand Down
3 changes: 2 additions & 1 deletion core/commands/storage/upload/upload/upload_shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ func UploadShard(rss *sessions.RenterSession, hp helper.IHostsProvider, price in
ctx, _ := context.WithTimeout(rss.Ctx, 60*time.Second)
output, err := remote.P2PCall(ctx, rss.CtxParams.N, rss.CtxParams.Api, hostPid, "/storage/upload/supporttokens")
if err != nil {
return nil
fmt.Printf("uploadShard, remote.P2PCall(supporttokens) timeout, hostPid = %v, will try again. \n", hostPid)
return err
}

fmt.Println("1 get from supporttokens,", string(output), reflect.TypeOf(output))
Expand Down
2 changes: 1 addition & 1 deletion spin/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

const (
hostContractsSyncPeriod = 2 * 60 * time.Minute
hostContractsSyncPeriod = 24 * 60 * time.Minute
hostContractsSyncTimeout = 10 * time.Minute
)

Expand Down

0 comments on commit d6c7622

Please sign in to comment.