Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 70 additions & 89 deletions content/en/develop/develop/tools/contracts/about-contracts.md
Original file line number Diff line number Diff line change
@@ -1,185 +1,166 @@
---
title: Get start @xpla/contracts
title: Get started with @xpla/contracts
weight: 10
type: docs
---

# About @xpladev/contracts
# About @xpla/contracts

`@xpladev/contracts` is a Solidity interface package for interacting with [CONX Chain's precompile contracts](../../../smart-contract-guide/evm/use-precompile-contract). This package provides type-safe interfaces for precompile contracts that allow access to various Cosmos SDK modules (bank, staking, governance, distribution, etc.) within the EVM environment.
`@xpla/contracts` is a collection of smart contracts for the [CONX Chain](../../../smart-contract-guide/evm/use-precompile-contract). The published package includes precompile interface sources (`.sol`) and ABIs (`.json`), providing type-safe interfaces for precompile contracts that allow access to Cosmos SDK module functionality within the EVM environment.

This documentation is based on **@xpla/contracts**.

## Overview

CONX Chain is a Cosmos SDK-based blockchain that supports Solidity smart contracts through the EVM module. Precompile contracts are pre-compiled contracts deployed at specific addresses that provide direct access to Cosmos SDK module functionality within the EVM environment.
CONX Chain is a Cosmos SDK-based blockchain that supports Solidity smart contracts through the EVM module. Precompile contracts are pre-deployed at specific addresses and expose Cosmos SDK module functionality to the EVM. The `@xpla/contracts` package provides standardized Solidity interfaces and ABIs for interacting with these precompile contracts.

## Package structure

The `@xpladev/contracts` package provides standardized Solidity interfaces for interacting with these precompile contracts.
After installation, use the following paths:

## Available Contracts
| Path | Description |
|------|-------------|
| `@xpla/contracts/precompiles/` | Solidity interface sources (`.sol`) |
| `@xpla/contracts/abi/precompiles/` | ABI as JSON (`.json`) or typed ESM (`.js` + `.d.ts`) |

### Core Interfaces
## Available precompiles

The package provides the following core precompile contract interfaces:
The package includes the following precompile interfaces:

- **IAuth** - Authentication and account management
- **IAuth** (`precompiles/auth/IAuth.sol`) – Authentication and account management
- Account address conversion between EVM and Cosmos formats
- Module account access and Bech32 prefix management
- Address format validation and conversion utilities

- **IBank** - Banking and token operations
- **IBank** (`precompiles/bank/IBank.sol`) – Banking and token operations
- Token transfers between accounts
- Balance and supply queries for any denomination
- Multi-denomination support through Coin arrays

- **IWasm** - CosmWasm contract support
- **IWasm** (`precompiles/wasm/IWasm.sol`) – CosmWasm contract support
- Contract instantiation and execution
- Cross-contract communication between EVM and Wasm
- Contract state queries and migration

- **StakingI** - Staking operations
- Validator creation and management
- Delegation, undelegation, and redelegation
- Validator and delegation queries with pagination

- **DistributionI** - Distribution and rewards
- Staking reward claims and withdrawals
- Validator commission management
- Community pool and validator rewards pool funding

- **IGov** - Governance operations
- Proposal submission and management
- Voting and deposit functionality
- Governance parameter queries

- **ISlashing** - Slashing operations
- Validator unjailing functionality
- Signing information queries
- Slashing parameter management
## Other precompiles (cosmos-evm-contracts)

### Utilities
For additional Cosmos SDK module precompiles (staking, distribution, governance, slashing, Bech32, etc.), use the [cosmos-evm-contracts](https://www.npmjs.com/package/cosmos-evm-contracts) package. CONX Chain’s EVM precompiles are compatible with these interfaces, so you can import and use them in the same project.

- **Bech32I** - Bech32 address encoding/decoding
- Hex to Bech32 address conversion
- Bech32 to hex address conversion
- Cross-chain address format support
**Included in cosmos-evm-contracts:** `bank`, `bech32`, `callbacks`, `common`, `distribution`, `erc20`, `gov`, `ics02`, `ics20`, `slashing`, `staking`, `werc20`.

## Type Definitions
Install and import in your contract as follows:

The package includes common type definitions used across contracts:
```sh
npm install cosmos-evm-contracts
```

- **common/Types.sol** - Shared types used across contracts
- Coin and DecCoin structures
- PageRequest and PageResponse for pagination
- Common enums and structs
```solidity
import "cosmos-evm-contracts/precompiles/staking/StakingI.sol";
import "cosmos-evm-contracts/precompiles/distribution/DistributionI.sol";
import "cosmos-evm-contracts/precompiles/gov/IGov.sol";
import "cosmos-evm-contracts/precompiles/slashing/ISlashing.sol";
import "cosmos-evm-contracts/precompiles/bech32/Bech32I.sol";
// Common types (structs) if needed:
// import "cosmos-evm-contracts/precompiles/common/Types.sol";
```

- **util/Types.sol** - Utility-specific types
- Specialized data structures for specific modules
- Custom type definitions for complex operations
Use **@xpla/contracts** for CONX-specific precompiles (auth, bank, wasm) and **cosmos-evm-contracts** for the rest.

## Key Features
## Key features

- **Type Safety**: Type-safe interfaces for all precompile contract functions
- **Standardized Interfaces**: Standard interfaces compatible with CONX Chain's precompile contracts
- **Easy Integration**: Easy integration with existing Solidity projects
- **Comprehensive Coverage**: Support for all major Cosmos SDK module functionalities
- **Modular Design**: Organized structure with core interfaces and utilities
- **Shared Types**: Common type definitions for consistent data handling
- **Type safety**: Type-safe interfaces for precompile contract functions
- **Dual output**: Solidity sources for compilation and ABIs for frontend/SDK use
- **Typed ABI**: ESM + TypeScript definitions for viem/ethers with inferred types
- **Easy integration**: Works with Hardhat and Foundry

# Getting Started
# Getting started

This guide will walk you through setting up a Hardhat project with `@xpla/contracts` and creating your first smart contract that interacts with CONX Chain's precompile contracts.
This guide walks through setting up a project with `@xpla/contracts` and creating a smart contract that interacts with CONX Chain precompiles.

## About This Tutorial
## About this tutorial

In this tutorial, you'll learn how to:
You will:

1. [Set up a Hardhat project](#1-set-up-your-hardhat-project)
2. [Install @xpla/contracts](#2-install-xplacontracts)
3. [Create a smart contract](#3-create-a-smart-contract)

By the end of this guide, you'll be able to create and deploy smart contracts that interact with CONX Chain's precompile contracts.

## Prerequisites

- [Node.js v22 or later](https://nodejs.org/)
- [npm or pnpm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm)
- [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm), [pnpm](https://pnpm.io/), or [yarn](https://yarnpkg.com/)
- Basic knowledge of Solidity and smart contract development

## 1. Set up Your Hardhat Project
## 1. Set up your Hardhat project

1. Create a new directory for your project:
1. Create a new directory and enter it:

```sh
mkdir my-xpla-contracts-project
cd my-xpla-contracts-project
```

2. Initialize your Hardhat project:
2. Initialize a Hardhat project:

```sh
npx hardhat --init
npx hardhat init
```

This command will prompt you with configuration options. You can accept the default answers to quickly scaffold a working setup.

Using the defaults will:
- Initialize the project in the current directory
- Use the sample project that includes the Node.js test runner and viem
- Automatically install all the required dependencies
Accept the default options to get a project with the Node.js test runner and viem. This will initialize the project and install dependencies.

## 2. Install @xpla/contracts

Install the `@xpla/contracts` package to access CONX Chain's precompile contract interfaces:
Install the package:

```sh
npm install @xpla/contracts
# or: npm install @xpla/contracts@1.9.0-rc3
```

Or if you're using pnpm:
With pnpm:

```sh
pnpm add @xpla/contracts
```

## 3. Create a Smart Contract
With yarn:

```sh
yarn add @xpla/contracts
```

## 3. Create a smart contract

Now let's create a simple smart contract that demonstrates how to use the precompile contracts. Create a new file `contracts/XplaExample.sol`:
Create `contracts/XplaExample.sol` and import the precompile interfaces from `precompiles/`:

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

import "@xpla/contracts/interfaces/IBank.sol";
import "@xpla/contracts/interfaces/IAuth.sol";
import "@xpla/contracts/util/Types.sol";
import "@xpla/contracts/precompiles/bank/IBank.sol";
import "@xpla/contracts/precompiles/auth/IAuth.sol";

contract XplaExample {
IBank public bankContract;
IAuth public authContract;

constructor() {
// Initialize precompile contract interfaces
bankContract = IBank(0x1000000000000000000000000000000000000001);
authContract = IAuth(0x1000000000000000000000000000000000000005);
}

// Function to check balance using Bank precompile

function checkBalance(address account, string memory denom) public view returns (uint256) {
uint256 balance = bankContract.balance(account, denom);
return balance;
return bankContract.balance(account, denom);
}

// Function to convert EVM address to Cosmos address

function convertAddress(address evmAddress) public view returns (string memory) {
string memory cosmosAddress = authContract.addressBytesToString(evmAddress);
return cosmosAddress;
return authContract.addressBytesToString(evmAddress);
}

// Function to get total supply of a token

function getTokenSupply(string memory denom) public view returns (uint256) {
return bankContract.supplyOf(denom);
}

// Function to get Bech32 prefix

function getBech32Prefix() public view returns (string memory) {
return authContract.bech32Prefix();
}
Expand Down
18 changes: 12 additions & 6 deletions content/en/develop/develop/tools/contracts/common-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ type: docs

# Common Examples

This page provides practical examples of how to use `@xpla/contracts` to interact with CONX Chain's precompile contracts. We'll walk through a complete staking pool implementation that demonstrates real-world usage of the StakingI interface.
This page provides practical examples of how to use CONX Chain's precompile contracts in your Solidity projects. The staking pool example uses the **StakingI** interface from the [cosmos-evm-contracts](https://www.npmjs.com/package/cosmos-evm-contracts) package (for precompiles such as staking, distribution, gov, use `cosmos-evm-contracts`; for CONX-specific auth, bank, wasm use `@xpla/contracts`).

## StakingPool Contract Example

The StakingPool contract demonstrates how to create a staking pool that utilizes CONX Chain's StakingI precompile contract to manage validator delegations while providing additional features like reward distribution and user management.
The StakingPool contract demonstrates how to create a staking pool that utilizes CONX Chain's StakingI precompile contract to manage validator delegations. The **StakingI** interface is imported from **cosmos-evm-contracts**, not from `@xpla/contracts`. Ensure both packages are installed if you use CONX-specific precompiles in the same project.

### Contract Overview

Expand All @@ -22,10 +22,16 @@ The StakingPool contract provides:

### Complete Contract Implementation

Install the **cosmos-evm-contracts** package (StakingI is not part of `@xpla/contracts`):

```sh
npm install cosmos-evm-contracts
```

```solidity
pragma solidity ^0.8.28;

import "@xpla/contracts/interfaces/StakingI.sol";
import "cosmos-evm-contracts/precompiles/staking/StakingI.sol";

/// @title Simple Staking Pool
/// @dev A simple staking pool contract utilizing StakingI interface
Expand Down Expand Up @@ -174,7 +180,7 @@ contract StakingPool {
/// @dev Query user's reward information
function pendingRewards(address _user) external view returns (uint256) {
// TBD ...
return 0
return 0;
}

/// @dev Query pool information
Expand Down Expand Up @@ -224,7 +230,7 @@ contract StakingPool {
```solidity
StakingI public constant stakingContract = StakingI(0x0000000000000000000000000000000000000800);
```
The contract directly integrates with CONX Chain's StakingI precompile contract to perform actual validator delegations.
The contract uses the **StakingI** interface from **cosmos-evm-contracts** and integrates with CONX Chain's StakingI precompile at the address above to perform actual validator delegations. Install with: `npm install cosmos-evm-contracts`.

#### 2. **Staking Function**
```solidity
Expand Down Expand Up @@ -411,4 +417,4 @@ To test the StakingPool contract:
3. **Flexible Configuration**: Pool manager can adjust reward rates
4. **Event Tracking**: Comprehensive event emission for frontend integration

This example demonstrates how to effectively use `@xpla/contracts` to build complex DeFi applications that integrate seamlessly with CONX Chain's native functionality.
This example demonstrates how to use **cosmos-evm-contracts** (StakingI) together with CONX Chain's precompiles to build DeFi applications that integrate with the chain's native staking functionality. Use **@xpla/contracts** for auth, bank, and wasm precompiles and **cosmos-evm-contracts** for staking, distribution, gov, slashing, and others.
Loading