Skip to content

Commit

Permalink
Add type definitions for the Stellar SDK
Browse files Browse the repository at this point in the history
These are based on the code examples and on our own usage.
  • Loading branch information
swansontec committed Aug 24, 2023
1 parent 2088bc2 commit 6739d09
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 33 deletions.
1 change: 0 additions & 1 deletion src/declare-modules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ declare module 'react-native' {
}
}

declare module 'stellar-sdk'
declare module 'tronweb' {
export const utils: {
crypto: {
Expand Down
65 changes: 65 additions & 0 deletions src/stellar-sdk.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* eslint-disable @typescript-eslint/no-extraneous-class */

declare module 'stellar-sdk' {
export class Account {
constructor(publicKey: string, accountSequence: number)
}

export class Asset {
static native: () => Asset
}

export class Keypair {
static fromRawEd25519Seed: (key: number[]) => Keypair
static fromSecret: (key: string) => Keypair
static fromPublicKey: (key: string) => Keypair
publicKey: () => string
secret: () => string
}

export class Memo {
static hash: (hex: string) => Memo
static id: (number: string) => Memo
static return: (address: string) => Memo
static text: (text: string) => Memo
}

export class Operation {
static createAccount: (params: any) => Operation
static payment: (params: any) => Operation
}

export class Transaction {
sign: (keypair: Keypair) => void
fee: number
}

export class TransactionBuilder {
constructor(account: Account, options: any)
addMemo: (memo: Memo) => TransactionBuilder
addOperation: (operation: Operation) => TransactionBuilder
setTimeout: (time: number) => TransactionBuilder
build: () => Transaction
}

export class Server {
constructor(server: string)
serverName: string
}

interface StellarApi {
Account: typeof Account
Asset: typeof Asset
Keypair: typeof Keypair
Memo: typeof Memo
Network: {
usePublicNetwork: () => void
}
Operation: typeof Operation
Server: typeof Server
TransactionBuilder: typeof TransactionBuilder
}

const stellarApi: StellarApi
export default stellarApi
}
43 changes: 13 additions & 30 deletions src/stellar/StellarEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
JsonObject,
NoAmountSpecifiedError
} from 'edge-core-js/types'
import stellarApi from 'stellar-sdk'
import stellarApi, { Transaction } from 'stellar-sdk'

import { CurrencyEngine } from '../common/CurrencyEngine'
import { PluginEnvironment } from '../common/innerPlugin'
Expand Down Expand Up @@ -56,10 +56,9 @@ export class StellarEngine extends CurrencyEngine<
> {
networkInfo: StellarNetworkInfo
fetchCors: EdgeFetchFunction
stellarApi: Object
activatedAccountsCache: { [publicAddress: string]: boolean }
pendingTransactionsIndex: number
pendingTransactionsMap: { [index: number]: Object }
pendingTransactionsMap: { [index: number]: Transaction }
fees: { low: number; standard: number; high: number }
otherData!: StellarWalletOtherData

Expand All @@ -72,7 +71,6 @@ export class StellarEngine extends CurrencyEngine<
super(env, tools, walletInfo, opts)
this.networkInfo = env.networkInfo
this.fetchCors = getFetchCors(env.io)
this.stellarApi = {}
this.activatedAccountsCache = {}
this.pendingTransactionsIndex = 0
this.pendingTransactionsMap = {}
Expand Down Expand Up @@ -136,7 +134,6 @@ export class StellarEngine extends CurrencyEngine<
funcs = this.tools.stellarApiServers.map(api => async () => {
// @ts-expect-error
const result = await api[func](...params)
// @ts-expect-error
return { server: api.serverName, result }
})
out = await asyncWaterfall(funcs)
Expand All @@ -155,7 +152,6 @@ export class StellarEngine extends CurrencyEngine<
this.walletLocalData.blockHeight <= blockHeight &&
this.tools.highestTxHeight <= blockHeight
) {
// @ts-expect-error
return { server: serverApi.serverName, result }
} else {
throw new Error('Height out of date')
Expand All @@ -173,7 +169,6 @@ export class StellarEngine extends CurrencyEngine<
.cursor(this.otherData.lastPagingToken)
.forAccount(...params)
.call()
// @ts-expect-error
return { server: serverApi.serverName, result }
})
out = await asyncWaterfall(funcs)
Expand All @@ -185,7 +180,6 @@ export class StellarEngine extends CurrencyEngine<
this.tools.stellarApiServers.map(async serverApi => {
// @ts-expect-error
const result = await serverApi[func](...params)
// @ts-expect-error
return { server: serverApi.serverName, result }
})
)
Expand Down Expand Up @@ -500,8 +494,7 @@ export class StellarEngine extends CurrencyEngine<

