UNPKG

@moveflow/widget

Version:

> ⚠️ **This is a testnet version** of the MoveFlow Checkout Widget. It is intended for development and testing purposes only. Do not use for mainnet payments.

133 lines (132 loc) 4.98 kB
"use client"; import { jsx as _jsx } from "react/jsx-runtime"; import { useEffect, useState, createContext } from "react"; import netConfApt from "../../config/aptos/aptos-config"; const INTERVALS = { day: 1000 * 60 * 60 * 24, month: 1000 * 60 * 60 * 24 * 30, year: 1000 * 60 * 60 * 24 * 365, }; const convertRateTypeToSeconds = (rateType) => { const selectedInterval = INTERVALS[rateType || "day"]; return selectedInterval ? selectedInterval / 1000 : 0; }; export const AptosContext = createContext(null); export const AptosContextProvider = ({ children, ...props }) => { const [address, setAddress] = useState(null); const [isConnectedAptos, setIsconnectedAptos] = useState(false); const [wallet, setWallet] = useState(""); console.log("AptosContextProvider: Rendering"); useEffect(() => { const checkConnection = async () => { const isConnected = await checkIsConnected(wallet); setIsconnectedAptos(isConnected); }; checkConnection(); }, [wallet]); const handleDisconnect = async () => { try { if (wallet === "petra") await window.aptos.disconnect(); else if (wallet === "martian") await window.martian.disconnect(); setWallet(""); checkIsConnected(wallet); } catch (e) { console.log(e); } }; const createSubscription = async (payment) => { try { const startTimeStr = BigInt(payment.startTime).toString(); const stopTimeStr = BigInt(payment.endTime).toString(); if (payment.endTime <= payment.startTime) { throw new Error("Stop time must be greater than start time"); } const decimal = 8; const depositAmount = payment.amountType === "fixed" ? BigInt(payment.streamRate * 10 ** Number(decimal)) : BigInt(payment.depositAmount * 10 ** Number(decimal)); const transaction = { type: "entry_function_payload", function: `${netConfApt.contract}::subscription::create`, arguments: [ payment.receiver, depositAmount.toString(), startTimeStr, stopTimeStr, convertRateTypeToSeconds(payment.rateType), payment.amountType === "fixed" ? "10000000" : "0", ], type_arguments: ["0x1::aptos_coin::AptosCoin"], }; const res = await window.aptos.signAndSubmitTransaction(transaction); console.log(res.hash); } catch (error) { console.error("Error creating subscription:", error); throw error; } }; const handleConnect = async (wallet) => { try { if (wallet === "petra") { await window.aptos.connect(); } else if (wallet === "martian") { await window.martian.connect(); } setWallet(wallet); // Wait for the connection status to be checked const isConnected = await checkIsConnected(wallet); // Ensure that isConnected is correctly set console.log("isConnected after connection:", isConnected); } catch (e) { console.log(e); } }; const checkIsConnected = async (wallet) => { try { let isConnectedAptos; if (wallet === "petra") { isConnectedAptos = await window.aptos.isConnected(); setIsconnectedAptos(isConnectedAptos); } else if (wallet === "martian") { isConnectedAptos = await window.martian.isConnected(); setIsconnectedAptos(isConnectedAptos); } return isConnectedAptos; } catch (error) { console.error("Error checking connection:", error); setIsconnectedAptos(false); // Set isConnectedAptos to false in case of an error return false; } }; useEffect(() => { if (isConnectedAptos && wallet === "petra") { window?.aptos.account().then((data) => { setAddress(data.address); }); } else if (isConnectedAptos && wallet === "martian") { window?.martian.account().then((data) => { setAddress(data.address); }); } else { setAddress(null); } }, [isConnectedAptos, wallet]); const datacontext = { handleConnect: handleConnect, handleDisconnect: handleDisconnect, createSubscription: createSubscription, address: address, isConnectedAptos: isConnectedAptos, }; return (_jsx(AptosContext.Provider, { value: datacontext, children: children })); };