Skip to content

Commit

Permalink
Merge pull request #603 from EdgeApp/william/stellar-types
Browse files Browse the repository at this point in the history
Stellar types
  • Loading branch information
swansontec authored Aug 28, 2023
2 parents 9578b61 + 69d0f0a commit a6825c2
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 50 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
19 changes: 2 additions & 17 deletions src/ripple/RippleEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
import {
getBalanceChanges,
OfferCreate,
Payment as PaymentJson,
rippleTimeToUnixTime,
TrustSet,
unixTimeToRippleTime,
Expand Down Expand Up @@ -91,24 +92,8 @@ const tfSetNoRipple = 131072 // No Rippling Flag for token sends
const TRUST_LINE_APPROVAL_AMOUNT = '1000000'
const SET_TRUST_LINE_FEE = '12'

interface PaymentJson {
Amount:
| string
| {
currency: string
issuer: string
value: string
}
TransactionType: string
Account: string
Destination: string
Fee: string
DestinationTag?: number
Flags?: number
}

interface XrpParams {
preparedTx: Object
preparedTx: PaymentJson
}

export class XrpEngine extends CurrencyEngine<
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 @@ -137,7 +135,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 @@ -156,7 +153,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 @@ -174,7 +170,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 @@ -186,7 +181,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 @@ -509,8 +503,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 @@ -526,37 +519,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 @@ -627,11 +614,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()

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 @@ -9,7 +9,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 @@ -31,7 +31,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 a6825c2

Please sign in to comment.