diff --git a/src/server_api.ts b/src/server_api.ts index 38b315519..e24d375cf 100644 --- a/src/server_api.ts +++ b/src/server_api.ts @@ -1,9 +1,58 @@ -import { Asset, AssetType } from "stellar-base"; +import { Asset } from "stellar-base"; import { Omit } from "utility-types"; import { Horizon } from "./horizon_api"; +// more types +import { AccountRecordSigners as AccountRecordSignersType } from "./types/account"; +import { AssetRecord as AssetRecordType } from "./types/assets"; +import { + AccountCreated, + // Types of effects + AccountCredited, + AccountDebited, + AccountFlagsUpdated, + AccountHomeDomainUpdated, + AccountSponsporshipCreated, + AccountSponsporshipRemoved, + AccountSponsporshipUpdated, + AccountThresholdsUpdated, + BaseEffectRecord, + ClaimableBalanceClaimantCreated, + ClaimableBalanceClaimed, + ClaimableBalanceCreated, + ClaimableBalanceSponsorshipCreated, + ClaimableBalanceSponsorshipRemoved, + ClaimableBalanceSponsorshipUpdated, + DataCreated, + DataRemoved, + DataUpdated, + DateSponsporshipCreated, + DateSponsporshipRemoved, + DateSponsporshipUpdated, + SequenceBumped, + SignerCreated, + SignerRemoved, + SignerSponsorshipCreated, + SignerSponsorshipRemoved, + SignerSponsorshipUpdated, + SignerUpdated, + TrustlineAuthorized, + TrustlineAutorizedToMaintainLiabilities, + TrustlineCreated, + TrustlineDeautorized, + TrustlineRemoved, + TrustlineSponsporshipCreated, + TrustlineSponsporshipRemoved, + TrustlineSponsporshipUpdated, + TrustlineUpdated, +} from "./types/effects"; +import { OfferRecord as OfferRecordType } from "./types/offer"; + /* tslint:disable-next-line: no-namespace */ export namespace ServerApi { + export type OfferRecord = OfferRecordType; + export type AccountRecordSigners = AccountRecordSignersType; + export type AssetRecord = AssetRecordType; export interface CollectionPage< T extends Horizon.BaseResponse = Horizon.BaseResponse > { @@ -25,10 +74,54 @@ export namespace ServerApi { T extends Horizon.BaseResponse = Horizon.BaseResponse > = (options?: CallFunctionTemplateOptions) => Promise>; - export interface AccountRecordSigners { - key: string; - weight: number; - type: string; + type BaseEffectRecordFromTypes = + | AccountCreated + | AccountCredited + | AccountDebited + | AccountThresholdsUpdated + | AccountHomeDomainUpdated + | AccountFlagsUpdated + | DataCreated + | DataRemoved + | DataUpdated + | SequenceBumped + | SignerCreated + | SignerRemoved + | SignerUpdated + | TrustlineCreated + | TrustlineRemoved + | TrustlineUpdated + | TrustlineAuthorized + | TrustlineDeautorized + | TrustlineAutorizedToMaintainLiabilities + | ClaimableBalanceCreated + | ClaimableBalanceClaimed + | ClaimableBalanceClaimantCreated + | AccountSponsporshipCreated + | AccountSponsporshipRemoved + | AccountSponsporshipUpdated + | TrustlineSponsporshipCreated + | TrustlineSponsporshipUpdated + | TrustlineSponsporshipRemoved + | DateSponsporshipCreated + | DateSponsporshipUpdated + | DateSponsporshipRemoved + | ClaimableBalanceSponsorshipCreated + | ClaimableBalanceSponsorshipRemoved + | ClaimableBalanceSponsorshipUpdated + | SignerSponsorshipCreated + | SignerSponsorshipUpdated + | SignerSponsorshipRemoved; + + export type EffectRecord = BaseEffectRecordFromTypes & EffectRecordMethods; + export interface ClaimableBalanceRecord extends Horizon.BaseResponse { + id: string; + paging_token: string; + asset: string; + amount: string; + sponsor?: string; + last_modified_ledger: number; + claimants: Horizon.Claimant[]; } export interface AccountRecord extends Horizon.BaseResponse { id: string; @@ -56,110 +149,11 @@ export namespace ServerApi { payments: CallCollectionFunction; trades: CallCollectionFunction; } - - export interface ClaimableBalanceRecord extends Horizon.BaseResponse { - id: string; - paging_token: string; - asset: string; - amount: string; - sponsor?: string; - last_modified_ledger: number; - claimants: Horizon.Claimant[]; - } - - export interface EffectRecord extends Horizon.BaseResponse { - account: string; - paging_token: string; - type_i: string; - type: string; - created_at: string; - id: string; - - // account_debited / credited / trustline_created - amount?: any; - asset_type?: string; - asset_code?: string; - asset_issuer?: string; - - // trustline_created / removed - limit?: string; - - // signer_created - public_key?: string; - - // trade - offer_id?: number | string; - bought_amount?: string; - bought_asset_type?: string; - bought_asset_code?: string; - bought_asset_issuer?: string; - sold_amount?: string; - sold_asset_type?: string; - sold_asset_code?: string; - sold_asset_issuer?: string; - - // account_created - starting_balance?: string; - - // These were retrieved from the go repo, not through direct observation - // so they could be wrong! - - // account thresholds updated - low_threshold?: number; - med_threshold?: number; - high_threshold?: number; - - // home domain updated - home_domain?: string; - - // account flags updated - auth_required_flag?: boolean; - auth_revokable_flag?: boolean; - - // seq bumped - new_seq?: number | string; - - // signer created / removed / updated - weight?: number; - key?: string; - - // trustline authorized / deauthorized - trustor?: string; - - // claimable_balance_created - // claimable_balance_claimant_created - // claimable_balance_claimed - balance_id?: string; - asset?: string; - predicate?: Horizon.Predicate; - - // account_sponsorship_created - // trustline_sponsorship_created - // claimable_balance_sponsorship_created - // signer_sponsorship_created - // data_sponsorship_created - sponsor?: string; - signer?: string; - data_name?: string; - - // account_sponsorship_updated - // account_sponsorship_removed - // trustline_sponsorship_updated - // trustline_sponsorship_removed - // claimable_balance_sponsorship_updated - // claimable_balance_sponsorship_removed - // signer_sponsorship_updated - // signer_sponsorship_removed - // data_sponsorship_updated - // data_sponsorship_removed - new_sponsor?: string; - former_sponsor?: string; - + interface EffectRecordMethods { operation?: CallFunction; precedes?: CallFunction; succeeds?: CallFunction; } - export interface LedgerRecord extends Horizon.BaseResponse { id: string; paging_token: string; @@ -186,26 +180,6 @@ export namespace ServerApi { transactions: CallCollectionFunction; } - export interface OfferAsset { - asset_type: AssetType; - asset_code?: string; - asset_issuer?: string; - } - - export interface OfferRecord extends Horizon.BaseResponse { - id: number | string; - paging_token: string; - seller: string; - selling: OfferAsset; - buying: OfferAsset; - amount: string; - price_r: Horizon.PriceRShorthand; - price: string; - last_modified_ledger: number; - last_modified_time: string; - sponsor?: string; - } - import OperationResponseType = Horizon.OperationResponseType; import OperationResponseTypeI = Horizon.OperationResponseTypeI; export interface BaseOperationRecord< @@ -218,7 +192,6 @@ export namespace ServerApi { effects: CallCollectionFunction; transaction: CallFunction; } - export interface CreateAccountOperationRecord extends BaseOperationRecord< OperationResponseType.createAccount, @@ -350,7 +323,6 @@ export namespace ServerApi { | BeginSponsoringFutureReservesOperationRecord | EndSponsoringFutureReservesOperationRecord | RevokeSponsorshipOperationRecord; - export interface TradeRecord extends Horizon.BaseResponse { id: string; paging_token: string; @@ -374,7 +346,6 @@ export namespace ServerApi { counter: CallFunction; operation: CallFunction; } - export interface TransactionRecord extends Omit { ledger_attr: Horizon.TransactionResponse["ledger"]; @@ -387,21 +358,6 @@ export namespace ServerApi { self: CallFunction; succeeds: CallFunction; } - - export interface AssetRecord extends Horizon.BaseResponse { - asset_type: AssetType.credit4 | AssetType.credit12; - asset_code: string; - asset_issuer: string; - paging_token: string; - accounts: Horizon.AssetAccounts; - num_claimable_balances: number; - balances: Horizon.AssetBalances; - claimable_balances_amount: string; - amount: string; - num_accounts: number; - flags: Horizon.Flags; - } - export interface OrderbookRecord extends Horizon.BaseResponse { bids: Array<{ price_r: { @@ -422,7 +378,6 @@ export namespace ServerApi { base: Asset; counter: Asset; } - export interface PaymentPathRecord extends Horizon.BaseResponse { path: Array<{ asset_code: string; diff --git a/src/types/account.ts b/src/types/account.ts new file mode 100644 index 000000000..e7ac20253 --- /dev/null +++ b/src/types/account.ts @@ -0,0 +1,5 @@ +export interface AccountRecordSigners { + key: string; + weight: number; + type: string; +} diff --git a/src/types/assets.ts b/src/types/assets.ts new file mode 100644 index 000000000..3698ce397 --- /dev/null +++ b/src/types/assets.ts @@ -0,0 +1,22 @@ +import { AssetType } from "stellar-base"; +import { Horizon } from "./../horizon_api"; + +export interface OfferAsset { + asset_type: AssetType; + asset_code?: string; + asset_issuer?: string; +} + +export interface AssetRecord extends Horizon.BaseResponse { + asset_type: AssetType.credit4 | AssetType.credit12; + asset_code: string; + asset_issuer: string; + paging_token: string; + accounts: Horizon.AssetAccounts; + num_claimable_balances: number; + balances: Horizon.AssetBalances; + claimable_balances_amount: string; + amount: string; + num_accounts: number; + flags: Horizon.Flags; +} diff --git a/src/types/effects.ts b/src/types/effects.ts new file mode 100644 index 000000000..94ae1f32c --- /dev/null +++ b/src/types/effects.ts @@ -0,0 +1,225 @@ +import { Horizon } from "./../horizon_api"; +import { OfferAsset } from "./assets"; + +// Reference: GO SDK https://github.com/stellar/go/blob/ec5600bd6b2b6900d26988ff670b9ca7992313b8/services/horizon/internal/resourceadapter/effects.go +export enum EffectType { + "account_created", + "account_removed", + "account_credited", + "account_debited", + "account_thresholds_updated", + "account_home_domain_updated", + "account_flags_updated", + "account_inflation_destination_updated", + "signer_created", + "signer_removed", + "signer_updated", + "trustline_created", + "trustline_removed", + "trustline_updated", + "trustline_authorized", + "trustline_authorized_to_maintain_liabilities", + "trustline_deauthorized", + "trustline_flags_updated", + "offer_created", + "offer_removed", + "offer_updated", + "trade", + "data_created", + "data_removed", + "data_updated", + "sequence_bumped", + "claimable_balance_created", + "claimable_balance_claimant_created", + "claimable_balance_claimed", + "account_sponsorship_created", + "account_sponsorship_updated", + "account_sponsorship_removed", + "trustline_sponsorship_created", + "trustline_sponsorship_updated", + "trustline_sponsorship_removed", + "data_sponsorship_created", + "data_sponsorship_updated", + "data_sponsorship_removed", + "claimable_balance_sponsorship_created", + "claimable_balance_sponsorship_updated", + "claimable_balance_sponsorship_removed", + "signer_sponsorship_created", + "signer_sponsorship_updated", + "signer_sponsorship_removed", + "claimable_balance_clawed_back", +} + +export interface BaseEffectRecord extends Horizon.BaseResponse { + id: string; + account: string; + paging_token: string; + type_i: string; + type: EffectType; + created_at: string; +} + +export interface AccountCreated extends BaseEffectRecord { + starting_balance: string; +} + +export interface AccountCredited extends BaseEffectRecord, OfferAsset { + amount: string; +} + +export type AccountDebited = AccountCredited; + +export interface AccountThresholdsUpdated extends BaseEffectRecord { + low_threshold: number; + med_threshold: number; + high_threshold: number; +} + +export interface AccountHomeDomainUpdated extends BaseEffectRecord { + home_domain: string; +} + +export interface AccountFlagsUpdated extends BaseEffectRecord { + auth_required_flag: boolean; + auth_revokable_flag: boolean; +} + +interface DataEvents extends BaseEffectRecord { + name: boolean; + value: boolean; +} + +export type DataCreated = DataEvents; +export type DataUpdated = DataEvents; +export type DataRemoved = DataEvents; + +export interface SequenceBumped extends BaseEffectRecord { + new_seq: number | string; +} + +interface SignerEvents extends BaseEffectRecord { + weight: number; + key: string; + public_key: string; +} + +export type SignerCreated = SignerEvents; +export type SignerRemoved = SignerEvents; +export type SignerUpdated = SignerEvents; + +interface TrustlineEvents extends BaseEffectRecord, OfferAsset { + limit: string; +} + +export type TrustlineCreated = TrustlineEvents; +export type TrustlineRemoved = TrustlineEvents; +export type TrustlineUpdated = TrustlineEvents; + +export interface TrustlineAuthorized extends BaseEffectRecord { + asset_type: OfferAsset["asset_type"]; + asset_code: OfferAsset["asset_code"]; + trustor: string; +} + +export type TrustlineDeautorized = TrustlineAuthorized; +export type TrustlineAutorizedToMaintainLiabilities = TrustlineAuthorized; + +export interface ClaimableBalanceCreated extends BaseEffectRecord { + amount: string; + balance_id: string; + asset: string; +} + +export type ClaimableBalanceClaimed = ClaimableBalanceCreated; +export interface ClaimableBalanceClaimantCreated + extends ClaimableBalanceCreated { + predicate: Horizon.Predicate; +} + +interface SponsershipFields { + sponsor: string; + new_sponsor: string; + former_sponsor: string; +} + +interface AccountSponsporshipEvents + extends BaseEffectRecord, + SponsershipFields {} +export type AccountSponsporshipCreated = Omit< + AccountSponsporshipEvents, + "new_sponsor" | "former_sponsor" +>; +export type AccountSponsporshipUpdated = Omit< + AccountSponsporshipEvents, + "sponsor" +>; +export type AccountSponsporshipRemoved = Omit< + AccountSponsporshipEvents, + "new_sponsor" | "sponsor" +>; + +interface TrustlineSponsporshipEvents + extends BaseEffectRecord, + SponsershipFields { + asset: string; +} + +export type TrustlineSponsporshipCreated = Omit< + TrustlineSponsporshipEvents, + "new_sponsor" | "former_sponsor" +>; +export type TrustlineSponsporshipUpdated = Omit< + TrustlineSponsporshipEvents, + "sponsor" +>; +export type TrustlineSponsporshipRemoved = Omit< + TrustlineSponsporshipEvents, + "new_sponsor" | "sponsor" +>; + +interface DataSponsporshipEvents extends BaseEffectRecord, SponsershipFields { + data_name: string; +} + +export type DateSponsporshipCreated = Omit< + DataSponsporshipEvents, + "new_sponsor" | "former_sponsor" +>; +export type DateSponsporshipUpdated = Omit; +export type DateSponsporshipRemoved = Omit< + DataSponsporshipEvents, + "new_sponsor" | "sponsor" +>; + +interface ClaimableBalanceSponsorshipEvents + extends BaseEffectRecord, + SponsershipFields { + balance_id: string; +} + +export type ClaimableBalanceSponsorshipCreated = Omit< + ClaimableBalanceSponsorshipEvents, + "new_sponsor" | "former_sponsor" +>; +export type ClaimableBalanceSponsorshipUpdated = Omit< + ClaimableBalanceSponsorshipEvents, + "sponsor" +>; +export type ClaimableBalanceSponsorshipRemoved = Omit< + ClaimableBalanceSponsorshipEvents, + "new_sponsor" | "sponsor" +>; + +interface SignerSponsorshipEvents extends BaseEffectRecord, SponsershipFields { + signer: string; +} + +export type SignerSponsorshipCreated = Omit< + SignerSponsorshipEvents, + "new_sponsor" | "former_sponsor" +>; +export type SignerSponsorshipUpdated = Omit; +export type SignerSponsorshipRemoved = Omit< + SignerSponsorshipEvents, + "new_sponsor" | "sponsor" +>; diff --git a/src/types/offer.ts b/src/types/offer.ts new file mode 100644 index 000000000..9319a33f5 --- /dev/null +++ b/src/types/offer.ts @@ -0,0 +1,22 @@ +import { AssetType } from "stellar-base"; +import { Horizon } from "./../horizon_api"; + +export interface OfferAsset { + asset_type: AssetType; + asset_code?: string; + asset_issuer?: string; +} + +export interface OfferRecord extends Horizon.BaseResponse { + id: number | string; + paging_token: string; + seller: string; + selling: OfferAsset; + buying: OfferAsset; + amount: string; + price_r: Horizon.PriceRShorthand; + price: string; + last_modified_ledger: number; + last_modified_time: string; + sponsor?: string; +} diff --git a/src/types/trade.ts b/src/types/trade.ts new file mode 100644 index 000000000..d744922f3 --- /dev/null +++ b/src/types/trade.ts @@ -0,0 +1,14 @@ +import { BaseEffectRecord } from "./effects"; + +export interface Trade extends BaseEffectRecord { + seller: string; + offer_id: number | string; + bought_amount: string; + bought_asset_type: string; + bought_asset_code: string; + bought_asset_issuer: string; + sold_amount: string; + sold_asset_type: string; + sold_asset_code: string; + sold_asset_issuer: string; +}