import React, { useEffect, useState } from 'react' import type { NextPage } from 'next' // import Head from "next/head"; // import Image from "next/image"; // import styles from "../styles/Home.module.css"; import { SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate' // import { Coin } from "@cosmjs/stargate"; import { toast } from 'react-toastify' import BigNumber from 'bignumber.js' import { useQueryClient } from '@tanstack/react-query' import Container from 'components/Container' import Button from 'components/Button' import useWalletStore from 'stores/useWalletStore' import { chain } from 'utils/chains' import { contractAddresses } from 'config/contracts' import { hardcodedFee } from 'utils/contants' import Spinner from 'components/Spinner' import useCreditManagerStore from 'stores/useCreditManagerStore' import { queryKeys } from 'types/query-keys-factory' const Home: NextPage = () => { const [sendAmount, setSendAmount] = useState('') const [recipientAddress, setRecipientAddress] = useState('') const [allTokens, setAllTokens] = useState(null) const [walletTokens, setWalletTokens] = useState(null) const [borrowAmount, setBorrowAmount] = useState(0) const [error, setError] = useState(null) const [isLoading, setIsLoading] = useState(false) const address = useWalletStore((s) => s.address) const selectedAccount = useCreditManagerStore((s) => s.selectedAccount) const queryClient = useQueryClient() const [signingClient, setSigningClient] = useState() useEffect(() => { ;(async () => { if (!window.keplr) return const offlineSigner = window.keplr.getOfflineSigner(chain.chainId) const clientInstance = await SigningCosmWasmClient.connectWithSigner(chain.rpc, offlineSigner) setSigningClient(clientInstance) })() }, [address]) const handleSendClick = async () => { setError(null) setIsLoading(true) try { // console.log(await signingClient.getHeight()); // console.log( // "contract info", // signingClient.getContract( // "osmo1zf26ahe5gqjtvnedh7ems7naf2wtw3z4ll6atf3t0hptal8ss4vq2mlx6w" // ) // ); const res = await signingClient?.sendTokens( address, recipientAddress, [ { denom: chain.stakeCurrency.coinMinimalDenom, amount: BigNumber(sendAmount) .times(10 ** chain.stakeCurrency.coinDecimals) .toString(), }, ], hardcodedFee ) console.log('txResponse', res) toast.success(
Check transaction
, { autoClose: false } ) } catch (e: any) { console.log(e) setError(e.message) } finally { setIsLoading(false) } } const handleCreateCreditAccount = async () => { setError(null) setIsLoading(true) try { // 200000 gas used const executeMsg = { create_credit_account: {}, } const createResult = await signingClient?.execute( address, contractAddresses.creditManager, executeMsg, hardcodedFee ) console.log('mint result', createResult) toast.success(
Check transaction
, { autoClose: false } ) queryClient.invalidateQueries(queryKeys.creditAccounts(address)) } catch (e: any) { console.log(e) setError(e.message) } finally { setIsLoading(false) } } // https://github.com/mars-protocol/rover/blob/master/scripts/types/generated/account-nft/AccountNft.types.ts const handleGetCreditAccounts = async () => { setError(null) setIsLoading(true) try { const allTokensQueryMsg = { all_tokens: {}, } const allTokensResponse = await signingClient?.queryContractSmart( contractAddresses.accountNft, allTokensQueryMsg ) setAllTokens(allTokensResponse.tokens) console.log('all tokens', allTokensResponse) // Returns de owner of a specific "credit account" // const ownerOfQueryMsg = { // owner_of: { // include_expired: false, // token_id: "1", // }, // }; // const ownerResponse = await signingClient.queryContractSmart( // contractAddresses.accountNft, // ownerOfQueryMsg // ); // console.log("res owner", ownerResponse); const tokensQueryMsg = { tokens: { owner: address, }, } const tokensResponse = await signingClient?.queryContractSmart( contractAddresses.accountNft, tokensQueryMsg ) console.log('res tokens', tokensResponse) setWalletTokens(tokensResponse.tokens) } catch (e: any) { console.log(e) setError(e.message) } finally { setIsLoading(false) } } const handleBorrowClick = async () => { setError(null) setIsLoading(true) try { if (!selectedAccount) return const executeMsg = { update_credit_account: { account_id: selectedAccount, actions: [ { borrow: { denom: 'uosmo', amount: BigNumber(borrowAmount) .times(10 ** 6) .toString(), }, }, ], }, } const borrowResult = await signingClient?.execute( address, contractAddresses.creditManager, executeMsg, hardcodedFee ) console.log('borrow result', borrowResult) toast.success(
Check transaction
, { autoClose: false } ) queryClient.invalidateQueries(queryKeys.creditAccounts(address)) queryClient.invalidateQueries(queryKeys.creditAccountsPositions(selectedAccount)) queryClient.invalidateQueries(queryKeys.tokenBalance(address, 'uosmo')) } catch (e: any) { console.log(e) setError(e.message) } finally { setIsLoading(false) } } return (

Send Tokens

Address:

setRecipientAddress(e.target.value)} />

Amount:

setSendAmount(e.target.value)} />

Create Credit Account (Mint NFT)

Get all Credit Accounts

Borrow OSMO

setBorrowAmount(e.target.valueAsNumber)} />
{allTokens && (
All Tokens

- {allTokens.length} total

{allTokens.map((token) => (

{token}

))}
)} {walletTokens && ( <>
Your Tokens

- {walletTokens.length} total

{walletTokens.map((token) => (

{token}

))} )}
{error &&
{error}
} {isLoading && (
)}
) } export default Home