Coti Ethers
0
Coti Ethers 在 Cursor 中的规则与最佳实践。
Upendra Singh
1 条规则
# COTI-Ethers: Onboarding & Smart Contract Integration Guide
This rule provides essential context and best practices for building applications on the COTI blockchain using the `coti-ethers` library. It is intended for developers who want to onboard users and interact with smart contracts in a secure, user-friendly way.
## What is coti-ethers?
`coti-ethers` is a TypeScript/JavaScript library that enables seamless integration with the COTI blockchain in web applications. It provides wallet connection, transaction management, and smart contract interaction capabilities tailored for COTI's network, abstracting away low-level blockchain details.
## What does onboarding mean?
Onboarding in the COTI context refers to securely connecting a user's wallet, generating or recovering their AES key (for encrypted operations), and preparing the user for blockchain interactions. This process ensures the user is authenticated and ready to send transactions or interact with smart contracts on COTI.
## Key Concepts
- **Wallet Connection:** Use coti-ethers to connect user wallets via browser providers (e.g., MetaMask, WalletConnect) and manage session state.
- **AES Key Management:** Onboarding includes generating or recovering an AES key for the user, which is required for secure operations on COTI.
- **Smart Contract Integration:** Interact with COTI smart contracts using the abstractions provided by coti-ethers, including contract instantiation, method calls, and event listening.
- **Network Handling:** Ensure the user is connected to the correct COTI network (e.g., Testnet, Mainnet) and handle network switching gracefully.
## Example: User Onboarding Flow
Reference: @App.tsx
```tsx
import { useState } from 'react';
import { useWeb3ModalProvider } from '@web3modal/ethers/react';
import { BrowserProvider } from '@coti-io/coti-ethers';
function App() {
const [aesKey, setAesKey] = useState('');
const [txPending, setTxPending] = useState(false);
const { walletProvider } = useWeb3ModalProvider();
async function onboard() {
if (!walletProvider) throw Error('Wallet provider is undefined');
const ethersProvider = new BrowserProvider(walletProvider);
const signer = await ethersProvider.getSigner();
setTxPending(true);
await signer.generateOrRecoverAes();
const key = signer.getUserOnboardInfo();
setAesKey(key?.aesKey!);
setTxPending(false);
}
return (
<div>
<w3m-button />
<button onClick={onboard}>Onboard</button>
<h3>AES KEY: {aesKey}</h3>
{txPending && <p>Tx pending...</p>}
</div>
);
}
```
## Example: Smart Contract Integration
Reference: @coti-web3modal.ts
```ts
import { BrowserProvider, Contract } from '@coti-io/coti-ethers';
export class CotiContractManager {
private contract: Contract;
private signer: any;
constructor(address: string, abi: any, provider: BrowserProvider) {
this.contract = new Contract(address, abi, provider);
}
async initialize() {
this.signer = await this.contract.provider.getSigner();
this.contract = this.contract.connect(this.signer);
}
async transfer(to: string, amount: string) {
const gasEstimate = await this.contract.transfer.estimateGas(to, amount);
const gasLimit = Math.floor(gasEstimate.toNumber() * 1.2);
return this.contract.transfer(to, amount, { gasLimit });
}
}
```
## Best Practices
- Always validate wallet connection and network before any operation.
- Handle errors and loading states for all blockchain interactions.
- Never expose private keys or sensitive data in the frontend.
- Use TypeScript for type safety and maintainability.
- Store sensitive onboarding data (like AES keys) securely.
## File Structure Reference
- Main onboarding logic: @App.tsx
- Contract integration patterns: @coti-web3modal.ts
## Summary
Use `coti-ethers` to provide a secure, robust onboarding experience and seamless smart contract integration for your COTI-based applications. Follow the examples and best practices above to ensure a smooth user experience and reliable blockchain operations.相关插件
Hedera Toolbox↓ 1
生产级 MCP server,让 AI agent 计量访问实时 Hedera 区块链数据:查询代币价格、筛查身份、监控治理、写入防篡改的 HCS 合规记录并分析智能合约,按调用以 HBAR 微支付结算。Next.js↓ 36.8k
Next.js 在 Cursor 中的规则与最佳实践。Front End↓ 32.1k
Front End 在 Cursor 中的规则与最佳实践。Expo↓ 17.7k
Expo 在 Cursor 中的规则与最佳实践。Nuxtjs↓ 6k
Nuxtjs 在 Cursor 中的规则与最佳实践。Chrome Extension↓ 5.8k
Chrome Extension 在 Cursor 中的规则与最佳实践。Nestjs↓ 5.3k
Nestjs 在 Cursor 中的规则与最佳实践。React Native↓ 3.3k
React Native 在 Cursor 中的规则与最佳实践。