Skip to content
This repository has been archived by the owner on Feb 2, 2024. It is now read-only.

Commit

Permalink
fix: wallet balances to sats and cents with ok sign (#75)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastien Verreault <[email protected]>
  • Loading branch information
sebastienverreault and sebver-pm-me authored Feb 24, 2022
1 parent 713685b commit d7116d8
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 23 deletions.
107 changes: 107 additions & 0 deletions migrations/20220224015723769_wallet-fix.sql
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 $$;
25 changes: 15 additions & 10 deletions src/DealerRemoteWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "@apollo/client/core"
import fetch from "node-fetch"
import { pino } from "pino"
import { cents2usd, sat2btc } from "./utils"

const IN_MEMORY_CACHE_CONFIG = {
typePolicies: {
Expand All @@ -18,22 +19,22 @@ const IN_MEMORY_CACHE_CONFIG = {
// read() {
// return [
// {
// id: "USDWallet",
// balance: 100,
// walletCurrency: "USD",
// },
// {
// id: "BTCWallet",
// balance: 1_000_000, // 100 USD @ 10k USD/BTC
// balance: 1_000_000, // 100 USD in sats @ 10k USD/BTC
// walletCurrency: "BTC",
// },
// {
// id: "USDWallet",
// balance: -10000, // 100 USD in cents
// walletCurrency: "USD",
// },
// ]
// },
// },
// getLastOnChainAddress: {
// read() {
// return {
// id: "bc1qmyhq2rm8edqv076dj89r5utskt3394m7xu3pge",
// id: "tb1q740rclcmn0sz6etpxrlmya0x0kudcvgqrhg8tf",
// }
// },
// },
Expand Down Expand Up @@ -96,12 +97,16 @@ export class DealerRemoteWallet implements GaloyWallet {

const usdWallet = result.data.wallets?.find((wallet) => wallet?.id === "USDWallet")
const usdWalletId = usdWallet?.id
// TODO: figure out if the balance will always be positive or not in that new implementation
const usdWalletBalance = -usdWallet?.balance ?? NaN
const usdWalletBalance = usdWallet?.balance ?? NaN

return {
ok: true,
value: { btcWalletId, btcWalletBalance, usdWalletId, usdWalletBalance },
value: {
btcWalletId,
btcWalletBalance: sat2btc(btcWalletBalance),
usdWalletId,
usdWalletBalance: cents2usd(usdWalletBalance),
},
}
} catch (error) {
logger.error(
Expand Down
11 changes: 8 additions & 3 deletions src/DealerRemoteWalletV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "@apollo/client/core"
import fetch from "node-fetch"
import { pino } from "pino"
import { cents2usd, sat2btc } from "./utils"

import { QUERIES, MUTATIONS } from "@galoymoney/client"

Expand Down Expand Up @@ -78,12 +79,16 @@ export class DealerRemoteWalletV2 implements GaloyWallet {
(wallet) => wallet?.__typename !== "BTCWallet",
)
const usdWalletId = usdWallet?.id
// TODO: figure out if the balance will always be positive or not in that new implementation
const usdWalletBalance = -usdWallet?.balance ?? NaN
const usdWalletBalance = usdWallet?.balance ?? NaN

return {
ok: true,
value: { btcWalletId, btcWalletBalance, usdWalletId, usdWalletBalance },
value: {
btcWalletId,
btcWalletBalance: sat2btc(btcWalletBalance),
usdWalletId,
usdWalletBalance: cents2usd(usdWalletBalance),
},
}
} catch (error) {
logger.error(
Expand Down
23 changes: 14 additions & 9 deletions src/DealerSimulatedWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "@apollo/client/core"
import fetch from "node-fetch"
import { pino } from "pino"
import { cents2usd, sat2btc } from "./utils"

const IN_MEMORY_CACHE_CONFIG = {
typePolicies: {
Expand All @@ -17,16 +18,16 @@ const IN_MEMORY_CACHE_CONFIG = {
wallets: {
read() {
return [
{
id: "USDWallet",
balance: 100,
walletCurrency: "USD",
},
{
id: "BTCWallet",
balance: 1_000_000, // 100 USD @ 10k USD/BTC
balance: 1_000_000, // 100 USD in sats @ 10k USD/BTC
walletCurrency: "BTC",
},
{
id: "USDWallet",
balance: -10000, // 100 USD in cents
walletCurrency: "USD",
},
]
},
},
Expand Down Expand Up @@ -96,12 +97,16 @@ export class DealerSimulatedWallet implements GaloyWallet {

const usdWallet = result.data.wallets?.find((wallet) => wallet?.id === "USDWallet")
const usdWalletId = usdWallet?.id
// TODO: figure out if the balance will always be positive or not in that new implementation
const usdWalletBalance = -usdWallet?.balance ?? NaN
const usdWalletBalance = usdWallet?.balance ?? NaN

return {
ok: true,
value: { btcWalletId, btcWalletBalance, usdWalletId, usdWalletBalance },
value: {
btcWalletId,
btcWalletBalance: sat2btc(btcWalletBalance),
usdWalletId,
usdWalletBalance: cents2usd(usdWalletBalance),
},
}
} catch (error) {
logger.error(
Expand Down
2 changes: 1 addition & 1 deletion test/integration/GraphqlRepository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("GraphqlRepository", () => {
const walletObj = walletRow.jsonData[0]
expect(walletObj).toBeTruthy()
expect(walletObj.id).toBe("BTCWallet")
expect(walletObj.balance).toBeLessThanOrEqual(0)
expect(walletObj.balance).toBeGreaterThanOrEqual(0)
expect(walletObj.walletCurrency).toBe("BTC")

const walletObj2 = walletRow.jsonData[1]
Expand Down

0 comments on commit d7116d8

Please sign in to comment.