Guides
Step-by-step guides for Doxa development
Development Guides
This section contains detailed guides for common development tasks.
Canister Development
Creating a New Canister
- Create a new Motoko file:
actor {
private var state = 0;
public func increment() : async Nat {
state += 1;
state
};
}
- Add to
dfx.json
:
{
"canisters": {
"my_canister": {
"main": "src/my_canister.mo",
"type": "motoko"
}
}
}
- Deploy:
dfx deploy my_canister
Managing Cycles
- Check balance:
dfx canister call my_canister get_cycle_balance
- Transfer cycles:
dfx ledger transfer --amount 0.1 --memo 0 --to <CANISTER_ID>
Authentication
Implementing NFID Auth
- Add dependencies:
npm install @dfinity/agent @dfinity/auth-client
- Initialize auth:
import { AuthClient } from '@dfinity/auth-client';
const client = await AuthClient.create();
const identity = client.getIdentity();
- Make authenticated calls:
const agent = new HttpAgent({
identity,
host: 'https://ic0.app'
});
Error Handling
Best Practices
- Use Result type:
type Result<T, E> = variant {
Ok: T;
Err: E;
};
public func handle_error(err : SwapError) : Text {
switch(err) {
case (#InsufficientLiquidity) "Add more tokens to pool";
case (#ExpiredTransaction) "Retry with updated rate";
case (#AuthFailed) "Reconnect wallet";
}
};
- Log errors:
Debug.print("Error: " # debug_show(err));
Testing
Unit Tests
- Create test file:
import Debug "mo:base/Debug";
import Principal "mo:base/Principal";
actor {
public func test_create_pool() : async Bool {
let result = await create_pool(
Principal.fromText("xeka7-7iaaa-..."),
Principal.fromText("ryjl3-tyaaa-...")
);
switch(result) {
case (#Ok(_)) true;
case (#Err(_)) false;
}
};
}
- Run tests:
dfx test
Deployment
Mainnet Deployment
- Set network:
dfx network use ic
- Deploy:
dfx deploy --network ic --wallet $(dfx identity get-wallet)
- Verify:
dfx canister --network ic status <CANISTER_ID>