# Create and Deploy a Token

This guide explains how to **set up Web3.js**, **create wallets**, **query balances**, and **transfer CMX and CRC-20 tokens** on the **ClubMOS Blockchain**.

ClubMOS is **EVM-compatible**, so standard Ethereum Web3 workflows apply with minimal changes.

***

### **Step 1: Set Up Web3.js**

#### **Install Web3.js**

```bash
npm install web3
```

#### **Initialize Web3 Connection**

```javascript
const Web3 = require("web3");

// Connect to ClubMOS RPC
const web3 = new Web3("https://mainnet3.mosscan.com");
```

> ✅ Ensure your application is connected to **Chain ID 2255** when signing transactions.

***

### **Step 2: Create a New Wallet Address**

```javascript
const account = web3.eth.accounts.create();

console.log("Address:", account.address);
console.log("Private Key:", account.privateKey);
```

#### **Important Security Notes**

* Use this method for backend wallet generation only
* Never expose private keys in frontend code
* Store keys securely (HSM, vaults, encrypted storage)

***

### **Step 3: Get Transaction History (by Address)**

Web3.js does **not** provide native transaction history queries.\
You must either:

* Scan blocks manually, or
* Use **MOS Scan API** (recommended for production)

#### **Basic Block Scanning Example**

```javascript
async function getTransactions(address, startBlock, endBlock) {
  for (let i = startBlock; i <= endBlock; i++) {
    const block = await web3.eth.getBlock(i, true);
    if (!block || !block.transactions) continue;

    block.transactions.forEach(tx => {
      if (tx.from === address || tx.to === address) {
        console.log("Tx found in block", i, tx);
      }
    });
  }
}

getTransactions("0xYourAddress", 100, 120);
```

> ⚠️ For real applications, always use **MOS Scan indexing APIs** instead of block scanning.

***

### **Step 4: Get Native Coin Balance (MOS)**

```javascript
async function getBalance(address) {
  const balance = await web3.eth.getBalance(address);
  console.log(
    "Balance:",
    web3.utils.fromWei(balance, "ether"),
    "MOS"
  );
}

getBalance("0xYourAddress");
```

Returns the **MOS native coin balance** of the address.

***

### **Step 5: Get CRC-20 Token Balance**

#### **Minimal CRC-20 ABI**

```javascript
const tokenABI = [
  {
    constant: true,
    inputs: [{ name: "_owner", type: "address" }],
    name: "balanceOf",
    outputs: [{ name: "balance", type: "uint256" }],
    type: "function"
  }
];
```

#### **Fetch Token Balance**

```javascript
async function getTokenBalance(tokenAddress, userAddress) {
  const token = new web3.eth.Contract(tokenABI, tokenAddress);
  const balance = await token.methods.balanceOf(userAddress).call();
  console.log("Token Balance:", balance);
}

getTokenBalance("0xYourTokenAddress", "0xYourAddress");
```

***

### **Step 6: Transfer Native Coin (CMX)**

```javascript
async function sendMOS(from, to, amount, privateKey) {
  const tx = {
    from,
    to,
    value: web3.utils.toWei(amount, "ether"),
    gas: 21000
  };

  const signedTx = await web3.eth.accounts.signTransaction(tx, privateKey);
  const receipt = await web3.eth.sendSignedTransaction(
    signedTx.rawTransaction
  );

  console.log("Transaction Hash:", receipt.transactionHash);
}

sendMOS(
  "0xYourAddress",
  "0xReceiverAddress",
  "0.1",
  "0xYourPrivateKey"
);
```

***

### **Step 7: Transfer CRC-20 Tokens**

#### **CRC-20 Transfer ABI**

```javascript
const tokenABI = [
  {
    constant: false,
    inputs: [
      { name: "_to", type: "address" },
      { name: "_value", type: "uint256" }
    ],
    name: "transfer",
    outputs: [{ name: "", type: "bool" }],
    type: "function"
  }
];
```

#### **Send Tokens**

```javascript
async function sendToken(
  tokenAddress,
  from,
  to,
  amount,
  privateKey
) {
  const token = new web3.eth.Contract(tokenABI, tokenAddress);
  const data = token.methods.transfer(to, amount).encodeABI();

  const tx = {
    from,
    to: tokenAddress,
    data,
    gas: 100000
  };

  const signedTx = await web3.eth.accounts.signTransaction(tx, privateKey);
  const receipt = await web3.eth.sendSignedTransaction(
    signedTx.rawTransaction
  );

  console.log("Token Tx Hash:", receipt.transactionHash);
}

// Example: 1 token with 18 decimals
sendToken(
  "0xYourTokenAddress",
  "0xYourAddress",
  "0xReceiverAddress",
  "1000000000000000000",
  "0xYourPrivateKey"
);
```

***

### **Best Practices for ClubMOS Developers**

* Always confirm **Chain ID = 2255**
* Use **MOS Scan APIs** for indexing & analytics
* Never expose private keys client-side
* Test on internal environments before production
* Verify contracts on **mosscan.com**

***

### **Summary**

Using **Web3.js**, developers can easily:

* Generate wallets
* Query balances
* Transfer CMX and CRC tokens
* Build DeFi, NFT, and enterprise applications

ClubMOS combines **EVM familiarity**, **low fees**, and **developer-first tooling** for real-world blockchain adoption.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://clubmos.gitbook.io/clubmos-docs/developers/create-and-deploy-a-token.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
