Skip to content

Commit 6506070

Browse files
committed
add react-query sample in telescope-with-contract
1 parent a7394a2 commit 6506070

26 files changed

+1296
-559
lines changed

examples/telescope-with-contracts/components/sell-nfts.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export const SellNfts = () => {
6060
const transferModalControl = useDisclosure();
6161
const burnModalControl = useDisclosure();
6262
const updatePriceModalControl = useDisclosure();
63+
const updatePriceModalRQControl = useDisclosure();
6364
const removeListingModalControl = useDisclosure();
6465

6566
useEffect(() => {
@@ -252,6 +253,7 @@ export const SellNfts = () => {
252253
transferNft: transferModalControl.onOpen,
253254
removeListing: removeListingModalControl.onOpen,
254255
updatePrice: updatePriceModalControl.onOpen,
256+
updatePriceRQ: updatePriceModalRQControl.onOpen,
255257
}}
256258
/>
257259
)}
@@ -289,6 +291,18 @@ export const SellNfts = () => {
289291
update={update}
290292
token={selectedToken}
291293
price={price}
294+
isRQ={false}
295+
/>
296+
)}
297+
298+
{selectedToken && selectedCollection && price && (
299+
<UpdatePriceModal
300+
modalControl={updatePriceModalRQControl}
301+
collection={selectedCollection}
302+
update={update}
303+
token={selectedToken}
304+
price={price}
305+
isRQ={true}
292306
/>
293307
)}
294308

