diff --git a/packages/nextjs/app/faucet/page.tsx b/packages/nextjs/app/faucet/page.tsx new file mode 100644 index 0000000..c9c2b1e --- /dev/null +++ b/packages/nextjs/app/faucet/page.tsx @@ -0,0 +1,686 @@ +"use client"; + +import Link from "next/link"; +import { useMoonWalletContext } from "../../components/ScaffoldEthAppWithProviders"; +import { useMoonSDK } from "../../hooks/moon"; +import type { NextPage } from "next"; +import { useAccount, useContractWrite } from "wagmi"; +import { useTransactor } from "~~/hooks/scaffold-eth"; + +const Faucet: NextPage = () => { + const writeTx = useTransactor(); + const connectedAddress: string = useAccount()?.address ?? ""; + const { moonWallet } = useMoonWalletContext(); + const account = connectedAddress || moonWallet; + const { contractCall } = useMoonSDK(); + + const wrapAbi = [ + { + constant: true, + inputs: [], + name: "name", + outputs: [{ name: "", type: "string" }], + payable: false, + stateMutability: "view", + type: "function", + }, + { + constant: false, + inputs: [ + { name: "guy", type: "address" }, + { name: "wad", type: "uint256" }, + ], + name: "approve", + outputs: [{ name: "", type: "bool" }], + payable: false, + stateMutability: "nonpayable", + type: "function", + }, + { + constant: true, + inputs: [], + name: "totalSupply", + outputs: [{ name: "", type: "uint256" }], + payable: false, + stateMutability: "view", + type: "function", + }, + { + constant: false, + inputs: [ + { name: "src", type: "address" }, + { name: "dst", type: "address" }, + { name: "wad", type: "uint256" }, + ], + name: "transferFrom", + outputs: [{ name: "", type: "bool" }], + payable: false, + stateMutability: "nonpayable", + type: "function", + }, + { + constant: false, + inputs: [{ name: "wad", type: "uint256" }], + name: "withdraw", + outputs: [], + payable: false, + stateMutability: "nonpayable", + type: "function", + }, + { + constant: true, + inputs: [], + name: "decimals", + outputs: [{ name: "", type: "uint8" }], + payable: false, + stateMutability: "view", + type: "function", + }, + { + constant: true, + inputs: [{ name: "", type: "address" }], + name: "balanceOf", + outputs: [{ name: "", type: "uint256" }], + payable: false, + stateMutability: "view", + type: "function", + }, + { + constant: true, + inputs: [], + name: "symbol", + outputs: [{ name: "", type: "string" }], + payable: false, + stateMutability: "view", + type: "function", + }, + { + constant: false, + inputs: [ + { name: "dst", type: "address" }, + { name: "wad", type: "uint256" }, + ], + name: "transfer", + outputs: [{ name: "", type: "bool" }], + payable: false, + stateMutability: "nonpayable", + type: "function", + }, + { + constant: false, + inputs: [], + name: "deposit", + outputs: [], + payable: true, + stateMutability: "payable", + type: "function", + }, + { + constant: true, + inputs: [ + { name: "", type: "address" }, + { name: "", type: "address" }, + ], + name: "allowance", + outputs: [{ name: "", type: "uint256" }], + payable: false, + stateMutability: "view", + type: "function", + }, + { payable: true, stateMutability: "payable", type: "fallback" }, + { + anonymous: false, + inputs: [ + { indexed: true, name: "src", type: "address" }, + { indexed: true, name: "guy", type: "address" }, + { indexed: false, name: "wad", type: "uint256" }, + ], + name: "Approval", + type: "event", + }, + { + anonymous: false, + inputs: [ + { indexed: true, name: "src", type: "address" }, + { indexed: true, name: "dst", type: "address" }, + { indexed: false, name: "wad", type: "uint256" }, + ], + name: "Transfer", + type: "event", + }, + { + anonymous: false, + inputs: [ + { indexed: true, name: "dst", type: "address" }, + { indexed: false, name: "wad", type: "uint256" }, + ], + name: "Deposit", + type: "event", + }, + { + anonymous: false, + inputs: [ + { indexed: true, name: "src", type: "address" }, + { indexed: false, name: "wad", type: "uint256" }, + ], + name: "Withdrawal", + type: "event", + }, + ]; + const routerAbi = [ + { + inputs: [ + { internalType: "address", name: "_factory", type: "address" }, + { internalType: "address", name: "_WETH", type: "address" }, + ], + stateMutability: "nonpayable", + type: "constructor", + }, + { + inputs: [], + name: "WETH", + outputs: [{ internalType: "address", name: "", type: "address" }], + stateMutability: "view", + type: "function", + }, + { + inputs: [ + { internalType: "address", name: "tokenA", type: "address" }, + { internalType: "address", name: "tokenB", type: "address" }, + { internalType: "uint256", name: "amountADesired", type: "uint256" }, + { internalType: "uint256", name: "amountBDesired", type: "uint256" }, + { internalType: "uint256", name: "amountAMin", type: "uint256" }, + { internalType: "uint256", name: "amountBMin", type: "uint256" }, + { internalType: "address", name: "to", type: "address" }, + { internalType: "uint256", name: "deadline", type: "uint256" }, + ], + name: "addLiquidity", + outputs: [ + { internalType: "uint256", name: "amountA", type: "uint256" }, + { internalType: "uint256", name: "amountB", type: "uint256" }, + { internalType: "uint256", name: "liquidity", type: "uint256" }, + ], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [ + { internalType: "address", name: "token", type: "address" }, + { internalType: "uint256", name: "amountTokenDesired", type: "uint256" }, + { internalType: "uint256", name: "amountTokenMin", type: "uint256" }, + { internalType: "uint256", name: "amountETHMin", type: "uint256" }, + { internalType: "address", name: "to", type: "address" }, + { internalType: "uint256", name: "deadline", type: "uint256" }, + ], + name: "addLiquidityETH", + outputs: [ + { internalType: "uint256", name: "amountToken", type: "uint256" }, + { internalType: "uint256", name: "amountETH", type: "uint256" }, + { internalType: "uint256", name: "liquidity", type: "uint256" }, + ], + stateMutability: "payable", + type: "function", + }, + { + inputs: [], + name: "factory", + outputs: [{ internalType: "address", name: "", type: "address" }], + stateMutability: "view", + type: "function", + }, + { + inputs: [ + { internalType: "uint256", name: "amountOut", type: "uint256" }, + { internalType: "uint256", name: "reserveIn", type: "uint256" }, + { internalType: "uint256", name: "reserveOut", type: "uint256" }, + ], + name: "getAmountIn", + outputs: [{ internalType: "uint256", name: "amountIn", type: "uint256" }], + stateMutability: "pure", + type: "function", + }, + { + inputs: [ + { internalType: "uint256", name: "amountIn", type: "uint256" }, + { internalType: "uint256", name: "reserveIn", type: "uint256" }, + { internalType: "uint256", name: "reserveOut", type: "uint256" }, + ], + name: "getAmountOut", + outputs: [{ internalType: "uint256", name: "amountOut", type: "uint256" }], + stateMutability: "pure", + type: "function", + }, + { + inputs: [ + { internalType: "uint256", name: "amountOut", type: "uint256" }, + { internalType: "address[]", name: "path", type: "address[]" }, + ], + name: "getAmountsIn", + outputs: [{ internalType: "uint256[]", name: "amounts", type: "uint256[]" }], + stateMutability: "view", + type: "function", + }, + { + inputs: [ + { internalType: "uint256", name: "amountIn", type: "uint256" }, + { internalType: "address[]", name: "path", type: "address[]" }, + ], + name: "getAmountsOut", + outputs: [{ internalType: "uint256[]", name: "amounts", type: "uint256[]" }], + stateMutability: "view", + type: "function", + }, + { + inputs: [ + { internalType: "uint256", name: "amountA", type: "uint256" }, + { internalType: "uint256", name: "reserveA", type: "uint256" }, + { internalType: "uint256", name: "reserveB", type: "uint256" }, + ], + name: "quote", + outputs: [{ internalType: "uint256", name: "amountB", type: "uint256" }], + stateMutability: "pure", + type: "function", + }, + { + inputs: [ + { internalType: "address", name: "tokenA", type: "address" }, + { internalType: "address", name: "tokenB", type: "address" }, + { internalType: "uint256", name: "liquidity", type: "uint256" }, + { internalType: "uint256", name: "amountAMin", type: "uint256" }, + { internalType: "uint256", name: "amountBMin", type: "uint256" }, + { internalType: "address", name: "to", type: "address" }, + { internalType: "uint256", name: "deadline", type: "uint256" }, + ], + name: "removeLiquidity", + outputs: [ + { internalType: "uint256", name: "amountA", type: "uint256" }, + { internalType: "uint256", name: "amountB", type: "uint256" }, + ], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [ + { internalType: "address", name: "token", type: "address" }, + { internalType: "uint256", name: "liquidity", type: "uint256" }, + { internalType: "uint256", name: "amountTokenMin", type: "uint256" }, + { internalType: "uint256", name: "amountETHMin", type: "uint256" }, + { internalType: "address", name: "to", type: "address" }, + { internalType: "uint256", name: "deadline", type: "uint256" }, + ], + name: "removeLiquidityETH", + outputs: [ + { internalType: "uint256", name: "amountToken", type: "uint256" }, + { internalType: "uint256", name: "amountETH", type: "uint256" }, + ], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [ + { internalType: "address", name: "token", type: "address" }, + { internalType: "uint256", name: "liquidity", type: "uint256" }, + { internalType: "uint256", name: "amountTokenMin", type: "uint256" }, + { internalType: "uint256", name: "amountETHMin", type: "uint256" }, + { internalType: "address", name: "to", type: "address" }, + { internalType: "uint256", name: "deadline", type: "uint256" }, + ], + name: "removeLiquidityETHSupportingFeeOnTransferTokens", + outputs: [{ internalType: "uint256", name: "amountETH", type: "uint256" }], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [ + { internalType: "address", name: "token", type: "address" }, + { internalType: "uint256", name: "liquidity", type: "uint256" }, + { internalType: "uint256", name: "amountTokenMin", type: "uint256" }, + { internalType: "uint256", name: "amountETHMin", type: "uint256" }, + { internalType: "address", name: "to", type: "address" }, + { internalType: "uint256", name: "deadline", type: "uint256" }, + { internalType: "bool", name: "approveMax", type: "bool" }, + { internalType: "uint8", name: "v", type: "uint8" }, + { internalType: "bytes32", name: "r", type: "bytes32" }, + { internalType: "bytes32", name: "s", type: "bytes32" }, + ], + name: "removeLiquidityETHWithPermit", + outputs: [ + { internalType: "uint256", name: "amountToken", type: "uint256" }, + { internalType: "uint256", name: "amountETH", type: "uint256" }, + ], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [ + { internalType: "address", name: "token", type: "address" }, + { internalType: "uint256", name: "liquidity", type: "uint256" }, + { internalType: "uint256", name: "amountTokenMin", type: "uint256" }, + { internalType: "uint256", name: "amountETHMin", type: "uint256" }, + { internalType: "address", name: "to", type: "address" }, + { internalType: "uint256", name: "deadline", type: "uint256" }, + { internalType: "bool", name: "approveMax", type: "bool" }, + { internalType: "uint8", name: "v", type: "uint8" }, + { internalType: "bytes32", name: "r", type: "bytes32" }, + { internalType: "bytes32", name: "s", type: "bytes32" }, + ], + name: "removeLiquidityETHWithPermitSupportingFeeOnTransferTokens", + outputs: [{ internalType: "uint256", name: "amountETH", type: "uint256" }], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [ + { internalType: "address", name: "tokenA", type: "address" }, + { internalType: "address", name: "tokenB", type: "address" }, + { internalType: "uint256", name: "liquidity", type: "uint256" }, + { internalType: "uint256", name: "amountAMin", type: "uint256" }, + { internalType: "uint256", name: "amountBMin", type: "uint256" }, + { internalType: "address", name: "to", type: "address" }, + { internalType: "uint256", name: "deadline", type: "uint256" }, + { internalType: "bool", name: "approveMax", type: "bool" }, + { internalType: "uint8", name: "v", type: "uint8" }, + { internalType: "bytes32", name: "r", type: "bytes32" }, + { internalType: "bytes32", name: "s", type: "bytes32" }, + ], + name: "removeLiquidityWithPermit", + outputs: [ + { internalType: "uint256", name: "amountA", type: "uint256" }, + { internalType: "uint256", name: "amountB", type: "uint256" }, + ], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [ + { internalType: "uint256", name: "amountOut", type: "uint256" }, + { internalType: "address[]", name: "path", type: "address[]" }, + { internalType: "address", name: "to", type: "address" }, + { internalType: "uint256", name: "deadline", type: "uint256" }, + ], + name: "swapETHForExactTokens", + outputs: [{ internalType: "uint256[]", name: "amounts", type: "uint256[]" }], + stateMutability: "payable", + type: "function", + }, + { + inputs: [ + { internalType: "uint256", name: "amountOutMin", type: "uint256" }, + { internalType: "address[]", name: "path", type: "address[]" }, + { internalType: "address", name: "to", type: "address" }, + { internalType: "uint256", name: "deadline", type: "uint256" }, + ], + name: "swapExactETHForTokens", + outputs: [{ internalType: "uint256[]", name: "amounts", type: "uint256[]" }], + stateMutability: "payable", + type: "function", + }, + { + inputs: [ + { internalType: "uint256", name: "amountOutMin", type: "uint256" }, + { internalType: "address[]", name: "path", type: "address[]" }, + { internalType: "address", name: "to", type: "address" }, + { internalType: "uint256", name: "deadline", type: "uint256" }, + ], + name: "swapExactETHForTokensSupportingFeeOnTransferTokens", + outputs: [], + stateMutability: "payable", + type: "function", + }, + { + inputs: [ + { internalType: "uint256", name: "amountIn", type: "uint256" }, + { internalType: "uint256", name: "amountOutMin", type: "uint256" }, + { internalType: "address[]", name: "path", type: "address[]" }, + { internalType: "address", name: "to", type: "address" }, + { internalType: "uint256", name: "deadline", type: "uint256" }, + ], + name: "swapExactTokensForETH", + outputs: [{ internalType: "uint256[]", name: "amounts", type: "uint256[]" }], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [ + { internalType: "uint256", name: "amountIn", type: "uint256" }, + { internalType: "uint256", name: "amountOutMin", type: "uint256" }, + { internalType: "address[]", name: "path", type: "address[]" }, + { internalType: "address", name: "to", type: "address" }, + { internalType: "uint256", name: "deadline", type: "uint256" }, + ], + name: "swapExactTokensForETHSupportingFeeOnTransferTokens", + outputs: [], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [ + { internalType: "uint256", name: "amountIn", type: "uint256" }, + { internalType: "uint256", name: "amountOutMin", type: "uint256" }, + { internalType: "address[]", name: "path", type: "address[]" }, + { internalType: "address", name: "to", type: "address" }, + { internalType: "uint256", name: "deadline", type: "uint256" }, + ], + name: "swapExactTokensForTokens", + outputs: [{ internalType: "uint256[]", name: "amounts", type: "uint256[]" }], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [ + { internalType: "uint256", name: "amountIn", type: "uint256" }, + { internalType: "uint256", name: "amountOutMin", type: "uint256" }, + { internalType: "address[]", name: "path", type: "address[]" }, + { internalType: "address", name: "to", type: "address" }, + { internalType: "uint256", name: "deadline", type: "uint256" }, + ], + name: "swapExactTokensForTokensSupportingFeeOnTransferTokens", + outputs: [], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [ + { internalType: "uint256", name: "amountOut", type: "uint256" }, + { internalType: "uint256", name: "amountInMax", type: "uint256" }, + { internalType: "address[]", name: "path", type: "address[]" }, + { internalType: "address", name: "to", type: "address" }, + { internalType: "uint256", name: "deadline", type: "uint256" }, + ], + name: "swapTokensForExactETH", + outputs: [{ internalType: "uint256[]", name: "amounts", type: "uint256[]" }], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [ + { internalType: "uint256", name: "amountOut", type: "uint256" }, + { internalType: "uint256", name: "amountInMax", type: "uint256" }, + { internalType: "address[]", name: "path", type: "address[]" }, + { internalType: "address", name: "to", type: "address" }, + { internalType: "uint256", name: "deadline", type: "uint256" }, + ], + name: "swapTokensForExactTokens", + outputs: [{ internalType: "uint256[]", name: "amounts", type: "uint256[]" }], + stateMutability: "nonpayable", + type: "function", + }, + { stateMutability: "payable", type: "receive" }, + ]; + + const amountETH = 100000000000000000n; + const amountETHmin = 90000000000000000n; + const amountUSDC = 400000; + + const { writeAsync: wrap } = useContractWrite({ + abi: wrapAbi, + address: "0x9c3C9283D3e44854697Cd22D3Faa240Cfb032889", + functionName: "deposit", + value: amountETH, + }); + + const handleWrap = async () => { + try { + if (moonWallet) + await contractCall( + moonWallet, + "0x9c3C9283D3e44854697Cd22D3Faa240Cfb032889", + wrapAbi as any, + "deposit", + [], + Number(amountETH), + ); + else await writeTx(wrap, { blockConfirmations: 1 }); + } catch (e) { + console.log("Unexpected error in writeTx", e); + } + }; + + const { writeAsync: approveETH } = useContractWrite({ + abi: wrapAbi, + address: "0x9c3C9283D3e44854697Cd22D3Faa240Cfb032889", + functionName: "approve", + args: ["0x8954AfA98594b838bda56FE4C12a09D7739D179b", amountETH], + }); + + const handleApproveETH = async () => { + try { + if (moonWallet) + await contractCall(moonWallet, "0x9c3C9283D3e44854697Cd22D3Faa240Cfb032889", wrapAbi as any, "approve", [ + "0x8954AfA98594b838bda56FE4C12a09D7739D179b", + amountETH, + ]); + else await writeTx(approveETH, { blockConfirmations: 1 }); + } catch (e) { + console.log("Unexpected error in writeTx", e); + } + }; + + const { writeAsync: approveUSDC } = useContractWrite({ + abi: wrapAbi, + address: "0x9999f7Fea5938fD3b1E26A12c3f2fb024e194f97", + functionName: "approve", + args: ["0x8954AfA98594b838bda56FE4C12a09D7739D179b", amountUSDC], + }); + + const handleApproveUSDC = async () => { + try { + if (moonWallet) + await contractCall(moonWallet, "0x9999f7Fea5938fD3b1E26A12c3f2fb024e194f97", wrapAbi as any, "approve", [ + "0x8954AfA98594b838bda56FE4C12a09D7739D179b", + amountUSDC, + ]); + else await writeTx(approveUSDC, { blockConfirmations: 1 }); + } catch (e) { + console.log("Unexpected error in writeTx", e); + } + }; + + const { writeAsync: addLiquidity } = useContractWrite({ + abi: routerAbi, + address: "0x8954AfA98594b838bda56FE4C12a09D7739D179b", + functionName: "addLiquidity", + args: [ + "0x9999f7Fea5938fD3b1E26A12c3f2fb024e194f97", + "0x9c3C9283D3e44854697Cd22D3Faa240Cfb032889", + amountUSDC, + amountETH, + (amountUSDC * 9) / 10, + amountETHmin, + account, + 999999999999, + ], + }); + + const handleAddLiquidity = async () => { + try { + if (moonWallet) + await contractCall(moonWallet, "0x8954AfA98594b838bda56FE4C12a09D7739D179b", routerAbi as any, "addLiquidity", [ + "0x9999f7Fea5938fD3b1E26A12c3f2fb024e194f97", + "0x9c3C9283D3e44854697Cd22D3Faa240Cfb032889", + amountUSDC, + amountETH, + (amountUSDC * 9) / 10, + amountETHmin, + account, + 999999999999, + ]); + else await writeTx(addLiquidity, { blockConfirmations: 1 }); + } catch (e) { + console.log("Unexpected error in writeTx", e); + } + }; + + return ( + <> +
+
+

+ Get testnet LP Token (Polygon Mumbai) +

+
+
+

Get MATIC and USDC from the faucets

+

+ MATIC faucet: +
+ + https://faucet.polygon.technology/ + +
+
+ USDC faucet: +
+ + https://faucet.circle.com/ + +

+
+
+

Check the transactions on Polygonscan

+

Event listeners have not been implemented yet. Click the link to view your account on PolygonScan.

+ + Go to PolygonScan + +

You can also find the transaction statuses in your Browser wallet (if not using Moon).

+
+
+

Wrap 0.1 MATIC

+ +
+
+

Approve tokens to Uniswap V2 Router

+ + +
+
+

Add Liquidity to Uniswap V2 pool

+ +
+
+

Enter the test tournament

+ + Go to Tournaments page + +
+
+
+
+ + ); +}; + +export default Faucet; diff --git a/packages/nextjs/app/page.tsx b/packages/nextjs/app/page.tsx index 5cb5785..b8afaf3 100644 --- a/packages/nextjs/app/page.tsx +++ b/packages/nextjs/app/page.tsx @@ -1,6 +1,6 @@ import Link from "next/link"; import type { NextPage } from "next"; -import { GlobeAsiaAustraliaIcon, PaperAirplaneIcon, ScissorsIcon } from "@heroicons/react/24/outline"; +import { BanknotesIcon, BookOpenIcon, PuzzlePieceIcon } from "@heroicons/react/24/outline"; const Home: NextPage = () => { return ( @@ -18,7 +18,7 @@ const Home: NextPage = () => {
- +

{/* */} About @@ -26,19 +26,19 @@ const Home: NextPage = () => {

- +

- - PLAY + + Play {" "}

- +

- {/* */} - LEADERBOARD - {/* {" "} */} + + LP Token Faucet + {" "}

diff --git a/packages/nextjs/components/Header.tsx b/packages/nextjs/components/Header.tsx index 9731897..3ba730a 100644 --- a/packages/nextjs/components/Header.tsx +++ b/packages/nextjs/components/Header.tsx @@ -5,7 +5,7 @@ import Image from "next/image"; import Link from "next/link"; import { usePathname } from "next/navigation"; import { WalletButton } from "./moon/walletButton"; -import { Bars3Icon, BugAntIcon } from "@heroicons/react/24/outline"; +import { BanknotesIcon, Bars3Icon, HomeIcon, PuzzlePieceIcon } from "@heroicons/react/24/outline"; import { FaucetButton, RainbowKitCustomConnectButton } from "~~/components/scaffold-eth"; import { useOutsideClick } from "~~/hooks/scaffold-eth"; @@ -19,15 +19,17 @@ export const menuLinks: HeaderMenuLink[] = [ { label: "Home", href: "/", + icon: , }, { label: "Tournaments", href: "/tournaments", + icon: , }, { - label: "Debug Contracts", - href: "/debug", - icon: , + label: "Faucet", + href: "/faucet", + icon: , }, ]; diff --git a/packages/nextjs/components/leaderboard/item.tsx b/packages/nextjs/components/leaderboard/item.tsx index 2b83244..db0aad9 100644 --- a/packages/nextjs/components/leaderboard/item.tsx +++ b/packages/nextjs/components/leaderboard/item.tsx @@ -1,4 +1,5 @@ // import { formatUnits } from "viem"; +import { useMoonWalletContext } from "../ScaffoldEthAppWithProviders"; import { useContractRead } from "wagmi"; import { useAccount } from "wagmi"; import { SparklesIcon, UserIcon } from "@heroicons/react/24/outline"; @@ -8,6 +9,8 @@ import DeployedContracts from "~~/contracts/deployedContracts"; export const Item = ({ tournament, player, score }: { tournament: string; player: string; score: number }) => { const connectedAddress: string = useAccount()?.address ?? ""; + const { moonWallet } = useMoonWalletContext(); + const account = connectedAddress || moonWallet; const chainId = 80001; const { data: playerRank, isLoading } = useContractRead({ @@ -38,7 +41,7 @@ export const Item = ({ tournament, player, score }: { tournament: string; player
{playerRank[0].toString()}
- {player === connectedAddress ? ( + {player === account ? (
You diff --git a/packages/nextjs/components/moon/sign.tsx b/packages/nextjs/components/moon/sign.tsx index 517a035..df951d3 100644 --- a/packages/nextjs/components/moon/sign.tsx +++ b/packages/nextjs/components/moon/sign.tsx @@ -1,9 +1,10 @@ import { useState } from "react"; import { useMoonWalletContext } from "../../components/ScaffoldEthAppWithProviders"; import { useMoonSDK } from "../../hooks/moon"; +import { Spinner } from "../Spinner"; //import { useCall } from "~~/hooks/call"; import { CreateAccountInput } from "@moonup/moon-api"; -import { formatEther } from "viem"; +import { formatEther, getAddress } from "viem"; //import { erc20ABI } from "wagmi"; import { ClipboardIcon } from "@heroicons/react/24/outline"; import { InputBase, InputPwd } from "~~/components/scaffold-eth"; @@ -15,14 +16,17 @@ export const Sign = () => { const [answer, setAnswer] = useState(""); const { moonWallet, setMoonWallet } = useMoonWalletContext(); const [balance, setBalance] = useState(0n); + const [action, setAction] = useState(""); const handleSignup = async (event: React.MouseEvent) => { event.preventDefault(); setAnswer(""); + setAction("signup"); try { // Check if Moon SDK is properly initialized and user is authenticated if (!moon) { console.error("User not authenticated"); + setAction(""); return; } @@ -47,15 +51,18 @@ export const Sign = () => { console.error(error); if (error) setAnswer(error.error.message); } + setAction(""); }; const handleLogin = async (event: React.MouseEvent) => { event.preventDefault(); setAnswer(""); + setAction("login"); try { // Check if Moon SDK is properly initialized and user is authenticated if (!moon) { console.error("User not authenticated"); + setAction(""); return; } @@ -77,7 +84,7 @@ export const Sign = () => { console.log(message2); if (message2) { addr = message2; - setMoonWallet(addr.data.keys[0]); + setMoonWallet(getAddress(addr.data.keys[0])); } const message4 = await moon.getAccountsSDK().getBalance(addr.data.keys[0], { chainId: "80001" }); @@ -90,6 +97,7 @@ export const Sign = () => { console.error(error); setAnswer(error.error.message); } + setAction(""); }; const handleDisconnect = async () => { @@ -135,11 +143,11 @@ export const Sign = () => {
- -
{answer}
diff --git a/packages/nextjs/hooks/call.ts b/packages/nextjs/hooks/call.ts deleted file mode 100644 index 2d486d1..0000000 --- a/packages/nextjs/hooks/call.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { Transaction } from "@moonup/moon-api"; -import { MoonSDK } from "@moonup/moon-sdk"; -import { encodeFunctionData } from "viem"; - -interface CallProps { - moon: MoonSDK | null; - moonWallet: string; - contract: string; - abi: any; - functionName: string; - args: any; -} - -export async function useCall(props: CallProps): Promise { - if (!props.moon) { - console.error("User not authenticated"); - return; - } - - // Define a type guard function to check if an object conforms to the Transaction interface - function isTransaction(obj: any): obj is Transaction { - return ( - obj && typeof obj.userop_transaction === "string" && Array.isArray(obj.transactions) - // Add more checks for other properties if necessary - ); - } - - const encodedData = encodeFunctionData({ - abi: props.abi, - functionName: props.functionName, - args: props.args, - }); - - const data = { - to: props.contract, - data: encodedData, - chain_id: "80001", - encoding: "utf-8", - }; - - const rawTx = await props.moon.getAccountsSDK().signTransaction(props.moonWallet, data); - - console.log(rawTx); - if (isTransaction(rawTx.data.data)) { - const res: Transaction = rawTx.data.data; - if (res.transactions) { - const raw = res.transactions[0].raw_transaction || ""; - const tx = await props.moon.getAccountsSDK().broadcastTx(props.moonWallet, { - chainId: "80001", - rawTransaction: raw, - }); - console.log(tx); - } - } -} diff --git a/packages/nextjs/hooks/moon.ts b/packages/nextjs/hooks/moon.ts index 5262a5a..a66714d 100644 --- a/packages/nextjs/hooks/moon.ts +++ b/packages/nextjs/hooks/moon.ts @@ -16,7 +16,14 @@ interface MoonSDKHook { signMessage: (msg: string) => Promise; updateToken: (token: string) => Promise; updateRefreshToken: (token: string) => Promise; - contractCall: (moonWallet: string, contract: string, abi: any[], functionName: string, args: any[]) => Promise; + contractCall: ( + moonWallet: string, + contract: string, + abi: any[], + functionName: string, + args: any[], + value?: number, + ) => Promise; // signTransaction: (transaction: TransactionResponse) => Promise; // Add other methods as needed } @@ -77,7 +84,14 @@ export function useMoonSDK(): MoonSDKHook { } }; - const contractCall = async (moonWallet: string, contract: string, abi: any[], functionName: string, args: any[]) => { + const contractCall = async ( + moonWallet: string, + contract: string, + abi: any[], + functionName: string, + args: any[], + value?: number, + ) => { if (!moon) { console.error("User not authenticated"); return; @@ -95,6 +109,7 @@ export function useMoonSDK(): MoonSDKHook { chain_id: "80001", encoding: "utf-8", gas: "1000000", + value: value ? value.toString() : "0", }; const rawTx = await moon.getAccountsSDK().signTransaction(moonWallet, data);