Merge pull request #588 from c9s/fix-net-asset

fix: add interest and fix net asset column
This commit is contained in:
Yo-An Lin 2022-05-04 19:17:15 +08:00 committed by GitHub
commit bfb61c3129
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 135 additions and 25 deletions

View File

@ -0,0 +1,19 @@
-- +up
-- +begin
ALTER TABLE `nav_history_details`
MODIFY COLUMN `net_asset` DECIMAL(32, 8) DEFAULT 0.00000000 NOT NULL,
CHANGE COLUMN `balance_in_usd` `net_asset_in_usd` DECIMAL(32, 2) DEFAULT 0.00000000 NOT NULL,
CHANGE COLUMN `balance_in_btc` `net_asset_in_btc` DECIMAL(32, 20) DEFAULT 0.00000000 NOT NULL;
-- +end
-- +begin
ALTER TABLE `nav_history_details`
ADD COLUMN `interest` DECIMAL(32, 20) UNSIGNED DEFAULT 0.00000000 NOT NULL;
-- +end
-- +down
-- +begin
ALTER TABLE `nav_history_details`
DROP COLUMN `interest`;
-- +end

View File

@ -2,16 +2,16 @@
-- +begin
CREATE TABLE `nav_history_details`
(
gid bigint unsigned auto_increment PRIMARY KEY,
`exchange` VARCHAR NOT NULL DEFAULT '',
`subaccount` VARCHAR NOT NULL DEFAULT '',
time DATETIME(3) NOT NULL DEFAULT (strftime('%s','now')),
currency VARCHAR NOT NULL,
balance_in_usd DECIMAL DEFAULT 0.00000000 NOT NULL,
balance_in_btc DECIMAL DEFAULT 0.00000000 NOT NULL,
balance DECIMAL DEFAULT 0.00000000 NOT NULL,
available DECIMAL DEFAULT 0.00000000 NOT NULL,
locked DECIMAL DEFAULT 0.00000000 NOT NULL
`gid` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`exchange` VARCHAR(30) NOT NULL DEFAULT '',
`subaccount` VARCHAR(30) NOT NULL DEFAULT '',
`time` DATETIME(3) NOT NULL DEFAULT (strftime('%s', 'now')),
`currency` VARCHAR(30) NOT NULL,
`net_asset_in_usd` DECIMAL DEFAULT 0.00000000 NOT NULL,
`net_asset_in_btc` DECIMAL DEFAULT 0.00000000 NOT NULL,
`balance` DECIMAL DEFAULT 0.00000000 NOT NULL,
`available` DECIMAL DEFAULT 0.00000000 NOT NULL,
`locked` DECIMAL DEFAULT 0.00000000 NOT NULL
);
-- +end
-- +begin

View File

@ -1,8 +1,8 @@
-- +up
ALTER TABLE `nav_history_details` ADD COLUMN `session` VARCHAR(50) NOT NULL;
ALTER TABLE `nav_history_details` ADD COLUMN `borrowed` DECIMAL UNSIGNED DEFAULT 0.00000000 NOT NULL;
ALTER TABLE `nav_history_details` ADD COLUMN `net_asset` DECIMAL UNSIGNED DEFAULT 0.00000000 NOT NULL;
ALTER TABLE `nav_history_details` ADD COLUMN `price_in_usd` DECIMAL UNSIGNED DEFAULT 0.00000000 NOT NULL;
ALTER TABLE `nav_history_details` ADD COLUMN `borrowed` DECIMAL DEFAULT 0.00000000 NOT NULL;
ALTER TABLE `nav_history_details` ADD COLUMN `net_asset` DECIMAL DEFAULT 0.00000000 NOT NULL;
ALTER TABLE `nav_history_details` ADD COLUMN `price_in_usd` DECIMAL DEFAULT 0.00000000 NOT NULL;
ALTER TABLE `nav_history_details` ADD COLUMN `is_margin` BOOL DEFAULT FALSE NOT NULL;
ALTER TABLE `nav_history_details` ADD COLUMN `is_isolated` BOOL DEFAULT FALSE NOT NULL;
ALTER TABLE `nav_history_details` ADD COLUMN `isolated_symbol` VARCHAR(30) DEFAULT '' NOT NULL;

