From 1fff7bf0a6d9ae2befe1c5bb52a0b43a6719bbdf Mon Sep 17 00:00:00 2001 From: Adw8 Date: Wed, 7 Aug 2024 17:22:34 +0530 Subject: [PATCH] Display validator message on page --- .env.example | 3 +- package.json | 2 + src/pages/ConnectWallet.tsx | 2 +- src/pages/CreateValidator.tsx | 135 +++++++++++++++++++++++++++++++++- 4 files changed, 137 insertions(+), 5 deletions(-) diff --git a/.env.example b/.env.example index 22cb233..207b2cd 100644 --- a/.env.example +++ b/.env.example @@ -3,7 +3,8 @@ REACT_APP_ETHEREUM_MAINNET_CHAIN_ID=1 REACT_APP_LACONICD_CHAIN_ID=laconic_9000-1 REACT_APP_REGISTRY_GQL_ENDPOINT=http://localhost:9473/api REACT_APP_LACONICD_RPC_ENDPOINT=http://localhost:26657 -REACT_APP_LACONICD_DENOM=alnt REACT_APP_FAUCET_ENDPOINT=http://localhost:4000 REACT_APP_WALLET_META_URL=http://localhost:3000 REACT_APP_SUMSUB_API_ENDPOINT= +REACT_APP_STAKING_AMOUNT=1000000000000000 +REACT_APP_LACONICD_DENOM=alnt diff --git a/package.json b/package.json index 7a76c72..0a61602 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,8 @@ "private": true, "dependencies": { "@cerc-io/registry-sdk": "^0.2.5", + "@cosmjs/encoding": "^0.32.4", + "@cosmjs/proto-signing": "^0.32.4", "@cosmjs/stargate": "^0.32.4", "@emotion/react": "^11.11.4", "@emotion/styled": "^11.11.0", diff --git a/src/pages/ConnectWallet.tsx b/src/pages/ConnectWallet.tsx index 5975303..aabaa2e 100644 --- a/src/pages/ConnectWallet.tsx +++ b/src/pages/ConnectWallet.tsx @@ -1,5 +1,5 @@ import React, { useEffect } from "react"; -import {useLocation, useNavigate, useParams } from "react-router-dom"; +import {useNavigate, useParams } from "react-router-dom"; import { Button, Box, Container, Typography, colors } from "@mui/material"; diff --git a/src/pages/CreateValidator.tsx b/src/pages/CreateValidator.tsx index a2cf718..cb872b3 100644 --- a/src/pages/CreateValidator.tsx +++ b/src/pages/CreateValidator.tsx @@ -1,14 +1,98 @@ import { Box, MenuItem, Select, Typography } from '@mui/material' -import React from 'react' -import { useWalletConnectContext } from '../context/WalletConnectContext' +import React, { useMemo, useState } from 'react' +import { enqueueSnackbar } from 'notistack'; import { useNavigate } from 'react-router-dom'; +import { MsgCreateValidatorEncodeObject } from '@cosmjs/stargate'; +import { fromBech32, toBech32 } from '@cosmjs/encoding'; +import { LoadingButton } from '@mui/lab'; + +import { useWalletConnectContext } from '../context/WalletConnectContext' +import { encodePubkey } from '@cosmjs/proto-signing'; + const CreateValidator = () => { - const {session} = useWalletConnectContext(); + const {session, signClient} = useWalletConnectContext(); const navigate = useNavigate(); + + // TODO: Handle + const [cosmosAddress, setCosmosAddress] = useState(''); + const [isLoading, setIsLoading] = useState(false); + const [pubkey, setPubkey] = useState(''); + if (!session){ navigate("/connect-wallet") } + const changePrefix = (address: string, newPrefix: string): string => { + return toBech32(newPrefix, fromBech32(address).data); + } + + const createValidatorMessage: MsgCreateValidatorEncodeObject = + useMemo(() => { + return { + typeUrl: "/cosmos.staking.v1beta1.MsgCreateValidator", + value: { + description: { + moniker: "dockerNode", + identity: "", + website: "", + securityContact: "", + details: "", + }, + commission: { + maxChangeRate: "10000000000000000", // 0.01 + maxRate: "200000000000000000", // 0.2 + rate: "100000000000000000", // 0.1 + }, + minSelfDelegation: "1", + delegatorAddress: cosmosAddress, + validatorAddress: changePrefix(cosmosAddress, "laconicvaloper"), + pubkey: encodePubkey({ + type: "tendermint/PubKeyEd25519", + value: pubkey, + }), + value: { + amount: process.env.REACT_APP_STAKING_AMOUNT!, + denom: process.env.REACT_APP_LACONICD_DENOM!, + }, + }, + }; + }, [cosmosAddress, pubkey]); + + const sendTransaction = async ( + transactionMessage: MsgCreateValidatorEncodeObject + ) => { + + try { + setIsLoading(true); + + const params = { transactionMessage, signer: cosmosAddress }; + const responseFromWallet = await signClient!.request<{ + code: number; + }>({ + topic: session!.topic, + chainId: `cosmos:${process.env.REACT_APP_LACONICD_CHAIN_ID}`, + request: { + method: "cosmos_sendTransaction", + params, + }, + }); + if (responseFromWallet.code !== 0) { + enqueueSnackbar("Transaction not sent", { variant: "error" }); + } else { + navigate("/onboarding-success", { + state: { + cosmosAddress + } + }); + } + } catch (error) { + console.error(error); + enqueueSnackbar("Error in sending transaction", { variant: "error" }); + } finally { + setIsLoading(false); + } + }; + return ( { > Create a validator Select Laconic account: + + {Boolean(cosmosAddress) && ( +
+ +
+              {/* TODO: Use replacer for Uint8 array */}
+              {JSON.stringify(createValidatorMessage, null, 2)}{" "}
+            
+
+ + { + await sendTransaction(createValidatorMessage); + }} + loading={isLoading} + > + Send transaction + + +
+ )}
+ ) }