-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
478 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
Copyright © 2020 NAME HERE <EMAIL ADDRESS> | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/fox-one/ftoken/core" | ||
"github.com/shopspring/decimal" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// fswapAssetCmd represents the load fswap assets command | ||
var fswapAssetCmd = &cobra.Command{ | ||
Use: "fswap", | ||
Short: "load fswap assets", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
ctx := cmd.Context() | ||
database, err := provideDatabase() | ||
if err != nil { | ||
cmd.PrintErrf("provideDatabase failed: %v", err) | ||
return | ||
} | ||
defer database.Close() | ||
|
||
assets := provideAssetStore(database) | ||
|
||
resp, err := http.Get("https://api.4swap.org/api/assets") | ||
if err != nil { | ||
cmd.PrintErrf("fetch 4swap assets failed: %v", err) | ||
return | ||
} | ||
|
||
var body struct { | ||
Timestamp int64 `json:"ts"` | ||
Data struct { | ||
Assets []struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
Symbol string `json:"symbol"` | ||
Logo string `json:"logo"` | ||
ChainID string `json:"chain_id"` | ||
Price decimal.Decimal `json:"price"` | ||
} `json:"assets"` | ||
Timestamp int64 `json:"ts"` | ||
TransactionCount24H int `json:"transaction_count_24h"` | ||
Volume24H decimal.Decimal `json:"volume_24h"` | ||
} `json:"data"` | ||
} | ||
|
||
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil { | ||
cmd.PrintErrf("decode 4swap assets resp failed: %v", err) | ||
return | ||
} | ||
|
||
for _, item := range body.Data.Assets { | ||
asset := core.Asset{ | ||
AssetID: item.ID, | ||
Name: item.Name, | ||
Symbol: item.Symbol, | ||
Logo: item.Logo, | ||
ChainID: item.ChainID, | ||
} | ||
if err := assets.Save(ctx, &asset); err != nil { | ||
cmd.PrintErr(err) | ||
return | ||
} | ||
|
||
cmd.Println(asset.Symbol, "saved") | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
assetCmd.AddCommand(fswapAssetCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
Copyright © 2020 NAME HERE <EMAIL ADDRESS> | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/shopspring/decimal" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// ringsAssetCmd represents the load rings assets command | ||
var ringsAssetCmd = &cobra.Command{ | ||
Use: "rings", | ||
Short: "load rings assets", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
ctx := cmd.Context() | ||
database, err := provideDatabase() | ||
if err != nil { | ||
cmd.PrintErrf("provideDatabase failed: %v", err) | ||
return | ||
} | ||
defer database.Close() | ||
|
||
assets := provideAssetStore(database) | ||
client := provideMixinClient() | ||
assetz := provideAssetService(client) | ||
|
||
resp, err := http.Get("https://rings-api.pando.im/api/v1/markets/all") | ||
if err != nil { | ||
cmd.PrintErrf("fetch rings markets failed: %v", err) | ||
return | ||
} | ||
|
||
var body struct { | ||
Data []struct { | ||
AssetID string `json:"asset_id"` | ||
CtokenAssetID string `json:"ctoken_asset_id"` | ||
Price decimal.Decimal `json:"price"` | ||
PriceUpdatedAt *time.Time `json:"price_updated_at"` | ||
} `json:"data"` | ||
} | ||
|
||
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil { | ||
cmd.PrintErrf("decode rings markets resp failed: %v", err) | ||
return | ||
} | ||
|
||
for _, item := range body.Data { | ||
{ | ||
asset, err := assetz.Find(ctx, item.AssetID) | ||
if err != nil { | ||
cmd.PrintErr(err) | ||
return | ||
} | ||
|
||
asset.Price = item.Price | ||
asset.PriceUpdatedAt = item.PriceUpdatedAt | ||
if err := assets.Save(ctx, asset); err != nil { | ||
cmd.PrintErr(err) | ||
return | ||
} | ||
cmd.Println(asset.Symbol, "saved") | ||
} | ||
|
||
{ | ||
asset, err := assetz.Find(ctx, item.CtokenAssetID) | ||
if err != nil { | ||
cmd.PrintErr(err) | ||
return | ||
} | ||
|
||
if err := assets.Save(ctx, asset); err != nil { | ||
cmd.PrintErr(err) | ||
return | ||
} | ||
cmd.Println(asset.Symbol, "saved") | ||
} | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
assetCmd.AddCommand(ringsAssetCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
Copyright © 2020 NAME HERE <EMAIL ADDRESS> | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// assetCmd represents the asset command | ||
var assetCmd = &cobra.Command{ | ||
Use: "asset", | ||
Short: "manager asset store", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
ctx := cmd.Context() | ||
database, err := provideDatabase() | ||
if err != nil { | ||
cmd.PrintErrf("provideDatabase failed: %v", err) | ||
return | ||
} | ||
defer database.Close() | ||
|
||
client := provideMixinClient() | ||
assetz := provideAssetService(client) | ||
assets := provideAssetStore(database) | ||
|
||
assetID, ok := getArg(args, 0) | ||
if !ok { | ||
cmd.PrintErr("args[0]: asset id is empty") | ||
return | ||
} | ||
|
||
asset, err := assetz.Find(ctx, assetID) | ||
if err != nil { | ||
cmd.PrintErr(err) | ||
return | ||
} | ||
|
||
if err := assets.Save(ctx, asset); err != nil { | ||
cmd.PrintErr(err) | ||
return | ||
} | ||
|
||
cmd.Println(asset.Symbol, "saved") | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(assetCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package cmd | ||
|
||
func getArg(args []string, idx int) (string, bool) { | ||
if idx < len(args) { | ||
return args[idx], true | ||
} | ||
|
||
return "", false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.