Skip to content

Commit

Permalink
MOD: add load asssets cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
pando-ci authored and cw35 committed Dec 22, 2023
1 parent 087e5a1 commit 6a566a8
Show file tree
Hide file tree
Showing 9 changed files with 478 additions and 11 deletions.
90 changes: 90 additions & 0 deletions cmd/asset-fswap.go
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)
}
100 changes: 100 additions & 0 deletions cmd/asset-rings.go
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)
}
62 changes: 62 additions & 0 deletions cmd/asset.go
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)
}
9 changes: 9 additions & 0 deletions cmd/utils.go
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
}
25 changes: 15 additions & 10 deletions core/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"errors"
"time"

"github.com/shopspring/decimal"
)

var (
Expand All @@ -12,16 +14,19 @@ var (

type (
Asset struct {
ID uint64 `sql:"PRIMARY_KEY;" json:"id"`
AssetID string `sql:"size:36;" json:"asset_id,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
Version int64 `sql:"not null" json:"version,omitempty"`
Verified bool `json:"verified"`
Name string `sql:"size:64" json:"name,omitempty"`
Symbol string `sql:"size:32" json:"symbol,omitempty"`
DisplaySymbol string `sql:"size:32;default:null;" json:"display_symbol,omitempty"`
ChainID string `sql:"size:36" json:"chain_id,omitempty"`
ID uint64 `sql:"PRIMARY_KEY;" json:"id"`
AssetID string `sql:"size:36;" json:"asset_id,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
Version int64 `sql:"not null" json:"version,omitempty"`
Verified bool `json:"verified"`
Name string `sql:"size:64" json:"name,omitempty"`
Symbol string `sql:"size:32" json:"symbol,omitempty"`
Logo string `sql:"size:256" json:"logo,omitempty"`
DisplaySymbol string `sql:"size:32;default:null;" json:"display_symbol,omitempty"`
ChainID string `sql:"size:36" json:"chain_id,omitempty"`
Price decimal.Decimal `sql:"type:decimal(32,8);default:0" json:"price,omitempty"`
PriceUpdatedAt *time.Time `sql:"default:null" json:"price_updated_at,omitempty"`
}

// AssetStore defines operations for working with assets on db.
Expand Down
28 changes: 27 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ require (
github.com/gofrs/uuid v4.2.0+incompatible
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/lib/pq v1.10.6
github.com/ethereum/go-ethereum v1.10.16
github.com/fatih/structs v1.1.0
github.com/fox-one/mixin-sdk-go v1.5.10
github.com/fox-one/pkg v1.5.5
github.com/go-chi/chi v4.1.2+incompatible
github.com/gofrs/uuid v4.2.0+incompatible
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/lib/pq v1.10.4
github.com/mitchellh/go-homedir v1.1.0
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c
github.com/patrickmn/go-cache v2.1.0+incompatible
Expand All @@ -32,7 +40,7 @@ require (
)

require (
filippo.io/edwards25519 v1.0.0 // indirect
filippo.io/edwards25519 v1.0.0-rc.1 // indirect
github.com/btcsuite/btcd v0.22.0-beta // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.1 // indirect
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce // indirect
Expand Down Expand Up @@ -92,6 +100,24 @@ require (
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
github.com/spf13/afero v1.8.1 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.10.1 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/tklauser/go-sysconf v0.3.9 // indirect
github.com/tklauser/numcpus v0.4.0 // indirect
github.com/ugorji/go/codec v1.2.6 // indirect
github.com/vmihailenco/tagparser v0.1.2 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down

0 comments on commit 6a566a8

Please sign in to comment.