Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix liabilities on ORM entities for Account and Trustline #209

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
32 changes: 0 additions & 32 deletions src/model/factories/balance_factory.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
38 changes: 32 additions & 6 deletions src/orm/entities/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
50 changes: 43 additions & 7 deletions src/orm/entities/trustline.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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] =
Expand All @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions tests/factories/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down