From b67cc1f7c42d1dd069ae3df265540cd0e0c24842 Mon Sep 17 00:00:00 2001 From: Timur Ramazanov Date: Fri, 13 Nov 2020 13:11:54 +0300 Subject: [PATCH] Fix liabilities on ORM entities for Account and Trustline --- package.json | 2 +- src/model/factories/balance_factory.ts | 32 ----------------- src/orm/entities/account.ts | 38 ++++++++++++++++---- src/orm/entities/trustline.ts | 50 ++++++++++++++++++++++---- tests/factories/account.ts | 2 -- yarn.lock | 8 ++--- 6 files changed, 80 insertions(+), 52 deletions(-) diff --git a/package.json b/package.json index 26bcbc18..bf462b4a 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "progress": "^2.0.3", "retry": "^0.12.0", "squel": "^5.12.2", - "stellar-base": "^3.0.4", + "stellar-base": "^4.0.2", "typeorm": "^0.2.17", "typescript-memoize": "^1.0.0-alpha.3", "winston": "^3.0.0" diff --git a/src/model/factories/balance_factory.ts b/src/model/factories/balance_factory.ts index 2c37d1c4..83b83aaf 100644 --- a/src/model/factories/balance_factory.ts +++ b/src/model/factories/balance_factory.ts @@ -1,42 +1,10 @@ import BigNumber from "bignumber.js"; -import { xdr as XDR } from "stellar-base"; import { Balance, IBalance } from "../"; import { Account } from "../../orm/entities"; import { MAX_INT64 } from "../../util"; import { getReservedBalance } from "../../util/base_reserve"; -export interface ITrustLineTableRow { - accountid: string; - assettype: number; - issuer: string; - assetcode: string; - tlimit: string; - balance: string; - flags: number; - lastmodified: number; - buyingliabilities: string | null; - sellingliabilities: string | null; -} - export class BalanceFactory { - public static fromDb(row: ITrustLineTableRow): Balance { - const balance = new BigNumber(row.balance); - const limit = new BigNumber(row.tlimit); - - const data: IBalance = { - account: row.accountid, - balance, - limit, - lastModified: row.lastmodified, - asset: `${row.assetcode}-${row.issuer}`, - authorized: (row.flags & XDR.TrustLineFlags.authorizedFlag().value) > 0, - spendableBalance: balance.minus(row.sellingliabilities || 0), - receivableBalance: limit.minus(row.buyingliabilities || 0).minus(balance) - }; - - return new Balance(data); - } - public static nativeForAccount(account: Account): IBalance { const balance = new BigNumber(account.balance); const limit = new BigNumber(MAX_INT64); diff --git a/src/orm/entities/account.ts b/src/orm/entities/account.ts index 1b6a389d..c3a82352 100644 --- a/src/orm/entities/account.ts +++ b/src/orm/entities/account.ts @@ -57,18 +57,44 @@ export class Account { lastModified: number; @Column({ - name: "buyingliabilities", - type: "bigint", + name: "extension", + type: "text", nullable: true, - transformer: BigNumberTransformer + transformer: { + from: (value: string | null) => { + if (!value) { + return null; + } + + const extension = xdr.AccountEntryExt.fromXDR(value, "base64") + + return new BigNumber(extension.v1().liabilities().buying()); + }, + // we don't actually need `to` transform, + // because we never write to the db, so it's just a stab + to: (value: BigNumber) => null + } }) buyingLiabilities: BigNumber | null; @Column({ - name: "sellingliabilities", - type: "bigint", + name: "extension", + type: "text", nullable: true, - transformer: BigNumberTransformer + transformer: { + from: (value: string | null) => { + if (!value) { + return null; + } + + const extension = xdr.AccountEntryExt.fromXDR(value, 'base64'); + + return new BigNumber(extension.v1().liabilities().selling()); + }, + // we don't actually need `to` transform, + // because we never write to the db, so it's just a stab + to: (value: BigNumber) => null + } }) sellingLiabilities: BigNumber | null; diff --git a/src/orm/entities/trustline.ts b/src/orm/entities/trustline.ts index 4f7c9248..1c9776d4 100644 --- a/src/orm/entities/trustline.ts +++ b/src/orm/entities/trustline.ts @@ -1,5 +1,5 @@ import BigNumber from "bignumber.js"; -import { xdr as XDR } from "stellar-base"; +import { xdr } from "stellar-base"; import { Column, Entity, PrimaryColumn } from "typeorm"; import { AccountID, AssetID, IBalance } from "../../model"; import { AssetFactory } from "../../model/factories"; @@ -32,11 +32,47 @@ export class TrustLine implements IBalance { @Column({ name: "lastmodified" }) lastModified: number; - @Column({ type: "bigint", name: "buyingliabilities", transformer: BigNumberTransformer }) - buyingLiabilities: BigNumber; - - @Column({ type: "bigint", name: "sellingliabilities", transformer: BigNumberTransformer }) - sellingLiabilities: BigNumber; + @Column({ + name: "extension", + type: "text", + nullable: true, + transformer: { + from: (value: string | null) => { + if (!value) { + return null; + } + + const extension = xdr.AccountEntryExt.fromXDR(value, "base64") + + return new BigNumber(extension.v1().liabilities().buying()); + }, + // we don't actually need `to` transform, + // because we never write to the db, so it's just a stab + to: (value: BigNumber) => null + } + }) + buyingLiabilities: BigNumber | null; + + @Column({ + name: "extension", + type: "text", + nullable: true, + transformer: { + from: (value: string | null) => { + if (!value) { + return null; + } + + const extension = xdr.AccountEntryExt.fromXDR(value, 'base64'); + + return new BigNumber(extension.v1().liabilities().selling()); + }, + // we don't actually need `to` transform, + // because we never write to the db, so it's just a stab + to: (value: BigNumber) => null + } + }) + sellingLiabilities: BigNumber | null; public static parsePagingToken(token: string) { const [accountId, , balance] = @@ -53,7 +89,7 @@ export class TrustLine implements IBalance { } public get authorized(): boolean { - return (this.flags & XDR.TrustLineFlags.authorizedFlag().value) > 0; + return (this.flags & xdr.TrustLineFlags.authorizedFlag().value) > 0; } public get spendableBalance(): BigNumber { diff --git a/tests/factories/account.ts b/tests/factories/account.ts index 241e7551..ba502a68 100644 --- a/tests/factories/account.ts +++ b/tests/factories/account.ts @@ -13,8 +13,6 @@ Factory.define("account") .attr("thresholds", "AQAAAA==") .attr("flags", 0) .attr("lastModified", 6) - .attr("sellingLiabilities", new BigNumber("8927364")) - .attr("buyingLiabilities", new BigNumber("2948361")); export default { build(overrides?: object): Account { diff --git a/yarn.lock b/yarn.lock index 96d6e523..c10087f3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5710,10 +5710,10 @@ stealthy-require@^1.1.1: resolved "https://registry.yarnpkg.com/stealthy-require/-/stealthy-require-1.1.1.tgz#35b09875b4ff49f26a777e509b3090a3226bf24b" integrity sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks= -stellar-base@^3.0.4: - version "3.0.4" - resolved "https://registry.yarnpkg.com/stellar-base/-/stellar-base-3.0.4.tgz#9f9b183921886c326609c37b701a725320e5e004" - integrity sha512-aSa5T7/y+jYmgdpyZKwvc13u1pR4MLI7yaDMaVV7tGp98lFcvd1F37QlZXf0j5QS7NVVpXWZ5iDmftZcjx2Euw== +stellar-base@^4.0.2: + version "4.0.2" + resolved "https://registry.yarnpkg.com/stellar-base/-/stellar-base-4.0.2.tgz#f31302d4e54721be63809f910ae6e986c4cb1aaa" + integrity sha512-ATRKXCCcNCCmSTU/BmoHGvOaOmPdm0OHQgXVQjn5gqLXyMpdAncRs5G4tLN0oU9Rz7OCcyBSU6isO1gd8T4EDQ== dependencies: base32.js "^0.1.0" bignumber.js "^4.0.0"