Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tailwind CSS CDN, import styles in index.tsx, redesign the home p… #51

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ Run development Server
```bash
yarn start
```
# keplr-example
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@types/react-dom": "^18.2.7",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.21.3",
"react-scripts": "5.0.1",
"typescript": "^4.9.5"
},
Expand Down Expand Up @@ -42,12 +43,14 @@
"devDependencies": {
"buffer": "^6.0.3",
"crypto-browserify": "^3.12.0",
"daisyui": "^4.6.0",
"https-browserify": "^1.0.0",
"os-browserify": "^0.3.0",
"process": "^0.11.10",
"react-app-rewired": "^2.2.1",
"stream-browserify": "^3.0.0",
"stream-http": "^3.2.0",
"tailwindcss": "^3.4.1",
"ts-proto": "^1.157.0",
"zx": "^7.2.3"
}
Expand Down
7 changes: 7 additions & 0 deletions postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
plugins: {
tailwindcss: {},

autoprefixer: {},
},
};
23 changes: 23 additions & 0 deletions public/fakeData.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[
{
"id": "1",
"name": "Widget A",
"description": "A high-quality widget with advanced features.",
"amount": 15,
"price": 29.99
},
{
"id": "2",
"name": "Gizmo B",
"description": "An innovative gizmo for everyday use.",
"amount": 8,
"price": 49.99
},
{
"id": "3",
"name": "Doohickey C",
"description": "The latest doohickey with cutting-edge technology.",
"amount": 20,
"price": 39.99
}
]
1 change: 1 addition & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
content="Web site created using create-react-app"
/>
<title>Keplr Example App</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<div id="root"></div>
Expand Down
178 changes: 1 addition & 177 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,185 +1,9 @@
import React, {useEffect} from 'react';
import {getKeplrFromWindow} from "./util/getKeplrFromWindow";
import {OsmosisChainInfo} from "./constants";
import {Balances} from "./types/balance";
import {Dec, DecUtils} from "@keplr-wallet/unit";
import {sendMsgs} from "./util/sendMsgs";
import {api} from "./util/api";
import {simulateMsgs} from "./util/simulateMsgs";
import {MsgSend} from "./proto-types-gen/src/cosmos/bank/v1beta1/tx";
import "./styles/container.css";
import "./styles/button.css";
import "./styles/item.css";

function App() {
const [address, setAddress] = React.useState<string>('');
const [balance, setBalance] = React.useState<string>('');

const [recipient, setRecipient] = React.useState<string>('');
const [amount, setAmount] = React.useState<string>('');

useEffect(() => {
init();
}, []);

const init = async () => {
const keplr = await getKeplrFromWindow();

if(keplr) {
try {
await keplr.experimentalSuggestChain(OsmosisChainInfo);
} catch (e) {
if (e instanceof Error) {
console.log(e.message);
}
}
}
}

const getKeyFromKeplr = async () => {
const key = await window.keplr?.getKey(OsmosisChainInfo.chainId);
if (key) {
setAddress(key.bech32Address)
}
}

const getBalance = async () => {
const key = await window.keplr?.getKey(OsmosisChainInfo.chainId);

if (key) {
const uri = `${OsmosisChainInfo.rest}/cosmos/bank/v1beta1/balances/${key.bech32Address}?pagination.limit=1000`;

const data = await api<Balances>(uri);
const balance = data.balances.find((balance) => balance.denom === "uosmo");
const osmoDecimal = OsmosisChainInfo.currencies.find((currency) => currency.coinMinimalDenom === "uosmo")?.coinDecimals;

if(balance) {
const amount = new Dec(balance.amount, osmoDecimal);
setBalance(`${amount.toString(osmoDecimal)} OSMO`)
} else {
setBalance(`0 OSMO`)
}
}
}

const sendBalance = async () => {
if (window.keplr) {
const key = await window.keplr.getKey(OsmosisChainInfo.chainId);
const protoMsgs = {
typeUrl: "/cosmos.bank.v1beta1.MsgSend",
value: MsgSend.encode({
fromAddress: key.bech32Address,
toAddress: recipient,
amount: [
{
denom: "uosmo",
amount: DecUtils.getTenExponentN(6).mul(new Dec(amount)).truncate().toString(),
},
],
}).finish(),
}

try {
const gasUsed = await simulateMsgs(
OsmosisChainInfo,
key.bech32Address,
[protoMsgs],
[{denom: "uosmo",
amount: "236",}]
);

if(gasUsed) {
await sendMsgs(
window.keplr,
OsmosisChainInfo,
key.bech32Address,
[protoMsgs],
{
amount: [{denom: "uosmo",
amount: "236",}],
gas: Math.floor(gasUsed * 1.5).toString(),
})
}
} catch (e) {
if (e instanceof Error) {
console.log(e.message);
}
}

}
}


return (
<div className="root-container">
<div style={{
display: "flex",
justifyContent: "center",
padding: "16px"
}}>
<img src="/keplr-logo.png" style={{maxWidth: "200px"}} alt="keplr-logo" />
</div>



<div className="item-container">
<div className="item">
<div className="item-title">
Get OSMO Address
</div>

<div className="item-content">
<div>
Address: {address}
</div>

<div>
<button className="keplr-button" onClick={getKeyFromKeplr}>Get Address</button>
</div>
</div>
</div>

<div className="item">
<div className="item-title">
Get OSMO Balance
</div>

<div className="item-content">
Balance: {balance}

<button className="keplr-button" onClick={getBalance}>Get Balance</button>
</div>
</div>

<div className="item">
<div className="item-title">
Send OSMO
</div>

<div className="item-content">
<div style={{
display: "flex",
flexDirection: "column"
}}>
Recipient:
<input type="text" value={recipient} onChange={(e) => setRecipient(e.target.value)} />
</div>

<div style={{
display: "flex",
flexDirection: "column"
}}>
Amount:
<input type="text" value={amount} onChange={(e) => setAmount(e.target.value)} />
</div>

<button className="keplr-button" onClick={sendBalance}>Send</button>
</div>

</div>
</div>
</div>
);
return <div className="root-container"></div>;
}

export default App;
Loading