diff --git a/migrations/mysql/20220504184155_fix_net_asset_column.sql b/migrations/mysql/20220504184155_fix_net_asset_column.sql new file mode 100644 index 000000000..2d5a9d271 --- /dev/null +++ b/migrations/mysql/20220504184155_fix_net_asset_column.sql @@ -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 diff --git a/migrations/sqlite3/20211211034818_add_nav_history_details.sql b/migrations/sqlite3/20211211034818_add_nav_history_details.sql index 163b9787e..56860040c 100644 --- a/migrations/sqlite3/20211211034818_add_nav_history_details.sql +++ b/migrations/sqlite3/20211211034818_add_nav_history_details.sql @@ -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 diff --git a/migrations/sqlite3/20220503144849_add_margin_info_to_nav.sql b/migrations/sqlite3/20220503144849_add_margin_info_to_nav.sql index 9f6cec2da..7f2fc06e0 100644 --- a/migrations/sqlite3/20220503144849_add_margin_info_to_nav.sql +++ b/migrations/sqlite3/20220503144849_add_margin_info_to_nav.sql @@ -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; diff --git a/migrations/sqlite3/20220504184155_fix_net_asset_column.sql b/migrations/sqlite3/20220504184155_fix_net_asset_column.sql new file mode 100644 index 000000000..96993735e --- /dev/null +++ b/migrations/sqlite3/20220504184155_fix_net_asset_column.sql @@ -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 diff --git a/pkg/migrations/mysql/20220504184155_fix_net_asset_column.go b/pkg/migrations/mysql/20220504184155_fix_net_asset_column.go new file mode 100644 index 000000000..c986d66fe --- /dev/null +++ b/pkg/migrations/mysql/20220504184155_fix_net_asset_column.go @@ -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 +} diff --git a/pkg/migrations/sqlite3/20211211034818_add_nav_history_details.go b/pkg/migrations/sqlite3/20211211034818_add_nav_history_details.go index 63ee75254..4dc6eda9e 100644 --- a/pkg/migrations/sqlite3/20211211034818_add_nav_history_details.go +++ b/pkg/migrations/sqlite3/20211211034818_add_nav_history_details.go @@ -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 } diff --git a/pkg/migrations/sqlite3/20220503144849_add_margin_info_to_nav.go b/pkg/migrations/sqlite3/20220503144849_add_margin_info_to_nav.go index d3098f7da..849e7e19a 100644 --- a/pkg/migrations/sqlite3/20220503144849_add_margin_info_to_nav.go +++ b/pkg/migrations/sqlite3/20220503144849_add_margin_info_to_nav.go @@ -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 } diff --git a/pkg/migrations/sqlite3/20220504184155_fix_net_asset_column.go b/pkg/migrations/sqlite3/20220504184155_fix_net_asset_column.go new file mode 100644 index 000000000..d398ad527 --- /dev/null +++ b/pkg/migrations/sqlite3/20220504184155_fix_net_asset_column.go @@ -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 +} diff --git a/pkg/service/account.go b/pkg/service/account.go index d64cdc176..2c599ca08 100644 --- a/pkg/service/account.go +++ b/pkg/service/account.go @@ -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, diff --git a/pkg/types/asset.go b/pkg/types/asset.go index 13c34b5a7..76737744c 100644 --- a/pkg/types/asset.go +++ b/pkg/types/asset.go @@ -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"` } diff --git a/pkg/types/balance.go b/pkg/types/balance.go index 5ca3906d3..bf3fb4cf0 100644 --- a/pkg/types/balance.go +++ b/pkg/types/balance.go @@ -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 }