для загрузки приложения HTX
Мои активы
Мои ордера
Сообщение
Моя ставка комиссии
Мои награды
Мои Рефералы
Защита
Настройки OTC
Управление суб-аккаунтом
Управление API
Панель инструментов Брокера
SUI(SUI Network)
2023/10/13 17:53
Today Mysten Labs introduced dApp Kit, an all-in-one solution for developing React applications and decentralized applications (dApps) on Sui. The @mysten/dapp-kit
is a brand-new SDK tailored for React and designed to streamline essential tasks such as connecting to wallets, signing transactions, and fetching data from RPC nodes. dApp Kit offers both themeable, pre built components like to simplify wallet interactions, as well as lower-level hooks and utilities to simplify creating your custom components.
dApp kit is a distillation of lessons learned from Mysten Labs, created with a goal of making it easier for everyone to start building dApps. In fact, Mysten Labs is starting to use dApp kit across all their own dApps; from Sui Explorer to Sui Wallet, every app we built uses dApp kit. We are just getting started, but we are excited to share the kit, and help get it into the hands of more builders!
For comprehensive documentation on dApp Kit, check out the full docs. In this blog post, we will guide you through setting up dApp Kit in your React project.
To get started with dApp Kit, first install it along with react-query:
npm install --save @mysten/dapp-kit @mysten/sui.js @tanstack/react-query
With the packages installed, you need to set up some providers in the application to ensure that dApp Kit functions smoothly:
import '@mysten/dapp-kit/dist/index.css';
import { SuiClientProvider, WalletProvider } from '@mysten/dapp-kit';
import { getFullnodeUrl } from '@mysten/sui.js/client';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
const queryClient = new QueryClient();
const networks = {
localnet: { url: getFullnodeUrl('localnet') },
devnet: { url: getFullnodeUrl('devnet') },
testnet: { url: getFullnodeUrl('testnet') },
mainnet: { url: getFullnodeUrl('mainnet') },
};
ReactDOM.createRoot(document.getElementById('root')!).render(
,
);
In this code snippet, you:
Now that the app is set up properly, you can dive into using dApp Kit's features.
To enable users to connect their Sui Wallet to the dApp, you can easily add a ConnectButton
:
import { ConnectButton } from '@mysten/dapp-kit';
function App() {
return (
Hello, world
);
}
This code will render a Button that opens a Modal and prompts users to connect their wallet. Once connected, users will see their connected walle and have the option to disconnect their wallet again.
dApp Kit provides a variety of hooks for managing wallet state. For instance, the useCurrentWallet
hook enables you to retrieve information about the user's connected account:
import { ConnectButton, useCurrentAccount } from '@mysten/dapp-kit';
function App() {
const account = useCurrentAccount();
return (
{account ? 'No wallet connected' : `Your address is ${account.address}`}
);
}
This allows you to display relevant information based on the user's wallet status.
dApp Kit also facilitates data fetching for the currently connected user. The useSuiClientQuery
hook can be used to call RPC methods. You can use getOwnedObjects
to access and display a list of objects owned by the connected account:
import { ConnectButton, useCurrentAccount } from '@mysten/dapp-kit';
function App() {
const account = useCurrentAccount();
return (
{account ? 'No wallet connected' : }
);
}
export function OwnedObjects() {
const account = useCurrentAccount()!;
const { data } = useSuiClientQuery('getOwnedObjects', { owner: account.address });
return (
{data.data.map((object) => (
- {object.data?.objectId}
))}
);
}
You can read more about the hooks available for making RPC calls in the docs.
Many dApps require the ability to create and sign transaction blocks. dApp Kit simplifies this process with the useSignAndExecuteTransactionBlock
hook. Let's create a button that sends SUI to a predefined address:
import { signAndExecuteTransactionBlock } from '@mysten/dapp-kit';
import { TransactionBlock } from '@mysten/sui.js/transactions';
export function SendSui() {
const { mutateAsync: signAndExecuteTransactionBlock } = useSignAndExecuteTransactionBlock();
function sendMessage() {
const txb = new TransactionBlock();
const coin = txb.splitCoins(txb.gas, [10]);
txb.transferObjects([coin], 'Ox...');
signAndExecuteTransactionBlock({
transactionBlock: txb,
}).then(async (result) => {
alert('Sui sent successfully');
});
}
return sendMessage()}>Send me Sui!;
}
When the button is pressed it will:
TransactionBlock
splitCoins
transaction that splits off SUI from the gas coin into a new cointransferObject
transaction that transfers the new coin to another addressTransactionBlock
using the connected walletalert
to let you know when the transaction has been executeddApp Kit has many more features to help you quickly and easily build dApps. For more details and advanced features, explore the full documentation.
CoinCommander
what happened
2023-10-13 18:02ОтветитьЛайк