diff --git a/components/dataViews/TransactionInfo.tsx b/components/dataViews/TransactionInfo.tsx index 2abb112..0abb9ef 100644 --- a/components/dataViews/TransactionInfo.tsx +++ b/components/dataViews/TransactionInfo.tsx @@ -5,7 +5,7 @@ import { DbTransaction } from "../../types"; import { useAppContext } from "../../context/AppContext"; import HashView from "./HashView"; import StackableContainer from "../layout/StackableContainer"; -import { printableCoins } from "../../lib/displayHelpers"; +import { printableCoins, printableCoin } from "../../lib/displayHelpers"; interface Props { tx: DbTransaction; @@ -16,38 +16,88 @@ const TransactionInfo = (props: Props) => { return ( + + ); +}; + +export default withRouter(ReDelegationForm); diff --git a/components/forms/RewardsForm.tsx b/components/forms/RewardsForm.tsx new file mode 100644 index 0000000..c1a2b30 --- /dev/null +++ b/components/forms/RewardsForm.tsx @@ -0,0 +1,155 @@ +import axios from "axios"; +import { Account, calculateFee } from "@cosmjs/stargate"; +import { Decimal } from "@cosmjs/math"; +import { assert } from "@cosmjs/utils"; +import React, { useState } from "react"; +import { withRouter, NextRouter } from "next/router"; +import { useAppContext } from "../../context/AppContext"; +import Button from "../inputs/Button"; +import Input from "../inputs/Input"; +import StackableContainer from "../layout/StackableContainer"; +import { checkValidatorAddress } from "../../lib/displayHelpers"; + +interface Props { + address: string | null; + accountOnChain: Account | null; + router: NextRouter; + closeForm: () => void; +} + +const RewardsForm = (props: Props) => { + const { state } = useAppContext(); + const [validatorAddress, setValidatorAddress] = useState(""); + const [amount, setAmount] = useState("0"); + const [memo, setMemo] = useState(""); + const [gas, setGas] = useState(200000); + const [gasPrice, _setGasPrice] = useState(state.chain.gasPrice); + const [_processing, setProcessing] = useState(false); + const [addressError, setAddressError] = useState(""); + + const createTransaction = (txValidatorAddress: string, txAmount: string, txGas: number) => { + const amountInAtomics = Decimal.fromUserInput( + txAmount, + Number(state.chain.displayDenomExponent), + ).atomics; + const msgDelegatorReward = { + delegatorAddress: props.address, + validatorAddress: txValidatorAddress, + amount: { + amount: amountInAtomics, + denom: state.chain.denom, + }, + }; + const msg = { + typeUrl: "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward", + value: msgDelegatorReward, + }; + assert(gasPrice, "gasPrice missing"); + const fee = calculateFee(Number(txGas), gasPrice); + const { accountOnChain } = props; + assert(accountOnChain, "accountOnChain missing"); + return { + accountNumber: accountOnChain.accountNumber, + sequence: accountOnChain.sequence, + chainId: state.chain.chainId, + msgs: [msg], + fee: fee, + memo: memo, + }; + }; + + const handleCreate = async () => { + assert(state.chain.addressPrefix, "addressPrefix missing"); + const validatorAddressError = checkValidatorAddress(validatorAddress, "cosmosvaloper"); + if (validatorAddressError) { + setAddressError( + `Invalid address for network ${state.chain.chainId}: ${validatorAddressError}`, + ); + return; + } + + setProcessing(true); + const tx = createTransaction(validatorAddress, amount, gas); + console.log(tx, "tx data"); + const dataJSON = JSON.stringify(tx); + const res = await axios.post("/api/transaction", { dataJSON }); + console.log(dataJSON, "tx dataJSON", res); + const { transactionID } = res.data; + props.router.push(`${props.address}/transaction/${transactionID}`); + }; + + assert(state.chain.addressPrefix, "addressPrefix missing"); + + return ( + + +

Claim Rewards

+
+ ) => setValidatorAddress(e.target.value)} + error={addressError} + placeholder={`E.g. cosmosvaloper1sjllsnramtg3ewxqwwrwjxfgc4n4ef9u2lcnj0`} + /> +
+
+ ) => setAmount(e.target.value)} + /> +
+
+ ) => + setGas(parseInt(e.target.value, 10)) + } + /> +
+
+ +
+
+ ) => setMemo(e.target.value)} + /> +
+ +

Create UnDelegation

+
+ ) => setValidatorAddress(e.target.value)} + error={addressError} + placeholder={`E.g. cosmosvaloper1sjllsnramtg3ewxqwwrwjxfgc4n4ef9u2lcnj0`} + /> +
+
+ ) => setAmount(e.target.value)} + /> +
+
+ ) => + setGas(parseInt(e.target.value, 10)) + } + /> +
+
+ +
+
+ ) => setMemo(e.target.value)} + /> +
+