From e6c622efcbac8e334b6717a8f48fda8076c0f557 Mon Sep 17 00:00:00 2001 From: Rabea Abdelwahab Date: Mon, 26 Apr 2021 18:34:25 -0400 Subject: [PATCH] Provide types for effects (closes #299). Types provided for effects, with the Go package being the reference. Moved Asset, Trade and Offer Types to their own files. Kept service_api exporting back types imported to not break anything in other files. --- CHANGELOG.md | 1 + src/server_api.ts | 205 +++++++++++------------------------ src/types/account.ts | 5 + src/types/assets.ts | 15 +++ src/types/effects.ts | 251 +++++++++++++++++++++++++++++++++++++++++++ src/types/offer.ts | 22 ++++ src/types/trade.ts | 14 +++ 7 files changed, 369 insertions(+), 144 deletions(-) create mode 100644 src/types/account.ts create mode 100644 src/types/assets.ts create mode 100644 src/types/effects.ts create mode 100644 src/types/offer.ts create mode 100644 src/types/trade.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e5e558a2..b4f4a2cf5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ A breaking change will get clearly marked in this log. - Added TransactionCallBuilder.forClaimableBalance(), and OperationCallBuilder.forClaimableBalance(). - Added support for new `accounts`, `balances`, `claimable_balances_amount`, and `num_claimable_balances` fields on Assets. +- Added types for all Effects supported as an enum, and moved Trade, Asset, Offer, and Account types to separate files. [(#635)](https://github.com/stellar/js-stellar-sdk/pull/635) ## [v8.1.1](https://github.com/stellar/js-stellar-sdk/compare/v8.1.0...v8.1.1) diff --git a/src/server_api.ts b/src/server_api.ts index 38b315519..970ec624e 100644 --- a/src/server_api.ts +++ b/src/server_api.ts @@ -1,9 +1,19 @@ -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 * as Effects from "./types/effects"; +import { OfferRecord as OfferRecordType } from "./types/offer"; +import { Trade } from "./types/trade"; + /* 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 +35,55 @@ export namespace ServerApi { T extends Horizon.BaseResponse = Horizon.BaseResponse > = (options?: CallFunctionTemplateOptions) => Promise>; - export interface AccountRecordSigners { - key: string; - weight: number; - type: string; + type BaseEffectRecordFromTypes = + | Effects.AccountCreated + | Effects.AccountCredited + | Effects.AccountDebited + | Effects.AccountThresholdsUpdated + | Effects.AccountHomeDomainUpdated + | Effects.AccountFlagsUpdated + | Effects.DataCreated + | Effects.DataRemoved + | Effects.DataUpdated + | Effects.SequenceBumped + | Effects.SignerCreated + | Effects.SignerRemoved + | Effects.SignerUpdated + | Effects.TrustlineCreated + | Effects.TrustlineRemoved + | Effects.TrustlineUpdated + | Effects.TrustlineAuthorized + | Effects.TrustlineDeauthorized + | Effects.TrustlineAuthorizedToMaintainLiabilities + | Effects.ClaimableBalanceCreated + | Effects.ClaimableBalanceClaimed + | Effects.ClaimableBalanceClaimantCreated + | Effects.AccountSponsorshipCreated + | Effects.AccountSponsorshipRemoved + | Effects.AccountSponsorshipUpdated + | Effects.TrustlineSponsorshipCreated + | Effects.TrustlineSponsorshipUpdated + | Effects.TrustlineSponsorshipRemoved + | Effects.DateSponsorshipCreated + | Effects.DateSponsorshipUpdated + | Effects.DateSponsorshipRemoved + | Effects.ClaimableBalanceSponsorshipCreated + | Effects.ClaimableBalanceSponsorshipRemoved + | Effects.ClaimableBalanceSponsorshipUpdated + | Effects.SignerSponsorshipCreated + | Effects.SignerSponsorshipUpdated + | Effects.SignerSponsorshipRemoved + | Trade; + + 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 +111,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 +142,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 +154,6 @@ export namespace ServerApi { effects: CallCollectionFunction; transaction: CallFunction; } - export interface CreateAccountOperationRecord extends BaseOperationRecord< OperationResponseType.createAccount, @@ -350,7 +285,6 @@ export namespace ServerApi { | BeginSponsoringFutureReservesOperationRecord | EndSponsoringFutureReservesOperationRecord | RevokeSponsorshipOperationRecord; - export interface TradeRecord extends Horizon.BaseResponse { id: string; paging_token: string; @@ -374,7 +308,6 @@ export namespace ServerApi { counter: CallFunction; operation: CallFunction; } - export interface TransactionRecord extends Omit { ledger_attr: Horizon.TransactionResponse["ledger"]; @@ -387,21 +320,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 +340,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..7e078ddcf --- /dev/null +++ b/src/types/assets.ts @@ -0,0 +1,15 @@ +import { AssetType } from "stellar-base"; +import { Horizon } from "./../horizon_api"; +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..7ac80d892 --- /dev/null +++ b/src/types/effects.ts @@ -0,0 +1,251 @@ +import { Horizon } from "./../horizon_api"; +import { OfferAsset } from "./offer"; + +// Reference: GO SDK https://github.com/stellar/go/blob/ec5600bd6b2b6900d26988ff670b9ca7992313b8/services/horizon/internal/resourceadapter/effects.go +export enum EffectType { + // account effects + account_created = 0, + account_removed = 1, + account_credited = 2, + account_debited = 3, + account_thresholds_updated = 4, + account_home_domain_updated = 5, + account_flags_updated = 6, + account_inflation_destination_updated = 7, + // signer effects + signer_created = 10, + signer_removed = 11, + signer_updated = 12, + // trustline effects + trustline_created = 20, + trustline_removed = 21, + trustline_updated = 22, + trustline_authorized = 23, + trustline_deauthorized = 24, + trustline_authorized_to_maintain_liabilities = 25, // deprecated, use trustline_flags_updated + trustline_flags_updated = 26, + // trading effects + offer_created = 30, + offer_removed = 31, + offer_updated = 32, + trade = 33, + // data effects + data_created = 40, + data_removed = 41, + data_updated = 42, + sequence_bumped = 43, + // claimable balance effects + claimable_balance_created = 50, + claimable_balance_claimant_created = 51, + claimable_balance_claimed = 52, + // sponsorship effects + account_sponsorship_created = 60, + account_sponsorship_updated = 61, + account_sponsorship_removed = 62, + trustline_sponsorship_created = 63, + trustline_sponsorship_updated = 64, + trustline_sponsorship_removed = 65, + data_sponsorship_created = 66, + data_sponsorship_updated = 67, + data_sponsorship_removed = 68, + claimable_balance_sponsorship_created = 69, + claimable_balance_sponsorship_updated = 70, + claimable_balance_sponsorship_removed = 71, + signer_sponsorship_created = 72, + signer_sponsorship_updated = 73, + signer_sponsorship_removed = 74, + claimable_balance_clawed_back = 80, +} +export interface BaseEffectRecord extends Horizon.BaseResponse { + id: string; + account: string; + paging_token: string; + type_i: EffectType; + type: string; + created_at: string; +} +export interface AccountCreated extends BaseEffectRecord { + type_i: EffectType.account_created; + starting_balance: string; +} +export interface AccountCredited extends BaseEffectRecord, OfferAsset { + type_i: EffectType.account_credited; + amount: string; +} +export interface AccountDebited extends BaseEffectRecord { + type_i: EffectType.account_debited; + amount: string; +} +export interface AccountThresholdsUpdated extends BaseEffectRecord { + type_i: EffectType.account_thresholds_updated; + low_threshold: number; + med_threshold: number; + high_threshold: number; +} +export interface AccountHomeDomainUpdated extends BaseEffectRecord { + type_i: EffectType.account_home_domain_updated; + home_domain: string; +} +export interface AccountFlagsUpdated extends BaseEffectRecord { + type_i: EffectType.account_flags_updated; + auth_required_flag: boolean; + auth_revokable_flag: boolean; +} +interface DataEvents extends BaseEffectRecord { + name: boolean; + value: boolean; +} +export interface DataCreated extends DataEvents { + type_i: EffectType.data_created; +} +export interface DataUpdated extends DataEvents { + type_i: EffectType.data_updated; +} +export interface DataRemoved extends DataEvents { + type_i: EffectType.data_removed; +} +export interface SequenceBumped extends BaseEffectRecord { + type_i: EffectType.sequence_bumped; + new_seq: number | string; +} +interface SignerEvents extends BaseEffectRecord { + weight: number; + key: string; + public_key: string; +} +export interface SignerCreated extends SignerEvents { + type_i: EffectType.signer_created; +} +export interface SignerRemoved extends SignerEvents { + type_i: EffectType.signer_removed; +} +export interface SignerUpdated extends SignerEvents { + type_i: EffectType.signer_updated; +} +interface TrustlineEvents extends BaseEffectRecord, OfferAsset { + limit: string; +} +export interface TrustlineCreated extends TrustlineEvents { + type_i: EffectType.trustline_created; +} +export interface TrustlineRemoved extends TrustlineEvents { + type_i: EffectType.trustline_removed; +} +export interface TrustlineUpdated extends TrustlineEvents { + type_i: EffectType.trustline_updated; +} +export interface TrustlineAuthorized extends BaseEffectRecord { + type_i: EffectType.trustline_authorized; + asset_type: OfferAsset["asset_type"]; + asset_code: OfferAsset["asset_code"]; + trustor: string; +} +export interface TrustlineDeauthorized + extends Omit { + type_i: EffectType.trustline_deauthorized; +} +export interface TrustlineAuthorizedToMaintainLiabilities + extends Omit { + type_i: EffectType.trustline_authorized_to_maintain_liabilities; +} +export interface ClaimableBalanceCreated extends BaseEffectRecord { + type_i: EffectType.claimable_balance_created; + amount: string; + balance_type_i: string; + asset: string; +} +export interface ClaimableBalanceClaimed + extends Omit { + type_i: EffectType.claimable_balance_claimed; +} + +export interface ClaimableBalanceClaimantCreated + extends Omit { + type_i: EffectType.claimable_balance_claimant_created; +} +interface SponsershipFields { + sponsor: string; + new_sponsor: string; + former_sponsor: string; +} +interface AccountSponsorshipEvents + extends BaseEffectRecord, + SponsershipFields {} + +export type AccountSponsorshipCreated = Omit< + AccountSponsorshipEvents, + "new_sponsor" | "former_sponsor" +> & { type_i: EffectType.account_sponsorship_created }; +export type AccountSponsorshipUpdated = Omit< + AccountSponsorshipEvents, + "sponsor" +> & { type_i: EffectType.account_sponsorship_updated }; +export type AccountSponsorshipRemoved = Omit< + AccountSponsorshipEvents, + "new_sponsor" | "sponsor" +> & { type_i: EffectType.account_sponsorship_removed }; +interface TrustlineSponsorshipEvents + extends BaseEffectRecord, + SponsershipFields { + asset: string; +} +export type TrustlineSponsorshipCreated = Omit< + TrustlineSponsorshipEvents, + "new_sponsor" | "former_sponsor" +> & { type_i: EffectType.trustline_sponsorship_created }; +export type TrustlineSponsorshipUpdated = Omit< + TrustlineSponsorshipEvents, + "sponsor" +> & { type_i: EffectType.trustline_sponsorship_updated }; +export type TrustlineSponsorshipRemoved = Omit< + TrustlineSponsorshipEvents, + "new_sponsor" | "sponsor" +> & { type_i: EffectType.trustline_sponsorship_removed }; +interface DataSponsorshipEvents extends BaseEffectRecord, SponsershipFields { + data_name: string; +} +export type DateSponsorshipCreated = Omit< + DataSponsorshipEvents, + "new_sponsor" | "former_sponsor" +> & { type_i: EffectType.data_sponsorship_created }; +export type DateSponsorshipUpdated = Omit & { + type_i: EffectType.data_sponsorship_updated; +}; +export type DateSponsorshipRemoved = Omit< + DataSponsorshipEvents, + "new_sponsor" | "sponsor" +> & { type_i: EffectType.data_sponsorship_removed }; +interface ClaimableBalanceSponsorshipEvents + extends BaseEffectRecord, + SponsershipFields { + balance_type_i: string; +} + +export type ClaimableBalanceSponsorshipCreated = Omit< + ClaimableBalanceSponsorshipEvents, + "new_sponsor" | "former_sponsor" +> & { type_i: EffectType.claimable_balance_sponsorship_created }; +export type ClaimableBalanceSponsorshipUpdated = Omit< + ClaimableBalanceSponsorshipEvents, + "sponsor" +> & { type_i: EffectType.claimable_balance_sponsorship_updated }; +export type ClaimableBalanceSponsorshipRemoved = Omit< + ClaimableBalanceSponsorshipEvents, + "new_sponsor" | "sponsor" +> & { type_i: EffectType.claimable_balance_sponsorship_removed }; +interface SignerSponsorshipEvents extends BaseEffectRecord, SponsershipFields { + signer: string; +} + +export type SignerSponsorshipCreated = Omit< + SignerSponsorshipEvents, + "new_sponsor" | "former_sponsor" +> & { type_i: EffectType.signer_sponsorship_created }; +export type SignerSponsorshipUpdated = Omit< + SignerSponsorshipEvents, + "sponsor" +> & { type_i: EffectType.signer_sponsorship_updated }; +export type SignerSponsorshipRemoved = Omit< + SignerSponsorshipEvents, + "new_sponsor" | "sponsor" +> & { type_i: EffectType.signer_sponsorship_removed }; 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; +}