dappbundle/btcmortgage/index.html

2512 lines
180 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BTC Mortgage</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link
href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,400;0,500;0,700;1,400;1,500;1,700&display=swap"
rel="stylesheet">
<link rel="stylesheet" href="css/main.css">
</head>
<body onload="onLoadStartUp()" class="hidden">
<idb-support></idb-support>
<sm-notifications id="notification_drawer"></sm-notifications>
<sm-popup id="confirmation_popup">
<h4 id="confirm_title"></h4>
<p id="confirm_message"></p>
<div class="flex align-center gap-0-5 margin-left-auto">
<button class="button cancel-button">Cancel</button>
<button class="button button--primary confirm-button">OK</button>
</div>
</sm-popup>
<sm-popup id="prompt_popup">
<h4 id="prompt_title"></h4>
<p id="prompt_message"></p>
<sm-form>
<sm-input id="prompt_input"></sm-input>
<div class="flex align-center gap-0-5 margin-left-auto">
<button class="button cancel-button">Cancel</button>
<button class="button confirm-button button--primary" type="submit">OK</button>
</div>
</sm-form>
</sm-popup>
<div id="app_body"></div>
<sm-popup id="user_popup">
<header slot="header" class="popup__header">
<button class="popup__header__close">
<svg class="icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
<path fill="none" d="M0 0h24v24H0z" />
<path
d="M12 10.586l4.95-4.95 1.414 1.414-4.95 4.95 4.95 4.95-1.414 1.414-4.95-4.95-4.95 4.95-1.414-1.414 4.95-4.95-4.95-4.95L7.05 5.636z" />
</svg>
</button>
<h3>Profile</h3>
</header>
<section class="grid gap-2">
<div id="agency_name_container" class="grid gap-0-5"> </div>
<div class="grid gap-0-5">
<h5>My Bitcoin address</h5>
<sm-copy class="my-btc-address"></sm-copy>
</div>
<div class="grid gap-0-5">
<h5>My FLO address</h5>
<sm-copy class="my-flo-address"></sm-copy>
</div>
<button class="button button--danger justify-self-start" onclick="signOut()">Sign out</button>
</section>
</sm-popup>
<sm-popup id="request_loan_popup">
<header slot="header" class="popup__header">
<button class="popup__header__close">
<svg class="icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
<path fill="none" d="M0 0h24v24H0z" />
<path
d="M12 10.586l4.95-4.95 1.414 1.414-4.95 4.95 4.95 4.95-1.414 1.414-4.95-4.95-4.95 4.95-1.414-1.414 4.95-4.95-4.95-4.95L7.05 5.636z" />
</svg>
</button>
<h3>Request loan</h3>
</header>
<section id="request_loan_content"></section>
</sm-popup>
<sm-popup id="request_details_popup">
<header slot="header" class="popup__header">
<button class="popup__header__close">
<svg class="icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
<path fill="none" d="M0 0h24v24H0z" />
<path
d="M12 10.586l4.95-4.95 1.414 1.414-4.95 4.95 4.95 4.95-1.414 1.414-4.95-4.95-4.95 4.95-1.414-1.414 4.95-4.95-4.95-4.95L7.05 5.636z" />
</svg>
</button>
<h3>Loan details</h3>
</header>
<section id="request_details_content" class="grid gap-1-5"></section>
</sm-popup>
<script type="text/javascript" id="floGlobals">
/* Constants for FLO blockchain operations !!Make sure to add this at beginning!! */
const floGlobals = {
blockchain: "FLO",
adminID: "FDnimh9nU7qg9AsiSotQf9aPUvNYisckSW",
SNStorageID: "FSF5igBd9xC7ZBB511DZG8PVB2eT93YE3M",
application: "TEST_MODE",
}
</script>
<script type="text/javascript" src="scripts/neverland.min.js"></script>
<script type="text/javascript" src="scripts/components.js"></script>
<script type="text/javascript" src="scripts/lib.js"></script>
<script type="text/javascript" src="scripts/floCrypto.js"></script>
<script type="text/javascript" src="scripts/btcOperator.js"></script>
<script type="text/javascript" src="scripts/floBlockchainAPI.js"></script>
<script type="text/javascript" src="scripts/floTokenAPI.js"></script>
<script type="text/javascript" src="scripts/compactIDB.js"></script>
<script type="text/javascript" src="scripts/floCloudAPI.js"></script>
<script type="text/javascript" src="scripts/floDapps.js"></script>
<script type="text/javascript" src="scripts/btcMortgage.js"></script>
<script type="text/javascript">
const { Component, useState, useEffect, html, render: renderElem } = neverland;
// Use to store global variables
const uiGlobals = {
connectionErrorNotification: []
}
//Checks for internet connection status
if (!navigator.onLine)
uiGlobals.connectionErrorNotification.push(notify('There seems to be a problem connecting to the internet, Please check you internet connection.', 'error'))
window.addEventListener('offline', () => {
uiGlobals.connectionErrorNotification.push(notify('There seems to be a problem connecting to the internet, Please check you internet connection.', 'error'))
})
window.addEventListener('online', () => {
uiGlobals.connectionErrorNotification.forEach(notification => getRef("notification_drawer").remove(notification))
notify('We are back online.', 'success')
location.reload()
uiGlobals.connectionErrorNotification = []
})
// Use instead of document.getElementById
function getRef(elementId) {
return document.getElementById(elementId)
}
//Function for displaying toast notifications. pass in error for mode param if you want to show an error.
function notify(message, mode, options = {}) {
let icon
switch (mode) {
case 'success':
icon = `<svg class="icon icon--success" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24"><path fill="none" d="M0 0h24v24H0z"/><path d="M10 15.172l9.192-9.193 1.415 1.414L10 18l-6.364-6.364 1.414-1.414z"/></svg>`
break;
case 'error':
icon = `<svg class="icon icon--error" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24"><path fill="none" d="M0 0h24v24H0z"/><path d="M12 22C6.477 22 2 17.523 2 12S6.477 2 12 2s10 4.477 10 10-4.477 10-10 10zm-1-7v2h2v-2h-2zm0-8v6h2V7h-2z"/></svg>`
options.pinned = true
break;
}
if (mode === 'error') {
console.trace()
}
return getRef("notification_drawer").push(message, { icon, ...options });
}
let zIndex = 50
// function required for popups or modals to appear
function openPopup(popupId, pinned) {
zIndex++
getRef(popupId).setAttribute('style', `z-index: ${zIndex}`)
return getRef(popupId).show({ pinned })
}
// hides the popup or modal
function closePopup(options = {}) {
if (popupStack.peek() === undefined)
return;
popupStack.peek().popup.hide(options)
}
// displays a popup for asking permission. Use this instead of JS confirm
/**
@param {string} title - Title of the popup
@param {object} options - Options for the popup
@param {string} options.message - Message to be displayed in the popup
@param {string} options.cancelText - Text for the cancel button
@param {string} options.confirmText - Text for the confirm button
@param {boolean} options.danger - If true, confirm button will be red
*/
const getConfirmation = (title, options = {}) => {
return new Promise(resolve => {
const { message = '', cancelText = 'Cancel', confirmText = 'OK', danger = false } = options
getRef('confirm_title').innerText = title;
getRef('confirm_message').innerText = message;
const cancelButton = getRef('confirmation_popup').querySelector('.cancel-button');
const confirmButton = getRef('confirmation_popup').querySelector('.confirm-button')
confirmButton.textContent = confirmText
cancelButton.textContent = cancelText
if (danger)
confirmButton.classList.add('button--danger')
else
confirmButton.classList.remove('button--danger')
const { closed } = openPopup('confirmation_popup')
confirmButton.onclick = () => {
closePopup({ payload: true })
}
cancelButton.onclick = () => {
closePopup()
}
closed.then((payload) => {
confirmButton.onclick = null
cancelButton.onclick = null
if (payload)
resolve(true)
else
resolve(false)
})
})
}
function getFormattedTime(timestamp, format) {
try {
if (String(timestamp).length < 13)
timestamp *= 1000
let [day, month, date, year] = new Date(timestamp).toString().split(' '),
minutes = new Date(timestamp).getMinutes(),
hours = new Date(timestamp).getHours(),
currentTime = new Date().toString().split(' ')
minutes = minutes < 10 ? `0${minutes}` : minutes
let finalHours = ``;
if (hours > 12)
finalHours = `${hours - 12}:${minutes}`
else if (hours === 0)
finalHours = `12:${minutes}`
else
finalHours = `${hours}:${minutes}`
finalHours = hours >= 12 ? `${finalHours} PM` : `${finalHours} AM`
switch (format) {
case 'date-only':
return `${month} ${date}, ${year}`;
break;
case 'time-only':
return finalHours;
default:
return `${month} ${date}, ${year} at ${finalHours}`;
}
} catch (e) {
console.error(e);
return timestamp;
}
}
class Router {
constructor(options = {}) {
const { routes = {}, state = {}, routingStart, routingEnd } = options
this.routes = routes
this.state = state
this.routingStart = routingStart
this.routingEnd = routingEnd
this.lastPage = null
window.addEventListener('hashchange', e => this.routeTo(window.location.hash))
}
addRoute(route, callback) {
this.routes[route] = callback
}
async routeTo(path) {
try {
let page
let wildcards = []
let queryString
let params
[path, queryString] = path.split('?');
if (path.includes('#'))
path = path.split('#')[1];
if (path.includes('/'))
[, page, ...wildcards] = path.split('/')
else
page = path
this.state = { page, wildcards, lastPage: this.lastPage }
if (queryString) {
params = new URLSearchParams(queryString)
this.state.params = Object.fromEntries(params)
}
if (this.routingStart) {
this.routingStart(this.state)
}
if (this.routes[page]) {
// Fallback for browsers that don't support View transition API:
if (!document.startViewTransition) {
await this.routes[page](this.state)
this.lastPage = page
return;
}
// With a transition:
document.startViewTransition(async () => {
await this.routes[page](this.state)
this.lastPage = page
});
} else {
if (this.routes['404']) {
this.routes['404'](this.state);
} else {
console.error(`No route found for '${page}' and no '404' route is defined.`);
}
}
if (this.routingEnd) {
this.routingEnd(this.state)
}
} catch (e) {
console.error(e)
}
}
}
function togglePrivateKeyVisibility(input) {
const target = input.closest('sm-input')
target.type = target.type === 'password' ? 'text' : 'password';
target.focusIn()
}
let privKeyResolver = null
function getSignedIn(passwordType) {
return new Promise((resolve, reject) => {
privKeyResolver = resolve
try {
console.log(floDapps.user.id)
getPromptInput('Enter password', '', {
isPassword: true,
}).then(password => {
if (password) {
resolve(password)
}
})
} catch (err) {
floGlobals.isPrivKeySecured = passwordType === 'PIN/Password';
if (!['#/landing', '#/sign_in', '#/sign_up'].some(route => window.location.hash.includes(route))) {
// if user not signed in, redirect to landing page
history.replaceState(null, null, '#/landing')
router.routeTo('#/landing')
} else {
router.routeTo(window.location.hash)
}
}
});
}
function setSecurePassword() {
if (!floGlobals.isPrivKeySecured) {
const password = getRef('secure_pwd_input').value.trim();
floDapps.securePrivKey(password).then(() => {
floGlobals.isPrivKeySecured = true;
notify('Password set successfully', 'success');
closePopup();
}).catch(err => {
notify(err, 'error');
})
}
}
function signOut() {
getConfirmation('Sign out?', { message: 'You are about to sign out of the app, continue?', confirmText: 'Leave', cancelText: 'Stay' })
.then(async (res) => {
if (res) {
await floDapps.clearCredentials();
location.reload();
}
});
}
// detect browser version
function detectBrowser() {
let ua = navigator.userAgent,
tem,
M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
if (/trident/i.test(M[1])) {
tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
return 'IE ' + (tem[1] || '');
}
if (M[1] === 'Chrome') {
tem = ua.match(/\b(OPR|Edge)\/(\d+)/);
if (tem != null) return tem.slice(1).join(' ').replace('OPR', 'Opera');
}
M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?'];
if ((tem = ua.match(/version\/(\d+)/i)) != null) M.splice(1, 1, tem[1]);
return M.join(' ');
}
function buttonLoader(id, show) {
const button = typeof id === 'string' ? document.getElementById(id) : id;
button.disabled = show;
const animOptions = {
duration: 200,
fill: 'forwards',
easing: 'ease'
}
if (show) {
button.parentNode.append(document.createElement('sm-spinner'))
button.animate([
{
clipPath: 'circle(100%)',
},
{
clipPath: 'circle(0)',
},
], animOptions)
} else {
button.animate([
{
clipPath: 'circle(0)',
},
{
clipPath: 'circle(100%)',
},
], animOptions).onfinish = () => {
const potentialTarget = button.parentNode.querySelector('sm-spinner')
if (potentialTarget) potentialTarget.remove();
}
}
}
function createRipple(event, target) {
const circle = document.createElement("span");
const diameter = Math.max(target.clientWidth, target.clientHeight);
const radius = diameter / 2;
const targetDimensions = target.getBoundingClientRect();
circle.style.width = circle.style.height = `${diameter}px`;
circle.style.left = `${event.clientX - (targetDimensions.left + radius)}px`;
circle.style.top = `${event.clientY - (targetDimensions.top + radius)}px`;
circle.classList.add("ripple");
const rippleAnimation = circle.animate(
[
{
opacity: 1,
transform: `scale(0)`
},
{
transform: "scale(4)",
opacity: 0,
},
],
{
duration: 600,
fill: "forwards",
easing: "ease-out",
}
);
target.append(circle);
rippleAnimation.onfinish = () => {
circle.remove();
};
}
</script>
<script type="text/javascript">
function formatAmount(amount = 0, currency = 'btc') {
// check if amount is a string and convert it to a number
if (typeof amount === 'string') {
amount = parseFloat(amount)
}
if (!amount)
return '0';
const formattedAmount = amount.toLocaleString(currency === 'inr' ? `en-IN` : 'en-US', {
style: 'currency',
currency,
minimumFractionDigits: 0,
maximumFractionDigits: 8,
currencyDisplay: 'code'
}).slice(4)
if (currency === 'usd')
return `$ ${formattedAmount}`
else
return `${formattedAmount} ${currency.toUpperCase()}`
}
const render = {
policy(policyId, details = {}) {
const { duration, interest, loan_collateral_ratio, policy_creation_time, pre_liquidation_threshold } = details;
function initLoanRequestProcess() {
const loanRequestForm = Component(() => {
const [loanAmount, setLoanAmount] = useState(0)
const [selfCollateral, setSelfCollateral] = useState(true)
const collateralAmount = btcMortgage.util.calcRequiredCollateral((loanAmount / floGlobals.btcRate), loan_collateral_ratio)
useEffect(() => {
if (!selfCollateral) {
getRef('collateral_provider').focusIn()
getRef('request_loan_content').querySelector('[data-address]').customValidation = (value) => {
return {
isValid: floCrypto.validateAddr(value),
errorText: `Invalid address.<br> It usually starts with "F", "1", "3" or "bc1".`
}
}
}
}, [selfCollateral])
return html`
<sm-form id="request_loan_form">
<sm-input id="loan_amount" type="number" placeholder="Loan amount" min="1" step="0.01" error-text="Amount must be greater than $1" oninput=${(e) => { setLoanAmount(e.target.value) }} animate required>
<div class="currency-symbol flex" slot="icon">
<svg class="icon" xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"><path d="M0 0h24v24H0V0z" fill="none"></path><path d="M11.8 10.9c-2.27-.59-3-1.2-3-2.15 0-1.09 1.01-1.85 2.7-1.85 1.78 0 2.44.85 2.5 2.1h2.21c-.07-1.72-1.12-3.3-3.21-3.81V3h-3v2.16c-1.94.42-3.5 1.68-3.5 3.61 0 2.31 1.91 3.46 4.7 4.13 2.5.6 3 1.48 3 2.41 0 .69-.49 1.79-2.7 1.79-2.06 0-2.87-.92-2.98-2.1h-2.2c.12 2.19 1.76 3.42 3.68 3.83V21h3v-2.15c1.95-.37 3.5-1.5 3.5-3.55 0-2.84-2.43-3.81-4.7-4.4z"></path></svg>
</div>
</sm-input>
<p>Collateral amount: <b>${formatAmount(collateralAmount)}</b> (${formatAmount(collateralAmount * floGlobals.btcRate, 'usd')})</p>
<div class="grid gap-1">
<label class="interact">
<input type="radio" name="collateral_type" value="self" checked=${selfCollateral} onchange=${() => setSelfCollateral(true)} required>
<div class="grid gap-0-3">
<b>I'll provide my own collateral</b>
<p>The collateral will be taken from your logged in BTC address</p>
</div>
</label>
<label class="interact">
<input type="radio" name="collateral_type" value="other" checked=${!selfCollateral} onchange=${() => setSelfCollateral(false)} required>
<div class="grid gap-0-3">
<b>Someone else will provide the collateral</b>
<p>
The collateral will be requested from the address you provide.
<br>
Your loan request will be sent to the collateral provider for approval.
</p>
</div>
</label>
</div>
${!selfCollateral ? html`
<sm-input id="collateral_provider" type="text" placeholder="Collateral provider address" animate data-address required></sm-input>
`: ''}
<button class="button button--primary" type="submit" onclick=${confirmDetails} disabled>Request ${loanAmount ? formatAmount(loanAmount, 'usd') : 'loan'}</button>
</sm-form>
`
})
renderElem(getRef('request_loan_content'), html`${loanRequestForm()}`)
openPopup('request_loan_popup').opened.then(() => {
getRef('loan_amount').focusIn()
})
}
let loanAmount = 0
let collateralProvider = floGlobals.myBtcID
function confirmDetails() {
loanAmount = parseFloat(getRef('loan_amount').value.trim())
collateralProvider = getRef('collateral_provider') ? getRef('collateral_provider').value.trim() : floGlobals.myBtcID
const collateralAmount = btcMortgage.util.calcRequiredCollateral((loanAmount / floGlobals.btcRate), loan_collateral_ratio)
renderElem(getRef('request_loan_content'), html`
<div class="grid gap-1-5">
<div class="grid gap-0-5">
<h5>Loan amount</h5>
<p>${formatAmount(loanAmount, 'usd')}</p>
</div>
<div class="grid gap-0-5">
<h5>Collateral amount</h5>
<p>${formatAmount(collateralAmount)} (${formatAmount(collateralAmount * floGlobals.btcRate, 'usd')})</p>
</div>
<div class="grid gap-0-5">
<h5>Collateral provider</h5>
<p>${getRef('collateral_provider') ? getRef('collateral_provider').value.trim() : 'Self'}</p>
</div>
<div class="grid gap-0-5">
<h5>Loan duration</h5>
<p>${duration}</p>
</div>
<div class="grid gap-0-5">
<h5>Interest</h5>
<p>${interest * 100}% p.a</p>
</div>
<div class="multi-state-button">
<button id="request_loan_button" class="button button--primary" onclick=${requestLoan}>Confirm</button>
</div>
</div>
`)
}
async function requestLoan() {
buttonLoader('request_loan_button', true)
try {
const isProvidingCollateral = floCrypto.isSameAddr(collateralProvider, floDapps.user.id)
const { vectorClock: loanRequestID } = await btcMortgage.requestLoanCollateral(loanAmount, policyId, collateralProvider)
if (isProvidingCollateral) {
await btcMortgage.requestLoan(loanRequestID, floDapps.user.id)
} else {
notify('Loan request sent to collateral provider', 'success')
}
location.hash = '#/home/in-process'
closePopup()
} catch (err) {
console.error(err)
notify('There was error requesting loan. please try again after some time.', 'error')
buttonLoader('request_loan_button', false)
}
}
return html`
<li class="policy" .dataset=${{ policyId }}>
<div class="grid gap-0-3">
<h5>Duration</h5>
<b>${duration}</b>
</div>
<div class="grid gap-0-3">
<h5>Interest</h5>
<b>${interest * 100}% p.a</b>
</div>
<div class="grid gap-0-3">
<div class="flex gap-0-3 tooltip">
<h5>Collateral required</h5>
<svg class="icon" xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M11 7h2v2h-2zm0 4h2v6h-2zm1-9C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8z"/></svg>
<p class="tooltip__content">
This shows the multiple of collateral required for the loan amount. <br>
For example, if the collateral ratio is 1.5x, then for a loan of $100, you need to provide collateral worth $150.
</p>
</div>
<b>${parseFloat((1 / loan_collateral_ratio).toFixed(2))}x</b>
</div>
<div class="grid gap-0-3">
<div class="flex gap-0-3 tooltip">
<h5>Pre-liquidation threshold</h5>
<svg class="icon" xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M11 7h2v2h-2zm0 4h2v6h-2zm1-9C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8z"/></svg>
<p class="tooltip__content">
This percentage is which the collateral value should not fall below.<br>
If so happens, the collateral will be liquidated and the loan will be closed.
</p>
</div>
<b>${pre_liquidation_threshold ? `${pre_liquidation_threshold * 100}%` : 'None'}</b>
</div>
<button class="button button--colored" onclick=${initLoanRequestProcess}>Request loan</button>
</li>
`
},
loanRequest(requestId, details) {
const { message: { borrower, coborrower, collateral: { btc_id, quantity, rate }, loan_amount, loan_collateral_req_id, loan_opening_process_id, policy_id }, vectorClock } = details;
const { duration, interest } = btcMortgage.policies[policy_id] || {};
const isRequester = floCrypto.isSameAddr(borrower, floGlobals.myFloID) || floCrypto.isSameAddr(coborrower, floGlobals.myFloID)
const [isLending, setIsLending] = useState(false)
async function startLendingProcess() {
const confirmation = await getConfirmation('Start lending process?', { message: `You'll be participating in loan lending. Continue?`, confirmText: 'Continue', cancelText: 'Cancel' })
if (!confirmation)
return;
setIsLending(true)
btcMortgage.respondLoan(vectorClock, borrower, coborrower).then(() => {
floCloudAPI.sendApplicationData({ loan_opening_process_id }, 'in_process_loan_request')
.then(() => {
notify('Lending process started successfully.', 'success')
location.hash = '#/home/lent'
})
.catch(error => console.log(error))
}).catch(err => {
console.error(err)
notify('There was error starting lending process. please try again after some time.', 'error')
}).finally(() => {
setIsLending(false)
})
}
return html`
<li class="loan-request">
${loan_opening_process_id ? html`
<p>${isRequester ? 'Your loan' : 'Loan'} request: ${loan_opening_process_id}</p>
`: ''}
<div class="flex flex-wrap align-items-start gap-1-5">
<div class="grid gap-0-3">
<p>Loan amount</p>
<b>${formatAmount(loan_amount, 'usd')}</b>
</div>
<div class="grid gap-0-3">
<p>Collateral amount</p>
<b>${formatAmount(quantity)}</b>
</div>
<div class="grid gap-0-3">
<p>Duration</p>
<b>${duration}</b>
</div>
<div class="grid gap-0-3">
<p>Interest</p>
<b>${interest * 100}% p.a</b>
</div>
</div>
<details>
<summary>
Borrower details
<svg class="icon down-arrow" xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"><path d="M24 24H0V0h24v24z" fill="none" opacity=".87"></path><path d="M16.59 8.59L12 13.17 7.41 8.59 6 10l6 6 6-6-1.41-1.41z"></path></svg>
</summary>
<div class="grid gap-1-5">
<div class="grid gap-0-3">
<p>Borrower</p>
<sm-copy value=${getBtcAddress(borrower)}>
<b>${getBtcAddress(borrower)}</b>
</sm-copy>
</div>
${floCrypto.isSameAddr(borrower, coborrower) ? html`
<div class="grid gap-0-3">
<p>Collateral provider</p>
<sm-copy value=${getBtcAddress(coborrower)}>
<b>${getBtcAddress(coborrower)}</b>
</sm-copy>
</div>
`: ''}
</div>
</details>
${!isRequester ? html`
<button class="button button--primary margin-left-auto" onclick=${startLendingProcess} disabled=${isLending}>
${isLending ? html`
Starting lending process
<sm-spinner class="margin-left-0-5"></sm-spinner>
`: html`
Start lending
`}
</button>
`: html``}
`
},
loanProcess(processId) {
const {
type,
loanOpeningProcessID,
borrower, isBorrower, coborrower, isCoBorrower, lender, isLender, loanAmount, collateral: { rate, quantity } = {}, policyID,
initiationTime, loanResponseTime, collateralLockAckTime, loanIssuedTime,
hasProvidedCollateral, hasAgreedToLend, hasLockedCollateral, hasIssuedLoan, hasRequestedCollateralRefund,
collateralRequestID, loanResponseID, collateralLockRequestID, collateralLockAckID, loanID, closingTxID,
unlockCollateralRequestTime, hasRequestedCollateralUnlock, unlockCollateralAckTime, hasRefundedCollateral,
unlockTxHex
} = uiGlobals.inProcessRequests[processId]
if (type === 'loanOpening') {
return Component(() => {
const [verifyingCollateral, setVerifyCollateral] = useState(false)
let collateralAmount = 0
if (!isLender)
collateralAmount = btcMortgage.util.calcRequiredCollateral((loanAmount / (rate || floGlobals.btcRate)), btcMortgage.policies[policyID]?.loan_collateral_ratio) || 0
async function verifyCollateral(e) {
const confirmation = await getConfirmation('Verify collateral?', { message: `You are about to check for ${formatAmount(collateralAmount)} as collateral. Continue?`, confirmText: 'Verify', cancelText: 'Cancel' })
if (!confirmation)
return;
setVerifyCollateral(true)
try {
await btcMortgage.requestLoan(collateralRequestID, borrower)
notify('Collateral verified successfully', 'success')
} catch (err) {
notify(err, 'error')
setVerifyCollateral(false)
}
}
const [lockingCollateral, setLockingCollateral] = useState(false)
async function lockCollateral() {
const confirmation = await getConfirmation('Lock collateral?', { message: `You are about to lock ${formatAmount(collateralAmount)} as collateral. Continue?`, confirmText: 'Lock', cancelText: 'Cancel' })
if (!confirmation)
return;
try {
setLockingCollateral(true)
let collateralLockID = collateralLockRequestID
if (!collateralLockID) {
const { vectorClock } = await btcMortgage.requestCollateralLock(loanResponseID, coborrower, lender, await floDapps.user.private)
collateralLockID = vectorClock
}
await btcMortgage.lockCollateral(collateralLockID, borrower, lender, await floDapps.user.private)
notify('Collateral locked successfully', 'success')
router.routeTo(location.hash)
} catch (err) {
notify(err, 'error')
setLockingCollateral(false)
}
}
async function requestCollateralLock() {
btcMortgage.requestCollateralLock(loanResponseID, coborrower, lender, await floDapps.user.private).then(() => {
notify('Collateral lock request sent successfully', 'success')
router.routeTo(location.hash)
}).catch(err => {
notify(err, 'error')
})
}
const [isIssuingLoan, setIsIssuingLoan] = useState(false)
const [failSafe, setFailSafe] = useState(null)
const [retringFailSafe, setRetryingFailSafe] = useState(false)
async function issueLoan() {
try {
const confirmation = await getConfirmation('Issue loan?', { message: `You are about to issue ${formatAmount(loanAmount)} as loan. Continue?`, confirmText: 'Issue', cancelText: 'Cancel' })
if (!confirmation)
return;
setIsIssuingLoan(true)
await btcMortgage.sendLoanAmount(collateralLockAckID, borrower, coborrower, await floDapps.user.private)
notify('Loan issued successfully', 'success')
} catch (err) {
notify(err.message || err, 'error')
console.error(err)
if (err.fail_safe)
setFailSafe(err.fail_safe)
} finally {
setIsIssuingLoan(false)
}
}
async function retryFailSafe() {
try {
setRetryingFailSafe(true)
await btcOperator.retryFailSafe(failSafe, await floDapps.user.private)
notify('Loan issued successfully', 'success')
} catch (err) {
notify(err, 'error')
} finally {
setRetryingFailSafe(false)
}
}
// check if loan hasn't been issued even after 24 hours of collateral lock
let hasLockedCollateralForMoreThan24Hours = false
useEffect(() => {
if (isCoBorrower && hasLockedCollateral && !hasIssuedLoan) {
const timeDiff = Date.now() - collateralLockAckTime
if (timeDiff > 86400000) {
hasLockedCollateralForMoreThan24Hours = true
}
}
}, [hasLockedCollateral, hasIssuedLoan])
const [requestingCollateralRefund, setRequestingCollateralRefund] = useState(false)
async function requestCollateralRefund() {
const confirmation = await getConfirmation('Request collateral refund?', { message: `You are about to request for collateral refund. Continue?`, confirmText: 'Request', cancelText: 'Cancel' })
if (!confirmation)
return;
try {
setRequestingCollateralRefund(true)
await btcMortgage.requestBanker.refundCollateral(collateralLockAckID, borrower, lender, await floDapps.user.private)
notify('Collateral refund requested successfully', 'success')
} catch (err) {
notify(err, 'error')
} finally {
setRequestingCollateralRefund(false)
}
}
function showLoanRequestDetails() {
renderElem(getRef('request_details_content'), html`
<div class="grid gap-0-3">
<p>Loan amount</p>
<b>${formatAmount(loanAmount, 'usd')}</b>
</div>
<div class="grid gap-0-3">
<p>Collateral amount</p>
<p><b>${formatAmount(collateralAmount)}</b> (${formatAmount(collateralAmount * floGlobals.btcRate, 'usd')})</p>
</div>
<div class="grid gap-0-3">
<p>Duration</p>
<b>${btcMortgage.policies[policyID]?.duration}</b>
</div>
<div class="grid gap-0-3">
<p>Interest</p>
<b>${btcMortgage.policies[policyID]?.interest * 100}% p.a</b>
</div>
<div class="grid gap-0-3">
<p>Borrower</p>
<sm-copy value=${getBtcAddress(borrower)}>
<b>${getBtcAddress(borrower)}</b>
</sm-copy>
</div>
${!floCrypto.isSameAddr(borrower, coborrower) ? html`
<div class="grid gap-0-3">
<p>Collateral provider</p>
<sm-copy value=${getBtcAddress(coborrower)}>
<b>${getBtcAddress(coborrower)}</b>
</sm-copy>
</div>
`: ''}
`)
openPopup('request_details_popup')
}
return html`
<li class="loan-process">
<div class="flex align-center gap-1 space-between">
${isBorrower ? html`
<h4 class="loan-process__type">Borrowing</h4>
`: html`
${isCoBorrower ? html`
<h4 class="loan-process__type">Co-borrowing</h4>
`: html`
<h4 class="loan-process__type">Lending</h4>
`}
`}
<button class="button button--colored button--small" onclick=${showLoanRequestDetails}>View details</button>
</div>
<ul>
${!isLender ? html`
<li class=${`${hasProvidedCollateral ? 'done' : ''}`}>
<div class="progress">
<div class="circle"></div>
<div class="line"></div>
</div>
<div class="details">
<h4>Initiated loan request</h4>
${hasProvidedCollateral ? html`
<time>${getFormattedTime(initiationTime)}</time>
`: html`
${isCoBorrower ? html`
<p>Verify that you have <b>${formatAmount(collateralAmount)}</b> as collateral</p>
<button class="button button--primary margin-right-auto" disabled=${verifyingCollateral} onclick=${verifyCollateral}>
${verifyingCollateral ? html`
Verifying collateral
<sm-spinner class="margin-left-0-5"></sm-spinner>
`: html`
Verify collateral
`}
</button>
`: html`
<p>Waiting for Co-Borrower to confirm collateral availability</p>
`}
`}
</div>
</li>
`: ''}
<li class=${`${hasAgreedToLend ? 'done' : ''}`}>
<div class="progress">
<div class="circle"></div>
<div class="line"></div>
</div>
<div class="details">
${hasAgreedToLend ? html`
<h4>Started lending process</h4>
<time>${getFormattedTime(loanResponseTime)}</time>
`: html`
<h4>Waiting for a lender</h4>
<p>Your loan request has been posted to the marketplace. Waiting for a lender to start lending process.</p>
`}
</div>
</li>
<li class=${`${hasLockedCollateral ? 'done' : ''}`}>
<div class="progress">
<div class="circle"></div>
<div class="line"></div>
</div>
<div class="details">
${hasLockedCollateral ? html`
<h4>Collateral locked</h4>
${hasLockedCollateralForMoreThan24Hours ? html`
${hasRequestedCollateralRefund ? html`
<p>Collateral refund requested. It'll be refunded within 24 hrs.</p>
`: html`
<p>
Loan has not been issued even after 24 hours of collateral lock.
You can request for collateral refund.
</p>
<button class="button button--primary margin-right-auto" disabled=${requestingCollateralRefund} onclick=${requestCollateralRefund}>
${requestingCollateralRefund ? html`
Requesting collateral refund
<sm-spinner class="margin-left-0-5"></sm-spinner>
`: html`
Request collateral refund
`}
</button>
`}
`: html`
<time>${getFormattedTime(collateralLockAckTime)}</time>
`}
`: html`
<h4>Waiting for collateral to be locked</h4>
<p>Collateral is locked in a 2 of 3 multisig address for safe keeping.</p>
<p>${isCoBorrower ? 'You need' : 'Loan co-borrower needs'} to lock collateral before loan can be issued.</p>
${hasAgreedToLend && (isBorrower || isCoBorrower) ? html`
${floCrypto.isSameAddr(borrower, coborrower) ? html`
<button class="button button--primary margin-right-auto" disabled=${lockingCollateral} onclick=${lockCollateral}>
${lockingCollateral ? html`
Locking collateral
<sm-spinner class="margin-left-0-5"></sm-spinner>
`: html`
Lock collateral
`}
</button>
`: html`
<button class="button button--primary margin-right-auto" onclick=${requestCollateralLock}>Request collateral lock</button>
`}
`: ''}
`}
</div>
</li>
<li class=${`${hasIssuedLoan ? 'done' : ''}`}>
<div class="progress">
<div class="circle"></div>
</div>
<div class="details">
${hasIssuedLoan ? html`
<h4>Loan issued</h4>
<time>${getFormattedTime(loanIssuedTime)}</time>
`: html`
${hasLockedCollateral && isLender ? html`
${failSafe ? html`
<h4>Loan failed to issue</h4>
<button class="button button--primary margin-right-auto" disabled=${retringFailSafe} onclick=${retryFailSafe}>
${retringFailSafe ? html`
Retrying
<sm-spinner class="margin-left-0-5"></sm-spinner>
`: html`
Retry
`}
</button>
`: html`
<h4>Loan is ready to be issued</h4>
<button class="button button--primary margin-right-auto" disabled=${isIssuingLoan} onclick=${issueLoan}>
${isIssuingLoan ? html`
Issuing loan
<sm-spinner class="margin-left-0-5"></sm-spinner>
`: html`
Issue loan
`}
</button>
`}
`: html`
<h4>Waiting for loan to be issued</h4>
`}
`}
</div>
</li>
</ul>
<p class="margin-left-auto">Request ID: #${loanOpeningProcessID}</p>
</li>
`
})()
} else if (type === 'loanClosing') {
return Component(() => {
const [isRequestingCollateralUnlock, setIsRequestingCollateralUnlock] = useState(false)
let failedToUnlockCollateralAfter24Hours = false
if (isCoBorrower && hasRequestedCollateralUnlock && !hasRefundedCollateral) {
const timeDiff = Date.now() - collateralLockAckTime
if (timeDiff > 86400000) {
failedToUnlockCollateralAfter24Hours = true
}
}
async function requestCollateralUnlock() {
const confirmation = await getConfirmation('Request collateral refund?', { message: `You are about to request for collateral refund. Continue?`, confirmText: 'Request', cancelText: 'Cancel' })
if (!confirmation)
return;
setIsRequestingCollateralUnlock(true)
try {
console.log('requesting collateral unlock', loanID, closingTxID, await floDapps.user.private)
await btcMortgage.requestUnlockCollateral(loanID, closingTxID, await floDapps.user.private, failedToUnlockCollateralAfter24Hours)
notify('Collateral refund requested successfully', 'success')
} catch (err) {
notify(err, 'error')
} finally {
setIsRequestingCollateralUnlock(false)
}
}
const [isUnlockingCollateral, setIsUnlockingCollateral] = useState(false)
const [hasUnlockedCollateral, setHasUnlockedCollateral] = useState(false)
async function unlockCollateral() {
const confirmation = await getConfirmation('Unlock collateral?', { message: `You are about to unlock collateral. Continue?`, confirmText: 'Unlock', cancelText: 'Cancel' })
if (!confirmation)
return;
setIsUnlockingCollateral(true)
try {
await btcMortgage.unlockCollateral(loanID, closingTxID, unlockTxHex, await floDapps.user.private)
notify('Collateral unlocked successfully', 'success')
setHasUnlockedCollateral(true)
} catch (err) {
notify(err, 'error')
setHasUnlockedCollateral(false)
} finally {
setIsUnlockingCollateral(false)
}
}
return html`
<li class="loan-process">
${isBorrower || coborrower ? html`
<h4 class="loan-process__type">Repaying loan</h4>
`: html`
<h4 class="loan-process__type">Borrower repaying</h4>
`}
<ul>
<li class="done">
<div class="progress">
<div class="circle"></div>
<div class="line"></div>
</div>
<div class="details">
<h4>Due amount paid</h4>
<a href=${`https://blockbook.ranchimall.net/tx/${closingTxID}`} target="_blank" class="button button--colored button--small margin-right-auto">Check on blockchain</a>
</div>
</li>
<li class=${hasRequestedCollateralUnlock ? 'done' : ''}>
<div class="progress">
<div class="circle"></div>
<div class="line"></div>
</div>
<div class="details">
${hasRequestedCollateralUnlock ? html`
${failedToUnlockCollateralAfter24Hours ? html`
<h4>Lender hasn't unlocked collateral</h4>
<p>Collateral failed to unlock even after 24 hours of loan closing. Please request to banker below.</p>
<button class="button button--primary margin-right-auto" disabled=${isRequestingCollateralUnlock} onclick=${requestCollateralUnlock}>
${isRequestingCollateralUnlock ? html`
Requesting collateral refund
<sm-spinner class="margin-left-0-5"></sm-spinner>
`: html`
Request collateral refund
`}
</button>
`: html`
<h4>Collateral unlock requested</h4>
<time>${getFormattedTime(unlockCollateralRequestTime)}</time>
`}
`: html`
${isCoBorrower ? html`
<h4>Request collateral refund</h4>
<button class="button button--primary margin-right-auto" disabled=${isRequestingCollateralUnlock} onclick=${requestCollateralUnlock}>
${isRequestingCollateralUnlock ? html`
Requesting collateral refund
<sm-spinner class="margin-left-0-5"></sm-spinner>
`: html`
Request collateral refund
`}
</button>
`: html`
<h4>Waiting for Co-borrower to request collateral refund</h4>
<p>Collateral refund can be requested only by Co-borrower</p>
`}
`}
</div>
</li>
<li class=${hasRefundedCollateral || hasUnlockedCollateral ? 'done' : ''}>
<div class="progress">
<div class="circle"></div>
</div>
<div class="details">
${hasRefundedCollateral || hasUnlockedCollateral ? html`
<h4>Collateral refunded. Loan closed.</h4>
<time>${getFormattedTime(unlockCollateralAckTime)}</time>
`: html`
${isLender ? html`
<h4>Unlock loan collateral</h4>
<p>Borrower has paid the loan amount. Please unlock the collateral.</p>
<button class="button button--primary margin-right-auto" disabled=${isUnlockingCollateral} onclick=${unlockCollateral}>
${isUnlockingCollateral ? html`
Unlocking collateral
<sm-spinner class="margin-left-0-5"></sm-spinner>
`: html`
Unlock collateral
`}
</button>
`: html`
<h4>Waiting for collateral to be refunded</h4>
<p>Collateral will be refunded within 24 hours of request.</p>
`}
`}
</div>
</li>
</ul>
</li>
`
})()
} else {
console.log('Unknown loan process type', type)
return html``
}
},
loan(loanID) {
const {
blocktime, borrower, borrower_sign, btc_start_rate,
coborrower, coborrower_sign, collateral_lock_id, collateral_value,
lender, lender_sign, loan_amount, loan_id, loan_opening_process_id, loan_transfer_id,
open_time, close_time, policy_id } = btcMortgage.loans[loanID];
const { hasRepaidLoan, hasRefundedCollateral, collateralUnlockAckTime, hasRequestedCollateralLiquidation, hasRequestedCollateralPreLiquidation } = uiGlobals.activeLoanQueries[loanID] || {}
const amountDue = btcMortgage.util.calcDueAmount(loan_amount, policy_id, open_time)
const [isRepayingLoan, setIsRepayingLoan] = useState(false)
async function initLoanRepayment() {
const confirmation = await getConfirmation('Repay loan?', { message: `You are about to repay ${formatAmount(amountDue, 'usd')} as loan. Continue?`, confirmText: 'Repay', cancelText: 'Cancel' })
if (!confirmation)
return;
setIsRepayingLoan(true)
try {
const { message: { closing_txid } } = await btcMortgage.repayLoan(loan_id, await floDapps.user.private)
notify('Loan repaid successfully', 'success')
if (isCoBorrower) {
await btcMortgage.requestUnlockCollateral(loan_id, closing_txid, await floDapps.user.private)
notify('Collateral refund requested', 'success')
}
} catch (err) {
notify(err, 'error')
} finally {
setIsRepayingLoan(false)
}
}
const isBorrower = floCrypto.isSameAddr(borrower, floDapps.user.id)
const isCoBorrower = floCrypto.isSameAddr(coborrower, floDapps.user.id)
const isLender = floCrypto.isSameAddr(lender, floDapps.user.id)
let loanStatus = '';
let loanStatusBadge = '';
if (close_time || hasRefundedCollateral) {
loanStatus = 'Closed'
loanStatusBadge = html`
<div class="status">
<svg class="icon" xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"><rect fill="none" height="24" width="24"/><path d="M22,5.18L10.59,16.6l-4.24-4.24l1.41-1.41l2.83,2.83l10-10L22,5.18z M19.79,10.22C19.92,10.79,20,11.39,20,12 c0,4.42-3.58,8-8,8s-8-3.58-8-8c0-4.42,3.58-8,8-8c1.58,0,3.04,0.46,4.28,1.25l1.44-1.44C16.1,2.67,14.13,2,12,2C6.48,2,2,6.48,2,12 c0,5.52,4.48,10,10,10s10-4.48,10-10c0-1.19-0.22-2.33-0.6-3.39L19.79,10.22z"/></svg>
Closed
</div>
`
} else {
if (hasRepaidLoan) {
loanStatus = 'Repaying'
loanStatusBadge = html`
<div class="status">
<svg class="icon" xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M11.99 2C6.47 2 2 6.48 2 12s4.47 10 9.99 10C17.52 22 22 17.52 22 12S17.52 2 11.99 2zM12 20c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8zm.5-13H11v6l5.25 3.15.75-1.23-4.5-2.67z"/></svg>
Repaying
</div>
`
} else {
loanStatus = 'Active'
loanStatusBadge = html`
<div class="status">
<svg class="icon" xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"><g><rect fill="none" height="24" width="24"/></g><g><path d="M11,21h-1l1-7H7.5c-0.88,0-0.33-0.75-0.31-0.78C8.48,10.94,10.42,7.54,13.01,3h1l-1,7h3.51c0.4,0,0.62,0.19,0.4,0.66 C12.97,17.55,11,21,11,21z"/></g></svg>
Active
</div>
`
}
}
const isLoanOverdue = Date.now() > (open_time + decodeDateStringToMilliseconds(btcMortgage.policies[policy_id]?.duration)) && loanStatus === 'Active';
const [isClaimingCollateral, setIsClaimingCollateral] = useState(false)
async function claimCollateral() {
const confirmation = await getConfirmation('Claim collateral?', { message: `Your claim request will be handled by our banker within 48hrs. Continue?`, confirmText: 'Claim', cancelText: 'Cancel' })
if (!confirmation)
return;
setIsClaimingCollateral(true)
try {
await btcMortgage.requestBanker.liquidateCollateral(loan_id, await floDapps.user.private)
notify('Collateral claim request sent successfully', 'success')
} catch (err) {
notify(err, 'error')
} finally {
setIsClaimingCollateral(false)
}
}
const hasGoneBelowThreshold = btcMortgage.util.calcRateRatio(floGlobals.btcRate, btcMortgage.policies[policy_id]?.pre_liquidation_threshold) || false;
const [isRequestingCollateralPreLiquidation, setIsRequestingCollateralPreLiquidation] = useState(false)
async function requestCollateralPreLiquidation() {
const confirmation = await getConfirmation('Request collateral pre-liquidation?', { message: `You are about to request for collateral pre-liquidation. Continue?`, confirmText: 'Request', cancelText: 'Cancel' })
if (!confirmation)
return;
setIsRequestingCollateralPreLiquidation(true)
try {
await btcMortgage.requestBanker.preLiquidateCollateral(loan_id, await floDapps.user.private)
notify('Collateral pre-liquidation request sent successfully', 'success')
} catch (err) {
notify(err, 'error')
} finally {
setIsRequestingCollateralPreLiquidation(false)
}
}
return html`
<li class="loan">
${loanStatusBadge}
<sm-copy value=${loan_id} clip-text>
<p>Loan ID: ${loan_id}</p>
</sm-copy>
<div class="flex flex-wrap gap-1-5 align-items-start">
<div class="flex flex-wrap gap-1-5 align-items-start">
<div class="grid gap-0-3">
<p>Loan amount</p>
<b>${formatAmount(loan_amount, 'usd')}</b>
</div>
<div class="grid gap-0-3">
<p>Collateral amount</p>
<b>${formatAmount(collateral_value)}</b>
</div>
<div class="grid gap-0-3">
<p>Duration</p>
<b>${btcMortgage.policies[policy_id]?.duration}</b>
</div>
<div class="grid gap-0-3">
<p>Interest</p>
<b>${btcMortgage.policies[policy_id]?.interest * 100}% p.a</b>
</div>
</div>
<div class="flex flex-wrap gap-1-5 align-items-start">
<div class="grid gap-0-3">
<p>Opening date</p>
<b>${getFormattedTime(open_time)}</b>
</div>
${loanStatus === 'Closed' ? html`
<div class="grid gap-0-3">
<p>Closing date</p>
<b>${getFormattedTime(close_time || collateralUnlockAckTime)}</b>
</div>
`: html`
<div class="grid gap-0-3">
<p>Loan deadline</p>
<b>${getFormattedTime(open_time + decodeDateStringToMilliseconds(btcMortgage.policies[policy_id]?.duration))}</b>
</div>
`}
</div>
</div>
<div class="grid gap-0-3">
<p>Interest ${isLender ? 'earned' : loanStatus === 'Closed' ? 'paid' : 'accrued'}</p>
<b style=${`color: ${isLender ? 'var(--green)' : 'var(--danger-color)'}`}>${formatAmount(amountDue - loan_amount, 'usd')}</b>
</div>
<details>
<summary>
Borrower details
<svg class="icon down-arrow" xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"><path d="M24 24H0V0h24v24z" fill="none" opacity=".87"></path><path d="M16.59 8.59L12 13.17 7.41 8.59 6 10l6 6 6-6-1.41-1.41z"></path></svg>
</summary>
<div class="grid gap-1-5">
<div class="grid">
<p>Borrower</p>
<sm-copy value=${getBtcAddress(borrower)}>
<b>${getBtcAddress(borrower)}</b>
</sm-copy>
</div>
${!floCrypto.isSameAddr(borrower, coborrower) ? html`
<div class="grid">
<p>Collateral provider</p>
<sm-copy value=${getBtcAddress(coborrower)}>
<b>${getBtcAddress(coborrower)}</b>
</sm-copy>
</div>
`: ''}
</div>
</details>
${isBorrower && loanStatus === 'active' ? html`
<div class="flex align-center space-between gap-1-5">
<div class="grid gap-0-3">
<p>Amount due (If paid now)</p>
<b style="font-size: 1.2rem;">${formatAmount(amountDue, 'usd')}</b>
</div>
<button class="button button--primary margin-left-auto" disabled=${isRepayingLoan} onclick=${initLoanRepayment}>
${isRepayingLoan ? html`
Repaying loan
<sm-spinner class="margin-left-0-5"></sm-spinner>
`: html`
Repay loan
`}
</button>
</div>
`: ''}
${loanStatus === 'closed' && isLender && isLoanOverdue ? html`
<div class="flex align-center space-between gap-1-5">
${hasRequestedCollateralLiquidation ? html`
<div class="grid gap-0-3">
<h4>Collateral liquidation requested</h4>
<p>Our banker will resolve your request within 48hrs.</p>
</div>
`: html`
<div class="grid gap-0-3">
<h4>Loan repayment overdue</h4>
<p>Borrower has not repaid the loan even after the deadline. You can now claim the collateral.</p>
</div>
<button class="button button--primary margin-left-auto" disabled=${isClaimingCollateral} onclick=${claimCollateral}>
${isClaimingCollateral ? html`
Claiming collateral
<sm-spinner class="margin-left-0-5"></sm-spinner>
`: html`
Claim collateral
`}
</button>
`}
</div>
`: ''}
</li>
`
}
}
// routing logic
const router = new Router({
routingStart(state) {
const { page } = state;
if (floGlobals.myFloID && !getRef('main_header')) {
renderElem(getRef('app_body'), html`
<header id="main_header">
<div class="app-brand margin-right-auto hide-on-small">
<svg id="main_logo" class="icon" viewBox="0 0 27.25 32"> <title>RanchiMall</title> <path d="M27.14,30.86c-.74-2.48-3-4.36-8.25-6.94a20,20,0,0,1-4.2-2.49,6,6,0,0,1-1.25-1.67,4,4,0,0,1,0-2.26c.37-1.08.79-1.57,3.89-4.55a11.66,11.66,0,0,0,3.34-4.67,6.54,6.54,0,0,0,.05-2.82C20,3.6,18.58,2,16.16.49c-.89-.56-1.29-.64-1.3-.24a3,3,0,0,1-.3.72l-.3.55L13.42.94C13,.62,12.4.26,12.19.15c-.4-.2-.73-.18-.72.05a9.39,9.39,0,0,1-.61,1.33s-.14,0-.27-.13C8.76.09,8-.27,8,.23A11.73,11.73,0,0,1,6.76,2.6C4.81,5.87,2.83,7.49.77,7.49c-.89,0-.88,0-.61,1,.22.85.33.92,1.09.69A5.29,5.29,0,0,0,3,8.33c.23-.17.45-.29.49-.26a2,2,0,0,1,.22.63A1.31,1.31,0,0,0,4,9.34a5.62,5.62,0,0,0,2.27-.87L7,8l.13.55c.19.74.32.82,1,.65a7.06,7.06,0,0,0,3.46-2.47l.6-.71-.06.64c-.17,1.63-1.3,3.42-3.39,5.42L6.73,14c-3.21,3.06-3,5.59.6,8a46.77,46.77,0,0,0,4.6,2.41c.28.13,1,.52,1.59.87,3.31,2,4.95,3.92,4.95,5.93a2.49,2.49,0,0,0,.07.77h0c.09.09,0,.1.9-.14a2.61,2.61,0,0,0,.83-.32,3.69,3.69,0,0,0-.55-1.83A11.14,11.14,0,0,0,17,26.81a35.7,35.7,0,0,0-5.1-2.91C9.37,22.64,8.38,22,7.52,21.17a3.53,3.53,0,0,1-1.18-2.48c0-1.38.71-2.58,2.5-4.23,2.84-2.6,3.92-3.91,4.67-5.65a3.64,3.64,0,0,0,.42-2A3.37,3.37,0,0,0,13.61,5l-.32-.74.29-.48c.17-.27.37-.63.46-.8l.15-.3.44.64a5.92,5.92,0,0,1,1,2.81,5.86,5.86,0,0,1-.42,1.94c0,.12-.12.3-.15.4a9.49,9.49,0,0,1-.67,1.1,28,28,0,0,1-4,4.29C8.62,15.49,8.05,16.44,8,17.78a3.28,3.28,0,0,0,1.11,2.76c.95,1,2.07,1.74,5.25,3.32,3.64,1.82,5.22,2.9,6.41,4.38A4.78,4.78,0,0,1,21.94,31a3.21,3.21,0,0,0,.14.92,1.06,1.06,0,0,0,.43-.05l.83-.22.46-.12-.06-.46c-.21-1.53-1.62-3.25-3.94-4.8a37.57,37.57,0,0,0-5.22-2.82A13.36,13.36,0,0,1,11,21.19a3.36,3.36,0,0,1-.8-4.19c.41-.85.83-1.31,3.77-4.15,2.39-2.31,3.43-4.13,3.43-6a5.85,5.85,0,0,0-2.08-4.29c-.23-.21-.44-.43-.65-.65A2.5,2.5,0,0,1,15.27.69a10.6,10.6,0,0,1,2.91,2.78A4.16,4.16,0,0,1,19,6.16a4.91,4.91,0,0,1-.87,3c-.71,1.22-1.26,1.82-4.27,4.67a9.47,9.47,0,0,0-2.07,2.6,2.76,2.76,0,0,0-.33,1.54,2.76,2.76,0,0,0,.29,1.47c.57,1.21,2.23,2.55,4.65,3.73a32.41,32.41,0,0,1,5.82,3.24c2.16,1.6,3.2,3.16,3.2,4.8a1.94,1.94,0,0,0,.09.76,4.54,4.54,0,0,0,1.66-.4C27.29,31.42,27.29,31.37,27.14,30.86ZM6.1,7h0a3.77,3.77,0,0,1-1.46.45L4,7.51l.68-.83a25.09,25.09,0,0,0,3-4.82A12,12,0,0,1,8.28.76c.11-.12.77.32,1.53,1l.63.58-.57.84A10.34,10.34,0,0,1,6.1,7Zm5.71-1.78A9.77,9.77,0,0,1,9.24,7.18h0a5.25,5.25,0,0,1-1.17.28l-.58,0,.65-.78a21.29,21.29,0,0,0,2.1-3.12c.22-.41.42-.76.44-.79s.5.43.9,1.24L12,5ZM13.41,3a2.84,2.84,0,0,1-.45.64,11,11,0,0,1-.9-.91l-.84-.9.19-.45c.34-.79.39-.8,1-.31A9.4,9.4,0,0,1,13.8,2.33q-.18.34-.39.69Z" /> </svg>
<div class="app-name">
<div class="app-name__company">RanchiMall</div>
<h4 class="app-name__title">
BTC Mortgage
</h4>
</div>
</div>
<theme-toggle></theme-toggle>
<button id="user_popup_button" class="button--small" onclick="openPopup('user_popup')">
<svg class="icon margin-right-0-5" width="64" height="64" viewBox="0 0 64 64" fill="none" xmlns="http://www.w3.org/2000/svg"> <mask id="path-1-outside-1_16_6" maskUnits="userSpaceOnUse" x="3" y="3" width="58" height="58"> <rect fill="white" x="3" y="3" width="58" height="58" /> <path fill-rule="evenodd" clip-rule="evenodd" d="M31.946 43.6019C28.8545 47.1308 28.7789 51.9719 31.9811 55.1957C35.0078 52.2797 35.0105 46.8446 31.946 43.6019ZM31.9487 10.6835C24.6452 19.1291 24.9206 29.1137 31.9433 37.4108C39.0929 28.9436 39.1118 19.0076 31.9487 10.6808V10.6835ZM37.1111 35.051C43.1861 28.841 50.5976 27.4208 59 28.7654C56.2919 34.9754 52.4714 39.9353 45.7214 42.53C47.2118 41.3609 48.5699 40.4051 49.7984 39.3089C51.8504 37.481 53.303 35.267 54.0293 32.6075C54.2588 31.7678 53.9618 31.5302 53.1896 31.406C50.3627 30.9524 47.7086 31.5464 45.1733 32.702C40.9073 34.646 37.4324 37.6403 34.1735 40.8938C34.1168 40.9532 34.0925 41.0396 33.9899 41.2259C40.3754 41.4689 45.2381 44.0177 48.119 49.9064C44.3768 50.738 40.9532 50.5625 37.778 48.5213C40.1702 49.2557 42.557 49.7903 45.176 48.5483C44.852 48.0083 44.663 47.3765 44.231 47.0228C43.0511 46.0589 41.8415 45.0842 40.505 44.3498C38.4395 43.2212 36.131 42.692 33.5768 42.1682C37.0247 48.2081 36.1607 53.6918 31.946 59C29.9858 56.5295 28.6142 53.9456 28.2389 50.8946C27.8609 47.8328 28.7222 45.0518 30.1991 42.3896C26.9321 41.8658 19.8824 45.6485 18.7916 48.5645C21.3215 49.8443 23.7893 49.2611 26.33 48.3161C24.5048 50.1953 19.2425 50.9648 15.8108 49.8335C18.6593 44.0285 23.4977 41.4716 30.0263 41.2286C29.5457 40.7426 29.1893 40.3673 28.8167 40.0082C26.1005 37.3892 23.2142 34.9862 19.823 33.2366C17.0501 31.8056 14.1422 30.9092 10.9481 31.3871C10.5701 31.4438 10.1948 31.5356 9.6656 31.6436C10.6376 36.6386 13.9856 39.7355 18.1274 42.3464C13.775 41.8199 6.431 34.214 5 28.7546C13.397 27.3938 20.849 28.8842 26.897 35.2319C21.8939 24.089 24.5561 14.2259 31.946 5C39.2765 14.153 41.9657 23.9459 37.1111 35.051" /> </mask> <path fill-rule="evenodd" clip-rule="evenodd" d="M31.946 43.6019C28.8545 47.1308 28.7789 51.9719 31.9811 55.1957C35.0078 52.2797 35.0105 46.8446 31.946 43.6019ZM31.9487 10.6835C24.6452 19.1291 24.9206 29.1137 31.9433 37.4108C39.0929 28.9436 39.1118 19.0076 31.9487 10.6808V10.6835ZM37.1111 35.051C43.1861 28.841 50.5976 27.4208 59 28.7654C56.2919 34.9754 52.4714 39.9353 45.7214 42.53C47.2118 41.3609 48.5699 40.4051 49.7984 39.3089C51.8504 37.481 53.303 35.267 54.0293 32.6075C54.2588 31.7678 53.9618 31.5302 53.1896 31.406C50.3627 30.9524 47.7086 31.5464 45.1733 32.702C40.9073 34.646 37.4324 37.6403 34.1735 40.8938C34.1168 40.9532 34.0925 41.0396 33.9899 41.2259C40.3754 41.4689 45.2381 44.0177 48.119 49.9064C44.3768 50.738 40.9532 50.5625 37.778 48.5213C40.1702 49.2557 42.557 49.7903 45.176 48.5483C44.852 48.0083 44.663 47.3765 44.231 47.0228C43.0511 46.0589 41.8415 45.0842 40.505 44.3498C38.4395 43.2212 36.131 42.692 33.5768 42.1682C37.0247 48.2081 36.1607 53.6918 31.946 59C29.9858 56.5295 28.6142 53.9456 28.2389 50.8946C27.8609 47.8328 28.7222 45.0518 30.1991 42.3896C26.9321 41.8658 19.8824 45.6485 18.7916 48.5645C21.3215 49.8443 23.7893 49.2611 26.33 48.3161C24.5048 50.1953 19.2425 50.9648 15.8108 49.8335C18.6593 44.0285 23.4977 41.4716 30.0263 41.2286C29.5457 40.7426 29.1893 40.3673 28.8167 40.0082C26.1005 37.3892 23.2142 34.9862 19.823 33.2366C17.0501 31.8056 14.1422 30.9092 10.9481 31.3871C10.5701 31.4438 10.1948 31.5356 9.6656 31.6436C10.6376 36.6386 13.9856 39.7355 18.1274 42.3464C13.775 41.8199 6.431 34.214 5 28.7546C13.397 27.3938 20.849 28.8842 26.897 35.2319C21.8939 24.089 24.5561 14.2259 31.946 5C39.2765 14.153 41.9657 23.9459 37.1111 35.051" /> <path d="M31.946 43.6019L32.6728 42.915L31.918 42.1163L31.1938 42.943L31.946 43.6019ZM31.9811 55.1957L31.2716 55.9004L31.9657 56.5992L32.6749 55.9159L31.9811 55.1957ZM31.9487 10.6835L32.7051 11.3376L32.9487 11.0559V10.6835H31.9487ZM31.9433 37.4108L31.18 38.0569L31.9442 38.9597L32.7074 38.056L31.9433 37.4108ZM31.9487 10.6808L32.7068 10.0287L30.9487 7.98495V10.6808H31.9487ZM59 28.7654L59.9166 29.1651L60.4326 27.9819L59.158 27.778L59 28.7654ZM45.7214 42.53L45.1042 41.7432L46.0802 43.4634L45.7214 42.53ZM49.7984 39.3089L49.1332 38.5622L49.1326 38.5628L49.7984 39.3089ZM54.0293 32.6075L53.0647 32.3439L53.0646 32.3441L54.0293 32.6075ZM53.1896 31.406L53.3484 30.4187L53.348 30.4186L53.1896 31.406ZM45.1733 32.702L45.588 33.612L45.5881 33.6119L45.1733 32.702ZM34.1735 40.8938L33.467 40.1861L33.4585 40.1946L33.4501 40.2033L34.1735 40.8938ZM33.9899 41.2259L33.114 40.7435L32.3319 42.1635L33.9519 42.2252L33.9899 41.2259ZM48.119 49.9064L48.3359 50.8826L49.5751 50.6072L49.0173 49.4669L48.119 49.9064ZM37.778 48.5213L38.0715 47.5653L37.2372 49.3625L37.778 48.5213ZM45.176 48.5483L45.6045 49.4518L46.6008 48.9794L46.0335 48.0338L45.176 48.5483ZM44.231 47.0228L44.8645 46.2491L44.8637 46.2484L44.231 47.0228ZM40.505 44.3498L40.9866 43.4734L40.9845 43.4723L40.505 44.3498ZM33.5768 42.1682L33.7777 41.1886L31.6127 40.7446L32.7083 42.664L33.5768 42.1682ZM31.946 59L31.1626 59.6216L31.9457 60.6085L32.7292 59.6218L31.946 59ZM28.2389 50.8946L29.2314 50.7725L29.2314 50.7721L28.2389 50.8946ZM30.1991 42.3896L31.0736 42.8747L31.7652 41.6279L30.3574 41.4022L30.1991 42.3896ZM18.7916 48.5645L17.855 48.2141L17.5413 49.0527L18.3402 49.4568L18.7916 48.5645ZM26.33 48.3161L27.0473 49.0128L25.9814 47.3788L26.33 48.3161ZM15.8108 49.8335L14.9131 49.393L14.4073 50.4237L15.4977 50.7832L15.8108 49.8335ZM30.0263 41.2286L30.0635 42.2279L32.3372 42.1433L30.7373 40.5255L30.0263 41.2286ZM28.8167 40.0082L28.1226 40.7281L28.1228 40.7282L28.8167 40.0082ZM19.823 33.2366L19.3644 34.1252L19.3645 34.1253L19.823 33.2366ZM10.9481 31.3871L10.8001 30.3981L10.7998 30.3982L10.9481 31.3871ZM9.6656 31.6436L9.46564 30.6638L8.49474 30.8619L8.68401 31.8346L9.6656 31.6436ZM18.1274 42.3464L18.0073 43.3392L18.6607 41.5005L18.1274 42.3464ZM5 28.7546L4.84003 27.7675L3.75363 27.9435L4.03268 29.0082L5 28.7546ZM26.897 35.2319L26.173 35.9217L27.8093 34.8223L26.897 35.2319ZM31.946 5L32.7265 4.37488L31.9461 3.40036L31.1655 4.37483L31.946 5ZM31.1938 42.943C27.7974 46.8199 27.6566 52.261 31.2716 55.9004L32.6906 54.491C29.9012 51.6828 29.9116 47.4417 32.6982 44.2609L31.1938 42.943ZM32.6749 55.9159C36.1304 52.5867 36.081 46.5214 32.6728 42.915L31.2192 44.2888C33.94 47.1678 33.8852 51.9727 31.2873 54.4755L32.6749 55.9159ZM31.1923 10.0294C27.4048 14.4092 25.5379 19.2455 25.5737 24.1102C25.6095 28.971 27.5438 33.7608 31.18 38.0569L32.7066 36.7647C29.3201 32.7637 27.6054 28.4127 27.5736 24.0955C27.5419 19.7822 29.1891 15.4034 32.7051 11.3376L31.1923 10.0294ZM32.7074 38.056C36.4083 33.6729 38.31 28.8504 38.3133 23.9938C38.3165 19.1359 36.42 14.3451 32.7068 10.0287L31.1906 11.3329C34.6405 15.3433 36.3161 19.6839 36.3133 23.9925C36.3104 28.3024 34.6279 32.6815 31.1792 36.7656L32.7074 38.056ZM30.9487 10.6808V10.6835H32.9487V10.6808H30.9487ZM37.8259 35.7503C43.6131 29.8345 50.6632 28.444 58.842 29.7528L59.158 27.778C50.532 26.3976 42.7591 27.8475 36.3963 34.3517L37.8259 35.7503ZM58.0834 28.3657C55.4402 34.4268 51.7791 39.1301 45.3626 41.5966L46.0802 43.4634C53.1637 40.7405 57.1436 35.524 59.9166 29.1651L58.0834 28.3657ZM46.3386 43.3168C47.7733 42.1914 49.2057 41.178 50.4642 40.055L49.1326 38.5628C47.9341 39.6322 46.6503 40.5304 45.1042 41.7432L46.3386 43.3168ZM50.4636 40.0556C52.6466 38.111 54.2121 35.7338 54.994 32.871L53.0646 32.3441C52.3939 34.8002 51.0542 36.851 49.1332 38.5622L50.4636 40.0556ZM54.9939 32.8711C55.1262 32.3872 55.2248 31.6946 54.7698 31.1186C54.3643 30.6053 53.7257 30.4794 53.3484 30.4187L53.0308 32.3933C53.1086 32.4058 53.17 32.4181 53.2184 32.43C53.2671 32.442 53.2955 32.4518 53.3091 32.4572C53.3227 32.4626 53.3139 32.4606 53.2926 32.4461C53.2699 32.4307 53.2353 32.4025 53.2004 32.3583C53.1832 32.3366 53.1677 32.3132 53.1543 32.2886C53.1409 32.264 53.1307 32.2402 53.1231 32.2182C53.1079 32.1742 53.1053 32.144 53.1049 32.1366C53.1046 32.1302 53.1059 32.1427 53.1007 32.1797C53.0955 32.2165 53.0849 32.27 53.0647 32.3439L54.9939 32.8711ZM53.348 30.4186C50.274 29.9254 47.4176 30.5801 44.7585 31.7921L45.5881 33.6119C47.9996 32.5127 50.4514 31.9794 53.0312 32.3934L53.348 30.4186ZM44.7586 31.792C40.3293 33.8104 36.7535 36.9051 33.467 40.1861L34.88 41.6015C38.1113 38.3755 41.4853 35.4816 45.588 33.612L44.7586 31.792ZM33.4501 40.2033C33.3063 40.354 33.2291 40.5156 33.2033 40.5674C33.1672 40.64 33.1517 40.6749 33.114 40.7435L34.8658 41.7083C34.9307 41.5906 34.9786 41.4891 34.9945 41.4572C35.0049 41.4363 35.0004 41.4467 34.9882 41.4667C34.9731 41.4914 34.9435 41.5355 34.8969 41.5843L33.4501 40.2033ZM33.9519 42.2252C37.0233 42.3421 39.6629 43.0109 41.8597 44.3098C44.0485 45.6039 45.857 47.5582 47.2207 50.3459L49.0173 49.4669C47.5001 46.3659 45.4368 44.1014 42.8776 42.5882C40.3265 41.0798 37.342 40.3527 34.0279 40.2266L33.9519 42.2252ZM47.9021 48.9302C44.3241 49.7253 41.1983 49.5313 38.3188 47.6801L37.2372 49.3625C40.7081 51.5937 44.4295 51.7507 48.3359 50.8826L47.9021 48.9302ZM37.4845 49.4773C39.9089 50.2215 42.6205 50.8669 45.6045 49.4518L44.7475 47.6448C42.4935 48.7137 40.4315 48.2899 38.0715 47.5653L37.4845 49.4773ZM46.0335 48.0338C45.8849 47.7862 45.8212 47.6158 45.6292 47.2504C45.4736 46.9544 45.2423 46.5584 44.8645 46.2491L43.5975 47.7965C43.6517 47.8409 43.7309 47.9377 43.8588 48.181C43.9503 48.3549 44.1431 48.7704 44.3185 49.0628L46.0335 48.0338ZM44.8637 46.2484C43.6913 45.2906 42.4149 44.2582 40.9866 43.4734L40.0234 45.2262C41.2681 45.9102 42.4109 46.8272 43.5983 47.7972L44.8637 46.2484ZM40.9845 43.4723C38.7711 42.2628 36.3236 41.7107 33.7777 41.1886L33.3759 43.1478C35.9384 43.6733 38.1079 44.1796 40.0255 45.2273L40.9845 43.4723ZM32.7083 42.664C34.346 45.5328 34.9255 48.203 34.6411 50.7474C34.3555 53.3015 33.1882 55.8274 31.1628 58.3782L32.7292 59.6218C34.9185 56.8644 36.2905 53.9944 36.6287 50.9696C36.9679 47.9351 36.2555 44.8435 34.4453 41.6724L32.7083 42.664ZM32.7294 58.3784C30.8397 55.9968 29.577 53.5818 29.2314 50.7725L27.2464 51.0167C27.6514 54.3094 29.1319 57.0622 31.1626 59.6216L32.7294 58.3784ZM29.2314 50.7721C28.8877 47.9881 29.6602 45.4224 31.0736 42.8747L29.3247 41.9045C27.7842 44.6812 26.8341 47.6775 27.2464 51.0171L29.2314 50.7721ZM30.3574 41.4022C29.3303 41.2375 28.1125 41.4187 26.9159 41.7622C25.6987 42.1117 24.4105 42.6564 23.2045 43.307C21.999 43.9574 20.8488 44.728 19.9166 45.543C19.005 46.3399 18.2137 47.2553 17.855 48.2141L19.7282 48.9149C19.9149 48.4157 20.4139 47.7647 21.233 47.0487C22.0314 46.3507 23.0524 45.6616 24.1541 45.0672C25.2552 44.4732 26.4102 43.9882 27.4678 43.6846C28.5458 43.3751 29.4344 43.2798 30.0408 43.377L30.3574 41.4022ZM18.3402 49.4568C21.2702 50.939 24.0958 50.214 26.6786 49.2534L25.9814 47.3788C23.4828 48.3082 21.3728 48.7496 19.243 47.6722L18.3402 49.4568ZM25.6127 47.6194C24.9334 48.3187 23.4333 48.9382 21.5294 49.2141C19.6667 49.484 17.6562 49.3889 16.1239 48.8838L15.4977 50.7832C17.3971 51.4094 19.7336 51.4952 21.8162 51.1934C23.8578 50.8976 25.9014 50.1927 27.0473 49.0128L25.6127 47.6194ZM16.7085 50.274C18.0554 47.5292 19.8486 45.5942 22.044 44.3081C24.2482 43.0169 26.918 42.345 30.0635 42.2279L29.9891 40.2293C26.606 40.3552 23.5923 41.0832 21.0331 42.5824C18.4649 44.0869 16.4147 46.3328 14.9131 49.393L16.7085 50.274ZM30.7373 40.5255C30.2757 40.0586 29.8942 39.6578 29.5106 39.2882L28.1228 40.7282C28.4844 41.0768 28.8157 41.4266 29.3153 41.9317L30.7373 40.5255ZM29.5108 39.2883C26.7637 36.6396 23.7967 34.1615 20.2815 32.3479L19.3645 34.1253C22.6317 35.8109 25.4373 38.1388 28.1226 40.7281L29.5108 39.2883ZM20.2816 32.348C17.4111 30.8666 14.2829 29.877 10.8001 30.3981L11.0961 32.3761C14.0015 31.9414 16.6891 32.7446 19.3644 34.1252L20.2816 32.348ZM10.7998 30.3982C10.388 30.4599 9.94468 30.566 9.46564 30.6638L9.86556 32.6234C10.4449 32.5052 10.7522 32.4277 11.0964 32.376L10.7998 30.3982ZM8.68401 31.8346C9.73479 37.2345 13.3673 40.5279 17.5941 43.1923L18.6607 41.5005C14.6039 38.9431 11.5404 36.0427 10.6472 31.4526L8.68401 31.8346ZM18.2475 41.3536C17.3819 41.2489 16.2594 40.7693 14.9918 39.9335C13.742 39.1093 12.427 37.9891 11.1875 36.7057C8.68437 34.1137 6.62095 30.9947 5.96732 28.5011L4.03268 29.0082C4.81005 31.9739 7.13413 35.3875 9.74885 38.095C11.0683 39.4613 12.4947 40.6825 13.8908 41.6031C15.2691 42.512 16.6967 43.1806 18.0073 43.3392L18.2475 41.3536ZM5.15997 29.7417C13.3246 28.4186 20.4108 29.874 26.173 35.9217L27.621 34.5421C21.2872 27.8944 13.4694 26.369 4.84003 27.7675L5.15997 29.7417ZM27.8093 34.8223C25.3851 29.4232 24.8341 24.3801 25.7561 19.5859C26.6809 14.7778 29.1009 10.1516 32.7265 5.62517L31.1655 4.37483C27.4012 9.07433 24.7952 13.9927 23.7921 19.2082C22.7863 24.4378 23.4058 29.8977 25.9847 35.6415L27.8093 34.8223ZM31.1655 5.62512C34.7619 10.1157 37.1747 14.7077 38.1168 19.4869C39.0562 24.2523 38.5469 29.2701 36.1948 34.6504L38.0274 35.4515C40.5299 29.7268 41.1033 24.2956 40.0791 19.1001C39.0576 13.9183 36.4606 9.03731 32.7265 4.37488L31.1655 5.62512Z" mask="url(#path-1-outside-1_16_6)" /> </svg>
<div class="overflow-ellipsis">${floGlobals.myBtcID}</div>
</button>
</header>
<div id="sub_page_container"></div>
`)
if (userAddressTimeInterval) clearInterval(userAddressTimeInterval);
userAddressTimeInterval = setInterval(() => {
if (!isShowingFloID) {
renderElem(getRef('user_popup_button'), html`
<svg class="icon margin-right-0-5" width="64" height="64" viewBox="0 0 64 64" fill="none" xmlns="http://www.w3.org/2000/svg"> <mask id="path-1-outside-1_16_6" maskUnits="userSpaceOnUse" x="3" y="3" width="58" height="58"> <rect fill="white" x="3" y="3" width="58" height="58" /> <path fill-rule="evenodd" clip-rule="evenodd" d="M31.946 43.6019C28.8545 47.1308 28.7789 51.9719 31.9811 55.1957C35.0078 52.2797 35.0105 46.8446 31.946 43.6019ZM31.9487 10.6835C24.6452 19.1291 24.9206 29.1137 31.9433 37.4108C39.0929 28.9436 39.1118 19.0076 31.9487 10.6808V10.6835ZM37.1111 35.051C43.1861 28.841 50.5976 27.4208 59 28.7654C56.2919 34.9754 52.4714 39.9353 45.7214 42.53C47.2118 41.3609 48.5699 40.4051 49.7984 39.3089C51.8504 37.481 53.303 35.267 54.0293 32.6075C54.2588 31.7678 53.9618 31.5302 53.1896 31.406C50.3627 30.9524 47.7086 31.5464 45.1733 32.702C40.9073 34.646 37.4324 37.6403 34.1735 40.8938C34.1168 40.9532 34.0925 41.0396 33.9899 41.2259C40.3754 41.4689 45.2381 44.0177 48.119 49.9064C44.3768 50.738 40.9532 50.5625 37.778 48.5213C40.1702 49.2557 42.557 49.7903 45.176 48.5483C44.852 48.0083 44.663 47.3765 44.231 47.0228C43.0511 46.0589 41.8415 45.0842 40.505 44.3498C38.4395 43.2212 36.131 42.692 33.5768 42.1682C37.0247 48.2081 36.1607 53.6918 31.946 59C29.9858 56.5295 28.6142 53.9456 28.2389 50.8946C27.8609 47.8328 28.7222 45.0518 30.1991 42.3896C26.9321 41.8658 19.8824 45.6485 18.7916 48.5645C21.3215 49.8443 23.7893 49.2611 26.33 48.3161C24.5048 50.1953 19.2425 50.9648 15.8108 49.8335C18.6593 44.0285 23.4977 41.4716 30.0263 41.2286C29.5457 40.7426 29.1893 40.3673 28.8167 40.0082C26.1005 37.3892 23.2142 34.9862 19.823 33.2366C17.0501 31.8056 14.1422 30.9092 10.9481 31.3871C10.5701 31.4438 10.1948 31.5356 9.6656 31.6436C10.6376 36.6386 13.9856 39.7355 18.1274 42.3464C13.775 41.8199 6.431 34.214 5 28.7546C13.397 27.3938 20.849 28.8842 26.897 35.2319C21.8939 24.089 24.5561 14.2259 31.946 5C39.2765 14.153 41.9657 23.9459 37.1111 35.051" /> </mask> <path fill-rule="evenodd" clip-rule="evenodd" d="M31.946 43.6019C28.8545 47.1308 28.7789 51.9719 31.9811 55.1957C35.0078 52.2797 35.0105 46.8446 31.946 43.6019ZM31.9487 10.6835C24.6452 19.1291 24.9206 29.1137 31.9433 37.4108C39.0929 28.9436 39.1118 19.0076 31.9487 10.6808V10.6835ZM37.1111 35.051C43.1861 28.841 50.5976 27.4208 59 28.7654C56.2919 34.9754 52.4714 39.9353 45.7214 42.53C47.2118 41.3609 48.5699 40.4051 49.7984 39.3089C51.8504 37.481 53.303 35.267 54.0293 32.6075C54.2588 31.7678 53.9618 31.5302 53.1896 31.406C50.3627 30.9524 47.7086 31.5464 45.1733 32.702C40.9073 34.646 37.4324 37.6403 34.1735 40.8938C34.1168 40.9532 34.0925 41.0396 33.9899 41.2259C40.3754 41.4689 45.2381 44.0177 48.119 49.9064C44.3768 50.738 40.9532 50.5625 37.778 48.5213C40.1702 49.2557 42.557 49.7903 45.176 48.5483C44.852 48.0083 44.663 47.3765 44.231 47.0228C43.0511 46.0589 41.8415 45.0842 40.505 44.3498C38.4395 43.2212 36.131 42.692 33.5768 42.1682C37.0247 48.2081 36.1607 53.6918 31.946 59C29.9858 56.5295 28.6142 53.9456 28.2389 50.8946C27.8609 47.8328 28.7222 45.0518 30.1991 42.3896C26.9321 41.8658 19.8824 45.6485 18.7916 48.5645C21.3215 49.8443 23.7893 49.2611 26.33 48.3161C24.5048 50.1953 19.2425 50.9648 15.8108 49.8335C18.6593 44.0285 23.4977 41.4716 30.0263 41.2286C29.5457 40.7426 29.1893 40.3673 28.8167 40.0082C26.1005 37.3892 23.2142 34.9862 19.823 33.2366C17.0501 31.8056 14.1422 30.9092 10.9481 31.3871C10.5701 31.4438 10.1948 31.5356 9.6656 31.6436C10.6376 36.6386 13.9856 39.7355 18.1274 42.3464C13.775 41.8199 6.431 34.214 5 28.7546C13.397 27.3938 20.849 28.8842 26.897 35.2319C21.8939 24.089 24.5561 14.2259 31.946 5C39.2765 14.153 41.9657 23.9459 37.1111 35.051" /> <path d="M31.946 43.6019L32.6728 42.915L31.918 42.1163L31.1938 42.943L31.946 43.6019ZM31.9811 55.1957L31.2716 55.9004L31.9657 56.5992L32.6749 55.9159L31.9811 55.1957ZM31.9487 10.6835L32.7051 11.3376L32.9487 11.0559V10.6835H31.9487ZM31.9433 37.4108L31.18 38.0569L31.9442 38.9597L32.7074 38.056L31.9433 37.4108ZM31.9487 10.6808L32.7068 10.0287L30.9487 7.98495V10.6808H31.9487ZM59 28.7654L59.9166 29.1651L60.4326 27.9819L59.158 27.778L59 28.7654ZM45.7214 42.53L45.1042 41.7432L46.0802 43.4634L45.7214 42.53ZM49.7984 39.3089L49.1332 38.5622L49.1326 38.5628L49.7984 39.3089ZM54.0293 32.6075L53.0647 32.3439L53.0646 32.3441L54.0293 32.6075ZM53.1896 31.406L53.3484 30.4187L53.348 30.4186L53.1896 31.406ZM45.1733 32.702L45.588 33.612L45.5881 33.6119L45.1733 32.702ZM34.1735 40.8938L33.467 40.1861L33.4585 40.1946L33.4501 40.2033L34.1735 40.8938ZM33.9899 41.2259L33.114 40.7435L32.3319 42.1635L33.9519 42.2252L33.9899 41.2259ZM48.119 49.9064L48.3359 50.8826L49.5751 50.6072L49.0173 49.4669L48.119 49.9064ZM37.778 48.5213L38.0715 47.5653L37.2372 49.3625L37.778 48.5213ZM45.176 48.5483L45.6045 49.4518L46.6008 48.9794L46.0335 48.0338L45.176 48.5483ZM44.231 47.0228L44.8645 46.2491L44.8637 46.2484L44.231 47.0228ZM40.505 44.3498L40.9866 43.4734L40.9845 43.4723L40.505 44.3498ZM33.5768 42.1682L33.7777 41.1886L31.6127 40.7446L32.7083 42.664L33.5768 42.1682ZM31.946 59L31.1626 59.6216L31.9457 60.6085L32.7292 59.6218L31.946 59ZM28.2389 50.8946L29.2314 50.7725L29.2314 50.7721L28.2389 50.8946ZM30.1991 42.3896L31.0736 42.8747L31.7652 41.6279L30.3574 41.4022L30.1991 42.3896ZM18.7916 48.5645L17.855 48.2141L17.5413 49.0527L18.3402 49.4568L18.7916 48.5645ZM26.33 48.3161L27.0473 49.0128L25.9814 47.3788L26.33 48.3161ZM15.8108 49.8335L14.9131 49.393L14.4073 50.4237L15.4977 50.7832L15.8108 49.8335ZM30.0263 41.2286L30.0635 42.2279L32.3372 42.1433L30.7373 40.5255L30.0263 41.2286ZM28.8167 40.0082L28.1226 40.7281L28.1228 40.7282L28.8167 40.0082ZM19.823 33.2366L19.3644 34.1252L19.3645 34.1253L19.823 33.2366ZM10.9481 31.3871L10.8001 30.3981L10.7998 30.3982L10.9481 31.3871ZM9.6656 31.6436L9.46564 30.6638L8.49474 30.8619L8.68401 31.8346L9.6656 31.6436ZM18.1274 42.3464L18.0073 43.3392L18.6607 41.5005L18.1274 42.3464ZM5 28.7546L4.84003 27.7675L3.75363 27.9435L4.03268 29.0082L5 28.7546ZM26.897 35.2319L26.173 35.9217L27.8093 34.8223L26.897 35.2319ZM31.946 5L32.7265 4.37488L31.9461 3.40036L31.1655 4.37483L31.946 5ZM31.1938 42.943C27.7974 46.8199 27.6566 52.261 31.2716 55.9004L32.6906 54.491C29.9012 51.6828 29.9116 47.4417 32.6982 44.2609L31.1938 42.943ZM32.6749 55.9159C36.1304 52.5867 36.081 46.5214 32.6728 42.915L31.2192 44.2888C33.94 47.1678 33.8852 51.9727 31.2873 54.4755L32.6749 55.9159ZM31.1923 10.0294C27.4048 14.4092 25.5379 19.2455 25.5737 24.1102C25.6095 28.971 27.5438 33.7608 31.18 38.0569L32.7066 36.7647C29.3201 32.7637 27.6054 28.4127 27.5736 24.0955C27.5419 19.7822 29.1891 15.4034 32.7051 11.3376L31.1923 10.0294ZM32.7074 38.056C36.4083 33.6729 38.31 28.8504 38.3133 23.9938C38.3165 19.1359 36.42 14.3451 32.7068 10.0287L31.1906 11.3329C34.6405 15.3433 36.3161 19.6839 36.3133 23.9925C36.3104 28.3024 34.6279 32.6815 31.1792 36.7656L32.7074 38.056ZM30.9487 10.6808V10.6835H32.9487V10.6808H30.9487ZM37.8259 35.7503C43.6131 29.8345 50.6632 28.444 58.842 29.7528L59.158 27.778C50.532 26.3976 42.7591 27.8475 36.3963 34.3517L37.8259 35.7503ZM58.0834 28.3657C55.4402 34.4268 51.7791 39.1301 45.3626 41.5966L46.0802 43.4634C53.1637 40.7405 57.1436 35.524 59.9166 29.1651L58.0834 28.3657ZM46.3386 43.3168C47.7733 42.1914 49.2057 41.178 50.4642 40.055L49.1326 38.5628C47.9341 39.6322 46.6503 40.5304 45.1042 41.7432L46.3386 43.3168ZM50.4636 40.0556C52.6466 38.111 54.2121 35.7338 54.994 32.871L53.0646 32.3441C52.3939 34.8002 51.0542 36.851 49.1332 38.5622L50.4636 40.0556ZM54.9939 32.8711C55.1262 32.3872 55.2248 31.6946 54.7698 31.1186C54.3643 30.6053 53.7257 30.4794 53.3484 30.4187L53.0308 32.3933C53.1086 32.4058 53.17 32.4181 53.2184 32.43C53.2671 32.442 53.2955 32.4518 53.3091 32.4572C53.3227 32.4626 53.3139 32.4606 53.2926 32.4461C53.2699 32.4307 53.2353 32.4025 53.2004 32.3583C53.1832 32.3366 53.1677 32.3132 53.1543 32.2886C53.1409 32.264 53.1307 32.2402 53.1231 32.2182C53.1079 32.1742 53.1053 32.144 53.1049 32.1366C53.1046 32.1302 53.1059 32.1427 53.1007 32.1797C53.0955 32.2165 53.0849 32.27 53.0647 32.3439L54.9939 32.8711ZM53.348 30.4186C50.274 29.9254 47.4176 30.5801 44.7585 31.7921L45.5881 33.6119C47.9996 32.5127 50.4514 31.9794 53.0312 32.3934L53.348 30.4186ZM44.7586 31.792C40.3293 33.8104 36.7535 36.9051 33.467 40.1861L34.88 41.6015C38.1113 38.3755 41.4853 35.4816 45.588 33.612L44.7586 31.792ZM33.4501 40.2033C33.3063 40.354 33.2291 40.5156 33.2033 40.5674C33.1672 40.64 33.1517 40.6749 33.114 40.7435L34.8658 41.7083C34.9307 41.5906 34.9786 41.4891 34.9945 41.4572C35.0049 41.4363 35.0004 41.4467 34.9882 41.4667C34.9731 41.4914 34.9435 41.5355 34.8969 41.5843L33.4501 40.2033ZM33.9519 42.2252C37.0233 42.3421 39.6629 43.0109 41.8597 44.3098C44.0485 45.6039 45.857 47.5582 47.2207 50.3459L49.0173 49.4669C47.5001 46.3659 45.4368 44.1014 42.8776 42.5882C40.3265 41.0798 37.342 40.3527 34.0279 40.2266L33.9519 42.2252ZM47.9021 48.9302C44.3241 49.7253 41.1983 49.5313 38.3188 47.6801L37.2372 49.3625C40.7081 51.5937 44.4295 51.7507 48.3359 50.8826L47.9021 48.9302ZM37.4845 49.4773C39.9089 50.2215 42.6205 50.8669 45.6045 49.4518L44.7475 47.6448C42.4935 48.7137 40.4315 48.2899 38.0715 47.5653L37.4845 49.4773ZM46.0335 48.0338C45.8849 47.7862 45.8212 47.6158 45.6292 47.2504C45.4736 46.9544 45.2423 46.5584 44.8645 46.2491L43.5975 47.7965C43.6517 47.8409 43.7309 47.9377 43.8588 48.181C43.9503 48.3549 44.1431 48.7704 44.3185 49.0628L46.0335 48.0338ZM44.8637 46.2484C43.6913 45.2906 42.4149 44.2582 40.9866 43.4734L40.0234 45.2262C41.2681 45.9102 42.4109 46.8272 43.5983 47.7972L44.8637 46.2484ZM40.9845 43.4723C38.7711 42.2628 36.3236 41.7107 33.7777 41.1886L33.3759 43.1478C35.9384 43.6733 38.1079 44.1796 40.0255 45.2273L40.9845 43.4723ZM32.7083 42.664C34.346 45.5328 34.9255 48.203 34.6411 50.7474C34.3555 53.3015 33.1882 55.8274 31.1628 58.3782L32.7292 59.6218C34.9185 56.8644 36.2905 53.9944 36.6287 50.9696C36.9679 47.9351 36.2555 44.8435 34.4453 41.6724L32.7083 42.664ZM32.7294 58.3784C30.8397 55.9968 29.577 53.5818 29.2314 50.7725L27.2464 51.0167C27.6514 54.3094 29.1319 57.0622 31.1626 59.6216L32.7294 58.3784ZM29.2314 50.7721C28.8877 47.9881 29.6602 45.4224 31.0736 42.8747L29.3247 41.9045C27.7842 44.6812 26.8341 47.6775 27.2464 51.0171L29.2314 50.7721ZM30.3574 41.4022C29.3303 41.2375 28.1125 41.4187 26.9159 41.7622C25.6987 42.1117 24.4105 42.6564 23.2045 43.307C21.999 43.9574 20.8488 44.728 19.9166 45.543C19.005 46.3399 18.2137 47.2553 17.855 48.2141L19.7282 48.9149C19.9149 48.4157 20.4139 47.7647 21.233 47.0487C22.0314 46.3507 23.0524 45.6616 24.1541 45.0672C25.2552 44.4732 26.4102 43.9882 27.4678 43.6846C28.5458 43.3751 29.4344 43.2798 30.0408 43.377L30.3574 41.4022ZM18.3402 49.4568C21.2702 50.939 24.0958 50.214 26.6786 49.2534L25.9814 47.3788C23.4828 48.3082 21.3728 48.7496 19.243 47.6722L18.3402 49.4568ZM25.6127 47.6194C24.9334 48.3187 23.4333 48.9382 21.5294 49.2141C19.6667 49.484 17.6562 49.3889 16.1239 48.8838L15.4977 50.7832C17.3971 51.4094 19.7336 51.4952 21.8162 51.1934C23.8578 50.8976 25.9014 50.1927 27.0473 49.0128L25.6127 47.6194ZM16.7085 50.274C18.0554 47.5292 19.8486 45.5942 22.044 44.3081C24.2482 43.0169 26.918 42.345 30.0635 42.2279L29.9891 40.2293C26.606 40.3552 23.5923 41.0832 21.0331 42.5824C18.4649 44.0869 16.4147 46.3328 14.9131 49.393L16.7085 50.274ZM30.7373 40.5255C30.2757 40.0586 29.8942 39.6578 29.5106 39.2882L28.1228 40.7282C28.4844 41.0768 28.8157 41.4266 29.3153 41.9317L30.7373 40.5255ZM29.5108 39.2883C26.7637 36.6396 23.7967 34.1615 20.2815 32.3479L19.3645 34.1253C22.6317 35.8109 25.4373 38.1388 28.1226 40.7281L29.5108 39.2883ZM20.2816 32.348C17.4111 30.8666 14.2829 29.877 10.8001 30.3981L11.0961 32.3761C14.0015 31.9414 16.6891 32.7446 19.3644 34.1252L20.2816 32.348ZM10.7998 30.3982C10.388 30.4599 9.94468 30.566 9.46564 30.6638L9.86556 32.6234C10.4449 32.5052 10.7522 32.4277 11.0964 32.376L10.7998 30.3982ZM8.68401 31.8346C9.73479 37.2345 13.3673 40.5279 17.5941 43.1923L18.6607 41.5005C14.6039 38.9431 11.5404 36.0427 10.6472 31.4526L8.68401 31.8346ZM18.2475 41.3536C17.3819 41.2489 16.2594 40.7693 14.9918 39.9335C13.742 39.1093 12.427 37.9891 11.1875 36.7057C8.68437 34.1137 6.62095 30.9947 5.96732 28.5011L4.03268 29.0082C4.81005 31.9739 7.13413 35.3875 9.74885 38.095C11.0683 39.4613 12.4947 40.6825 13.8908 41.6031C15.2691 42.512 16.6967 43.1806 18.0073 43.3392L18.2475 41.3536ZM5.15997 29.7417C13.3246 28.4186 20.4108 29.874 26.173 35.9217L27.621 34.5421C21.2872 27.8944 13.4694 26.369 4.84003 27.7675L5.15997 29.7417ZM27.8093 34.8223C25.3851 29.4232 24.8341 24.3801 25.7561 19.5859C26.6809 14.7778 29.1009 10.1516 32.7265 5.62517L31.1655 4.37483C27.4012 9.07433 24.7952 13.9927 23.7921 19.2082C22.7863 24.4378 23.4058 29.8977 25.9847 35.6415L27.8093 34.8223ZM31.1655 5.62512C34.7619 10.1157 37.1747 14.7077 38.1168 19.4869C39.0562 24.2523 38.5469 29.2701 36.1948 34.6504L38.0274 35.4515C40.5299 29.7268 41.1033 24.2956 40.0791 19.1001C39.0576 13.9183 36.4606 9.03731 32.7265 4.37488L31.1655 5.62512Z" mask="url(#path-1-outside-1_16_6)" /> </svg>
<div class="overflow-ellipsis">${floGlobals.myFloID}</div>
`)
isShowingFloID = true
} else {
renderElem(getRef('user_popup_button'), html`
<svg class="icon margin-right-0-5" xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"><g><rect fill="none" height="24" width="24"/></g><g><path d="M17.06,11.57C17.65,10.88,18,9.98,18,9c0-1.86-1.27-3.43-3-3.87L15,3h-2v2h-2V3H9v2H6v2h2v10H6v2h3v2h2v-2h2v2h2v-2 c2.21,0,4-1.79,4-4C19,13.55,18.22,12.27,17.06,11.57z M10,7h4c1.1,0,2,0.9,2,2s-0.9,2-2,2h-4V7z M15,17h-5v-4h5c1.1,0,2,0.9,2,2 S16.1,17,15,17z"/></g></svg>
<div class="overflow-ellipsis">${floGlobals.myBtcID}</div>
`)
isShowingFloID = false
}
}, 1000 * 60);
}
},
routingEnd() {
document.querySelectorAll(".my-flo-address").forEach(elem => {
if (elem.tagName === 'SM-COPY') {
elem.value = floGlobals.myFloID
} else
elem.textContent = floGlobals.myFloID
})
document.querySelectorAll(".my-btc-address").forEach(elem => {
if (elem.tagName === 'SM-COPY') {
elem.value = floGlobals.myBtcID
} else
elem.textContent = floGlobals.myBtcID
})
document.querySelectorAll('sm-input[data-address]').forEach(input => input.customValidation = (value) => {
return {
isValid: floCrypto.validateAddr(value),
errorText: `Invalid address.<br> It usually starts with "F", "1", "3" or "bc1".`
}
})
}
})
router.addRoute('loading', (state) => {
renderElem(getRef('app_body'), html`
<article id="loading">
<sm-spinner></sm-spinner>
<div class="grid gap-1 justify-items-center">
<h4>Loading RanchiMall BTC Mortgage</h4>
<button class="button button--colored" onclick=${clearCredentials}>Reset</button>
</div>
</article>
`);
})
router.addRoute('landing', (state) => {
renderElem(getRef('app_body'), html`
<article id="landing">
<header id="main_header">
<div class="app-brand margin-right-auto">
<svg id="main_logo" class="icon" viewBox="0 0 27.25 32"> <title>RanchiMall</title> <path d="M27.14,30.86c-.74-2.48-3-4.36-8.25-6.94a20,20,0,0,1-4.2-2.49,6,6,0,0,1-1.25-1.67,4,4,0,0,1,0-2.26c.37-1.08.79-1.57,3.89-4.55a11.66,11.66,0,0,0,3.34-4.67,6.54,6.54,0,0,0,.05-2.82C20,3.6,18.58,2,16.16.49c-.89-.56-1.29-.64-1.3-.24a3,3,0,0,1-.3.72l-.3.55L13.42.94C13,.62,12.4.26,12.19.15c-.4-.2-.73-.18-.72.05a9.39,9.39,0,0,1-.61,1.33s-.14,0-.27-.13C8.76.09,8-.27,8,.23A11.73,11.73,0,0,1,6.76,2.6C4.81,5.87,2.83,7.49.77,7.49c-.89,0-.88,0-.61,1,.22.85.33.92,1.09.69A5.29,5.29,0,0,0,3,8.33c.23-.17.45-.29.49-.26a2,2,0,0,1,.22.63A1.31,1.31,0,0,0,4,9.34a5.62,5.62,0,0,0,2.27-.87L7,8l.13.55c.19.74.32.82,1,.65a7.06,7.06,0,0,0,3.46-2.47l.6-.71-.06.64c-.17,1.63-1.3,3.42-3.39,5.42L6.73,14c-3.21,3.06-3,5.59.6,8a46.77,46.77,0,0,0,4.6,2.41c.28.13,1,.52,1.59.87,3.31,2,4.95,3.92,4.95,5.93a2.49,2.49,0,0,0,.07.77h0c.09.09,0,.1.9-.14a2.61,2.61,0,0,0,.83-.32,3.69,3.69,0,0,0-.55-1.83A11.14,11.14,0,0,0,17,26.81a35.7,35.7,0,0,0-5.1-2.91C9.37,22.64,8.38,22,7.52,21.17a3.53,3.53,0,0,1-1.18-2.48c0-1.38.71-2.58,2.5-4.23,2.84-2.6,3.92-3.91,4.67-5.65a3.64,3.64,0,0,0,.42-2A3.37,3.37,0,0,0,13.61,5l-.32-.74.29-.48c.17-.27.37-.63.46-.8l.15-.3.44.64a5.92,5.92,0,0,1,1,2.81,5.86,5.86,0,0,1-.42,1.94c0,.12-.12.3-.15.4a9.49,9.49,0,0,1-.67,1.1,28,28,0,0,1-4,4.29C8.62,15.49,8.05,16.44,8,17.78a3.28,3.28,0,0,0,1.11,2.76c.95,1,2.07,1.74,5.25,3.32,3.64,1.82,5.22,2.9,6.41,4.38A4.78,4.78,0,0,1,21.94,31a3.21,3.21,0,0,0,.14.92,1.06,1.06,0,0,0,.43-.05l.83-.22.46-.12-.06-.46c-.21-1.53-1.62-3.25-3.94-4.8a37.57,37.57,0,0,0-5.22-2.82A13.36,13.36,0,0,1,11,21.19a3.36,3.36,0,0,1-.8-4.19c.41-.85.83-1.31,3.77-4.15,2.39-2.31,3.43-4.13,3.43-6a5.85,5.85,0,0,0-2.08-4.29c-.23-.21-.44-.43-.65-.65A2.5,2.5,0,0,1,15.27.69a10.6,10.6,0,0,1,2.91,2.78A4.16,4.16,0,0,1,19,6.16a4.91,4.91,0,0,1-.87,3c-.71,1.22-1.26,1.82-4.27,4.67a9.47,9.47,0,0,0-2.07,2.6,2.76,2.76,0,0,0-.33,1.54,2.76,2.76,0,0,0,.29,1.47c.57,1.21,2.23,2.55,4.65,3.73a32.41,32.41,0,0,1,5.82,3.24c2.16,1.6,3.2,3.16,3.2,4.8a1.94,1.94,0,0,0,.09.76,4.54,4.54,0,0,0,1.66-.4C27.29,31.42,27.29,31.37,27.14,30.86ZM6.1,7h0a3.77,3.77,0,0,1-1.46.45L4,7.51l.68-.83a25.09,25.09,0,0,0,3-4.82A12,12,0,0,1,8.28.76c.11-.12.77.32,1.53,1l.63.58-.57.84A10.34,10.34,0,0,1,6.1,7Zm5.71-1.78A9.77,9.77,0,0,1,9.24,7.18h0a5.25,5.25,0,0,1-1.17.28l-.58,0,.65-.78a21.29,21.29,0,0,0,2.1-3.12c.22-.41.42-.76.44-.79s.5.43.9,1.24L12,5ZM13.41,3a2.84,2.84,0,0,1-.45.64,11,11,0,0,1-.9-.91l-.84-.9.19-.45c.34-.79.39-.8,1-.31A9.4,9.4,0,0,1,13.8,2.33q-.18.34-.39.69Z" /> </svg>
<div class="app-name">
<div class="app-name__company">RanchiMall</div>
<h4 class="app-name__title">
BTC Mortgage
</h4>
</div>
</div>
<theme-toggle></theme-toggle>
</header>
<section id="hero_section" class="landing__section">
<div class="landing__section__info-wrapper">
<h1>Borrow. Lend.<br> Grow your assets.</h1>
<p>
Use your BTC to borrow USD tokens or lend USD tokens to earn interest.
</p>
<div class="flex gap-0-5">
<a href="#/sign_in" class="button button--primary">Sign in</a>
<a href="#/sign_up" class="button button--colored">Get started</a>
</div>
</div>
<div class="landing__section__image-wrapper">
<img src="assets/hero-illustration.svg" alt="Landing image">
</div>
</section>
<section class="landing__section">
<div class="landing__section__info-wrapper">
<div class="grid gap-0-3">
<h4 class="section__sub-header">How it works?</h4>
<h2>LOAN PROCESS</h2>
</div>
<ol type="1">
<li>
<h4>Choose a loan policy that suits your needs</h4>
<p>
You can choose from a variety of loan policies with different interest rates, durations and collateral ratios.
</p>
</li>
<li>
<h4>Enter amount of loan you need & select collateral provider</h4>
<p>
You can either provide collateral on your own or request from someone else you know. <br>
The required collateral will be displayed on the screen.
</p>
</li>
<li>
<h4>Wait for lender to accept your loan request</h4>
<p>
Your loan request is displayed in marketplace in search of potential lenders. <br>
Once a lender accepts your request, you will be notified.
</p>
</li>
<li>
<h4>Lock collateral</h4>
<p>
Your collateral will locked in a 2 of 3 Bitcoin multisig until loan is re-paid
</p>
</li>
<li>
<h4>Loan issued</h4>
<p>
Loan will be issued within 24hrs after locking collateral. <br>
*If your loan is not issued even after 24hrs, you can request collateral refund.
</p>
</li>
</ol>
</div>
<div class="landing__section__image-wrapper">
<img src="assets/loan-process.svg" alt="loan process">
</div>
</section>
</article>
`);
})
function handleSignIn() {
privKeyResolver(getRef('private_key_field').value.trim());
router.routeTo('loading');
}
router.addRoute('sign_in', (state) => {
renderElem(getRef('app_body'), html`
<article id="sign_in">
<section class="grid gap-1-5">
<button onclick="history.back()" class="button button--colored icon-only margin-right-auto">
<svg class="icon" xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M20 11H7.83l5.59-5.59L12 4l-8 8 8 8 1.41-1.41L7.83 13H20v-2z"/></svg>
</button>
<div class="grid gap-0-5">
<h1 style="font-size: 2rem;">Sign in</h1>
<p>Welcome back, glad to see you again</p>
</div>
<sm-form id="sign_in_form">
<sm-input id="private_key_field" class="password-field" type="password"
placeholder=${`${floGlobals.isPrivKeySecured ? 'Password' : 'BTC / FLO private key'}`} error-text="Private key is invalid" required>
<label slot="right" class="interact">
<input type="checkbox" class="hidden" autocomplete="off" readonly
onchange="togglePrivateKeyVisibility(this)">
<svg class="icon invisible" xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"> <title>Hide password</title> <path d="M0 0h24v24H0zm0 0h24v24H0zm0 0h24v24H0zm0 0h24v24H0z" fill="none" /> <path d="M12 7c2.76 0 5 2.24 5 5 0 .65-.13 1.26-.36 1.83l2.92 2.92c1.51-1.26 2.7-2.89 3.43-4.75-1.73-4.39-6-7.5-11-7.5-1.4 0-2.74.25-3.98.7l2.16 2.16C10.74 7.13 11.35 7 12 7zM2 4.27l2.28 2.28.46.46C3.08 8.3 1.78 10.02 1 12c1.73 4.39 6 7.5 11 7.5 1.55 0 3.03-.3 4.38-.84l.42.42L19.73 22 21 20.73 3.27 3 2 4.27zM7.53 9.8l1.55 1.55c-.05.21-.08.43-.08.65 0 1.66 1.34 3 3 3 .22 0 .44-.03.65-.08l1.55 1.55c-.67.33-1.41.53-2.2.53-2.76 0-5-2.24-5-5 0-.79.2-1.53.53-2.2zm4.31-.78l3.15 3.15.02-.16c0-1.66-1.34-3-3-3l-.17.01z" /> </svg>
<svg class="icon visible" xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"> <title>Show password</title> <path d="M0 0h24v24H0z" fill="none" /> <path d="M12 4.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5zM12 17c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5zm0-8c-1.66 0-3 1.34-3 3s1.34 3 3 3 3-1.34 3-3-1.34-3-3-3z" /> </svg>
</label>
</sm-input>
<button id="sign_in_button" onclick="handleSignIn()" class="button button--primary" type="submit" disabled>Sign in</button>
</sm-form>
<p>
New here? <a href="#/sign_up">get your login credentials</a>
</p>
</section>
</article>
`);
if (!floGlobals.isPrivKeySecured)
getRef('private_key_field').customValidation = (value) => {
return {
isValid: floCrypto.getPubKeyHex(value),
errorText: `Invalid private key.<br> It's a long string of random characters usually starting with 'L' or 'R'.`
}
};
getRef('private_key_field').focusIn();
})
function handleSignUp() {
const privKey = getRef('generated_private_key').value.trim();
privKeyResolver(privKey);
router.routeTo('loading');
}
router.addRoute('sign_up', (state) => {
function renderCred(type = 'flo') {
let address, privateKey
if (type === 'flo') {
({ floID: address, privKey: privateKey } = floCrypto.generateNewID())
} else if (type === 'btc') {
({ bech32Address: address, wif: privateKey } = btcOperator.newKeys)
} else {
throw new Error('Invalid type')
}
renderElem(getRef('generated_keys_wrapper'), html`
<div class="grid gap-0-5">
<h5>${type.toUpperCase()} address</h5>
<sm-copy id="generated_flo_address" value=${address}></sm-copy>
</div>
<div class="grid gap-0-5">
<h5>Private key</h5>
<sm-copy id="generated_private_key" value=${privateKey}></sm-copy>
</div>
`)
}
renderElem(getRef('app_body'), html`
<article id="sign_up">
<section class="grid gap-1-5">
<div class="flex align-center space-between gap-1">
<button onclick="history.back()" class="button button--colored icon-only margin-right-auto">
<svg class="icon" xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M20 11H7.83l5.59-5.59L12 4l-8 8 8 8 1.41-1.41L7.83 13H20v-2z"/></svg>
</button>
<sm-chips onchange=${(e) => renderCred(e.target.value)}>
<sm-chip value="btc" selected="true">BTC</sm-chip>
<sm-chip value="flo">FLO</sm-chip>
</sm-chips>
</div>
<div id="generated_keys_warning" class="flex gap-1">
<svg class="icon" xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"> <path d="M0 0h24v24H0z" fill="none" /> <path d="M1 21h22L12 2 1 21zm12-3h-2v-2h2v2zm0-4h-2v-4h2v4z" /> </svg>
<div class="grid gap-0-5">
<strong>
<h3> Keep your keys safe! </h3>
</strong>
<p>Don't share with anyone. Once lost private key can't be recovered.</p>
</div>
</div>
<div id="generated_keys_wrapper" class="grid gap-1-5"></div>
<button id="sign_up_button" onclick="handleSignUp()" class="button button--primary w-100">Sign in with these credentials</button>
<p class="margin-top-1">You can use these credentials with other RanchiMall apps too. </p>
</section>
</article>
`);
renderCred('btc')
})
let isShowingFloID = false;
const btcAddresses = {}
const floAddresses = {}
function getBtcAddress(floAddress) {
if (btcOperator.validateAddress(floAddress)) return floAddress
if (!btcAddresses[floAddress])
btcAddresses[floAddress] = btcOperator.convert.legacy2bech(floAddress)
return btcAddresses[floAddress]
}
function getFloAddress(btcAddress) {
if (!floAddresses[btcAddress])
floAddresses[btcAddress] = floCrypto.toFloID(btcAddress)
return floAddresses[btcAddress]
}
let userAddressTimeInterval;
function renderHome(appState) {
const { lastPage, page, params: { } = {}, wildcards = [] } = appState || {};
const balanceCard = Component(() => {
const [btcBalance, setBtcBalance] = useState(floGlobals.memoBalance['BTC'])
const [floBalance, setFloBalance] = useState(floGlobals.memoBalance['FLO'])
const [usdBalance, setUsdBalance] = useState(floGlobals.memoBalance['USD'])
const [isRefreshing, setIsRefreshing] = useState(false)
function fetchBalance() {
setIsRefreshing(true)
Promise.allSettled([
btcOperator.getBalance(floGlobals.myBtcID),
floBlockchainAPI.getBalance(floGlobals.myFloID),
floTokenAPI.getBalance(floGlobals.myFloID, 'usd')
]).then(result => {
const [btcBalance, floBalance, usdBalance] = result.map(({ value }) => value)
floGlobals.memoBalance = {
'FLO': floBalance,
'BTC': btcBalance,
'USD': usdBalance
}
setBtcBalance(btcBalance)
setFloBalance(floBalance)
setUsdBalance(usdBalance)
}).catch(error => {
console.error(error)
}).finally(() => {
setIsRefreshing(false)
})
}
useEffect(() => {
if (lastPage !== page)
fetchBalance()
}, [])
return html`
<div class="grid gap-1">
<div class="flex align-center space-between gap-1">
<h4>My balances</h4>
<button class="button button--colored button--small" disabled=${isRefreshing} onclick=${fetchBalance}>
${isRefreshing ? html`
Refreshing <sm-spinner class="margin-left-0-5"></sm-spinner>
`: html`
Refresh
`}
</button>
</div>
<ul id="balance_list">
<li class="balance-card">
<div class="flex align-center space-between gap-1">
<p>BTC</p>
<a href=${`https://ranchimall.github.io/btcwallet/#/check_details?query=${floGlobals.myBtcID}`} target="_blank" class="button button--small button--colored">History</a>
</div>
<span>${formatAmount(btcBalance)}</span>
</li>
<li class="balance-card">
<div class="flex align-center space-between gap-1">
<p>USD</p>
<a href=${`https://ranchimall.github.io/flowallet/#/search?type=address&query=${floGlobals.myFloID}&page=1`} target="_blank" class="button button--small button--colored">History</a>
</div>
<span>${formatAmount(usdBalance, 'usd')}</span>
</li>
<li class="balance-card">
<div class="flex align-center space-between gap-1">
<p>FLO</p>
<a href=${`https://ranchimall.github.io/flowallet/#/search?type=address&query=${floGlobals.myFloID}&page=1`} target="_blank" class="button button--small button--colored">History</a>
</div>
<span>${formatAmount(floBalance, 'FLO')}</span>
</li>
</ul>
</div>
`
})
if (floCrypto.isSameAddr(btcMortgage.banker.id, floDapps.user.id)) {
const bankerInbox = Component(() => {
const [isProcessingBankerRequest, setIsProcessingBankerRequest] = useState(false)
async function processBankerRequest(requestId) {
const {
message: {
loan_id, closing_txid, unlock_tx_hex,
borrower, coborrower, lender
},
time,
vectorClock,
note,
type
} = floGlobals.bankerRequests[requestId];
try {
setIsProcessingBankerRequest(true)
switch (type) {
case 'type_unlock_collateral_request': {
const confirmation = await getConfirmation('Process collateral unlock request?', {
confirmText: 'Process',
})
if (!confirmation) return;
await btcMortgage.unlockCollateral(loan_id, closing_txid, unlock_tx_hex, await floDapps.user.private)
notify('Collateral unlocked', 'success')
} break;
case 'type_refund_collateral_request': {
const confirmation = await getConfirmation('Process collateral refund request?', {
confirmText: 'Process',
})
if (!confirmation) return
await btcMortgage.banker.refundCollateral(vectorClock, borrower, coborrower, lender, await floDapps.user.private)
notify('Collateral refund processed', 'success')
} break;
case 'type_liquate_collateral_request': {
const confirmation = await getConfirmation('Process collateral liquidation request?', {
confirmText: 'Process',
})
if (!confirmation) return
await btcMortgage.banker.liquidateCollateral(vectorClock, await floDapps.user.private)
notify('Collateral liquidated', 'success')
} break;
case 'type_pre_liquidate_collateral_request': {
const confirmation = await getConfirmation('Process pre-liquidation request?', {
confirmText: 'Process',
})
if (!confirmation) return
await btcMortgage.requestBanker.preLiquidateCollateral(loan_id, await floDapps.user.private)
notify('Pre-liquidation processed', 'success')
} break;
}
await floCloudAPI.noteApplicationData(vectorClock, 'processed')
renderHome()
} catch (error) {
notify(error.message || error, 'error')
} finally {
setIsProcessingBankerRequest(false)
}
}
const requests = Object.entries(floGlobals.bankerRequests || {}).map(([requestId, requestDetails]) => {
const {
message: {
borrower, coborrower, lender
},
time,
vectorClock,
type
} = requestDetails;
let requestTitle
switch (type) {
case 'type_unlock_collateral_request':
requestTitle = 'Collateral unlock request'
break;
case 'type_refund_collateral_request':
requestTitle = 'Collateral refund request'
case 'type_liquate_collateral_request':
requestTitle = 'Collateral liquidation request'
break;
case 'type_pre_liquidate_collateral_request':
requestTitle = 'Pre-liquidation request'
break;
}
return html`
<li class="grid gap-1 banker-request">
<p>${getFormattedTime(time)}</p>
<h4>${requestTitle}</h4>
<div class="grid gap-0-5">
<p>Borrower</p>
<sm-copy value=${borrower}></sm-copy>
</div>
<div class="grid gap-0-5">
<p>Co-borrower</p>
<sm-copy value=${coborrower}></sm-copy>
</div>
<div class="grid gap-0-5">
<p>Lender</p>
<sm-copy value=${lender}></sm-copy>
</div>
<button class="button button--primary" onclick=${() => processBankerRequest(vectorClock)} disabled=${isProcessingBankerRequest}>
${isProcessingBankerRequest ? html`
Processing <sm-spinner class="margin-left-0-5"></sm-spinner>
`: html`
Process
`}
</button>
</li>
`
})
const [bankerView, setBankerView] = useState(wildcards[0] || 'inbox')
function handleChange(e) {
history.pushState(null, null, `#/home/${e.target.value}`)
setBankerView(e.target.value)
}
const [duration, setDuration] = useState(0)
const [durationUnit, setDurationUnit] = useState('Y')
let durationUnitInput
switch (durationUnit) {
case 'D':
durationUnitInput = html`
<sm-select id="policy_duration" required>
${[...Array(31)].map((_, i) => html`
<sm-option value=${i + 1}>${i + 1}</sm-option>
`)}
</sm-select>
`
break;
case 'M':
case 'Y':
durationUnitInput = html` <sm-input id="policy_duration" class="flex-1" type="number" min="1" max="100" error-text="Must be between 1-100" required></sm-input> `
break;
}
const [isCreatingPolicy, setIsCreatingPolicy] = useState(false)
async function createPolicy() {
const interestRate = parseFloat(getRef('policy_interest_rate').value.trim())
const duration = parseInt(getRef('policy_duration').value.trim())
const collateralRatio = parseFloat(getRef('loan_to_collateral_ratio').value.trim())
const preLiquidateCollateralThreshold = parseFloat(getRef('policy_pre_liquidation_threshold').value.trim()) || null
const confirmation = await getConfirmation('Create policy?', {
message: `
You are about to create a new policy with the following details: \n
Interest rate: ${interestRate}% \n
Duration: ${duration} ${durationUnit} \n
Collateral ratio: ${collateralRatio}% \n
${preLiquidateCollateralThreshold ? `Pre-liquidation threshold: ${preLiquidateCollateralThreshold}% \n` : ''}
`,
confirmText: 'Create',
})
if (!confirmation) return
try {
setIsCreatingPolicy(true)
const txid = await btcMortgage.writePolicy(await floDapps.user.private, `${duration}${durationUnit}`, interestRate, preLiquidateCollateralThreshold, collateralRatio)
notify('Policy created. May take 20 Mins to be visible in policy list.', 'success')
getRef('create_policy_form').reset()
} catch (error) {
notify(error.message || error, 'error')
} finally {
setIsCreatingPolicy(false)
}
}
function policyCard(policyId, details = {}) {
const { duration, interest, loan_collateral_ratio, policy_creation_time, pre_liquidation_threshold } = details
return html`
<li class="grid gap-1-5" style="background-color: rgba(var(--foreground-color),1); padding: 1rem; border-radius: 0.5rem;">
<p>${getFormattedTime(policy_creation_time)}</p>
<div class="flex flex-wrap gap-1-5">
<div class="grid gap-0-3">
<p>Interest rate</p>
<b>${interest * 100}%</b>
</div>
<div class="grid gap-0-3">
<p>Duration</p>
<b>${duration}</b>
</div>
<div class="grid gap-0-3">
<p>Loan to collateral ratio</p>
<b>${loan_collateral_ratio * 100}%</b>
</div>
${pre_liquidation_threshold ? html`
<div class="grid gap-0-3">
<p>Pre-liquidation threshold</p>
<b>${pre_liquidation_threshold * 100}%</b>
</div>
`: ''}
</div>
<a href=${`https://blockbook.ranchimall.net/tx/${policyId}`} target="_blank" class="button button--small button--colored margin-left-auto">Check on blockchain</a>
</li>
`
}
const policies = Object.entries(btcMortgage.policies)
.sort((a, b) => b[1].policy_creation_time - a[1].policy_creation_time)
.map(([policyId, policyDetails]) => policyCard(policyId, policyDetails))
return html`
<section class=${`grid gap-1 ${bankerView}`}>
<sm-chips onchange=${handleChange}>
<sm-chip value="inbox" selected=${bankerView === 'inbox'}>
Inbox
${requests.length ? html`<span class="badge">${requests.length}</span>` : ''}
</sm-chip>
<sm-chip value="create-policy" selected=${bankerView === 'create-policy'}>
Create policy
</sm-chip>
<sm-chip value="policies" selected=${bankerView === 'policies'}>
Policies
</sm-chip>
</sm-chips>
${bankerView === 'inbox' ? html`
<h3>Banker Inbox</h3>
${requests.length ? html`
<ul id="banker_inbox_list">
${requests}
</ul>
`: html` <strong> No requests </strong> `}
`: ``}
${bankerView === 'policies' ? html`
<h3>Policies</h3>
<ul class="grid gap-1">
${policies}
</ul>
`: ''}
${bankerView === 'create-policy' ? html`
<h3>Create policy</h3>
<sm-form id="create_policy_form">
<div class="grid gap-0-5">
<p>Interest rate (%)</p>
<sm-input id="policy_interest_rate" type="number" min="1" max="100" step="0.01" error-text="Must be between 1-100" required></sm-input>
</div>
<div class="grid gap-0-5">
<p>Duration</p>
<div class="flex gap-0-5">
${durationUnitInput}
<sm-select onchange=${e => setDurationUnit(e.target.value)} required>
<sm-option value="D" selected=${durationUnit === 'D'}>Days</sm-option>
<sm-option value="M" selected=${durationUnit === 'M'}>Months</sm-option>
<sm-option value="Y" selected=${durationUnit === 'Y'}>Years</sm-option>
</sm-select>
</div>
</div>
<div class="grid gap-0-5">
<div class="flex align-center tooltip gap-0-3">
<p>Loan to collateral ratio (%)</p>
<svg class="icon" xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M11 7h2v2h-2zm0 4h2v6h-2zm1-9C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8z"/></svg>
<p class="tooltip__content">
The percentage of collateral value that can be borrowed. <br>
e.g. 50% means 1 BTC collateral can be borrowed as 0.5 BTC worth of USD tokens.
</p>
</div>
<sm-input id="loan_to_collateral_ratio" type="number" min="1" max="100" step="0.01" error-text="Must be between 1-100" required></sm-input>
</div>
<div class="grid gap-0-5">
<div class="flex align-center tooltip gap-0-3">
<p>Pre-liquidation threshold (%) (Optional)</p>
<svg class="icon" xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M11 7h2v2h-2zm0 4h2v6h-2zm1-9C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8z"/></svg>
<p class="tooltip__content">
If the collateral value falls below this threshold, the collateral can be liquidated. <br>
This is an optional field.
</p>
</div>
<sm-input id="policy_pre_liquidation_threshold" type="number" min="1" max="100" step="0.01" error-text="Must be between 1-100"></sm-input>
</div>
${isCreatingPolicy ? html`
<button class="button button--primary" type="submit" disabled=${true}>
Creating <sm-spinner class="margin-left-0-5"></sm-spinner>
</button>
`: html`
<button class="button button--primary" type="submit" onclick=${createPolicy} disabled="true">Create</button>
`}
</sm-form>
`: ''}
</section>
`
})
renderElem(getRef('sub_page_container'), html`
<div id="main_page" class="grid gap-2">
${balanceCard()}
${bankerInbox()}
</div>
`);
} else {
// user homepage
const inbox = Component(() => {
const { borrowed, coBorrowed, lent } = groupLoans()
const defaultView = wildcards.length ? wildcards[0] : uiGlobals.sortedProcesses.length ? 'in-process' : 'my-loans'
const [view, setView] = useState(defaultView)
let loanRequests = [];
for (const key in floGlobals.loanRequests) {
const { message: { borrower, coborrower, loan_opening_process_id } } = floGlobals.loanRequests[key]
if (!loan_opening_process_id) continue // TODO: remove this check after all requests are updated
if (floGlobals.hasAgreedToLend.has(loan_opening_process_id)) continue // if user has already agreed to lend, don't show the request
loanRequests.push(key)
}
function handleChange(e) {
history.pushState(null, null, `#/home/${e.target.value}`)
setView(e.target.value)
}
if (
coBorrowed.length === 0 && view === 'coBorrowed' ||
lent.length === 0 && view === 'lent' ||
loanRequests.length === 0 && view === 'lend'
) {
// if there are no loans in the selected view, switch to my-loans
history.replaceState(null, null, `#/home/my-loans`)
setView('my-loans')
}
return html`
<section class="grid gap-1">
<sm-chips onchange=${handleChange}>
${uiGlobals.sortedProcesses.length ? html`
<sm-chip value="in-process" selected=${view === 'in-process'}>
In process
<span class="badge">${uiGlobals.sortedProcesses.length}</span>
</sm-chip>
` : ''}
<sm-chip value="my-loans" selected=${view === 'my-loans'}>
My loans
${borrowed.length ? html`<span class="badge">${borrowed.length}</span>` : ''}
</sm-chip>
${coBorrowed.length ? html`
<sm-chip value="coBorrowed" selected=${view === 'coBorrowed'}>
Co-borrowed
<span class="badge">${coBorrowed.length}</span>
</sm-chip>
` : ''}
${lent.length ? html`
<sm-chip value="lent" selected=${view === 'lent'}>
Lent
<span class="badge">${lent.length}</span>
</sm-chip>
` : ''}
${loanRequests.length ? html`
<sm-chip value="lend" selected=${view === 'lend'}>
Lend
<span class="badge">${loanRequests.length}</span>
</sm-chip>
`: ''}
</sm-chips>
<div class="grid gap-1">
${view === 'my-loans' ? html`
${borrowed.length ? html`
<ul class="grid gap-1">
${borrowed.map(loan => render.loan(loan))}
</ul>
<a href="#/apply-loan" class="button button--primary margin-right-auto">Apply for a new loan</a>
`: html`
<div class="grid gap-2 align-center justify-center" style="padding: 2vw 0;">
<div class="grid gap-0-5">
<h1>Looking for USD loans?</h1>
<p>Get a loan against your BTC collateral</p>
</div>
<a href="#/apply-loan" class="button button--primary margin-right-auto">Apply for a loan</a>
</div>
`}
`: ''}
${view === 'in-process' && uiGlobals.sortedProcesses.length ? html`
<ul class="grid gap-1">
${uiGlobals.sortedProcesses.map(processId => render.loanProcess(processId))}
</ul>
`: ''}
${view === 'coBorrowed' && coBorrowed.length ? html`
<ul class="grid gap-1">
${coBorrowed.map(loan => render.loan(loan))}
</ul>
`: html``}
${view === 'lent' && lent.length ? html`
<ul class="grid gap-1">
${lent.map(loan => render.loan(loan))}
</ul>
`: html``}
${view === 'lend' ? html`
<div class="grid gap-2 align-center justify-center" style="padding: 2vw 0;">
<div class="grid">
<h2>Earn interest on your USD tokens</h2>
<p>Lend your USD tokens to earn interest assured by BTC collateral.</p>
</div>
</div>
${loanRequests.length ? html`
<ul id="loan_requests_list">
${loanRequests.reverse().map(requestId => render.loanRequest(requestId, floGlobals.loanRequests[requestId]))}
</ul>
`: html`
<strong>
Loan requests will appear here
</strong>
`}
`: ''}
</div>
</section>
`
})
renderElem(getRef('sub_page_container'), html`
<div id="main_page" class="grid gap-2">
${balanceCard()}
${inbox()}
</div>
`);
}
}
router.addRoute('', renderHome)
router.addRoute('home', renderHome)
router.addRoute('apply-loan', async () => {
const loanApplication = Component(() => {
return html`
<article id="apply_loan" class="grid gap-1">
<button class="button button--colored button--small margin-right-auto" onclick=${() => history.back()}>Back</button>
<h1>Select a policy</h1>
<ul id="policy_list" class="grid gap-1">
${Object.entries(btcMortgage.policies).map(([policyId, policyDetails]) => render.policy(policyId, policyDetails))}
</ul>
</article>
`
})
renderElem(getRef('sub_page_container'), html`${loanApplication()}`);
})
router.addRoute('404', async () => {
renderElem(getRef('app_body'), html`
<article class="flex flex-direction-column align-center justify-center gap-1">
<h1>404</h1>
<h4>Page not found</h4>
</article>
`);
})
async function clearCredentials() {
await floDapps.clearCredentials();
location.reload();
}
</script>
<script type="text/javascript" id="onLoadStartUp">
function onLoadStartUp() {
if (window.quick) return;
floDapps.setCustomPrivKeyInput(getSignedIn)
const [browserName, browserVersion] = detectBrowser().split(' ');
const supportedVersions = {
Chrome: 85,
Firefox: 75,
Safari: 13,
}
if (browserName in supportedVersions) {
if (parseInt(browserVersion) < supportedVersions[browserName]) {
notify(`${browserName} ${browserVersion} is not fully supported, some features may not work properly. Please update to ${supportedVersions[browserName]} or higher.`, 'error')
}
} else {
notify('Browser is not fully compatible, some features may not work. for best experience please use Chrome, Edge, Firefox or Safari', 'error')
}
document.body.classList.remove('hidden')
document.addEventListener('keyup', (e) => {
if (e.key === 'Escape') {
closePopup()
}
})
document.addEventListener('copy', () => {
notify('copied', 'success')
})
document.addEventListener("pointerdown", (e) => {
if (e.target.closest(`button:not([disabled]), sm-button:not([disabled]), .interact:not([disabled="true"])`)) {
createRipple(e, e.target.closest("button, sm-button, .interact"));
}
});
document.querySelectorAll('.popup__header__close, .close-popup-on-click').forEach(elem => {
elem.addEventListener('click', () => {
closePopup()
})
})
router.routeTo('loading')
floDapps.launchStartUp().then(result => {
console.log(result)
floGlobals.myFloID = getFloAddress(floDapps.user.id);
floGlobals.myBtcID = getBtcAddress(floGlobals.myFloID)
floGlobals.isSubAdmin = floGlobals.subAdmins.includes(floGlobals.myFloID)
floGlobals.isAdmin = floGlobals.myFloID === floGlobals.adminID
floGlobals.loaded = false
floGlobals.loanRequests = {}
uiGlobals.activeLoanQueries = {}
floGlobals.btcRate = 1
floGlobals.memoBalance = {
'FLO': 0,
'BTC': 0,
'USD': 0
}
function fetchBtcRate() {
btcMortgage.getRate['BTC']().then(rate => {
floGlobals.btcRate = rate
}).catch(error => console.error(error))
}
fetchBtcRate()
setInterval(() => {
fetchBtcRate()
}, 1000 * 60 * 5) // check BTC rate every 5 minutes
btcMortgage.init().then(async result => {
console.info(result)
try {
if (floCrypto.isSameAddr(floDapps.user.id, btcMortgage.banker.id)) {
floCloudAPI.requestApplicationData(null, {
callback: (d, e) => {
if (e) return
for (const key in d) {
switch (d[key].type) {
case 'type_unlock_collateral_request':
case 'type_unlock_collateral_ack':
case 'type_refund_collateral_request':
case 'type_refund_collateral_ack':
case 'type_liquate_collateral_request':
case 'type_liquate_collateral_ack':
case 'type_pre_liquidate_collateral_request':
case 'type_preLiquidate_collateral_ack':
if (!floGlobals.bankerRequests)
floGlobals.bankerRequests = {}
if (!d[key].note) // if note is not present, it means the request is not processed yet
floGlobals.bankerRequests[key] = d[key]
break;
}
}
console.log('LOAN REQUESTS', d)
if (floGlobals.loaded) {
router.routeTo(window.location.hash)
}
}
})
} else {
const result = await Promise.allSettled([
new Promise((resolve, reject) => {
btcMortgage.viewMyInbox((requests, e) => {
if (e) return
console.log('MY INBOX', requests)
processInProcessRequests(requests, { isInbox: true })
if (floGlobals.loaded) {
router.routeTo(window.location.hash)
}
resolve()
}).catch(error => {
reject(error)
})
}),
new Promise((resolve, reject) => {
btcMortgage.listLoanRequests((requests, e) => {
if (e) return
console.log('LOAN REQUESTS', requests)
floGlobals.loanRequests = {
...floGlobals.loanRequests,
...requests
}
// if loan requests are from me, process them
processInProcessRequests(requests, { areLoanRequests: true })
if (floGlobals.loaded) {
router.routeTo(window.location.hash)
}
resolve()
}).catch(error => {
reject(error)
})
}),
new Promise((resolve, reject) => {
floCloudAPI.requestApplicationData('in_process_loan_request', {
callback: (requests, e) => {
if (e) return
parseInProcessRequests(requests)
console.log('IN PROCESS LOAN REQUEST', requests)
if (floGlobals.loaded) {
router.routeTo(window.location.hash)
}
resolve()
}
})
})
])
}
await Promise.all([...uiGlobals.relatedAddresses].map(address => {
return new Promise((resolve, reject) => {
floCloudAPI.requestApplicationData(null, {
senderID: floDapps.user.id,
receiverID: address,
callback: (requests, e) => {
if (e) return
console.log('MY OUTBOX', requests)
processInProcessRequests(requests)
if (floGlobals.loaded) {
router.routeTo(window.location.hash)
}
resolve()
}
})
})
}))
console.log(result)
if (['#/landing', '#/sign_in', '#/sign_up'].includes(window.location.hash)) {
history.replaceState(null, null, '#/home')
router.routeTo('home')
} else {
router.routeTo(window.location.hash)
}
floGlobals.loaded = true
} catch (error) {
notify(error, 'error')
}
}).catch(error => {
let isBrave = navigator.brave !== undefined
if (error === 'Cloud offline') {
document.body.prepend(document.createElement('adblocker-warning'))
} else if (error === "App database initiation failed") {
} else {
notify(error, "error")
}
})
}).catch(error => console.error(error))
}
/**
* @param {Object} requests
*/
function parseInProcessRequests(requests) {
if (!floGlobals.hasAgreedToLend)
floGlobals.hasAgreedToLend = new Set()
for (const key in requests) {
const { message: { loan_opening_process_id, closing_txid } } = requests[key]
floGlobals.hasAgreedToLend.add(loan_opening_process_id)
}
}
/**
* @param {string} inputString
*/
function decodeDateStringToMilliseconds(inputString) {
// Define a mapping for units to milliseconds (Y: years, M: months, D: days)
const unitToMilliseconds = {
Y: 31536000000, // 1 year = 365 days
M: 2592000000, // 1 month = 30 days (approximate)
D: 86400000 // 1 day = 24 hours
};
// Use a regular expression to match and capture the numeric part and unit
const match = inputString.match(/^([1-9]\d{0,4})(Y|M|D)$/);
if (match) {
const numericPart = parseInt(match[1], 10);
const unit = match[2];
if (unitToMilliseconds.hasOwnProperty(unit)) {
// Convert the numeric part to milliseconds using the unit
const milliseconds = numericPart * unitToMilliseconds[unit];
return milliseconds;
}
}
// Return null for invalid input
return null;
}
const completedLoanProcess = new Set()
uiGlobals.inProcessRequests = {}
uiGlobals.sortedProcesses = []
uiGlobals.relatedAddresses = new Set()
/**
* @param {Object} newRequests
* @param {Object} options
* @param {Boolean} options.isInbox
*/
function processInProcessRequests(newRequests = {}, options = {}) {
const { isInbox = false, areLoanRequests = false } = options
for (const key in newRequests) {
const {
message: {
borrower, coborrower, lender,
loan_amount, policy_id, loan_opening_process_id, loan_request_id, collateral_lock_id,
loan_id, closing_txid, unlock_tx_hex, unlock_collateral_id, type_unlock_collateral_ack
} = {},
type, time
} = newRequests[key];
if (borrower && areLoanRequests && !floCrypto.isSameAddr(borrower, floDapps.user.id)) continue // if loan request is not from me, ignore
if (loan_id && btcMortgage.loans[loan_id] && btcMortgage.loans[loan_id].close_time)
continue // Loan closed
if (loan_opening_process_id) {
if (completedLoanProcess.has(loan_opening_process_id) || Object.values(btcMortgage.loans).find(loan => loan.loan_opening_process_id === loan_opening_process_id)) {
completedLoanProcess.add(loan_opening_process_id)
continue // Request has been processed
}
if (!uiGlobals.inProcessRequests[loan_opening_process_id]) {
uiGlobals.inProcessRequests[loan_opening_process_id] = {
loanOpeningProcessID: loan_opening_process_id,
hasProvidedCollateral: false,
hasAgreedToLend: false,
hasLockedCollateral: false,
type: 'loanOpening'
}
}
if (borrower) {
uiGlobals.inProcessRequests[loan_opening_process_id].borrower = borrower
uiGlobals.inProcessRequests[loan_opening_process_id].isBorrower = floCrypto.isSameAddr(borrower, floDapps.user.id)
if (isInbox)
uiGlobals.relatedAddresses.add(borrower)
}
if (coborrower) {
uiGlobals.inProcessRequests[loan_opening_process_id].coborrower = coborrower
uiGlobals.inProcessRequests[loan_opening_process_id].isCoBorrower = floCrypto.isSameAddr(coborrower, floDapps.user.id)
if (isInbox)
uiGlobals.relatedAddresses.add(coborrower)
}
if (lender) {
uiGlobals.inProcessRequests[loan_opening_process_id].lender = lender
uiGlobals.inProcessRequests[loan_opening_process_id].isLender = floCrypto.isSameAddr(lender, floDapps.user.id)
if (isInbox)
uiGlobals.relatedAddresses.add(lender)
}
if (loan_amount) {
uiGlobals.inProcessRequests[loan_opening_process_id].loanAmount = loan_amount
}
if (policy_id) {
uiGlobals.inProcessRequests[loan_opening_process_id].policyID = policy_id
}
if (loan_request_id) {
uiGlobals.inProcessRequests[loan_opening_process_id].loanRequestID = loan_request_id
uiGlobals.inProcessRequests[loan_opening_process_id].hasProvidedCollateral = true
}
if (collateral_lock_id) {
uiGlobals.inProcessRequests[loan_opening_process_id].collateralLockID = collateral_lock_id
}
switch (type) {
case 'type_loan_collateral_request':
uiGlobals.inProcessRequests[loan_opening_process_id].initiationTime = time
uiGlobals.inProcessRequests[loan_opening_process_id].collateralRequestID = key
break;
case 'type_loan_request':
uiGlobals.inProcessRequests[loan_opening_process_id].hasProvidedCollateral = true
uiGlobals.inProcessRequests[loan_opening_process_id].loanRequestID = key
break;
case 'type_loan_response':
uiGlobals.inProcessRequests[loan_opening_process_id].loanResponseTime = time
uiGlobals.inProcessRequests[loan_opening_process_id].hasAgreedToLend = true
uiGlobals.inProcessRequests[loan_opening_process_id].loanResponseID = key
break;
case "type_collateral_lock_request":
uiGlobals.inProcessRequests[loan_opening_process_id].collateralLockRequestID = key
break;
case 'type_collateral_lock_ack':
uiGlobals.inProcessRequests[loan_opening_process_id].collateralLockAckTime = time
uiGlobals.inProcessRequests[loan_opening_process_id].hasLockedCollateral = true
uiGlobals.inProcessRequests[loan_opening_process_id].collateralLockAckID = key
break;
case 'type_refund_collateral_request':
uiGlobals.inProcessRequests[loan_opening_process_id].hasRequestedCollateralRefund = true
break;
}
} else {
if (loan_id) {
if (!uiGlobals.activeLoanQueries[loan_id])
uiGlobals.activeLoanQueries[loan_id] = {
loanID: loan_id,
}
switch (type) {
case 'type_loan_closed_ack':
uiGlobals.activeLoanQueries[loan_id].hasRepaidLoan = true
break;
case 'type_unlock_collateral_request':
uiGlobals.activeLoanQueries[loan_id].hasRequestedCollateralUnlock = true
break;
case 'type_unlock_collateral_ack':
uiGlobals.activeLoanQueries[loan_id].hasRefundedCollateral = true
uiGlobals.activeLoanQueries[loan_id].collateralUnlockAckTime = time
delete uiGlobals.inProcessRequests[closing_txid]
completedLoanProcess.add(closing_txid)
break;
case 'type_liquate_collateral_request':
uiGlobals.activeLoanQueries[loan_id].hasRequestedCollateralLiquidation = true
break;
case 'type_liquate_collateral_ack':
uiGlobals.activeLoanQueries[loan_id].hasLiquidatedCollateral = true
break;
case 'type_pre_liquidate_collateral_request':
uiGlobals.activeLoanQueries[loan_id].hasRequestedCollateralPreLiquidation = true
break;
}
}
if (closing_txid && !completedLoanProcess.has(closing_txid)) {
if (!uiGlobals.inProcessRequests[closing_txid])
uiGlobals.inProcessRequests[closing_txid] = {
loanID: loan_id,
closingTxID: closing_txid,
hasRequestedCollateralRefund: false,
hasRefundedCollateral: false,
hasPaidLoan: false,
type: 'loanClosing'
}
const { borrower, coborrower, lender } = btcMortgage.loans[loan_id] || {}
if (borrower) {
uiGlobals.inProcessRequests[closing_txid].borrower = borrower
uiGlobals.inProcessRequests[closing_txid].isBorrower = floCrypto.isSameAddr(borrower, floDapps.user.id)
if (isInbox)
uiGlobals.relatedAddresses.add(borrower)
}
if (coborrower) {
uiGlobals.inProcessRequests[closing_txid].coborrower = coborrower
uiGlobals.inProcessRequests[closing_txid].isCoBorrower = floCrypto.isSameAddr(coborrower, floDapps.user.id)
if (isInbox)
uiGlobals.relatedAddresses.add(coborrower)
}
if (lender) {
uiGlobals.inProcessRequests[closing_txid].lender = lender
uiGlobals.inProcessRequests[closing_txid].isLender = floCrypto.isSameAddr(lender, floDapps.user.id)
if (isInbox)
uiGlobals.relatedAddresses.add(lender)
}
if (unlock_tx_hex) {
uiGlobals.inProcessRequests[closing_txid].unlockTxHex = unlock_tx_hex
}
switch (type) {
case 'type_loan_closed_ack':
uiGlobals.inProcessRequests[closing_txid].loanClosedAckTime = time
uiGlobals.inProcessRequests[closing_txid].hasPaidLoan = true
break;
case 'type_unlock_collateral_request':
uiGlobals.inProcessRequests[closing_txid].unlockCollateralRequestTime = time
uiGlobals.inProcessRequests[closing_txid].hasRequestedCollateralUnlock = true
break;
case 'type_unlock_collateral_ack':
uiGlobals.inProcessRequests[closing_txid].unlockCollateralAckTime = time
uiGlobals.inProcessRequests[closing_txid].hasRefundedCollateral = true
completedLoanProcess.add(closing_txid)
break;
}
}
}
}
if (uiGlobals.relatedAddresses.has(floGlobals.myBtcID))
uiGlobals.relatedAddresses.delete(floGlobals.myBtcID)
if (uiGlobals.relatedAddresses.has(floGlobals.myFloID))
uiGlobals.relatedAddresses.delete(floGlobals.myFloID)
uiGlobals.sortedProcesses = Object.keys(uiGlobals.inProcessRequests).sort((a, b) => {
const aTime = uiGlobals.inProcessRequests[a].initiationTime || uiGlobals.inProcessRequests[a].loanResponseTime || uiGlobals.inProcessRequests[a].loanClosedAckTime || uiGlobals.inProcessRequests[a].unlockCollateralRequestTime;
const bTime = uiGlobals.inProcessRequests[b].initiationTime || uiGlobals.inProcessRequests[b].loanResponseTime || uiGlobals.inProcessRequests[b].loanClosedAckTime || uiGlobals.inProcessRequests[b].unlockCollateralRequestTime;
return bTime - aTime
})
}
function groupLoans() {
return Object.values(btcMortgage.loans)
.sort((a, b) => b.open_time - a.open_time)
.reduce((acc, loan) => {
const { loan_id, borrower, coborrower, lender } = loan
if (floCrypto.isSameAddr(borrower, floDapps.user.id) && floCrypto.isSameAddr(coborrower, floDapps.user.id))
acc.borrowed.push(loan_id)
else if (floCrypto.isSameAddr(coborrower, floDapps.user.id))
acc.coBorrowed.push(loan_id)
else if (floCrypto.isSameAddr(lender, floDapps.user.id))
acc.lent.push(loan_id)
return acc
}, {
borrowed: [],
coBorrowed: [],
lent: []
})
}
</script>
</body>
</html>