Skip to content

Commit 1c999ea

Browse files
committed
fix: improve check trustline in stellar example
1 parent 4e4d8f2 commit 1c999ea

2 files changed

Lines changed: 22 additions & 24 deletions

File tree

‎examples/basic/node-stellar/index.ts‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
import BigNumber from 'bignumber.js'
2121

2222
const HORIZON_URL = 'https://horizon.stellar.org'
23-
const NETWORK_PASSPHRASE = 'NETWORK_PASSPHRASE'
23+
const NETWORK_PASSPHRASE = 'NETWORK_PASSPHRASE' // eg. 'Public Global Stellar Network ; September 2015'
2424

2525
const SECRET_KEY = 'YOUR_STELLAR_SECRET_KEY'
2626
const API_KEY = 'c6381a79-2817-4602-83bf-6a641a409e32'
@@ -36,7 +36,7 @@ const SOROBAN_OP_TYPES = [
3636
]
3737

3838
const SOURCE_CHAIN = 'STELLAR'
39-
const SOURCE_TOKEN_ADDR = null
39+
const SOURCE_TOKEN_ADDR = 'SOURCE_TOKEN_ADDR' // eg. 'USDC-GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN'
4040
const DESTINATION_CHAIN = 'DESTINATION_CHAIN' // eg. BSC
4141
const DESTINATION_TOKEN_ADDR = 'DESTINATION_TOKEN_ADDR' // eg. 0x55d398326f99059ff775485246999027b3197955
4242
const DESTINATION_ADDRESS = 'DESTINATION_ADDRESS' // eg. 0x8b5b3F18db50713709da94f88f9f5EEc339D1E4E
@@ -78,24 +78,23 @@ async function checkStellarChangeTrustLinePrerequisite(
7878
const server = new StellarSdk.Horizon.Server(HORIZON_URL)
7979
const account = await server.loadAccount(prerequisite.wallet)
8080

81-
const balanceLines = account.balances.filter(
81+
const accountLines = account.balances.filter(
8282
(balance) =>
8383
balance.asset_type !== 'native' &&
8484
balance.asset_type !== 'liquidity_pool_shares'
8585
)
8686

87-
const targetBalanceLine = balanceLines.find(
88-
(balanceLine) =>
89-
balanceLine.asset_code === prerequisite.code &&
90-
balanceLine.asset_issuer === prerequisite.issuer
87+
const targetAccountLine = accountLines.find(
88+
(accountLine) =>
89+
accountLine.asset_code === prerequisite.code &&
90+
accountLine.asset_issuer === prerequisite.issuer
9191
)
9292

93-
if (targetBalanceLine) {
94-
const lineLimit = new BigNumber(targetBalanceLine.limit)
95-
const lineBalance = new BigNumber(targetBalanceLine.balance)
96-
const prerequisiteValue = new BigNumber(prerequisite.value)
93+
if (targetAccountLine) {
94+
const lineLimit = new BigNumber(targetAccountLine.limit)
95+
const targetLimit = new BigNumber(TRUST_LINE_INFINITE_VALUE)
9796

98-
if (lineLimit.gte(lineBalance.plus(prerequisiteValue))) {
97+
if (lineLimit.gte(targetLimit)) {
9998
console.log('Trust line already opened')
10099
return
101100
}

‎examples/main/node-stellar/index.ts‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
import BigNumber from 'bignumber.js'
2424

2525
const HORIZON_URL = 'https://horizon.stellar.org'
26-
const NETWORK_PASSPHRASE = 'NETWORK_PASSPHRASE'
26+
const NETWORK_PASSPHRASE = 'NETWORK_PASSPHRASE' // eg. 'Public Global Stellar Network ; September 2015'
2727

2828
const SECRET_KEY = 'YOUR_STELLAR_SECRET_KEY'
2929
const API_KEY = 'c6381a79-2817-4602-83bf-6a641a409e32'
@@ -39,7 +39,7 @@ const SOROBAN_OP_TYPES = [
3939
]
4040

4141
const SOURCE_CHAIN = 'STELLAR'
42-
const SOURCE_TOKEN_ADDR = null
42+
const SOURCE_TOKEN_ADDR = 'SOURCE_TOKEN_ADDR' // eg. 'USDC-GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN'
4343
const DESTINATION_CHAIN = 'DESTINATION_CHAIN' // eg. BSC
4444
const DESTINATION_TOKEN_ADDR = 'DESTINATION_TOKEN_ADDR' // eg. 0x55d398326f99059ff775485246999027b3197955
4545
const DESTINATION_ADDRESS = 'DESTINATION_ADDRESS' // eg. 0x8b5b3F18db50713709da94f88f9f5EEc339D1E4E
@@ -83,24 +83,23 @@ async function checkStellarChangeTrustLinePrerequisite(
8383
const server = new StellarSdk.Horizon.Server(HORIZON_URL)
8484
const account = await server.loadAccount(prerequisite.wallet)
8585

86-
const balanceLines = account.balances.filter(
86+
const accountLines = account.balances.filter(
8787
(balance) =>
8888
balance.asset_type !== 'native' &&
8989
balance.asset_type !== 'liquidity_pool_shares'
9090
)
9191

92-
const targetBalanceLine = balanceLines.find(
93-
(balanceLine) =>
94-
balanceLine.asset_code === prerequisite.code &&
95-
balanceLine.asset_issuer === prerequisite.issuer
92+
const targetAccountLine = accountLines.find(
93+
(accountLine) =>
94+
accountLine.asset_code === prerequisite.code &&
95+
accountLine.asset_issuer === prerequisite.issuer
9696
)
9797

98-
if (targetBalanceLine) {
99-
const lineLimit = new BigNumber(targetBalanceLine.limit)
100-
const lineBalance = new BigNumber(targetBalanceLine.balance)
101-
const prerequisiteValue = new BigNumber(prerequisite.value)
98+
if (targetAccountLine) {
99+
const lineLimit = new BigNumber(targetAccountLine.limit)
100+
const targetLimit = new BigNumber(TRUST_LINE_INFINITE_VALUE)
102101

103-
if (lineLimit.gte(lineBalance.plus(prerequisiteValue))) {
102+
if (lineLimit.gte(targetLimit)) {
104103
console.log('Trust line already opened')
105104
return
106105
}

0 commit comments

Comments
 (0)