From fe100b2bae3eab246fbf23111477a59c1535c0a6 Mon Sep 17 00:00:00 2001 From: Matias Poblete Date: Mon, 10 Jun 2024 17:14:18 -0400 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8FImprove=20trustline=20flow=20?= =?UTF-8?q?+=20add=20new=20anchors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Buy/BuyComponent.tsx | 80 ++++++++++++++++------------- 1 file changed, 45 insertions(+), 35 deletions(-) diff --git a/src/components/Buy/BuyComponent.tsx b/src/components/Buy/BuyComponent.tsx index 89b8c2d9..4b4d205e 100644 --- a/src/components/Buy/BuyComponent.tsx +++ b/src/components/Buy/BuyComponent.tsx @@ -31,11 +31,11 @@ const anchors: anchor[] = [ }, { name: 'MoneyGram', - home_domain: 'https://testanchor.stellar.org' + home_domain: 'https://stellar.moneygram.com' }, { name: 'MyKobo', - home_domain: 'https://testanchor.stellar.org' + home_domain: 'https://mykobo.co' }, ] @@ -48,7 +48,12 @@ function BuyComponent() { const checkTrustline = async () => { if(address){ - const account = await serverHorizon?.loadAccount(address) + let account + try { + account = await serverHorizon?.loadAccount(address) + } catch (error) { + console.error(error) + } const balances = account?.balances const hasTrustline = balances?.find((bal) => 'asset_code' in bal && 'asset_issuer' in bal && @@ -59,30 +64,6 @@ function BuyComponent() { }; - const buy = async () => { - await checkTrustline() - if(needTrustline){ - try { - setTrustline( - { - tokenSymbol: selectedToken?.name!, - tokenAdmin: selectedToken?.issuer!, - sorobanContext - } - ) - - } catch (error) { - console.error(error) - } - } - try { - InitDeposit(selectedAnchor?.home_domain!) - } catch (error) { - console.error(error) - } - } - - const sign = async (txn: any) => { const signedTransaction = await sorobanContext?.activeConnector?.signTransaction(txn, { networkPassphrase: activeChain?.networkPassphrase, @@ -126,14 +107,42 @@ function BuyComponent() { popup?.focus() - window.addEventListener('message', (event) => { - if (event.origin === homeDomain) { - console.log(event.data) - const transaction = event.data.transaction - if (transaction.status == 'complete') - popup?.close() + + } + + const buy = async () => { + if (!selectedToken || !selectedAnchor) { + return + } + await checkTrustline() + if (needTrustline) { + try { + console.log('setting trustline') + const res = await setTrustline( + { + tokenSymbol: selectedToken?.name!, + tokenAdmin: selectedToken?.issuer!, + sorobanContext + } + ) + console.log('response', res) + if (res === undefined) { + setNeedTrustline(true) + console.error('error setting trustline') + } + } catch (error) { + console.log('error setting trustline') + console.error(error) + setNeedTrustline(true) + } - }) + } else { + try { + InitDeposit(selectedAnchor?.home_domain!) + } catch (error) { + console.error(error) + } + } } useEffect(() => { @@ -156,7 +165,7 @@ function BuyComponent() {
Deposit to:
- setSelectedAnchor(anchors[0])}> + setSelectedAnchor(anchors[2])}> + {needTrustline ? 'Create Trustline and ' : ''} Buy {selectedToken ? selectedToken.name : ''} ):