update mixfetch, cosmoskit examples

This commit is contained in:
Lorexia
2023-10-01 17:00:06 +02:00
parent 784501f357
commit 593a6efbe7
2 changed files with 45 additions and 10 deletions
@@ -55,8 +55,32 @@ import { ChainProvider, useChain } from '@cosmos-kit/react';
import { assets, chains } from 'chain-registry';
import { wallets as ledger } from '@cosmos-kit/ledger';
import { wallets as keplr } from '@cosmos-kit/keplr';
import { AminoMsg, makeSignDoc } from '@cosmjs/amino';
import { MsgSend } from 'cosmjs-types/cosmos/bank/v1beta1/tx';
export const getDoc = (address: string) => {
const chainId = 'nyx';
const msg: AminoMsg = {
type: '/cosmos.bank.v1beta1.MsgSend',
value: MsgSend.fromPartial({
fromAddress: address,
toAddress: 'n1nn8tghp94n8utsgyg3kfttlxm0exgjrsqkuwu9',
amount: [{ amount: '1000', denom: 'unym' }],
}),
};
const fee = {
amount: [{ amount: '2000', denom: 'ucosm' }],
gas: '180000', // 180k
};
const memo = 'Use your power wisely';
const accountNumber = 15;
const sequence = 16;
const doc = makeSignDoc([msg], fee, chainId, memo, accountNumber, sequence);
return doc
};
function MyComponent() {
const {wallet, address, connect, getOfflineSignerDirect } =
const {wallet, address, connect, getOfflineSignerAmino } =
useChain('nyx');
React.useEffect(() => {
@@ -64,8 +88,9 @@ function MyComponent() {
}, []);
const sign = async () => {
const doc = {foo: 'bar'};
return getOfflineSignerDirect().signDirect(address, doc);
if (!address) return
const doc = getDoc(address);
return getOfflineSignerAmino().signAmino(address, doc);
};
return (
@@ -77,17 +102,28 @@ function MyComponent() {
{ wallet && <div>Address: <code>{address}</code> </div>}
</div>
<button onClick={() => {connect()}}>Connect wallet</button>
{address && <button onClick={() => {sign()}}>Sign message</button> }
</div>
);
}
export default function App() {
const assetsFixedUp = React.useMemo(() => {
const nyx = assets.find((a) => a.chain_name === 'nyx');
if (nyx) {
const nyxCoin = nyx.assets.find((a) => a.name === 'nyx');
if (nyxCoin) {
nyxCoin.coingecko_id = 'nyx';
}
nyx.assets = nyx.assets.reverse();
}
return assets;
}, [assets]);
return (
<ChainProvider
chains={[chains.find((c) => c.chain_id === 'nyx')]}
assetLists={[assets.find((a) => a.chain_name === 'nyx')]}
chains={[chains.find((c) => c.chain_id === 'nyx')!]}
assetLists={assetsFixedUp}
wallets={[...ledger, ...keplr]}
signerOptions={{
preferredSignType: () => 'amino',
@@ -99,4 +135,5 @@ export default function App() {
)
}
```
@@ -136,11 +136,10 @@ export function HttpGET() {
export function HttpPOST() {
async function post () {
const apiResponse = await mixFetch('https://api.example.com', {
const apiResponse = await mixFetch('https://postman-echo.com/post', {
method: 'POST',
mode: 'unsafe-ignore-cors',
body: JSON.stringify({ foo: 'bar' }),
headers: { [`Content-Type`]: 'application/json', Authorization: `Bearer ${process.env.AUTH_TOKEN}` }
headers: { 'Content-Type': 'application/json' }
}, mixFetchOptions)
console.log(apiResponse)
}
@@ -159,6 +158,5 @@ export default function App() {
</>
)
}
```