Skip to content
5 changes: 1 addition & 4 deletions src/model/account.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BigNumber } from "bignumber.js";
import { AccountFlags, AccountID, AccountThresholds, Signer } from "./";
import { AccountID, AccountThresholds, Signer } from "./";

export interface IAccountBase {
id: string;
Expand All @@ -9,7 +9,6 @@ export interface IAccountBase {
inflationDestination: AccountID;
homeDomain: string;
thresholds: AccountThresholds;
flags: AccountFlags;
signers?: Signer[];
}

Expand All @@ -27,7 +26,6 @@ export class Account implements IAccount {
public inflationDestination: AccountID;
public homeDomain: string;
public thresholds: AccountThresholds;
public flags: AccountFlags;
public lastModified: number;
public signers?: Signer[];
public readonly sellingLiabilities: BigNumber;
Expand All @@ -42,7 +40,6 @@ export class Account implements IAccount {
this.homeDomain = data.homeDomain;
this.lastModified = data.lastModified;
this.thresholds = data.thresholds;
this.flags = data.flags;
this.signers = data.signers;
this.sellingLiabilities = data.sellingLiabilities;
this.buyingLiabilities = data.buyingLiabilities;
Expand Down
7 changes: 0 additions & 7 deletions src/model/account_values.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { IAccountBase } from "./account";
import { AccountFlags } from "./account_flags";
import { AccountThresholds } from "./account_thresholds";
import { Signer } from "./signer";

Expand All @@ -15,7 +14,6 @@ export class AccountValues implements IAccountValues {
public inflationDestination: string;
public homeDomain: string;
public thresholds: AccountThresholds;
public flags: AccountFlags;
public signers: Signer[];

constructor(data: IAccountValues) {
Expand All @@ -26,7 +24,6 @@ export class AccountValues implements IAccountValues {
this.inflationDestination = data.inflationDestination;
this.homeDomain = data.homeDomain;
this.thresholds = data.thresholds;
this.flags = data.flags;

this.signers = Signer.sortArray(data.signers);
}
Expand All @@ -46,10 +43,6 @@ export class AccountValues implements IAccountValues {
}
}

if (!this.flags.equals(other.flags)) {
changedAttrs.push("flags");
}

if (!this.thresholds.equals(other.thresholds)) {
changedAttrs.push("thresholds");
}
Expand Down
64 changes: 64 additions & 0 deletions src/model/asset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { AccountID, AssetCode, IAssetInput } from "./";
import { AccountFlagsFactory } from "./factories";

interface IAssetData {
code: AssetCode;
issuer: AccountID;
totalSupply: string;
circulatingSupply: string;
holdersCount: string;
unauthorizedHoldersCount: string;
lastModifiedIn: number;
flags: number;
}

export class Asset {
public static readonly NATIVE_ID = "native";
public readonly code: AssetCode;
public readonly issuer: AccountID | null;
public readonly totalSupply: string;
public readonly circulatingSupply: string;
public readonly holdersCount: string;
public readonly unauthorizedHoldersCount: string;
public readonly lastModifiedIn: number;
public readonly authRequired: boolean;
public readonly authRevocable: boolean;
public readonly authImmutable: boolean;

constructor(data: IAssetData) {
this.code = data.code;
this.issuer = data.issuer;
this.totalSupply = data.totalSupply;
this.circulatingSupply = data.circulatingSupply;
this.holdersCount = data.holdersCount;
this.unauthorizedHoldersCount = data.unauthorizedHoldersCount;
this.lastModifiedIn = data.lastModifiedIn;

const parsedFlags = AccountFlagsFactory.fromValue(data.flags);
this.authRequired = parsedFlags.authRequired;
this.authRevocable = parsedFlags.authRevocable;
this.authImmutable = parsedFlags.authImmutable;
}

public get native(): boolean {
return this.code === "XLM" && this.issuer === null;
}

public get id() {
return this.native ? "native" : `${this.code}-${this.issuer}`;
}

public get paging_token() {
return this.id;
}

public toInput(): IAssetInput {
const res: IAssetInput = { code: this.code };

if (this.issuer) {
res.issuer = this.issuer;
}

return res;
}
}
5 changes: 2 additions & 3 deletions src/model/balance.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { BigNumber } from "bignumber.js";
import { Asset } from "stellar-sdk";
import { AccountID, AssetID } from "./";

export interface IBalanceBase {
account: AccountID;
asset: Asset;
asset: AssetID;
limit: BigNumber;
balance: BigNumber;
authorized: boolean;
Expand All @@ -24,7 +23,7 @@ export class Balance implements IBalance {
}

public account: AccountID;
public asset: Asset;
public asset: AssetID;
public readonly limit: BigNumber;
public readonly balance: BigNumber;
public authorized: boolean;
Expand Down
5 changes: 2 additions & 3 deletions src/model/balance_values.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { BigNumber } from "bignumber.js";
import { Asset } from "stellar-sdk";
import { IBalanceBase } from "./balance";
import { AssetID, IBalanceBase } from "./";

export class BalanceValues implements IBalanceBase {
public account: string;
public asset: Asset;
public asset: AssetID;
public limit: BigNumber;
public balance: BigNumber;
public authorized: boolean;
Expand Down
5 changes: 1 addition & 4 deletions src/model/factories/account_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { BigNumber } from "bignumber.js";
import { VarArray } from "js-xdr";
import { xdr } from "stellar-base";
import { Account, IAccount } from "../account";
import { SignerFactory } from "./";
import { AccountFlagsFactory } from "./account_flags_factory";
import { AccountThresholdsFactory } from "./account_thresholds_factory";
import { AccountThresholdsFactory, SignerFactory } from "./";

export interface IAccountTableRow {
accountid: string;
Expand Down Expand Up @@ -32,7 +30,6 @@ export class AccountFactory {
homeDomain: row.homedomain,
lastModified: row.lastmodified,
thresholds: AccountThresholdsFactory.fromValue(row.thresholds),
flags: AccountFlagsFactory.fromValue(row.flags),
sellingLiabilities: new BigNumber(row.sellingliabilities),
buyingLiabilities: new BigNumber(row.buyingliabilities)
};
Expand Down
5 changes: 1 addition & 4 deletions src/model/factories/account_values_factory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { AccountFlagsFactory } from "./account_flags_factory";
import { AccountThresholdsFactory } from "./account_thresholds_factory";
import { SignerFactory } from "./signer_factory";
import { AccountThresholdsFactory, SignerFactory } from "./";

import { AccountValues, IAccountValues } from "../";

Expand All @@ -22,7 +20,6 @@ export class AccountValuesFactory {
inflationDestination: xdr.inflationDest() || null,
homeDomain: xdr.homeDomain(),
thresholds,
flags: AccountFlagsFactory.fromValue(xdr.flags()),
signers
};

Expand Down
46 changes: 36 additions & 10 deletions src/model/factories/asset_factory.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,53 @@
import { Asset, xdr as XDR } from "stellar-base";
import { xdr as XDR } from "stellar-base";
import { Asset as StellarAsset } from "stellar-sdk";
import { AccountID, Asset, AssetCode, AssetID, IAssetInput } from "../";
import { HorizonAssetType } from "../../datasource/types";
import { IAssetInput } from "../asset_input";

export interface IAssetTableRow {
assetid: AssetID;
code: AssetCode;
issuer: AccountID;
total_supply: string;
circulating_supply: string;
holders_count: string;
unauthorized_holders_count: string;
flags: number;
last_activity: number;
}

export class AssetFactory {
public static fromDb(type: number, code: string, issuer: string) {
return type === XDR.AssetType.assetTypeNative().value ? Asset.native() : new Asset(code, issuer);
public static fromDb(row: IAssetTableRow): Asset {
return new Asset({
code: row.code,
issuer: row.issuer,
totalSupply: row.total_supply,
circulatingSupply: row.circulating_supply,
holdersCount: row.holders_count,
unauthorizedHoldersCount: row.unauthorized_holders_count,
lastModifiedIn: row.last_activity,
flags: row.flags
});
}

public static fromTrustline(type: number, code: string, issuer: string): StellarAsset {
return type === XDR.AssetType.assetTypeNative().value ? StellarAsset.native() : new StellarAsset(code, issuer);
}

public static fromHorizon(type: HorizonAssetType, code?: string, issuer?: string) {
return type === "native" ? Asset.native() : new Asset(code, issuer);
return type === "native" ? StellarAsset.native() : new StellarAsset(code!, issuer!);
}

public static fromInput(arg: IAssetInput) {
if (arg.issuer && arg.code) {
return new Asset(arg.code, arg.issuer);
return new StellarAsset(arg.code, arg.issuer);
}

return Asset.native();
return StellarAsset.native();
}

public static fromId(id: string) {
if (id === "native") {
return Asset.native();
return StellarAsset.native();
}

const [code, issuer] = id.split("-");
Expand All @@ -30,10 +56,10 @@ export class AssetFactory {
throw new Error(`Invalid asset id "${id}"`);
}

return new Asset(code, issuer);
return new StellarAsset(code, issuer);
}

public static fromXDR(xdr: any, encoding = "base64") {
return Asset.fromOperation(XDR.Asset.fromXDR(xdr, encoding));
return StellarAsset.fromOperation(XDR.Asset.fromXDR(xdr, encoding));
}
}
7 changes: 3 additions & 4 deletions src/model/factories/balance_factory.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import BigNumber from "bignumber.js";
import { Asset, xdr as XDR } from "stellar-base";
import { xdr as XDR } from "stellar-base";
import { Account, Balance, IBalance } from "../";
import { MAX_INT64 } from "../../util";
import { getReservedBalance } from "../../util/base_reserve";
import { AssetFactory } from "./";

export interface ITrustLineTableRow {
accountid: string;
Expand All @@ -28,7 +27,7 @@ export class BalanceFactory {
balance,
limit,
lastModified: row.lastmodified,
asset: AssetFactory.fromDb(row.assettype, row.assetcode, row.issuer),
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)
Expand All @@ -44,7 +43,7 @@ export class BalanceFactory {

return new Balance({
account: account.id,
asset: Asset.native(),
asset: "native",
balance,
limit,
authorized: true,
Expand Down
4 changes: 2 additions & 2 deletions src/model/factories/balance_values_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { BalanceValues } from "../balance_values";
export class BalanceValuesFactory {
public static fromXDR(xdr: any): BalanceValues {
return new BalanceValues({
asset: Asset.fromOperation(xdr.asset()),
asset: Asset.fromOperation(xdr.asset()).toString(),
account: publicKeyFromXDR(xdr),
balance: new BigNumber(xdr.balance().toString()),
limit: new BigNumber(xdr.limit().toString()),
Expand All @@ -19,7 +19,7 @@ export class BalanceValuesFactory {
public static fakeNativeFromXDR(xdr: any): BalanceValues {
return new BalanceValues({
account: publicKeyFromXDR(xdr),
asset: Asset.native(),
asset: "native",
limit: new BigNumber(MAX_INT64),
authorized: true,
balance: new BigNumber(xdr.balance().toString())
Expand Down
2 changes: 2 additions & 0 deletions src/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export * from "./account_thresholds";
export * from "./account_flags";
export * from "./account_values";
export * from "./account_subscription_payload";
export * from "./asset";
export * from "./asset_code";
export * from "./asset_id";
export * from "./asset_input";
export * from "./data_entry";
Expand Down
Loading