bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 54쪽 중 28쪽
;token=b88511da-5abd-48bb-8333-2b0b71100734" alt=""><figcaption></figcaption></figure>
After submitting the transaction, you will see the **transaction hash**, the **block height** at which the transaction was executed, and a **URL** to view the transaction details in the block explorer.
<figure><img src="https://3630978198-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FB5Z5ijJgMx0GJO3l1Il9%2Fuploads%2FueVqiq6QvxTOBZRBIhzB%2Fimage.png?alt=media&token=1663adec-0bc1-4289-bcb5-7e086b61e519" alt=""><figcaption></figcaption></figure>
You now have a functional React app that:
* Authenticates users via Meta Accounts
* Fetches and displays Token Factory token balance
* Enables users to send tokens to another address
# Integrating a Token Factory Token in a Smart Contract
This guide will walk you through integrating a **Token Factory token** into a **smart contract**. The contract will allow users to:
* Retrieve their Token Factory token balance.
* Send Token Factory tokens to another user.
## **Prerequisites**
Before deploying your smart contract on-chain, ensure you have completed the following setup steps:
* **Set up your local environment**: Follow the [installation and setup guide](/en/build-on-verona/computation/local-development/setting-up-env/installation-prerequisites-setup-local-environment) to configure your development environment.
* **Install the Verona daemon (`xiond`)**: Set up the `xiond` CLI by following the [installation instructions](/en/build-on-verona/tools/daemon-cli) to interact with the blockchain.
* Make sure you have [Docker](https://www.docker.com/get-started) installed and running, as it is required to compile your contract.
## **Creating the Smart Contract**
### **Initialize a New Contract**
We are going to use the [cw-counter](https://github.com/burnt-labs/cw-counter) contract as a base for our contract. Run the following command to create a new contract:
```sh
cargo generate --git https://github.com/burnt-labs/cw-counter --name token_factory_contract
cd token-factory-contract
```
### **Implementing Core Logic**
#### **Remove some files**
We need to remove the **src/helpers.rs** and **src/integration\_test.rs** files. Then update the **lib.rs** file with the following:
```rust
pub mod contract;
mod error;
pub mod msg;
pub mod state;
pub use crate::error::ContractError;
```
#### **Store the Token Address in Contract State**
Modify `state.rs` to store the **token denomination** in the contract’s persistent storage.
```rust
use cw_storage_plus::Item;
pub const TOKEN_DENOM: Item<String> = Item::new("token_denom");
```
#### **Define Contract Messages**
Delete the contents of `src/msg.rs` and replace with the following:
```rust
use cosmwasm_schema::{cw_serde, QueryResponses};
#[cw_serde]
pub struct InstantiateMsg {
pub token_denom: String, // Token denom (e.g., factory/xion.../mytoken)
}
#[cw_serde]
pub enum ExecuteMsg {
SendToken {
recipient: String,
},
}
#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
#[returns(BalanceResponse)]
GetBalance { address: String },
}
#[cw_serde]
pub struct BalanceResponse {
pub balance: String,
}
```
#### **Contract Logic**
Modify `src/contract.rs` to implement the **core logic of the contract**:
```rust
use cosmwasm_std::{
entry_point, to_binary, Addr, Binary, Coin, Deps, DepsMut, Env, MessageInfo, QueryRequest,
Response, StdResult, BankMsg, BankQuery, Uint128,
};
use crate::msg::{BalanceResponse, ExecuteMsg, InstantiateMsg, QueryMsg};
use crate::state::TOKEN_DENOM;
#[entry_point]
pub fn instantiate(
deps: DepsMut,
_env: Env,
_info: MessageInfo,
msg: InstantiateMsg,
) -> StdResult<Response> {
TOKEN_DENOM.save(deps.storage, &msg.token_denom)?;
Ok(Response::new()
.add_attribute("method", "instantiate")
.add_attribute("token_denom", msg.token_denom))
}
#[entry_point]
pub fn execute(
deps: DepsMut,
_env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> StdResult<Response> {
match msg {
ExecuteMsg::SendToken { recipient } => execute_send_token(deps, info, recipient),
}
}
fn execute_send_token(
deps: DepsMut,
info: MessageInfo,
recipient: String,
) -> StdResult<Response> {
if info.funds.len() != 1 {
return Err(cosmwasm_std::StdError::generic_err(
"Must send exactly one token type",
));
}
// Extract the token denomination and amount
let sent_coin = &info.funds[0];
let amount = sent_coin.amount;
let token_denom = sent_coin.denom.clone();
let transfer_msg = BankMsg::Send {
to_address: recipient.clone(),
amount: vec![Coin {
denom: token_denom.clone(),
amount,
}],
};
Ok(Response::new()
.add_message(transfer_msg)
.add_attribute("action", "send_token")
.add_attribute("sender", info.sender.to_string())
.add_attribute("recipient", recipient)
.add_attribute("amount", amount.to_string())
.add_attribute("token_denom", token_denom))
}
#[entry_point]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::GetBalance { address } => to_binary(&query_balance(deps, address)?),
}
}
fn query_balance(deps: Deps, address: String) -> StdResult<BalanceResponse> {
let token_denom = TOKEN_DENOM.load(deps.storage)?;
let balance_response: cosmwasm_std::BalanceResponse = deps.querier.query(&QueryRequest::Bank(
BankQuery::Balance {
address,
denom: token_denom.clone(),
},
))?;
Ok(BalanceResponse {
balance: balance_response.amount.amount.to_string(),
})
}
```
## **Optimize Contract**
Next, compile and optimize the smart contract using the [CosmWasm Optimizing Compiler](https://github.com/CosmWasm/optimizer). You need to have **Docker** running to execute the command below. Make sure you are in the root folder of your project and then executes the following:
```
docker run --rm -v "$(pwd)":/code \
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/optimizer:0.16.0
```
## **Upload Optimized Contract On-chain**
First, set your wallet address or key name by executing the following in your terminal:
```
WALLET="your-wallet-address-or-key-name-here"
```
Now, upload the contract to the blockchain:
```
RES=$(xiond tx wasm store ./artifacts/cw_counter.wasm \
--chain-id xion-testnet-2 \
--gas-adjustment 1.3 \
--gas-prices 0.001uxion \
--gas auto \
-y --output json \
--node https://rpc.xion-testnet-2.burnt.com:443 \
--from $WALLET)
```
After running the command, **extract the transaction hash** by executing:
```
echo $RES
```
Example output:
```
{
"height": "0",
"txhash": "B557242F3BBF2E68D228EBF6A792C3C617C8C8C984440405A578FBBB8A385035",
...
}
```
Copy the **txhash** value for the next step.
## **Retrieve the Code ID**
The **Code ID** is required for creating an instance of your contract.
Set your **transaction hash** you retrieved above by executing:
```
TXHASH="your-txhash-here"
```
Query the blockchain to get the **Code ID**:
```
CODE_ID=$(xiond query tx $TXHASH \
--node https://rpc.xion-testnet-2.burnt.com:443 \
--output json | jq -r '.events[-1].attributes[1].value')
```
Now, display the retrieved **Code ID**:
```
echo $CODE_ID
```
Example output:
```
1213
```
## **Instantiate the Contract**
To initialize the contract, set up the initialization message:
```
MSG='{ "token_denom": "factory/xion1xyz.../denom-name" }'
```
Replace `"factory/xion1xyz.../denom-name"` with the actual token factory token you created.
Now, instantiate the contract using the `CODE_ID` obtained from the previous step:
```
xiond tx wasm instantiate $CODE_ID "$MSG" \
--from $WALLET \
--label "token-factory-token" \
--gas-prices 0.025uxion \
--gas auto \
--gas-adjustment 1.3 \
-y --no-admin \
--chain-id xion-testnet-2 \
--node https://rpc.xion-testnet-2.burnt.com:443
```
Example output:
```
gas estimate: 217976
code: 0
txhash: 09D48FE11BE8D8BD4FCE11D236D80D180E7ED7707186B1659F5BADC4EC116F30
```
Copy the new **transaction hash** for the next step.
## **Retrieve the Contract Address**
Once the contract has been instantiated, it is assigned a unique contract address. Follow these steps to retrieve it:
Set the transaction hash (`TXHASH`) from the contract instantiation step:
```
TXHASH="your-txhash-here"
```
Replace `"your-txhash-here"` with the actual transaction hash from the previous step.
Query the blockchain to get the **contract address**:
```
CONTRACT=$(xiond query tx $TXHASH \
--node https://rpc.xion-testnet-2.burnt.com:443 \
--output json | jq -r '.events[] | select(.type == "instantiate") | .attributes[] | select(.key == "_contract_address") | .value')
```
Display the contract address:
```
echo $CONTRACT
```
Example output:
```
xion1v6476wrjmw8fhsh20rl4h6jadeh5sdvlhrt8jyk2szrl3pdj4musyxj6gl
```
## Querying the Contract
After deploying the contract, you can interact with it using queries.
The contract exposes a method to fetch the balance of a specific address. Define the query message in the following format:
```sh
QUERY='{"get_balance":{ "address": "add-address-here" }}'
```
Replace `"add-address-here"` with the actual address you want to check.
Run the following command to query the contract for the specified address's token balance:
```sh
xiond query wasm contract-state smart $CONTRACT "$QUERY" --output json --node https://rpc.xion-testnet-2.burnt.com:443
```
Where:
* `$CONTRACT` is the smart contract address retrieved in the previous step.
* `$QUERY` contains the request payload.
If the query is successful, you will receive a JSON response containing the balance of the specified address. Example output:
```
{
"data": {
"balance": "1000000"
}
}
```
This indicates that the queried address holds `1,000,000` units of the token.
## Execute Transactions
Now that you have an instance of the contract, you can interact with it by executing transactions. The contract accepts a message that allows token transfers from the sender to a specified recipient.
Define the message as follows:
```sh
SEND_TOKEN='{"send_token": { "recipient": "recipient-address-here" }}'
```
Replace `"recipient-address-here"` with the actual wallet address you want to send tokens to.
Run the following command to execute the token transfer:
```sh
xiond tx wasm execute $CONTRACT "$SEND_TOKEN" \
--from $WALLET \
--gas-prices 0.025uxion \
--gas auto \
--gas-adjustment 1.5 \
--amount "10factory/xion1ka5gdcv4m7kfzxkllapqdflenwe0fv8ftm357r/emp" \
-y \
--node https://rpc.xion-testnet-2.burnt.com:443 \
--chain-id xion-testnet-2
```
Where:
* `$CONTRACT`: The deployed contract address.
* `$SEND_TOKEN`: The execution message to send tokens.
* `--amount "10factory/xion1ka5gdcv4m7kfzxkllapqdflenwe0fv8ftm357r/emp"`: Specifies the token amount and denom being sent.
After executing this transaction, you can run the **get\_balance** query again to verify that the amount was deducted from your account and also check the recipient's address to see if they received the tokens.
# Websockets
A WebSocket is a communication protocol that provides two way communication channels over a single TCP connection between a client and server. This means that once a WebSocket connection is established, both the client and server can send and receive data simultaneously without the need for repeated HTTP requests.
## **Why Use WebSockets?**
WebSockets are especially valuable in situations where real-time updates are essential. Below are some typical use cases:
* **Blockchain and apps**: WebSockets allow for real-time tracking of blockchain activities, such as new transactions, changes in contract states, or validator updates.
* **Live Notifications**: Applications can instantly send notifications to users, like alerts for new messages or important updates.
* **Collaborative Tools**: Real-time collaboration platforms, such as shared document editors, depend on WebSockets to synchronize changes among users.
* **Gaming and Streaming**: WebSockets are utilized to provide real-time game state updates or to stream live video and audio content.
In the context of Verona, WebSockets enable developers to subscribe to blockchain events and receive instant updates, facilitating the development of dynamic and interactive apps.
# WebSockets: Real-Time Communication
WebSockets are a powerful tool for enabling real-time, bidirectional communication between a client and a server. Within the Verona ecosystem by integrating WebSockets, you can:
* Enable real-time updates for decentralized apps (apps).
* Stream blockchain events (e.g., transactions, smart contract triggers) to clients.
* Build interactive and responsive user experiences.
* Reduce latency compared to traditional polling methods.
## **Prerequisites**
Before diving in, ensure you have [NPM and Node.js](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) installed on your machine.:
## **Create a New Project**
### **Initialize a New Node.js Project**
Make sure you have `npm` and `node` installed. Then, create a new Node.js project by running:
```bash
npm init -y
```
This command creates a `package.json` file with default settings.
#### **Install Required Dependencies**
The most basic method for interacting with WebSockets in a Node.js environment is via the `ws` package. Install `ws` via the following command:
```bash
npm install ws
```
We will also use `uuid` to generate a unique id for our more complex example. Install `uuid` using the following command:
```bash
npm install uuid
```
### **Setting up** the Main Script
#### **Create an `index.js` File**
Within your project directory, create an `index.js.` This file will contain all the code we'll create in the sections below.
## Integrating WebSockets into your Project
Below, we’ll cover the simplest way to set up WebSockets in your JavaScript project.
### **Establishing a WebSocket Connection**
You can find websocket endpoints for any of the available Verona networks on the [Public Endpoints & Resources](/en/build-on-verona/references/public-endpoints-and-resources) page. Here’s how to connect to a Verona node using a WebSocket endpoint (add to your index.js file):
```javascript
const WebSocket = require('ws');
const socket = new WebSocket('wss://rpc.xion-testnet-2.burnt.com:443/websocket');
socket.on('open', () => {
console.log('WebSocket connection established');
});
socket.on('close', () => {
console.log('WebSocket connection closed');
});
socket.on('error', (error) => {
console.error('WebSocket error:', error);
});
```
### **Subscribing to Blockchain Events**
One of the most powerful features of WebSockets is the ability to subscribe to blockchain events in real time. For example, you can listen for new transactions or smart contract events.
#### **Example: Subscribing to New Blocks**
Add the following to your index.js file:
```javascript
socket.on('open', () => {
console.log('WebSocket connection established');
const subscribeMessage = JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'subscribe',
params: {
query: "tm.event='NewBlock'"
}
});
socket.send(subscribeMessage);
});
socket.on('message', (data) => {
console.log('Received data:', data.toString());
try {
const message = JSON.parse(data.toString());
if (message.result && message.result.data && message.result.data.value) {
console.log('New block:', message.result.data.value);
}
} catch (err) {
console.error('Error parsing message:', err);
}
});
```
### **Running the Project**
#### **Execute the Script**
Run the `index.js` script using Node.js:
```bash
node index.js
```
You should see output indicating that the connection to Websocket endpoint has been established, and new blocks and transactions will be logged to the console as they occur.
## **Example Project: Real-Time Transaction Tracker**
This code creates a real-time transaction monitoring system where any time a set address receives tokens the tracker will spit out the details of the transaction. Let's break it down section by section:
### Import and Setup
```javascript
const WebSocket = require('ws');
const { v4: uuidv4 } = require('uuid');
const address = 'xion1l5fq4la4jjh827wnay8g022z69mumz84jwl6zt';
let websocket;
let wsQuery;
```
This section imports the necessary libraries and sets up initial variables:
* The `ws` library provides WebSocket functionality for real-time communication
* `uuid` generates unique identifiers for each subscription request
* `address` stores the blockchain address we're monitoring
* `websocket` and `wsQuery` are initialized as empty variables to store the connection and query
### Main Function: queryForBalanceUpdate()
```javascript
const queryForBalanceUpdate = () => {
try {
websocket = new WebSocket('wss://rpc.xion-testnet-2.burnt.com:443/websocket');
wsQuery = {
jsonrpc: '2.0',
method: 'subscribe',
id: uuidv4().toString(),
params: {
query: `tm.event = 'Tx' AND transfer.recipient CONTAINS '${address}'`,
},
};
// Event handlers follow...
```
This function:
1. Establishes a WebSocket connection to the Verona testnet RPC endpoint
2. Creates a subscription request
3. Uses a unique ID (via UUID) for each request
4. Sets up a query filter that watches for transactions where our address appears as a recipient
### Event Handlers
```javascript
websocket.on('open', () => {
console.log('WebSocket connection established');
websocket.send(JSON.stringify(wsQuery));
});
websocket.on('message', (event) => {
const eventData = JSON.parse(event);
if (eventData && eventData.result && eventData.result.data) {
console.log('Matching transaction found: ' + JSON.stringify(eventData.result.data));
disconnectFromWebsocket();
}
});
websocket.on('error', (error) => {
console.error(error);
disconnectFromWebsocket();
});
```
These event handlers manage the WebSocket lifecycle:
* `open`: When connection is established, it sends our subscription request
* `message`: When data is received, it parses the JSON response, logs matching transactions, and disconnects
* `error`: Handles and logs any errors, then disconnects
### Cleanup Function: disconnectFromWebsocket()
```javascript
const disconnectFromWebsocket = () => {
if (!websocket || websocket.readyState !== WebSocket.OPEN) return;
websocket.send(JSON.stringify({ ...wsQuery, method: 'unsubscribe' }));
websocket.close();
websocket = null;
wsQuery = null;
};
```
This function ensures proper cleanup:
1. Checks if the connection is still open
2. Sends an unsubscribe request using the same ID as the subscription
3. Properly closes the connection
4. Resets the variables to null
### Process Management
```javascript
process.on('exit', () => {
disconnectFromWebsocket();
});
queryForBalanceUpdate();
```
The final section:
1. Sets up an event listener to clean up the connection when the Node.js process exits
2. Starts the monitoring by calling the main function
### **Putting It All Together**
#### **Example: Real-Time Transaction Tracker**
Here’s the complete example.
```javascript
// Import the WebSocket library for real-time bidirectional communication.
const WebSocket = require('ws');
// Import the UUID library to generate a unique ID for each subscription request.
const { v4: uuidv4 } = require('uuid');
// Define the address that is checked in the transactions.
const address = 'xion1...';
// Initialize websocket and wsQuery variables.
let websocket;
let wsQuery;
// This function initiates a WebSocket connection and sends a subscription request to track transactions that fulfill certain conditions.
const queryForBalanceUpdate = () => {
try {
// Open a new WebSocket connection to the specified URL.
websocket = new WebSocket('wss://rpc.xion-testnet-2.burnt.com:443/websocket');
// Define the subscription request. It asks for transactions where the recipient address, and checks for transactions to be published.
wsQuery = {
jsonrpc: '2.0',
method: 'subscribe',
id: uuidv4().toString(),
params: {
query: `tm.event = 'Tx' AND transfer.re프로젝트로 돌아가기