Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
39c31bf
WIP: alby support
louisinger Jul 11, 2023
1d1d207
feat: abstract wallet behavior
louisinger Jul 12, 2023
e372894
fix: network change & multiple wallets
louisinger Jul 18, 2023
818aaeb
fix: config & contracts context
louisinger Jul 19, 2023
51f028d
fix: build fails
louisinger Jul 20, 2023
c1c8608
fix: marina does not auto-connect if installed
louisinger Jul 26, 2023
8ba88e0
enrich WalletsModal
louisinger Jul 26, 2023
7d370b9
fix: alby connect
louisinger Jul 27, 2023
ea9b684
fix: loading state for ConnectButton
louisinger Jul 27, 2023
0c22453
merge "main" into alby
louisinger Aug 11, 2023
2ea4f48
revert syncing contract with marina wallet
louisinger Aug 11, 2023
819516c
add ConfigRepository (for AlbyWallet)
louisinger Aug 11, 2023
61eddc2
rename "hasBeenEnabled" -> "isEnabled"
louisinger Aug 11, 2023
4bd6359
fix mint.spec.ts
louisinger Aug 11, 2023
ea7a178
add network tag in wallets.tsx
louisinger Aug 16, 2023
c2ef20f
update AlbyWallet connect flow
louisinger Aug 22, 2023
7831bd6
fix tests
louisinger Aug 22, 2023
8986ba9
pay with Alby webln (Mint)
louisinger Aug 23, 2023
c39d72e
Merge branch 'alby' of github.com:louisinger/app into alby
louisinger Aug 23, 2023
99fe443
fix webLn invoice
louisinger Aug 23, 2023
a0080f9
Merge branch 'main' into alby
louisinger Aug 28, 2023
79816d3
Merge branch 'main' into alby
louisinger Oct 12, 2023
d7cb83c
rework WalletsModal
louisinger Oct 13, 2023
2bb6958
remove config repository
louisinger Oct 13, 2023
12aa0a1
switch to testnet (mint.spec.ts)
louisinger Oct 13, 2023
d188167
rename Marina wallet message
louisinger Oct 13, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions components/activities/list.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext, useEffect, useState } from 'react'
import { useContext } from 'react'
import { Activity, ActivityType } from 'lib/types'
import EmptyState from 'components/layout/empty'
import SomeError from 'components/layout/error'
Expand All @@ -12,14 +12,14 @@ interface ActivitiesListProps {
}