examples/telescope-with-contracts/components/ui/modal/nft-detail-modal.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export const NftDetailModal = ({
5252
burn: () => void;
5353
transferNft: () => void;
5454
updatePrice: () => void;
55+
updatePriceRQ: () => void;
5556
removeListing: () => void;
5657
};
5758
}) => {
@@ -162,7 +163,6 @@ export const NftDetailModal = ({
162163
/>
163164
)}
164165
<SplitText left="Owned by" right={ownerName} mb="18px" />
165-
166166
{token.forSale && token.saleType && (
167167
<SplitText
168168
left={priceText[token.saleType]}
@@ -175,7 +175,6 @@ export const NftDetailModal = ({
175175
mb="24px"
176176
/>
177177
)}
178-
179178
{token.forSale ? (
180179
<Flex gap="16px" mb="16px">
181180
<NormalButton
@@ -203,7 +202,17 @@ export const NftDetailModal = ({
203202
mb="16px"
204203
/>
205204
)}
206-
205+
{token.forSale && (
206+
<Flex gap="16px" mb="16px">
207+
<NormalButton
208+
onClick={openModals.updatePriceRQ}
209+
size={{ h: '36px', w: '100%' }}
210+
leftIcon={<Icon as={AiOutlineTag} boxSize={4} />}
211+
text="Update Price(React Query)"
212+
type="solid"
213+
/>
214+
</Flex>
215+
)}
207216
<Flex gap="16px">
208217
<SimpleButton
209218
content="Transfer"

examples/telescope-with-contracts/components/ui/modal/update-price-modal.tsx

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
Flex,
1111
} from '@chakra-ui/react';
1212
import BigNumber from 'bignumber.js';
13-
import { useState } from 'react';
13+
import { useState, useEffect } from 'react';
1414
import { coin, exponent, marketplaceContract } from 'config';
1515
import { useColor, useTransactionToast } from 'hooks';
1616
import { isGtZero, toDisplayAmount, toRawAmount } from 'utils';
@@ -20,22 +20,32 @@ import { LargeButton } from '../nft/buttons';
2020
import { SplitText } from '../nft/nft-cards';
2121
import { Subtitle, Fees } from '../nft';
2222
import { useContracts } from '../../../src/codegen/contracts-context';
23+
import { useMarketplaceUpdateAskPriceMutation } from 'src/codegen/Marketplace.react-query';
2324

2425
export const UpdatePriceModal = ({
2526
modalControl,
2627
collection,
2728
token,
2829
price,
2930
update,
31+
isRQ,
3032
}: {
3133
token: Token;
3234
price: number;
35+
isRQ: boolean;
3336
modalControl: UseDisclosureReturn;
3437
collection: Collection;
3538
update: () => void;
3639
}) => {
3740
const [inputValue, setInputValue] = useState('');
3841
const [isLoading, setIsLoading] = useState(false);
42+
const {
43+
isSuccess: isRQSuccess,
44+
isError: isRQError,
45+
error: rqError,
46+
isLoading: isRQLoading,
47+
mutate: updateAskPrice,
48+
} = useMarketplaceUpdateAskPriceMutation();
3949

4050
const { showToast } = useTransactionToast();
4151

@@ -75,6 +85,44 @@ export const UpdatePriceModal = ({
7585
}
7686
};
7787

88+
const handleRQUpdateClick = async () => {
89+
try {
90+
console.log("react query's working.");
91+
92+
const marketplaceClient =
93+
marketplace.getSigningClient(marketplaceContract);
94+
updateAskPrice({
95+
client: marketplaceClient,
96+
msg: {
97+
collection: token.collectionAddr,
98+
price: {
99+
amount: toRawAmount(inputValue, exponent),
100+
denom: coin.base,
101+
},
102+
tokenId: parseInt(token.tokenId),
103+
},
104+
});
105+
} catch (ex) {
106+
showToast(TxResult.Failed, ex);
107+
console.error(ex);
108+
}
109+
};
110+
111+
useEffect(() => {
112+
if (isRQSuccess) {
113+
showToast(TxResult.Success);
114+
update();
115+
closeModal();
116+
}
117+
}, [isRQSuccess]);
118+
119+
useEffect(() => {
120+
if (isRQError) {
121+
showToast(TxResult.Failed, rqError);
122+
console.error(rqError);
123+
}
124+
}, [isRQError, rqError]);
125+
78126
const { textColor, bgColor } = useColor();
79127

80128
return (
@@ -140,9 +188,9 @@ export const UpdatePriceModal = ({
140188

141189
<LargeButton
142190
width="100%"
143-
btnContent="Update"
144-
handleClick={handleUpdateClick}
145-
isLoading={isLoading}
191+
btnContent={`Update${isRQ ? '(ReactQuery)' : ''}`}
192+
handleClick={isRQ ? handleRQUpdateClick : handleUpdateClick}
193+
isLoading={isLoading || isRQLoading}
146194
disabled={
147195
!inputValue ||
148196
new BigNumber(inputValue).isEqualTo(0) ||

examples/telescope-with-contracts/components/ui/nft/list-tab.tsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,6 @@ export const ListTab = ({
5151

5252
const { marketplace, sg721Updatable } = useContracts();
5353

54-
const hooks = marketplace.getReactQueryHooks(marketplaceContract);
55-
56-
const { useMarketplaceParamsQuery } = hooks;
57-
58-
useMarketplaceParamsQuery({
59-
options: {},
60-
});
61-
6254
const handleClick = async () => {
6355
if (!address) return;
6456
setIsLoading(true);

examples/telescope-with-contracts/pages/_app.tsx

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import { GasPrice } from '@cosmjs/stargate';
1414
import { SignerOptions } from '@cosmos-kit/core';
1515
import { Chain } from '@chain-registry/types';
1616
import { defaultTheme } from 'config';
17+
import { QueryClientProvider, QueryClient } from '@tanstack/react-query';
18+
19+
const queryClient = new QueryClient();
1720

1821
const client = new ApolloClient({
1922
uri: 'https://constellations-api.mainnet.stargaze-apis.com/graphql',
@@ -37,29 +40,31 @@ function CreateCosmosApp({ Component, pageProps }: AppProps) {
3740

3841
return (
3942
<ChakraProvider theme={defaultTheme}>
40-
<ChainProvider
41-
chains={chains}
42-
assetLists={assets}
43-
wallets={[...keplrWallets, ...cosmostationWallets, ...leapWallets]}
44-
walletConnectOptions={{
45-
signClient: {
46-
projectId: 'a8510432ebb71e6948cfd6cde54b70f7',
47-
relayUrl: 'wss://relay.walletconnect.org',
48-
metadata: {
49-
name: 'CosmosKit Template',
50-
description: 'CosmosKit dapp template',
51-
url: 'https://docs.cosmoskit.com/',
52-
icons: [],
43+
<QueryClientProvider client={queryClient}>
44+
<ChainProvider
45+
chains={chains}
46+
assetLists={assets}
47+
wallets={[...keplrWallets, ...cosmostationWallets, ...leapWallets]}
48+
walletConnectOptions={{
49+
signClient: {
50+
projectId: 'a8510432ebb71e6948cfd6cde54b70f7',
51+
relayUrl: 'wss://relay.walletconnect.org',
52+
metadata: {
53+
name: 'CosmosKit Template',
54+
description: 'CosmosKit dapp template',
55+
url: 'https://docs.cosmoskit.com/',
56+
icons: [],
57+
},
5358
},
54-
},
55-
}}
56-
wrappedWithChakra={true}
57-
signerOptions={signerOptions}
58-
>
59-
<ApolloProvider client={client}>
60-
<Component {...pageProps} />
61-
</ApolloProvider>
62-
</ChainProvider>
59+
}}
60+
wrappedWithChakra={true}
61+
signerOptions={signerOptions}
62+
>
63+
<ApolloProvider client={client}>
64+
<Component {...pageProps} />
65+
</ApolloProvider>
66+
</ChainProvider>
67+
</QueryClientProvider>
6368
</ChakraProvider>
6469
);
6570
}

examples/telescope-with-contracts/scripts/codegen.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ telescope({
8080
enabled: true,
8181
},
8282
reactQuery: {
83-
enabled: true
83+
enabled: true,
84+
version: 'v4',
85+
mutations: true,
8486
},
8587
useContractsHooks: {
8688
enabled: true

examples/telescope-with-contracts/src/codegen/Marketplace.client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* This file was automatically generated by @cosmwasm/ts-codegen@latest.
2+
* This file was automatically generated by @cosmwasm/ts-codegen@0.31.6.
33
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
44
* and run the @cosmwasm/ts-codegen generate command to regenerate this file.
55
*/

examples/telescope-with-contracts/src/codegen/Marketplace.message-composer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* This file was automatically generated by @cosmwasm/ts-codegen@latest.
2+
* This file was automatically generated by @cosmwasm/ts-codegen@0.31.6.
33
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
44
* and run the @cosmwasm/ts-codegen generate command to regenerate this file.
55
*/
Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,19 @@
11
/**
2-
* This file was automatically generated by @cosmwasm/[email protected].
3-
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
4-
* and run the @cosmwasm/ts-codegen generate command to regenerate this file.
5-
*/
2+
* This file was automatically generated by @cosmwasm/[email protected].
3+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
4+
* and run the @cosmwasm/ts-codegen generate command to regenerate this file.
5+
*/
66

7-
import { UseQueryResult } from 'react-query';
8-
import { ContractBase, IContractConstructor } from './contractContextBase';
9-
import {
10-
MarketplaceClient,
11-
MarketplaceQueryClient,
12-
} from './Marketplace.client';
13-
import { MarketplaceMessageComposer } from './Marketplace.message-composer';
14-
import {
15-
IMarketplaceReactQueryHooks,
16-
MarketplaceParamsQuery,
17-
useMarketplaceParamsQuery,
18-
} from './Marketplace.react-query';
19-
import { ParamsResponse } from './Marketplace.types';
20-
export class Marketplace extends ContractBase<
21-
MarketplaceClient,
22-
MarketplaceQueryClient,
23-
MarketplaceMessageComposer
24-
> {
7+
import { ContractBase, IContractConstructor } from "./contractContextBase";
8+
import { MarketplaceClient, MarketplaceQueryClient } from "./Marketplace.client";
9+
import { MarketplaceMessageComposer } from "./Marketplace.message-composer";
10+
export class Marketplace extends ContractBase<MarketplaceClient, MarketplaceQueryClient, MarketplaceMessageComposer> {
2511
constructor({
2612
address,
2713
cosmWasmClient,
28-
signingCosmWasmClient,
14+
signingCosmWasmClient
2915
}: IContractConstructor) {
30-
super(
31-
address,
32-
cosmWasmClient,
33-
signingCosmWasmClient,
34-
MarketplaceClient,
35-
MarketplaceQueryClient,
36-
MarketplaceMessageComposer
37-
);
16+
super(address, cosmWasmClient, signingCosmWasmClient, MarketplaceClient, MarketplaceQueryClient, MarketplaceMessageComposer);
3817
}
3918

40-
public getReactQueryHooks(contractAddr: string): IMarketplaceReactQueryHooks {
41-
const queryClient = this.getQueryClient(contractAddr);
42-
return {
43-
useMarketplaceParamsQuery({ options }) {
44-
return useMarketplaceParamsQuery({
45-
client: queryClient,
46-
options,
47-
});
48-
},
49-
};
50-
}
51-
}
19+
}

0 commit comments

Comments
 (0)