This repository has been archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: wallet balances to sats and cents with ok sign (#75)
Co-authored-by: Sebastien Verreault <[email protected]>
- Loading branch information
1 parent
713685b
commit d7116d8
Showing
5 changed files
with
145 additions
and
23 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,107 @@ | ||
-- Up Migration | ||
DO $$ | ||
DECLARE btcBalance FLOAT := 0; | ||
DECLARE usdBalance FLOAT := 0; | ||
DECLARE lastId INTEGER := 0; | ||
DECLARE result TEXT := ''; | ||
BEGIN | ||
SELECT | ||
json_data -> 0 ->> 'balance' as btcBalance | ||
FROM dealer.wallet | ||
WHERE json_data::json -> 0 ->> 'id' = 'BTCWallet' | ||
INTO btcBalance; | ||
raise notice 'btcBalance = %', btcBalance; | ||
|
||
SELECT | ||
json_data -> 1 ->> 'balance' as usdBalance | ||
FROM dealer.wallet | ||
WHERE json_data::json -> 1 ->> 'id' = 'USDWallet' | ||
into usdBalance; | ||
raise notice 'usdBalance = %', usdBalance; | ||
|
||
SELECT id | ||
FROM dealer.wallet | ||
ORDER BY id DESC | ||
LIMIT 1 | ||
into lastId; | ||
|
||
SELECT json_data | ||
from dealer.wallet | ||
WHERE id = lastId | ||
INTO result; | ||
raise notice 'original row: %', result; | ||
|
||
UPDATE dealer.wallet | ||
SET json_data = jsonb_set(json_data::jsonb, '{0,balance}', (ABS(btcBalance) * 100000000)::text::jsonb, false) | ||
WHERE id = lastId; | ||
|
||
SELECT json_data | ||
from dealer.wallet | ||
WHERE id = lastId | ||
INTO result; | ||
raise notice 'new row: %', result; | ||
|
||
UPDATE dealer.wallet | ||
SET json_data = jsonb_set(json_data::jsonb, '{1,balance}', (-ABS(usdBalance) * 100)::text::jsonb, false) | ||
WHERE id = lastId; | ||
|
||
SELECT json_data | ||
from dealer.wallet | ||
WHERE id = lastId | ||
INTO result; | ||
raise notice 'new row: %', result; | ||
END $$; | ||
|
||
-- Down Migration | ||
DO $$ | ||
DECLARE btcBalance FLOAT := 0; | ||
DECLARE usdBalance FLOAT := 0; | ||
DECLARE lastId INTEGER := 0; | ||
DECLARE result TEXT := ''; | ||
BEGIN | ||
SELECT | ||
json_data -> 0 ->> 'balance' as btcBalance | ||
FROM dealer.wallet | ||
WHERE json_data::json -> 0 ->> 'id' = 'BTCWallet' | ||
INTO btcBalance; | ||
raise notice 'btcBalance = %', btcBalance; | ||
|
||
SELECT | ||
json_data -> 1 ->> 'balance' as usdBalance | ||
FROM dealer.wallet | ||
WHERE json_data::json -> 1 ->> 'id' = 'USDWallet' | ||
into usdBalance; | ||
raise notice 'usdBalance = %', usdBalance; | ||
|
||
SELECT id | ||
FROM dealer.wallet | ||
ORDER BY id DESC | ||
LIMIT 1 | ||
into lastId; | ||
|
||
SELECT json_data | ||
from dealer.wallet | ||
WHERE id = lastId | ||
INTO result; | ||
raise notice 'original row: %', result; | ||
|
||
UPDATE dealer.wallet | ||
SET json_data = jsonb_set(json_data::jsonb, '{0,balance}', (ABS(btcBalance) / 100000000)::text::jsonb, false) | ||
WHERE id = lastId; | ||
|
||
SELECT json_data | ||
from dealer.wallet | ||
WHERE id = lastId | ||
INTO result; | ||
raise notice 'new row: %', result; | ||
|
||
UPDATE dealer.wallet | ||
SET json_data = jsonb_set(json_data::jsonb, '{1,balance}', (-ABS(usdBalance) / 100)::text::jsonb, false) | ||
WHERE id = lastId; | ||
|
||
SELECT json_data | ||
from dealer.wallet | ||
WHERE id = lastId | ||
INTO result; | ||
raise notice 'new row: %', result; | ||
END $$; |
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
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