~Webmodule~ to include a "Buy button" with streamlined crypto-pay transaction.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.9KB

  1. import React, { Dispatch, SetStateAction, useState, useEffect } from "react";
  2. import { TezosToolkit } from "@taquito/taquito";
  3. import { BeaconWallet } from "@taquito/beacon-wallet";
  4. import config from "./../config";
  5. interface BuyButtonProps {
  6. Tezos: TezosToolkit,
  7. FA2address: string,
  8. sender: string,
  9. receiver: string,
  10. amountUsd: number
  11. }
  12. interface CoinGeckoPrice {
  13. last_updated_at: number;
  14. usd: number;
  15. usd_24h_change: number;
  16. usd_24h_vol: number;
  17. usd_market_cap: number;
  18. }
  19. const BuyButton = ({
  20. Tezos,
  21. FA2address,
  22. sender,
  23. receiver,
  24. amountUsd
  25. }: BuyButtonProps ): JSX.Element => {
  26. const [tezUsd, setTezUsd] = useState<CoinGeckoPrice>(config.defaultTezPrice);
  27. const [tezPool, setTezPool] = useState<number>(0);
  28. const [tokenPool, setTokenPool] = useState<number>(0);
  29. const [fiat2Token, setFiat2Token] = useState<number>(0);
  30. useEffect(() => {
  31. getTezosPrice();
  32. getPoolSizes();
  33. }, [])
  34. // calculate Price in Token
  35. useEffect(() => {
  36. if( tezUsd.usd > 0 &&
  37. tezPool > 0 &&
  38. tokenPool > 0 ) {
  39. setFiat2Token( tokenPool / tezPool / tezUsd.usd)
  40. }
  41. }, [tezUsd,tezPool,tokenPool])
  42. const getTezosPrice = async (): Promise<void> => {
  43. // https://www.coingecko.com/en/api#explore-api
  44. 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")
  45. .then(res => res.json())
  46. .then(res => setTezUsd(res.tezos))
  47. }
  48. const getPoolSizes = async (): Promise<void> => {
  49. fetch(`https://api.tzkt.io/v1/contracts/KT1F3BqwEAoa2koYX4Hz7zJ8xfGSxxAGVT8t/storage`)
  50. .then(res => res.json())
  51. .then(item => {
  52. setTezPool( parseFloat(item.storage.tez_pool) );// / tezMultiplyer;
  53. setTokenPool( (item.storage.token_pool) );// / tokenMultiplyer;
  54. }
  55. )
  56. }
  57. const buyMethod = async (): Promise<void> => {
  58. makePayment();
  59. }
  60. const makePayment = async (): Promise<void> => {
  61. const contract = await Tezos.wallet.at(FA2address);
  62. const transfer_params = [
  63. {
  64. from_: sender,
  65. txs: [{
  66. to_: receiver,
  67. token_id:0,
  68. amount: amountUsd * fiat2Token
  69. }]
  70. }
  71. ];
  72. const op = await contract.methods.transfer(transfer_params).send();
  73. await op.confirmation();
  74. }
  75. return (<div><div className="sd">1 Us Dollar if worth{fiat2Token} Moneyheros</div>
  76. <div className="buttons">
  77. <button
  78. className="button"
  79. onClick={buyMethod}
  80. >Pay {amountUsd * fiat2Token} Moneyherocoins
  81. </button>
  82. </div></div>
  83. );
  84. };
  85. export default BuyButton;