View File

@ -0,0 +1,11 @@
-- +up
-- +begin
ALTER TABLE `nav_history_details` ADD COLUMN `interest` DECIMAL DEFAULT 0.00000000 NOT NULL;
-- +end
-- +down
-- +begin
SELECT 1;
-- +end

View File

@ -0,0 +1,39 @@
package mysql
import (
"context"
"github.com/c9s/rockhopper"
)
func init() {
AddMigration(upFixNetAssetColumn, downFixNetAssetColumn)
}
func upFixNetAssetColumn(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {
// This code is executed when the migration is applied.
_, err = tx.ExecContext(ctx, "ALTER TABLE `nav_history_details`\n MODIFY COLUMN `net_asset` DECIMAL(32, 8) DEFAULT 0.00000000 NOT NULL,\n CHANGE COLUMN `balance_in_usd` `net_asset_in_usd` DECIMAL(32, 2) DEFAULT 0.00000000 NOT NULL,\n CHANGE COLUMN `balance_in_btc` `net_asset_in_btc` DECIMAL(32, 20) DEFAULT 0.00000000 NOT NULL;")
if err != nil {
return err
}
_, err = tx.ExecContext(ctx, "ALTER TABLE `nav_history_details`\n ADD COLUMN `interest` DECIMAL(32, 20) UNSIGNED DEFAULT 0.00000000 NOT NULL;")
if err != nil {
return err
}
return err
}
func downFixNetAssetColumn(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {
// This code is executed when the migration is rolled back.
_, err = tx.ExecContext(ctx, "ALTER TABLE `nav_history_details`\n DROP COLUMN `interest`;")
if err != nil {
return err
}
return err
}

View File

@ -14,7 +14,7 @@ func init() {
func upAddNavHistoryDetails(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {
// This code is executed when the migration is applied.
_, err = tx.ExecContext(ctx, "CREATE TABLE `nav_history_details`\n(\n gid bigint unsigned auto_increment PRIMARY KEY,\n `exchange` VARCHAR NOT NULL DEFAULT '',\n `subaccount` VARCHAR NOT NULL DEFAULT '',\n time DATETIME(3) NOT NULL DEFAULT (strftime('%s','now')),\n currency VARCHAR NOT NULL,\n balance_in_usd DECIMAL DEFAULT 0.00000000 NOT NULL,\n balance_in_btc DECIMAL DEFAULT 0.00000000 NOT NULL,\n balance DECIMAL DEFAULT 0.00000000 NOT NULL,\n available DECIMAL DEFAULT 0.00000000 NOT NULL,\n locked DECIMAL DEFAULT 0.00000000 NOT NULL\n);")
_, err = tx.ExecContext(ctx, "CREATE TABLE `nav_history_details`\n(\n `gid` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,\n `exchange` VARCHAR(30) NOT NULL DEFAULT '',\n `subaccount` VARCHAR(30) NOT NULL DEFAULT '',\n `time` DATETIME(3) NOT NULL DEFAULT (strftime('%s', 'now')),\n `currency` VARCHAR(30) NOT NULL,\n `net_asset_in_usd` DECIMAL DEFAULT 0.00000000 NOT NULL,\n `net_asset_in_btc` DECIMAL DEFAULT 0.00000000 NOT NULL,\n `balance` DECIMAL DEFAULT 0.00000000 NOT NULL,\n `available` DECIMAL DEFAULT 0.00000000 NOT NULL,\n `locked` DECIMAL DEFAULT 0.00000000 NOT NULL\n);")
if err != nil {
return err
}

View File

@ -19,17 +19,17 @@ func upAddMarginInfoToNav(ctx context.Context, tx rockhopper.SQLExecutor) (err e
return err
}
_, err = tx.ExecContext(ctx, "ALTER TABLE `nav_history_details` ADD COLUMN `borrowed` DECIMAL UNSIGNED DEFAULT 0.00000000 NOT NULL;")
_, err = tx.ExecContext(ctx, "ALTER TABLE `nav_history_details` ADD COLUMN `borrowed` DECIMAL DEFAULT 0.00000000 NOT NULL;")
if err != nil {
return err
}
_, err = tx.ExecContext(ctx, "ALTER TABLE `nav_history_details` ADD COLUMN `net_asset` DECIMAL UNSIGNED DEFAULT 0.00000000 NOT NULL;")
_, err = tx.ExecContext(ctx, "ALTER TABLE `nav_history_details` ADD COLUMN `net_asset` DECIMAL DEFAULT 0.00000000 NOT NULL;")
if err != nil {
return err
}
_, err = tx.ExecContext(ctx, "ALTER TABLE `nav_history_details` ADD COLUMN `price_in_usd` DECIMAL UNSIGNED DEFAULT 0.00000000 NOT NULL;")
_, err = tx.ExecContext(ctx, "ALTER TABLE `nav_history_details` ADD COLUMN `price_in_usd` DECIMAL DEFAULT 0.00000000 NOT NULL;")
if err != nil {
return err
}

View File

@ -0,0 +1,34 @@
package sqlite3
import (
"context"
"github.com/c9s/rockhopper"
)
func init() {
AddMigration(upFixNetAssetColumn, downFixNetAssetColumn)
}
func upFixNetAssetColumn(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {
// This code is executed when the migration is applied.
_, err = tx.ExecContext(ctx, "ALTER TABLE `nav_history_details` ADD COLUMN `interest` DECIMAL DEFAULT 0.00000000 NOT NULL;")
if err != nil {
return err
}
return err
}
func downFixNetAssetColumn(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {
// This code is executed when the migration is rolled back.
_, err = tx.ExecContext(ctx, "SELECT 1;")
if err != nil {
return err
}
return err
}

View File

@ -31,8 +31,8 @@ func (s *AccountService) InsertAsset(time time.Time, session string, name types.
subaccount,
time,
currency,
balance_in_usd,
balance_in_btc,
net_asset_in_usd,
net_asset_in_btc,
balance,
available,
locked,

View File

@ -11,15 +11,21 @@ import (
)
type Asset struct {
Currency string `json:"currency" db:"currency"`
Total fixedpoint.Value `json:"total" db:"total"`
InUSD fixedpoint.Value `json:"inUSD" db:"in_usd"`
InBTC fixedpoint.Value `json:"inBTC" db:"in_btc"`
Currency string `json:"currency" db:"currency"`
Total fixedpoint.Value `json:"total" db:"total"`
NetAsset fixedpoint.Value `json:"netAsset" db:"net_asset"`
// InUSD is net asset in USD
InUSD fixedpoint.Value `json:"inUSD" db:"net_asset_in_usd"`
// InBTC is net asset in BTC
InBTC fixedpoint.Value `json:"inBTC" db:"net_asset_in_btc"`
Time time.Time `json:"time" db:"time"`
Locked fixedpoint.Value `json:"lock" db:"lock" `
Available fixedpoint.Value `json:"available" db:"available"`
Borrowed fixedpoint.Value `json:"borrowed" db:"borrowed"`
NetAsset fixedpoint.Value `json:"netAsset" db:"net_asset"`
PriceInUSD fixedpoint.Value `json:"priceInUSD" db:"price_in_usd"`
}

View File

@ -31,8 +31,9 @@ func (b Balance) Net() fixedpoint.Value {
total := b.Total()
netAsset := b.NetAsset
if netAsset.IsZero() {
netAsset = total.Sub(b.Borrowed)
netAsset = total.Sub(b.Borrowed).Sub(b.Interest)
}
return netAsset
}