Skip to main content

Protocol SDK

The Intuition Protocol SDK provides a powerful and intuitive interface for interacting with Intuition smart contracts. This package simplifies blockchain interactions by offering pre-built methods for reading from and writing to our smart contracts, handling wallet connections, and managing transactions.

Installation

npm install @intuition/protocol-sdk

Getting Started

Initialize the Protocol SDK with your configuration:

import { IntuitionProtocol } from '@intuition/protocol-sdk';

const protocol = new IntuitionProtocol({
network: 'mainnet', // or 'testnet'
rpcUrl: 'YOUR_RPC_URL', // Optional: defaults to public RPC
chainId: 1 // Ethereum mainnet
});

Wallet Connection

Connect to various wallet providers:

// Connect to MetaMask
await protocol.connectWallet('metamask');

// Connect to WalletConnect
await protocol.connectWallet('walletconnect');

// Get connected account
const account = await protocol.getAccount();

// Listen for account changes
protocol.onAccountChange((account) => {
console.log('Account changed:', account);
});

Contract Interactions

Reading Contract Data

  • getContractState - Get current contract state
  • getUserBalance - Get user's token balance
  • getTransactionHistory - Get historical transactions

Writing to Contracts

  • executeTransaction - Send transactions to contracts
  • approveSpending - Approve token spending
  • batchTransactions - Execute multiple transactions in one batch

Example Usage

Here are some common contract interaction examples:

// Read contract data
const balance = await protocol.getUserBalance({
tokenAddress: '0x...',
userAddress: '0x...'
});

// Execute a transaction
const tx = await protocol.executeTransaction({
contractAddress: '0x...',
method: 'transfer',
params: {
to: '0x...',
amount: '1000000000000000000' // 1 token in wei
}
});

// Approve token spending
const approval = await protocol.approveSpending({
tokenAddress: '0x...',
spenderAddress: '0x...',
amount: '1000000000000000000'
});

Transaction Management

The SDK provides robust transaction management features:

// Get transaction status
const status = await protocol.getTransactionStatus(txHash);

// Wait for transaction confirmation
const receipt = await protocol.waitForTransaction(txHash);

// Estimate gas costs
const gasEstimate = await protocol.estimateGas({
contractAddress: '0x...',
method: 'transfer',
params: {
to: '0x...',
amount: '1000000000000000000'
}
});

Event Listening

Listen to contract events:

// Subscribe to contract events
const subscription = protocol.subscribeToEvents({
contractAddress: '0x...',
eventName: 'Transfer',
callback: (event) => {
console.log('Transfer event:', event);
}
});

// Unsubscribe when done
subscription.unsubscribe();

Best Practices

  • Always check wallet connection status before executing transactions
  • Implement proper error handling for failed transactions
  • Use gas estimation before sending transactions
  • Handle network changes and reconnections gracefully
  • Clean up event listeners when components unmount

Security Considerations

  • Never store private keys in your application
  • Always verify contract addresses before interactions
  • Implement proper input validation for all parameters
  • Use the built-in security checks for transaction signing

Need Help?

If you need assistance with the Protocol SDK, you can: