Browse Source

unstyled version of a button that accepts dollar input and pays in Moneyherotokens

master
Dom SP 3 years ago
parent
commit
f95cbecf88
5 changed files with 131 additions and 89 deletions
  1. +1
    -1
      public/index.html
  2. +14
    -44
      src/App.tsx
  3. +65
    -38
      src/components/BuyButton.tsx
  4. +36
    -0
      src/config.ts
  5. +15
    -6
      src/index.js

+ 1
- 1
public/index.html View File

data-fa2-contract="KT1BB1uMwVvJ1M3vVHXWALs1RWdgTp1rnXTR" data-fa2-contract="KT1BB1uMwVvJ1M3vVHXWALs1RWdgTp1rnXTR"
data-swap-contract="KT1Ed11bNukFdYSc3qQFZ2HGYdF13XU6WZ4A" data-swap-contract="KT1Ed11bNukFdYSc3qQFZ2HGYdF13XU6WZ4A"
data-receiver="tz1hJncvXvL2VyctPE685GJPXDaRJ7dtiwjm" data-receiver="tz1hJncvXvL2VyctPE685GJPXDaRJ7dtiwjm"
data-amount="100"></div>
data-amount="32"></div>
<!-- <!--
This HTML file is a template. This HTML file is a template.
If you open it directly in the browser, you will see an empty page. If you open it directly in the browser, you will see an empty page.

+ 14
- 44
src/App.tsx View File

import React, { useState } from "react";
import React, { useState, useEffect } from "react";
import { TezosToolkit } from "@taquito/taquito"; import { TezosToolkit } from "@taquito/taquito";
import "./App.css"; import "./App.css";
import ConnectButton from "./components/ConnectWallet"; import ConnectButton from "./components/ConnectWallet";
PERMISSION_REQUEST_SUCCESS = "Wallet is connected" PERMISSION_REQUEST_SUCCESS = "Wallet is connected"
} }


interface AppProps {
fa2Contract: string,
type AppProps = {
swapContract: string, swapContract: string,
fa2Contract: string,
receiver: string, receiver: string,
amount: number amount: number
} }


const App = ({ const App = ({
fa2Contract,
swapContract, swapContract,
fa2Contract,
receiver, receiver,
amount amount
}: AppProps) => { }: AppProps) => {
const [storage, setStorage] = useState<number>(0); const [storage, setStorage] = useState<number>(0);
const [copiedPublicToken, setCopiedPublicToken] = useState<boolean>(false); const [copiedPublicToken, setCopiedPublicToken] = useState<boolean>(false);
const [beaconConnection, setBeaconConnection] = useState<boolean>(false); const [beaconConnection, setBeaconConnection] = useState<boolean>(false);
const [activeTab, setActiveTab] = useState<string>("transfer");


// Granadanet Increment/Decrement contract // Granadanet Increment/Decrement contract
const contractAddress: string = "KT1K3XVNzsmur7VRgY8CAHPUENaErzzEpe4e"; const contractAddress: string = "KT1K3XVNzsmur7VRgY8CAHPUENaErzzEpe4e";

// useEffect(() => {
// console.log(swapContract)
// }, [swapContract])
const generateQrCode = (): { __html: string } => { const generateQrCode = (): { __html: string } => {
const qr = qrcode(0, "L"); const qr = qrcode(0, "L");
qr.addData(publicToken || ""); qr.addData(publicToken || "");


return { __html: qr.createImgTag(4) }; return { __html: qr.createImgTag(4) };
}; };
if (publicToken && (!userAddress || isNaN(userBalance))) { if (publicToken && (!userAddress || isNaN(userBalance))) {
return ( return (
<div className="main-box"> <div className="main-box">
<h1>Taquito Boilerplate</h1>

<div id="dialog"> <div id="dialog">
<header>Try the Taquito Boilerplate App!</header>
<div id="content"> <div id="content">
<p className="text-align-center">
<i className="fas fa-broadcast-tower"></i>&nbsp; Connecting to
your wallet
</p>
<div <div
dangerouslySetInnerHTML={generateQrCode()} dangerouslySetInnerHTML={generateQrCode()}
className="text-align-center" className="text-align-center"
</p> </p>
</div> </div>
</div> </div>
<div id="footer">
<img src="built-with-taquito.png" alt="Built with Taquito" />
</div>
</div> </div>
); );
} else if (userAddress && !isNaN(userBalance)) { } else if (userAddress && !isNaN(userBalance)) {
return ( return (
<div className="main-box"> <div className="main-box">


<div id="tabs">
<div
id="transfer"
className={activeTab === "transfer" ? "active" : ""}
onClick={() => setActiveTab("transfer")}
>
Make a transfer
</div>
<div
id="contract"
className={activeTab === "contract" ? "active" : ""}
onClick={() => setActiveTab("contract")}
>
Interact with a contract
</div>
</div>
<div id="dialog">


<BuyButton
<div id="dialog">
<BuyButton
Tezos={Tezos} Tezos={Tezos}
sender={userAddress} sender={userAddress}
FA2address={fa2Contract} FA2address={fa2Contract}
receiver={receiver} receiver={receiver}
amount={amount}
amountUsd={amount}
/> />
<DisconnectButton <DisconnectButton
wallet={wallet} wallet={wallet}
setBeaconConnection={setBeaconConnection} setBeaconConnection={setBeaconConnection}
/> />
</div> </div>
<div id="footer">
<img src="built-with-taquito.png" alt="Built with Taquito" />
</div>
</div> </div>
); );
} else if (!publicToken && !userAddress && !userBalance) { } else if (!publicToken && !userAddress && !userBalance) {
return ( return (
<div className="main-box"> <div className="main-box">

<div id="dialog"> <div id="dialog">

<ConnectButton <ConnectButton
Tezos={Tezos} Tezos={Tezos}
setContract={setContract} setContract={setContract}
wallet={wallet} wallet={wallet}
/> />
</div> </div>
<div id="footer">
<img src="built-with-taquito.png" alt="Built with Taquito" />
</div>
</div> </div>
); );
} else { } else {

+ 65
- 38
src/components/BuyButton.tsx View File

import React, { Dispatch, SetStateAction, useState, useEffect } from "react"; import React, { Dispatch, SetStateAction, useState, useEffect } from "react";
import { TezosToolkit } from "@taquito/taquito"; import { TezosToolkit } from "@taquito/taquito";
import { BeaconWallet } from "@taquito/beacon-wallet"; import { BeaconWallet } from "@taquito/beacon-wallet";
import config from "./../config";




interface BuyButtonProps {
Tezos: TezosToolkit,
FA2address: string,
sender: string,
receiver: string,
amountUsd: number
}

interface CoinGeckoPrice {
last_updated_at: number;
usd: number;
usd_24h_change: number;
usd_24h_vol: number;
usd_market_cap: number;
}

const BuyButton = ({ const BuyButton = ({
Tezos, Tezos,
FA2address, FA2address,
sender, sender,
receiver, receiver,
amount
}: {
Tezos: TezosToolkit,
FA2address: string,
sender: string,
receiver: string,
amount: number
}): JSX.Element => {
amountUsd
}: BuyButtonProps ): JSX.Element => {
const getTokenPrice = async (): Promise<void> => {
const currency = "USD";
if (currency == "USD") {
const timestamp = new Date();
timestamp.setDate(timestamp.getDate() - 1);
const timestampString = timestamp.toISOString();
fetch(`https://api.tzkt.io/v1/contracts/KT1F3BqwEAoa2koYX4Hz7zJ8xfGSxxAGVT8t/storage/history?limit=700`)
.then(res => res.json())
.then(data => data.find((item: any) => item.timestamp < timestampString))
.then(item => {
if (item && item.value) {
console.log(item) ;
//const tez_pool = parseFloat(item.value.storage.tez_pool) / tezMultiplyer;
//const token_pool = parseFloat(item.value.storage.token_pool) / tokenMultiplyer;
//const price_yesterday = tez_pool / token_pool;
//setTokenTezPriceYesterday(price_yesterday);
}
})
const [tezUsd, setTezUsd] = useState<CoinGeckoPrice>(config.defaultTezPrice);
const [tezPool, setTezPool] = useState<number>(0);
const [tokenPool, setTokenPool] = useState<number>(0);
const [fiat2Token, setFiat2Token] = useState<number>(0);
useEffect(() => {
getTezosPrice();
getPoolSizes();
}, [])
// calculate Price in Token
useEffect(() => {
if( tezUsd.usd > 0 &&
tezPool > 0 &&
tokenPool > 0 ) {
setFiat2Token( tokenPool / tezPool / tezUsd.usd)
} }
}, [tezUsd,tezPool,tokenPool])
const getTezosPrice = async (): Promise<void> => {
// https://www.coingecko.com/en/api#explore-api
fetch("https://api.coingecko.com/api/v3/simple/price?ids=tezos&vs_currencies=usd&include_market_cap=true&include_24hr_vol=true&include_24hr_change=true&include_last_updated_at=true")
.then(res => res.json())
.then(res => setTezUsd(res.tezos))
}
const getPoolSizes = async (): Promise<void> => {
fetch(`https://api.tzkt.io/v1/contracts/KT1F3BqwEAoa2koYX4Hz7zJ8xfGSxxAGVT8t/storage`)
.then(res => res.json())
.then(item => {
setTezPool( parseFloat(item.storage.tez_pool) );// / tezMultiplyer;
setTokenPool( (item.storage.token_pool) );// / tokenMultiplyer;
}
)
}
const buyMethod = async (): Promise<void> => {
makePayment();
} }
const makePayment = async (): Promise<void> => { const makePayment = async (): Promise<void> => {
txs: [{ txs: [{
to_: receiver, to_: receiver,
token_id:0, token_id:0,
amount: amount
amount: amountUsd * fiat2Token
}] }]
} }
]; ];
await op.confirmation(); await op.confirmation();
} }
return (
return (<div><div className="sd">1 Us Dollar if worth{fiat2Token} Moneyheros</div>
<div className="buttons"> <div className="buttons">
<button <button
className="myButton"
onClick={getTokenPrice}
>Transfer {amount}&nbsp;
µ-tokens from: &nbsp;
{sender}
&nbsp;to:&nbsp;
{receiver}
</button>
</div>
className="button"
onClick={buyMethod}
>Pay {amountUsd * fiat2Token} Moneyherocoins
</button>
</div></div>
); );
}; };



+ 36
- 0
src/config.ts View File

import { NetworkType } from "@airgap/beacon-sdk";

const config = {
network: NetworkType.MAINNET,
rpcUrl: "",
defaultTezPrice: {
last_updated_at: 1621264908,
usd: 5.24,
usd_24h_change: -10.701803732742505,
usd_24h_vol: 410319278.74019855,
usd_market_cap: 4367588701.058423,
},
defaultTokenPrice: {
last_updated_at: 1621264908,
usd: 0.0,
usd_24h_change: 0,
usd_24h_vol: 0,
usd_market_cap: 0,
},
storageLimitSurcharge: 1.2, // multiplier
lpTokenDecimals: 1000000
};

switch (config.network) {
case NetworkType.MAINNET:
config.rpcUrl = "https://mainnet.api.tez.ie";
break;
case NetworkType.FLORENCENET:
config.rpcUrl = "https://rpc.florence.tzstats.com/";
break;
case NetworkType.CUSTOM:
config.rpcUrl = "http://localhost:8732/";
break;
}

export default config;

+ 15
- 6
src/index.js View File

import App from "./App.tsx"; import App from "./App.tsx";
import * as serviceWorker from "./serviceWorker"; import * as serviceWorker from "./serviceWorker";


ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById("root")
);
const appNode = document.getElementById("root");
if (appNode) {
ReactDOM.render(
<React.StrictMode>
<App
swapContract={appNode.dataset.swapContract}
fa2Contract={appNode.dataset.fa2Contract}
receiver={appNode.dataset.receiver}
amount={appNode.dataset.amount}
/>
</React.StrictMode>,
document.getElementById("root")
);
}


// If you want your app to work offline and load faster, you can change // If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls. // unregister() to register() below. Note this comes with some pitfalls.

Loading…
Cancel
Save