const ActivitiesList = ({ activityType }: ActivitiesListProps) => {
const { connected } = useContext(WalletContext)
const { wallets, initializing } = useContext(WalletContext)
const { activities, loading } = useContext(ContractsContext)

if (!connected)
if (loading || initializing) return <Spinner />
if (!wallets.length)
return (
<EmptyState>🔌 Connect your wallet to view your activities</EmptyState>
)
if (loading) return <Spinner />
if (!activities) return <SomeError>Error getting activities</SomeError>

const filteredActivities = activities.filter((a) => a.type === activityType)
Expand Down
2 changes: 1 addition & 1 deletion components/activities/row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const ActivityRow = ({ activity }: ActivityRowProps) => {
</div>
<div className="level-right">
<div className="level-item">
<ExplorerLink txid={txid} />
<ExplorerLink txid={txid} network={activity.network} />
</div>
<div className="level-item">
<p className="time">{prettyAgo(createdAt)}</p>
Expand Down
27 changes: 22 additions & 5 deletions components/assets/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,30 @@ import SomeError from 'components/layout/error'
import AssetRow from './row'
import Spinner from 'components/spinner'
import { ConfigContext } from 'components/providers/config'
import { WalletContext } from 'components/providers/wallet'
import { getAssetBalance } from 'lib/marina'

const AssetsList = () => {
const { config, loading } = useContext(ConfigContext)
const { wallets, balances } = useContext(WalletContext)

const [filteredAssets, setFilteredAssets] = useState<Asset[]>()
const [totalBalance, setTotalBalance] = useState<Record<string, number>>({})

useEffect(() => {
for (const asset of config.assets) {
let total = 0
for (const wallet of wallets) {
total += getAssetBalance(asset, balances[wallet.type])
}
setTotalBalance((prev) => ({ ...prev, [asset.id]: total }))
}
}, [balances, config.assets, wallets])

const { assets } = config
const [filteredAssets, setFilteredAssets] = useState<Asset[]>()

useEffect(() => {
setFilteredAssets(assets.filter((asset: Asset) => asset.isSynthetic))
}, [assets])
setFilteredAssets(config.assets.filter((asset: Asset) => asset.isSynthetic))
}, [config.assets])

if (loading) return <Spinner />
if (!filteredAssets) return <SomeError>Error getting assets</SomeError>
Expand All @@ -23,7 +36,11 @@ const AssetsList = () => {
<div className="assets-list">
{filteredAssets &&
filteredAssets.map((asset: Asset, index: number) => (
<AssetRow key={index} asset={asset} />
<AssetRow
key={index}
asset={asset}
balance={totalBalance[asset.id]}
/>
))}
</div>
)
Expand Down
8 changes: 2 additions & 6 deletions components/assets/row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@ import Image from 'next/image'
import { Asset } from 'lib/types'
import FilterButton from 'components/buttons/filter'
import TradeButton from 'components/buttons/trade'
import { useContext } from 'react'
import { WalletContext } from 'components/providers/wallet'
import { getAssetBalance } from 'lib/marina'
import ProgressBar from 'components/progress'
import LeftToMint from 'components/progress/leftToMint'

interface AssetRowProps {
asset: Asset
balance: number
}

const AssetRow = ({ asset }: AssetRowProps) => {
const { balances } = useContext(WalletContext)
const balance = getAssetBalance(asset, balances)
const AssetRow = ({ asset, balance }: AssetRowProps) => {
const disabled = !(asset.isAvailable && asset.id)

return (
Expand Down
18 changes: 13 additions & 5 deletions components/balance/fiat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { ContractsContext } from 'components/providers/contracts'
import { ConfigContext } from 'components/providers/config'

const BalanceInFiat = () => {
const { balances, connected } = useContext(WalletContext)
const {
wallets,
balances: balancesByWallet,
initializing,
} = useContext(WalletContext)
const { config } = useContext(ConfigContext)
const { loading } = useContext(ContractsContext)

Expand All @@ -22,14 +26,18 @@ const BalanceInFiat = () => {
useEffect(() => {
setBalance(
assets.reduce((prev, asset) => {
const quantity = getAssetBalance(asset, balances)
let quantity = 0
for (const balances of Object.values(balancesByWallet)) {
quantity += getAssetBalance(asset, balances)
}
return prev + quantity * asset.value
}, 0),
)
}, [assets, balances])
}, [assets, balancesByWallet])

if (!connected) return <p>🔌 Connect your wallet to view your balance</p>
if (loading) return <Spinner />
if (loading || initializing) return <Spinner />
if (!wallets || wallets.length === 0)
return <p>🔌 Connect your wallet to view your balance</p>

return (
<>
Expand Down
13 changes: 12 additions & 1 deletion components/balance/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { WalletContext } from 'components/providers/wallet'
import BalanceTable from './table'
import { useContext } from 'react'
import { WalletType } from 'lib/wallet'

const Balance = () => {
const { balances } = useContext(WalletContext)

return (
<div className="is-box has-pink-border">
<h3>Your balance</h3>
<BalanceTable />
{Object.entries(balances).map(([wallet, balances]) => (
<BalanceTable
key={wallet}
wallet={wallet as WalletType}
balances={balances}
/>
))}
</div>
)
}
Expand Down
11 changes: 3 additions & 8 deletions components/balance/row.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import Image from 'next/image'
import { prettyQuantity } from 'lib/pretty'
import { Asset } from 'lib/types'
import { useContext } from 'react'
import { getAssetBalance } from 'lib/marina'
import { WalletContext } from 'components/providers/wallet'

interface BalanceRowProps {
asset: Asset
amount: number
}
const BalanceRow = ({ asset }: BalanceRowProps) => {
const { balances } = useContext(WalletContext)
const BalanceRow = ({ asset, amount }: BalanceRowProps) => {
return (
<tr>
<td>
Expand All @@ -23,9 +20,7 @@ const BalanceRow = ({ asset }: BalanceRowProps) => {
</div>
<span className="ml-5">{asset.ticker}</span>
</td>
<td>
{prettyQuantity(getAssetBalance(asset, balances), asset.precision)}
</td>
<td>{prettyQuantity(amount, asset.precision)}</td>
<style jsx>{`
td:nth-child(2) {
text-align: right;
Expand Down
87 changes: 49 additions & 38 deletions components/balance/table.tsx
Original file line number Diff line number Diff line change
@@ -1,55 +1,66 @@
import { useContext } from 'react'
import { WalletContext } from 'components/providers/wallet'
import BalanceRow from './row'
import Spinner from 'components/spinner'
import { ContractsContext } from 'components/providers/contracts'
import { ConfigContext } from 'components/providers/config'
import { getAssetBalance } from 'lib/marina'
import { WalletType } from 'lib/wallet'

const BalanceTable = () => {
const { connected } = useContext(WalletContext)
type Props = {
wallet: WalletType
balances: Record<string, number>
}

const BalanceTable: React.FC<Props> = ({ wallet, balances }) => {
const { config } = useContext(ConfigContext)
const { loading } = useContext(ContractsContext)

const { assets } = config

if (!connected) return <p>🔌 Connect your wallet to view your balance</p>
if (loading) return <Spinner />

return (
<table className="table is-fullwidth">
<thead>
<tr>
<th>Ticker</th>
<th>
<abbr title="Quantity" />
Qty
</th>
</tr>
</thead>
<tbody>
{assets &&
assets.map((asset, index) => (
<BalanceRow key={index} asset={asset} />
))}
</tbody>
<style jsx>{`
table {
font-size: 0.9rem;
}
td:nth-child(2),
th:nth-child(2) {
text-align: right;
}
td:nth-child(2) {
color: #63159b;
}
img {
height: 20px;
position: relative;
top: 4px;
}
`}</style>
</table>
<>
<h5>{wallet}</h5>
<table className="table is-fullwidth">
<thead>
<tr>
<th>Ticker</th>
<th>
<abbr title="Quantity" />
Qty
</th>
</tr>
</thead>
<tbody>
{assets &&
assets.map((asset, index) => (
<BalanceRow
key={index}
asset={asset}
amount={getAssetBalance(asset, balances)}
/>
))}
</tbody>
<style jsx>{`
table {
font-size: 0.9rem;
}
td:nth-child(2),
th:nth-child(2) {
text-align: right;
}
td:nth-child(2) {
color: #63159b;
}
img {
height: 20px;
position: relative;
top: 4px;
}
`}</style>
</table>
</>
)
}

Expand Down
6 changes: 4 additions & 2 deletions components/banner/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { useContext } from 'react'

export default function Banner() {
const { network } = useContext(WalletContext)

if (network !== 'testnet') return <></>

return (
<>
<p>This is a Testnet environment. Assets have no value</p>
<p>
This is a Testnet environment. Assets have no value. Make sure your
connected wallets have Liquid testnet enabled.
</p>
<style jsx>{`
p {
background-color: #6b1d9c;
Expand Down
40 changes: 23 additions & 17 deletions components/borrow/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { swapDepositAmountOutOfBounds } from 'lib/swaps'
import { LightningEnabledTasks, Tasks } from 'lib/tasks'
import { Contract } from 'lib/types'
import { fromSatoshis } from 'lib/utils'
import { WalletType } from 'lib/wallet'
import Router from 'next/router'
import { useContext, useEffect, useState } from 'react'

Expand All @@ -17,31 +18,37 @@ interface BorrowButtonProps {
}

const BorrowButton = ({ contract, minRatio, ratio }: BorrowButtonProps) => {
const { balances, connected } = useContext(WalletContext)
const { wallets, balances } = useContext(WalletContext)
const { config } = useContext(ConfigContext)
const { setNewContract } = useContext(ContractsContext)

const [enoughFunds, setEnoughFunds] = useState(false)
const [enoughFunds, setEnoughFunds] = useState<WalletType[]>([])
const [mintLimitReached, setMintLimitReached] = useState(false)

const { collateral, oracles, synthetic } = contract

const { assets } = config

useEffect(() => {
const asset = assets.find((a) => a.ticker === contract.collateral.ticker)
setEnoughFunds([])
const asset = assets.find((a) => a.ticker === collateral.ticker)
if (!asset) return
const funds = getAssetBalance(asset, balances)
const needed = contract.collateral.quantity
const enoughFundsOnMarina = connected && funds > needed
if (LightningEnabledTasks[Tasks.Borrow]) {
const outOfBounds = swapDepositAmountOutOfBounds(needed)
setEnoughFunds(enoughFundsOnMarina || !outOfBounds)
} else {
setEnoughFunds(enoughFundsOnMarina)
if (!wallets.length) return

for (const wallet of wallets) {
const balance = getAssetBalance(asset, balances[wallet.type])
if (contract.collateral.quantity <= balance) {
setEnoughFunds((prev) => [...prev, wallet.type])
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [contract.collateral.quantity])
}, [
assets,
balances,
collateral.ticker,
contract.collateral.quantity,
contract.collateral.ticker,
wallets,
])

const handleClick = () => {
setNewContract(contract)
Expand All @@ -57,9 +64,8 @@ const BorrowButton = ({ contract, minRatio, ratio }: BorrowButtonProps) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [contract.synthetic.quantity])

const enabled =
connected &&
enoughFunds &&
const enabled = () =>
enoughFunds.length > 0 &&
collateral.quantity > 0 &&
collateral.value > 0 &&
ratio >= minRatio &&
Expand All @@ -74,7 +80,7 @@ const BorrowButton = ({ contract, minRatio, ratio }: BorrowButtonProps) => {
<div className="has-text-centered">
<button
className="button is-primary is-cta"
disabled={!enabled}
disabled={!enabled()}
onClick={handleClick}
>
Proceed to deposit
Expand Down
Loading