const exchangeAmount = div(nativeAmount, denom.multiplier, 7)

// @ts-expect-error
const account = new this.stellarApi.Account(
const account = new stellarApi.Account(
this.walletLocalData.publicKey,
this.otherData.accountSequence
)
Expand All @@ -517,37 +510,31 @@ export class StellarEngine extends CurrencyEngine<
? this.fees[edgeSpendInfo.networkFeeOption]
: BASE_FEE

// @ts-expect-error
const txBuilder = new this.stellarApi.TransactionBuilder(account, {
let txBuilder = new stellarApi.TransactionBuilder(account, {
fee: feeSetting
})
let transaction

if (mustCreateAccount) {
transaction = txBuilder.addOperation(
// @ts-expect-error
this.stellarApi.Operation.createAccount({
txBuilder = txBuilder.addOperation(
stellarApi.Operation.createAccount({
destination: publicAddress,
startingBalance: exchangeAmount
})
)
} else {
transaction = txBuilder.addOperation(
// @ts-expect-error
this.stellarApi.Operation.payment({
txBuilder = txBuilder.addOperation(
stellarApi.Operation.payment({
destination: publicAddress,
// @ts-expect-error
asset: this.stellarApi.Asset.native(),
asset: stellarApi.Asset.native(),
amount: exchangeAmount
})
)
}
if (memoId != null) {
// @ts-expect-error
const memo = this.stellarApi.Memo.id(memoId)
transaction = transaction.addMemo(memo)
const memo = stellarApi.Memo.id(memoId)
txBuilder = txBuilder.addMemo(memo)
}
transaction = transaction.build()
const transaction = txBuilder.build()

const networkFee = transaction.fee.toString()
nativeAmount = add(networkFee, nativeAmount) // Add fee to total
Expand Down Expand Up @@ -618,11 +605,9 @@ export class StellarEngine extends CurrencyEngine<
throw new Error('ErrorInvalidTransaction')
}
this.warn('Signing...')
// @ts-expect-error
const keypair = this.stellarApi.Keypair.fromSecret(
const keypair = stellarApi.Keypair.fromSecret(
stellarPrivateKeys.stellarKey
)
// @ts-expect-error
await transaction.sign(keypair)
} catch (e: any) {
this.error(
Expand Down Expand Up @@ -687,8 +672,6 @@ export async function makeCurrencyEngine(
const safeWalletInfo = asSafeStellarWalletInfo(walletInfo)
const engine = new StellarEngine(env, tools, safeWalletInfo, opts)

engine.stellarApi = stellarApi

await engine.loadEngine(tools, safeWalletInfo, opts)

return engine
Expand Down
4 changes: 2 additions & 2 deletions src/stellar/StellarTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
EdgeTokenMap,
EdgeWalletInfo
} from 'edge-core-js/types'
import stellarApi from 'stellar-sdk'
import stellarApi, { Server as StellarServer } from 'stellar-sdk'
import { serialize } from 'uri-js'
import parse from 'url-parse'

Expand All @@ -26,7 +26,7 @@ export class StellarTools implements EdgeCurrencyTools {
networkInfo: StellarNetworkInfo

highestTxHeight: number = 0
stellarApiServers: Object[]
stellarApiServers: StellarServer[]

constructor(env: PluginEnvironment<StellarNetworkInfo>) {
const { builtinTokens, currencyInfo, io, networkInfo } = env
Expand Down

0 comments on commit 6739d09

Please sign in to comment.