add type annotations to examples

This commit is contained in:
Lorexia
2023-09-24 11:07:31 +02:00
parent 9627fa0500
commit 63d2ed2fec
3 changed files with 90 additions and 113 deletions
@@ -77,6 +77,7 @@ function MyComponent() {
{ wallet && <div>Address: <code>{address}</code> </div>}
</div>
<button onClick={() => {connect()}}>Connect wallet</button>
{address && <button onClick={() => {sign()}}>Sign message</button> }
</div>
);
@@ -98,5 +99,4 @@ export default function App() {
)
}
```
@@ -27,7 +27,7 @@ import { mixFetch } from '@nymproject/mix-fetch-full-fat';
import React from 'react';
export function HttpGET() {
const [html, setHtml] = React.useState('')
const [html, setHtml] = React.useState<string>('')
async function get () {
const response = await mixFetch('https://nymtech.net')
const text = await response.text()
@@ -44,53 +44,50 @@ import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate";
##### Example: using the Mixnet smart contract client to query
In this example, we will use the `MixnetQueryClient`from the `Contract Clients` to simply query the contract and return a list of Mixnodes.
```js
```ts
import "./App.css";
import { contracts } from "@nymproject/contract-clients";
import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate";
import { useEffect, useState } from "react";
import { MixnodeDetails } from '@nymproject/types';
export default function Mixnodes() {
const [mixnodes, setMixnodes] = useState(null);
const [mixnodes, setMixnodes] = useState<MixnodeDetails[]>([]);
async function fetchMixnodes(){
// Set-up the CosmWasm Client
const cosmWasmClient = await SigningCosmWasmClient.connect("wss://rpc.nymtech.net:443");
const client = new contracts.Mixnet.MixnetQueryClient(
cosmWasmClient,
"n17srjznxl9dvzdkpwpw24gg668wc73val88a6m5ajg6ankwvz9wtst0cznr" // the contract address (which is different on mainnet, QA, etc)
);
console.log("client:", client)
const result = await client.getMixNodesDetailed({});
console.log(result)
setMixnodes(result.nodes)
}
useEffect(() => {
fetchMixnodes();
}, [])
return(
<>
<table>
<tbody>
{mixnodes?.map((value, index) => {
return(
<tr key={index}>
<td> {value?.bond_information?.mix_node?.identity_key} </td>
</tr>
)
})
}
</tbody>
</table>
</>
)
}
```
return(
<>
<table>
<tbody>
{mixnodes?.map((value, index) => {
return(
<tr key={index}>
<td> {value?.bond_information?.mix_node?.identity_key} </td>
</tr>
)
})
}
</tbody>
</table>
</>
)
}
```
@@ -120,66 +117,63 @@ export const mySettings = {
export const settings = mySettings;
```
```js
```ts
import "./App.css";
import { contracts } from "@nymproject/contract-clients";
import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate";
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { GasPrice } from "@cosmjs/stargate";
import { settings } from "./settings.ts";
import { MixnetClient } from '@nymproject/types';
const mainnetSettings = {
url: "wss://rpc.nymtech.net:443",
mixnetContractAddress: 'n17srjznxl9dvzdkpwpw24gg668wc73val88a6m5ajg6ankwvz9wtst0cznr',
mnemonic: 'charge solid adjust talk rose there because bridge screen next swear rose uphold hammer grant agree slam damp lazy position coconut cabbage endless welcome',
address: 'n1c7y676pe3av76r5usala759xgj0yplmvngu8u8'
};
const qaSettings = {
url: "wss://sandbox-validator1.nymtech.net/",
mixnetContractAddress: 'n1xr3rq8yvd7qplsw5yx90ftsr2zdhg4e9z60h5duusgxpv72hud3sjkxkav',
mnemonic: 'summer under connect sadness unveil region charge feed tank grant drift mass side ramp winter fit verb rare huge high garment moment achieve since',
address: 'n13uryxldwdllpakevsmt6n0uyfn3kgr2wvj5dnf'
};
const settings = qaSettings;
export default function Exec() {
let signer = null;
let address = null;
let signerMixnetClient = null;
let cosmWasmSigningClient = null;
let mixId = null;
let amountToDelegate = null;
let balance = null;
let nodeAddress = null;
let amountToSend = null;
let delegations = null;
let signer: DirectSecp256k1HdWallet;
let signerMixnetClient: MixnetClient;
let cosmWasmSigningClient: SigningCosmWasmClient;
let mixId: number;
let amountToDelegate: string;
let nodeAddress: string;
let amountToSend: string;
let delegations: any;
async function ExecuteOnNyx() {
// Signer
try {
// Generate a signer from a mnemonic
signer = await DirectSecp256k1HdWallet.fromMnemonic(settings.mnemonic, {
prefix: "n",
});
const accounts = await signer.getAccounts();
address = accounts[0].address;
} catch (error) {
console.error("Problem getting the signer: ", error);
}
try {
const cosmWasmClient = await SigningCosmWasmClient.connectWithSigner(
settings.url,
signer,
{
gasPrice: GasPrice.fromString("0.025unym"),
}
);
cosmWasmSigningClient = cosmWasmClient;
try {
balance = await cosmWasmSigningClient?.getBalance(address, "unym");
console.log("balance", balance);
} catch (error) {
console.error("problem geting the balance: ", error);
// cosmos client
signer = await DirectSecp256k1HdWallet.fromMnemonic(settings.mnemonic, {
prefix: "n",
});
const cosmWasmClient = await SigningCosmWasmClient.connectWithSigner(
settings.url,
signer,
{
gasPrice: GasPrice.fromString("0.025unym"),
}
);
// save globally
cosmWasmSigningClient = cosmWasmClient;
// nym client
const mixnetClient = new contracts.Mixnet.MixnetClient(
cosmWasmSigningClient,
settings.address, // sender (that account of the signer)
settings.mixnetContractAddress // contract address (different on mainnet, QA, etc)
);
// save globally
signerMixnetClient = mixnetClient;
const mixnetClient = new contracts.Mixnet.MixnetClient(
cosmWasmSigningClient,
settings.address, // sender (that account of the signer)
settings.mixnetContractAddress // contract address (different on mainnet, QA, etc)
);
signerMixnetClient = mixnetClient;
} catch (error) {
console.error("Problem getting the cosmWasmSigningClient: ", error);
}
}
// Get delegations
@@ -198,53 +192,36 @@ export default function Exec() {
if (!signerMixnetClient) {
return;
}
try {
const res = await signerMixnetClient.delegateToMixnode(
{ mixId },
"auto",
undefined,
[{ amount: `${amountToDelegate}`, denom: "unym" }]
);
console.log("delegations: ", res);
} catch (error) {
console.error(error);
}
const res = await signerMixnetClient.delegateToMixnode(
{ mixId },
"auto",
undefined,
[{ amount: `${amountToDelegate}`, denom: "unym" }]
);
console.log(res);
};
// Undelegate all
const doUndelegateAll = async () => {
if (!signerMixnetClient) {
return;
}
console.log("delegations", delegations);
try {
for (const delegation of delegations.delegations) {
await signerMixnetClient.undelegateFromMixnode(
{ mixId: delegation.mix_id },
"auto"
);
}
} catch (error) {
console.error(error);
for (const delegation of delegations.delegations) {
await signerMixnetClient.undelegateFromMixnode(
{ mixId: delegation.mix_id },
"auto"
);
}
};
// Sending tokens
const doSendTokens = async () => {
const memo = "test sending tokens";
try {
const res = await cosmWasmSigningClient.sendTokens(
settings.address,
nodeAddress,
[{ amount: amountToSend, denom: "unym" }],
"auto",
memo
);
console.log("res", res);
} catch (error) {
console.error(error);
}
const res = await cosmWasmSigningClient.sendTokens(
settings.address,
nodeAddress,
[{ amount: amountToSend, denom: "unym" }],
"auto",
memo
);
console.log(res);
};
ExecuteOnNyx();
@@ -274,7 +251,7 @@ export default function Exec() {
<input
type="number"
placeholder="Mixnode Id"
onChange={(e) => (mixId = e.target.value)}
onChange={(e) => (mixId = +e.target.value)}
/>
<input
type="number"