diff --git a/UPGRADES.md b/UPGRADES.md index 922bb6f..14e0274 100644 --- a/UPGRADES.md +++ b/UPGRADES.md @@ -1,9 +1,19 @@ +# General notes + +In the next commands, you should replace `flexassistant.db` by the content of the `database_file` setting if it's has been changed. + +# 1.2 to 1.3 + +The balance has reached maximum of **int64** type so the column should be converted to **real** to handle large numbers: + +``` +sqlite3 flexassistant.db < migrations/1.2_to_1.3.sql +``` + # 1.0 to 1.1 Some numeric types have been updated from **float64** to **int64**. Upgrade the database types by running the following migration: ``` sqlite3 flexassistant.db < migrations/1.0_to_1.1.sql -``` - -And replace `flexassistant.db` by the content of the `database_file` setting if it's has been changed. \ No newline at end of file +``` \ No newline at end of file diff --git a/client.go b/client.go index aa34fa5..b2f961d 100644 --- a/client.go +++ b/client.go @@ -66,12 +66,12 @@ func (f *FlexpoolClient) request(url string) ([]byte, error) { type BalanceResponse struct { Error string `json:"error"` Result struct { - Balance int64 `json:"balance"` + Balance float64 `json:"balance"` } `json:"result"` } // MinerBalance returns the current unpaid balance -func (f *FlexpoolClient) MinerBalance(coin string, address string) (int64, error) { +func (f *FlexpoolClient) MinerBalance(coin string, address string) (float64, error) { body, err := f.request(fmt.Sprintf("%s/miner/balance?coin=%s&address=%s", FlexpoolAPIURL, coin, address)) if err != nil { return 0, err @@ -87,9 +87,9 @@ type PaymentsResponse struct { Error string `json:"error"` Result struct { Data []struct { - Hash string `json:"hash"` - Value int64 `json:"value"` - Timestamp int64 `json:"timestamp"` + Hash string `json:"hash"` + Value float64 `json:"value"` + Timestamp int64 `json:"timestamp"` } `json:"data"` } `json:"result"` } @@ -165,9 +165,9 @@ type BlocksResponse struct { Error string `json:"error"` Result struct { Data []struct { - Hash string `json:"hash"` - Number int64 `json:"number"` - Reward int64 `json:"reward"` + Hash string `json:"hash"` + Number uint64 `json:"number"` + Reward float64 `json:"reward"` } `json:"data"` } `json:"result"` } diff --git a/main.go b/main.go index 17bf1cb..6d426a5 100644 --- a/main.go +++ b/main.go @@ -132,7 +132,7 @@ func main() { log.Warnf("Could not fetch unpaid balance: %v", err) continue } - log.Debugf("Unpaid balance %d", balance) + log.Debugf("Unpaid balance %.0f", balance) miner.Balance = balance if miner.Balance != dbMiner.Balance { dbMiner.Balance = balance diff --git a/migrations/1.2_to_1.3.sql b/migrations/1.2_to_1.3.sql new file mode 100644 index 0000000..3e7da6d --- /dev/null +++ b/migrations/1.2_to_1.3.sql @@ -0,0 +1,28 @@ +-- Migrate from 1.2 to 1.3 +-- SQLite doesn't support ALTER TABLE... ALTER TYPE +-- This migration file creates the new structures with "real" types +-- instead of "integer", move data to this new table then drop the old one. + +-- miners + +ALTER TABLE `miners` RENAME TO `miners_old`; + +CREATE TABLE `miners` ( + `id` integer, + `created_at` datetime, + `updated_at` datetime, + `deleted_at` datetime, + `coin` text, + `address` text NOT NULL UNIQUE, + `balance` real, + `last_payment_timestamp` integer, + PRIMARY KEY (`id`) +); + +INSERT INTO `miners` SELECT * FROM `miners_old`; + +DROP TABLE `miners_old`; + +CREATE INDEX `idx_miners_deleted_at` ON `miners`(`deleted_at`); + +SELECT "Database migrated to 1.3" \ No newline at end of file diff --git a/miner.go b/miner.go index c32633b..65466d6 100644 --- a/miner.go +++ b/miner.go @@ -19,7 +19,7 @@ type Miner struct { gorm.Model Coin string Address string `gorm:"unique;not null"` - Balance int64 + Balance float64 LastPaymentTimestamp int64 } @@ -56,12 +56,12 @@ func (m *Miner) String() string { // Payment to store payment attributes type Payment struct { Hash string - Value int64 + Value float64 Timestamp int64 } // NewPayment creates a Payment -func NewPayment(hash string, value int64, timestamp int64) *Payment { +func NewPayment(hash string, value float64, timestamp int64) *Payment { return &Payment{ Hash: hash, Value: value, diff --git a/pool.go b/pool.go index b5f39c5..daa1abf 100644 --- a/pool.go +++ b/pool.go @@ -10,7 +10,7 @@ import ( type Pool struct { gorm.Model Coin string `gorm:"unique;not null"` - LastBlockNumber int64 + LastBlockNumber uint64 } // NewPool creates a Pool @@ -25,13 +25,13 @@ func (p *Pool) String() string { // Block to store block attributes type Block struct { - Hash string `gorm:"unique;not null"` - Number int64 `gorm:"not null"` - Reward int64 `gorm:"not null"` + Hash string `gorm:"unique;not null"` + Number uint64 `gorm:"not null"` + Reward float64 `gorm:"not null"` } // NewBlock creates a Block -func NewBlock(hash string, number int64, reward int64) *Block { +func NewBlock(hash string, number uint64, reward float64) *Block { return &Block{ Hash: hash, Number: number, diff --git a/utils.go b/utils.go index 1ccc9b8..fa57434 100644 --- a/utils.go +++ b/utils.go @@ -12,7 +12,7 @@ const MojoToXCHDivider = 1000000000000 // ConvertCurrency divides the smallest unit of the currency to the currency itself // Example: for "eth", convert from Weis to ETH -func ConvertCurrency(coin string, value int64) (float64, error) { +func ConvertCurrency(coin string, value float64) (float64, error) { switch coin { case "eth": return ConvertWeis(value), nil @@ -24,13 +24,13 @@ func ConvertCurrency(coin string, value int64) (float64, error) { } // ConvertWeis converts the value from Weis to ETH -func ConvertWeis(value int64) float64 { - return float64(value) / WeisToETHDivider +func ConvertWeis(value float64) float64 { + return value / WeisToETHDivider } // ConvertMojo converts the value from Mojo to XCH -func ConvertMojo(value int64) float64 { - return float64(value) / MojoToXCHDivider +func ConvertMojo(value float64) float64 { + return value / MojoToXCHDivider } // ConvertAction returns "Miner" for Ethereum and "Farmer" for Chia