Skip to main content

What We’ll Build

In this guide, you’ll:
  1. Set up your development environment
  2. Connect to a NEXUS node
  3. Deploy a simple counter contract
  4. Interact with your contract
By the end, you’ll have a working smart contract on Bitcoin. Let’s go!

Step 1: Prerequisites

Before you begin, ensure you have:
  • Node.js 18+ for TypeScript SDK
  • Rust 1.70+ for Rust SDK and smart contracts
  • WASM target: rustup target add wasm32-unknown-unknown

Step 2: Installation

npm install @nexus/sdk
# or
yarn add @nexus/sdk
# or
pnpm add @nexus/sdk

Step 3: Connect to NEXUS

import { NexusClient, Wallet } from '@nexus/sdk';

// Create a new wallet
const wallet = new Wallet(undefined, 'regtest');
console.log('Address:', wallet.getAddress());

// Connect to NEXUS node
const client = new NexusClient({
  rpcUrl: 'http://localhost:8545',
  network: 'regtest',
}, wallet);

// Get current block height
const height = await client.getBlockHeight();
console.log('Block height:', height);

Step 4: Deploy Your First Contract

Now the fun part - let’s deploy a simple counter contract.

4a. Create a Contract

// my_contract/src/lib.rs
use nexus_sdk::{
    solidity::{*, SafeMath},
    contract_api::ez::ret,
    require,
};
use nexus_sdk::solidity::uint256 as U256;

// State variables
static COUNTER: Mapping<&[u8], U256> = Mapping::new(b"counter");
static OWNER: Mapping<&[u8], Address> = Mapping::new(b"owner");

nexus_fn! {
    fn init() {
        let sender = Blockchain::msg.sender();
        OWNER.set(&b"val".as_slice(), sender);
        COUNTER.set(&b"val".as_slice(), U256::from(0));
        ret::u32(1)
    }
}

nexus_fn! {
    fn increment() {
        let current = COUNTER.get(&b"val".as_slice());
        let new_value = current.add(U256::from(1));
        COUNTER.set(&b"val".as_slice(), new_value);
        
        let mut out = [0u8; 32];
        new_value.to_little_endian(&mut out);
        ret::u256(&out)
    }
}

nexus_fn! {
    fn get_count() {
        let current = COUNTER.get(&b"val".as_slice());
        let mut out = [0u8; 32];
        current.to_little_endian(&mut out);
        ret::u256(&out)
    }
}

4b. Compile to WASM

cargo build --target wasm32-unknown-unknown --release

4c. Deploy

import { readFileSync } from 'fs';

const wasm = readFileSync('./target/wasm32-unknown-unknown/release/my_contract.wasm');

const result = await client.deployContract(wasm);
console.log('Contract ID:', result.contractId);

4d. Interact with Contract

// Call increment
await client.callContract(contractId, 'increment', []);

// Query count
const count = await client.queryContract(contractId, 'get_count', []);
console.log('Count:', count);

Step 5: Get Vault Address for Deposits

🎉 Congrats! You’ve deployed your first contract. Now let’s get real BTC into NEXUS. To deposit BTC/ZEC/DOGE into NEXUS, you need a vault address:
// Get vault address for deposits
const vaultInfo = await client.getVaultAddress();

console.log('Deposit Address:', vaultInfo.vaultAddress);
console.log('L2 Address:', vaultInfo.l2Address);

// Send BTC to vaultAddress to receive vSAT on L2

You Did It! 🎉

You’ve successfully:
  • ✅ Set up your NEXUS development environment
  • ✅ Connected to a NEXUS node
  • ✅ Deployed a smart contract on Bitcoin
  • ✅ Interacted with your contract

Next Steps