-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add a trustline step to the borrow flow.
- Loading branch information
Showing
7 changed files
with
190 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type { CurrencyBinding } from 'src/currency-bindings'; | ||
import { useWallet } from 'src/stellar-wallet'; | ||
import { BorrowStep } from './BorrowStep'; | ||
import { TrustLineStep } from './TrustlineStep'; | ||
|
||
export interface BorrowModalProps { | ||
modalId: string; | ||
onClose: () => void; | ||
currency: CurrencyBinding; | ||
totalSupplied: bigint; | ||
} | ||
|
||
export const BorrowModal = ({ modalId, onClose, currency, totalSupplied }: BorrowModalProps) => { | ||
const { name, ticker } = currency; | ||
const { wallet, walletBalances, signTransaction, refetchBalances, prices } = useWallet(); | ||
|
||
const closeModal = () => { | ||
refetchBalances(); | ||
onClose(); | ||
}; | ||
|
||
// Modal is impossible to open before the wallet is loaded. | ||
if (!wallet || !walletBalances || !prices) return null; | ||
|
||
const isTrustline = walletBalances[ticker].trustLine; | ||
|
||
return ( | ||
<dialog id={modalId} className="modal"> | ||
<div className="modal-box w-full max-w-full md:w-[700px] p-10"> | ||
<h3 className="font-bold text-xl mb-4">Borrow {name}</h3> | ||
{!isTrustline ? ( | ||
<TrustLineStep | ||
onClose={closeModal} | ||
currency={currency} | ||
wallet={wallet} | ||
signTransaction={signTransaction} | ||
refetchBalances={refetchBalances} | ||
/> | ||
) : ( | ||
<BorrowStep | ||
onClose={closeModal} | ||
currency={currency} | ||
totalSupplied={totalSupplied} | ||
wallet={wallet} | ||
walletBalances={walletBalances} | ||
prices={prices} | ||
/> | ||
)} | ||
</div> | ||
{/* Invisible backdrop that closes the modal on click */} | ||
<form method="dialog" className="modal-backdrop"> | ||
<button onClick={closeModal} type="button"> | ||
close | ||
</button> | ||
</form> | ||
</dialog> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Button } from '@components/Button'; | ||
import { Loading } from '@components/Loading'; | ||
import { createAddTrustlineTransaction, sendTransaction } from '@lib/horizon'; | ||
import { useState } from 'react'; | ||
import type { CurrencyBinding } from 'src/currency-bindings'; | ||
import type { SignTransaction, Wallet } from 'src/stellar-wallet'; | ||
|
||
export interface TrustLineStepProps { | ||
onClose: () => void; | ||
currency: CurrencyBinding; | ||
wallet: Wallet; | ||
signTransaction: SignTransaction; | ||
refetchBalances: () => void; | ||
} | ||
|
||
export const TrustLineStep = ({ onClose, currency, wallet, signTransaction, refetchBalances }: TrustLineStepProps) => { | ||
const { name, ticker } = currency; | ||
|
||
const [isCreating, setIsCreating] = useState(false); | ||
|
||
const handleAddTrustlineClick = async () => { | ||
try { | ||
setIsCreating(true); | ||
const tx = await createAddTrustlineTransaction(wallet.address, currency); | ||
const signedTx = await signTransaction(tx.toXDR()); | ||
await sendTransaction(signedTx); | ||
alert(`Succesfully created a trustline for ${ticker}!`); | ||
} catch (err) { | ||
alert('Error creating a trustline :('); | ||
console.error('Error creating trustline:', err); | ||
} | ||
refetchBalances(); | ||
setIsCreating(false); | ||
}; | ||
|
||
return ( | ||
<> | ||
<p> | ||
You don't have a trustline for {name} in your wallet. Laina cannot transfer you the tokens without a trustline. | ||
</p> | ||
<div className="flex flex-row justify-end mt-8"> | ||
<Button onClick={onClose} variant="ghost" className="mr-4"> | ||
Cancel | ||
</Button> | ||
{!isCreating ? ( | ||
<Button onClick={handleAddTrustlineClick}>Create Trustline</Button> | ||
) : ( | ||
<Button disabled> | ||
<Loading /> | ||
Creating | ||
</Button> | ||
)} | ||
</div> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters