convert ethereum name into bsc

This commit is contained in:
raviycoder 2024-07-10 16:25:51 +05:30
parent 470c2011f1
commit 32244c78d3
2 changed files with 64 additions and 58 deletions

View File

@ -4,7 +4,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FLO Ethereum</title>
<title>Binance Smart Chain</title>
<link rel="stylesheet" href="css/main.min.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
@ -34,7 +34,7 @@
<div class="app-name">
<div class="app-name__company">RanchiMall</div>
<h4 class="app-name__title">
FLO Ethereum
Binance Smart Chain
</h4>
</div>
</div>
@ -428,16 +428,16 @@
</script>
<script>
const assetIcons = {
ether: `<svg class="icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"> <g clip-path="url(#clip0_201_2)"> <path d="M12 0L19.6368 12.4368L12.1633 16.8L4.36325 12.4368L12 0Z"/> <path d="M12 24L4.36325 13.6099L11.8367 18L19.6368 13.6099L12 24Z"/> </g> <defs> <clipPath id="clip0_201_2"> <rect width="24" height="24" fill="white"/> </clipPath> </defs> </svg>`,
Binance: `<svg class="icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"> <g clip-path="url(#clip0_201_2)"> <path d="M12 0L19.6368 12.4368L12.1633 16.8L4.36325 12.4368L12 0Z"/> <path d="M12 24L4.36325 13.6099L11.8367 18L19.6368 13.6099L12 24Z"/> </g> <defs> <clipPath id="clip0_201_2"> <rect width="24" height="24" fill="white"/> </clipPath> </defs> </svg>`,
usdc: `<svg class="icon" xmlns="http://www.w3.org/2000/svg" data-name="86977684-12db-4850-8f30-233a7c267d11" viewBox="0 0 2000 2000"> <path d="M1000 2000c554.17 0 1000-445.83 1000-1000S1554.17 0 1000 0 0 445.83 0 1000s445.83 1000 1000 1000z" fill="#2775ca"/> <path d="M1275 1158.33c0-145.83-87.5-195.83-262.5-216.66-125-16.67-150-50-150-108.34s41.67-95.83 125-95.83c75 0 116.67 25 137.5 87.5 4.17 12.5 16.67 20.83 29.17 20.83h66.66c16.67 0 29.17-12.5 29.17-29.16v-4.17c-16.67-91.67-91.67-162.5-187.5-170.83v-100c0-16.67-12.5-29.17-33.33-33.34h-62.5c-16.67 0-29.17 12.5-33.34 33.34v95.83c-125 16.67-204.16 100-204.16 204.17 0 137.5 83.33 191.66 258.33 212.5 116.67 20.83 154.17 45.83 154.17 112.5s-58.34 112.5-137.5 112.5c-108.34 0-145.84-45.84-158.34-108.34-4.16-16.66-16.66-25-29.16-25h-70.84c-16.66 0-29.16 12.5-29.16 29.17v4.17c16.66 104.16 83.33 179.16 220.83 200v100c0 16.66 12.5 29.16 33.33 33.33h62.5c16.67 0 29.17-12.5 33.34-33.33v-100c125-20.84 208.33-108.34 208.33-220.84z" fill="#fff"/> <path d="M787.5 1595.83c-325-116.66-491.67-479.16-370.83-800 62.5-175 200-308.33 370.83-370.83 16.67-8.33 25-20.83 25-41.67V325c0-16.67-8.33-29.17-25-33.33-4.17 0-12.5 0-16.67 4.16-395.83 125-612.5 545.84-487.5 941.67 75 233.33 254.17 412.5 487.5 487.5 16.67 8.33 33.34 0 37.5-16.67 4.17-4.16 4.17-8.33 4.17-16.66v-58.34c0-12.5-12.5-29.16-25-37.5zM1229.17 295.83c-16.67-8.33-33.34 0-37.5 16.67-4.17 4.17-4.17 8.33-4.17 16.67v58.33c0 16.67 12.5 33.33 25 41.67 325 116.66 491.67 479.16 370.83 800-62.5 175-200 308.33-370.83 370.83-16.67 8.33-25 20.83-25 41.67V1700c0 16.67 8.33 29.17 25 33.33 4.17 0 12.5 0 16.67-4.16 395.83-125 612.5-545.84 487.5-941.67-75-237.5-258.34-416.67-487.5-491.67z" fill="#fff"/></svg>`,
usdt: `<svg class="icon" xmlns="http://www.w3.org/2000/svg" id="Layer_1" data-name="Layer 1" viewBox="0 0 339.43 295.27"><title>tether-usdt-logo</title><path d="M62.15,1.45l-61.89,130a2.52,2.52,0,0,0,.54,2.94L167.95,294.56a2.55,2.55,0,0,0,3.53,0L338.63,134.4a2.52,2.52,0,0,0,.54-2.94l-61.89-130A2.5,2.5,0,0,0,275,0H64.45a2.5,2.5,0,0,0-2.3,1.45h0Z" style="fill:#50af95;fill-rule:evenodd"/><path d="M191.19,144.8v0c-1.2.09-7.4,0.46-21.23,0.46-11,0-18.81-.33-21.55-0.46v0c-42.51-1.87-74.24-9.27-74.24-18.13s31.73-16.25,74.24-18.15v28.91c2.78,0.2,10.74.67,21.74,0.67,13.2,0,19.81-.55,21-0.66v-28.9c42.42,1.89,74.08,9.29,74.08,18.13s-31.65,16.24-74.08,18.12h0Zm0-39.25V79.68h59.2V40.23H89.21V79.68H148.4v25.86c-48.11,2.21-84.29,11.74-84.29,23.16s36.18,20.94,84.29,23.16v82.9h42.78V151.83c48-2.21,84.12-11.73,84.12-23.14s-36.09-20.93-84.12-23.15h0Zm0,0h0Z" style="fill:#fff;fill-rule:evenodd"/><script xmlns=""/></svg>`,
}
window.smCompConfig = {
'sm-input': [
{
selector: '[data-eth-address]',
selector: '[data-BSC-address]',
customValidation: (value) => {
if (!value) return { isValid: false, errorText: 'Please enter a Ethereum address' }
if (!value) return { isValid: false, errorText: 'Please enter a Binance address' }
return {
isValid: ethOperator.isValidAddress(value),
errorText: `Invalid address.<br> It usually starts with "0x"`
@ -457,10 +457,10 @@
{
selector: '#check_balance_input',
customValidation: (value) => {
if (!value) return { isValid: false, errorText: 'Please enter a private key or eth address' }
if (!value) return { isValid: false, errorText: 'Please enter a private key or BSC address' }
return {
isValid: floCrypto.getPubKeyHex(value) || ethOperator.isValidAddress(value),
errorText: `Invalid private key or eth address"`
errorText: `Invalid private key or BSC address"`
}
}
}
@ -509,7 +509,7 @@
createRipple(e, e.target.closest("button, .interactive"));
}
});
compactIDB.initDB('floEthereum', {
compactIDB.initDB('floBinance', {
contacts: {}
}).then((result) => {
console.log(result)
@ -522,17 +522,17 @@
window.ethereum.on('chainChanged', (chainId) => {
window.currentChainId = chainId
if (chainId !== '0x1') {
renderError('Please switch MetaMask to Ethereum Mainnet')
renderError('Please switch MetaMask to Binance Mainnet')
} else {
router.routeTo(location.hash)
}
})
window.ethereum.request({
"method": "eth_chainId"
"method": "BSC_chainId"
}).then(chainId => {
window.currentChainId = chainId
if (chainId !== '0x1') {
renderError('Please switch MetaMask to Ethereum Mainnet')
renderError('Please switch MetaMask to Binance Mainnet')
} else {
router.routeTo(location.hash)
}
@ -554,7 +554,7 @@
// })
if (typeof window.ethereum !== 'undefined') {
ethereum.on('accountsChanged', (accounts) => {
getRef('eth_balance_wrapper').classList.add('hidden')
getRef('BSC_balance_wrapper').classList.add('hidden')
setMetaMaskStatus(accounts.length > 0)
});
ethereum.on('connect', (accounts) => {
@ -583,11 +583,11 @@
</aside>
<section id="balance_section" class="grid gap-1-5">
<h2>
Check Ether, USDC and USDT balance
Check Binance, USDC and USDT balance
</h2>
<sm-form oninvalid="handleInvalidSearch()">
<div id="input_wrapper">
<sm-input id="check_balance_input" class="password-field flex-1" placeholder="FLO/BTC private key or Eth address"
<sm-input id="check_balance_input" class="password-field flex-1" placeholder="FLO/BTC private key or BSC address"
type="password" animate required>
<svg class="icon" slot="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"></rect> </g> <g> <path d="M21,10h-8.35C11.83,7.67,9.61,6,7,6c-3.31,0-6,2.69-6,6s2.69,6,6,6c2.61,0,4.83-1.67,5.65-4H13l2,2l2-2l2,2l4-4.04L21,10z M7,15c-1.65,0-3-1.35-3-3c0-1.65,1.35-3,3-3s3,1.35,3,3C10,13.65,8.65,15,7,15z"> </path> </g> </svg>
<label slot="right" class="interact">
@ -604,11 +604,11 @@
</div>
</div>
</sm-form>
<div id="eth_balance_wrapper" class="grid gap-2 hidden"></div>
<div id="BSC_balance_wrapper" class="grid gap-2 hidden"></div>
</section>
`)
if (window.ethereum && !(window.currentChainId && window.currentChainId === '0x1')) {
renderError('Please switch MetaMask to Ethereum Mainnet')
renderError('Please switch MetaMask to Binance Mainnet')
}
renderSearchedAddressList()
}
@ -652,14 +652,14 @@
}
const renderedContacts = []
for (const floAddress in contacts) {
const { ethAddress } = contacts[floAddress]
const { BSCAddress } = contacts[floAddress]
renderedContacts.push(html`
<li class="contact" .dataset=${{ floAddress, ethAddress }}>
${floAddress === ethAddress ? html`
<li class="contact" .dataset=${{ floAddress, BSCAddress }}>
${floAddress === BSCAddress ? html`
`: html`
<sm-chips onchange=${e => e.target.closest('.contact').querySelector('sm-copy').value = e.target.value}>
<sm-chip value=${floAddress} selected>FLO</sm-chip>
<sm-chip value=${ethAddress}>ETH</sm-chip>
<sm-chip value=${BSCAddress}>BSC</sm-chip>
</sm-chips>
`}
<sm-copy value="${floAddress}"></sm-copy>
@ -667,7 +667,7 @@
<button class="button button--small" onclick=${() => deleteContact(floAddress)}>
Delete
</button>
<button class="button button--colored button--small" onclick=${() => checkBalance(ethAddress, floAddress)}>Check balance</button>
<button class="button button--colored button--small" onclick=${() => checkBalance(BSCAddress, floAddress)}>Check balance</button>
</div>
</li>`)
}
@ -676,44 +676,44 @@
console.error(error)
})
}
function checkBalance(ethAddress, floAddress) {
if (!ethAddress) {
function checkBalance(BSCAddress, floAddress) {
if (!BSCAddress) {
const keyToConvert = document.querySelector('#check_balance_input').value.trim()
if (ethOperator.isValidAddress(keyToConvert)) {
ethAddress = keyToConvert
BSCAddress = keyToConvert
} else {
if (/^[0-9a-fA-F]{64}$/.test(keyToConvert)) {
keyToConvert = coinjs.privkey2wif(keyToConvert)
}
const ethPrivateKey = coinjs.wif2privkey(keyToConvert).privkey;
ethAddress = floEthereum.ethAddressFromPrivateKey(ethPrivateKey)
BSCAddress = floEthereum.ethAddressFromPrivateKey(ethPrivateKey)
floAddress = floCrypto.getFloID(keyToConvert)
}
}
if (!ethAddress) return
if (!BSCAddress) return
buttonLoader('check_balance_button', true)
Promise.all([
ethOperator.getBalance(ethAddress),
ethOperator.getTokenBalance(ethAddress, 'usdc'),
ethOperator.getTokenBalance(ethAddress, 'usdt')
ethOperator.getBalance(BSCAddress),
ethOperator.getTokenBalance(BSCAddress, 'usdc'),
ethOperator.getTokenBalance(BSCAddress, 'usdt')
])
.then(([etherBalance, usdcBalance, usdtBalance]) => {
compactIDB.readData('contacts', floAddress || ethAddress).then(result => {
.then(([BinanceBalance, usdcBalance, usdtBalance]) => {
compactIDB.readData('contacts', floAddress || BSCAddress).then(result => {
if (result) return
compactIDB.addData('contacts', {
ethAddress,
}, floAddress || ethAddress).then(() => {
BSCAddress,
}, floAddress || BSCAddress).then(() => {
renderSearchedAddressList()
}).catch((error) => {
console.error(error)
})
})
renderElem(getRef('eth_balance_wrapper'), html`
renderElem(getRef('BSC_balance_wrapper'), html`
<div class="grid">
<div class="label">ETH address</div>
<sm-copy id="eth_address" value=${ethAddress}></sm-copy>
<div class="label">BSC address</div>
<sm-copy id="BSC_address" value=${BSCAddress}></sm-copy>
</div>
${floAddress && floAddress !== ethAddress ? html`
${floAddress && floAddress !== BSCAddress ? html`
<div class="grid">
<div class="label">FLO address</div>
<sm-copy id="flo_address" value=${floAddress}></sm-copy>
@ -723,8 +723,8 @@
<h4>Balance</h4>
<ul id="eth_address_balance" class="flex flex-direction-column gap-0-5">
<li class="flex align-center space-between">
<p>Ether</p>
<b id="ether_balance">${etherBalance} ETH</b>
<p>Binance</p>
<b id="Binance_balance">${BinanceBalance} BSC</b>
</li>
<li class="flex align-center space-between">
<p>USDC</p>
@ -737,8 +737,8 @@
</ul>
</div>
`)
getRef('eth_balance_wrapper').classList.remove('hidden')
getRef('eth_balance_wrapper').animate([
getRef('BSC_balance_wrapper').classList.remove('hidden')
getRef('BSC_balance_wrapper').animate([
{
transform: 'translateY(-1rem)',
opacity: 0
@ -761,10 +761,10 @@
function handleInvalidSearch() {
if (document.startViewTransition)
document.startViewTransition(() => {
getRef('eth_balance_wrapper').classList.add('hidden')
getRef('BSC_balance_wrapper').classList.add('hidden')
})
else {
getRef('eth_balance_wrapper').classList.add('hidden')
getRef('BSC_balance_wrapper').classList.add('hidden')
}
}
@ -788,13 +788,13 @@
<div class="flex space-between align-center">
<div class="flex flex-direction-column gap-0-5">
<h4>Sender</h4>
<p>Amount will be deducted from equivalent Ethereum address</p>
<p>Amount will be deducted from equivalent Binance address</p>
</div>
<button id="check_balance_button" class="button button--small button--colored" onclick="checkSenderBalance()" disabled>
Check balance
</button>
</div>
<sm-input id="private_key_input" placeholder="Sender's FLO/BTC/ETH private key" oninput=${handleSenderInput} data-private-key class="password-field" type="password" animate required>
<sm-input id="private_key_input" placeholder="Sender's FLO/BTC/BSC private key" oninput=${handleSenderInput} data-private-key class="password-field" type="password" animate required>
<svg class="icon" slot="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"></rect> </g> <g> <path d="M21,10h-8.35C11.83,7.67,9.61,6,7,6c-3.31,0-6,2.69-6,6s2.69,6,6,6c2.61,0,4.83-1.67,5.65-4H13l2,2l2-2l2,2l4-4.04L21,10z M7,15c-1.65,0-3-1.35-3-3c0-1.65,1.35-3,3-3s3,1.35,3,3C10,13.65,8.65,15,7,15z"> </path> </g> </svg>
<label slot="right" class="interact">
<input type="checkbox" class="hidden" autocomplete="off" readonly onchange="togglePrivateKeyVisibility(this)">
@ -808,15 +808,15 @@
<div class="flex flex-direction-column gap-0-5">
<h4>Receiver</h4>
<div class="grid gap-0-5">
<sm-input class="receiver-address" placeholder="Receiver's Ethereum address" data-eth-address animate required ></sm-input>
<sm-input class="receiver-address" placeholder="Receiver's Binance address" data-BSC-address animate required ></sm-input>
<div class="flex flex-direction-column gap-0-5">
<sm-input class="receiver-amount amount-shown flex-1" placeholder="Amount" type="number" step="0.000001" min="0.000001" error-text="Amount should be grater than 0.000001 ETHER" animate required>
<sm-input class="receiver-amount amount-shown flex-1" placeholder="Amount" type="number" step="0.000001" min="0.000001" error-text="Amount should be grater than 0.000001 Binance" animate required>
<div class="asset-symbol flex" slot="icon">
<svg class="icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"> <g clip-path="url(#clip0_201_2)"> <path d="M12 0L19.6368 12.4368L12.1633 16.8L4.36325 12.4368L12 0Z"/> <path d="M12 24L4.36325 13.6099L11.8367 18L19.6368 13.6099L12 24Z"/> </g> <defs> <clipPath id="clip0_201_2"> <rect width="24" height="24" fill="white"/> </clipPath> </defs> </svg>
</div>
</sm-input>
<sm-chips id="asset_selector" onchange=${handleAssetChange}>
<sm-chip value="ether" selected>ETHER</sm-chip>
<sm-chip value="Binance" selected>Binance</sm-chip>
<sm-chip value="usdc">USDC</sm-chip>
<sm-chip value="usdt">USDT</sm-chip>
</sm-chips>
@ -824,13 +824,13 @@
</div>
</div>
<div class="multi-state-button">
<button id="send_tx_button" class="button button--primary" type="submit" disabled onclick="sendTx()">Send ETHER</button>
<button id="send_tx_button" class="button button--primary" type="submit" disabled onclick="sendTx()">Send Binance</button>
</div>
</fieldset>
</sm-form>
`)
if (window.ethereum && !(window.currentChainId && window.currentChainId === '0x1')) {
renderError('Please switch MetaMask to Ethereum Mainnet')
renderError('Please switch MetaMask to Binance Mainnet')
}
})
function togglePrivateKeyVisibility(input) {
@ -852,7 +852,7 @@
renderElem(getRef('sender_balance_container'), html` Loading balance...<sm-spinner></sm-spinner> `)
const promises = [ethOperator.getBalance(address)]
const selectedAsset = getRef('asset_selector').value
if (selectedAsset !== 'ether')
if (selectedAsset !== 'Binance')
promises.push(ethOperator.getTokenBalance(address, selectedAsset))
Promise.all(promises).then(([ethBalance, tokenBalance]) => {
renderElem(getRef('sender_balance_container'), html`
@ -862,7 +862,7 @@
<sm-copy value=${address}><p>${address}<p></sm-copy>
</div>
<p>
Balance: <b class="amount-shown">${ethBalance} ETH</b> ${selectedAsset !== 'ether' ? html`| <b class="amount-shown">${tokenBalance} ${selectedAsset.toUpperCase()}</b>` : ''}
Balance: <b class="amount-shown">${ethBalance} BSC</b> ${selectedAsset !== 'Binance' ? html`| <b class="amount-shown">${tokenBalance} ${selectedAsset.toUpperCase()}</b>` : ''}
</p>
</div>
`)
@ -903,7 +903,7 @@
}
privateKey = coinjs.wif2privkey(privateKey).privkey;
switch (asset) {
case 'ether': {
case 'Binance': {
const tx = await ethOperator.sendTransaction({
privateKey,
receiver,
@ -998,7 +998,7 @@
renderElem(getRef('page_container'), html`
<div class="grid gap-1">
<h2>
Don't have an Ethereum address? Create one
Don't have an Binance address? Create one
</h2>
<button class="button button--primary interactive gap-0-5 margin-right-auto" onclick=${generateNewID}>
<svg class="icon" xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24"
@ -1022,7 +1022,7 @@
function generateNewID() {
const { floID, privKey } = floCrypto.generateNewID();
const ethPrivateKey = coinjs.wif2privkey(privKey).privkey;
const ethAddress = floEthereum.ethAddressFromPrivateKey(ethPrivateKey)
const BSCAddress = floEthereum.ethAddressFromPrivateKey(ethPrivateKey)
renderElem(getRef('created_address_wrapper'), html`
<ul id="generated_addresses" class="grid gap-1-5">
<li class="grid gap-0-5">
@ -1037,8 +1037,8 @@
</li>
<li class="grid gap-0-5">
<div>
<h5>Equivalent Ethereum Address</h5>
<sm-copy value="${ethAddress}"></sm-copy>
<h5>Equivalent Binance Address</h5>
<sm-copy value="${BSCAddress}"></sm-copy>
</div>
<div>
<h5>Private Key</h5>

View File

@ -242,10 +242,16 @@
}
function getProvider() {
// switches provider based on whether the user is using MetaMask or not
const bscMainnet = {
chainId: 56,
name: 'binance',
rpc: 'https://bsc-dataseed.binance.org/',
explorer: 'https://bscscan.com'
};
if (window.ethereum) {
return new ethers.providers.Web3Provider(window.ethereum);
} else {
return new ethers.providers.JsonRpcProvider(`https://mainnet.infura.io/v3/6e12fee52bdd48208f0d82fb345bcb3c`)
return new ethers.providers.JsonRpcProvider(bscMainnet.rpc, bscMainnet)
}
}
function connectToMetaMask() {