@cantor8/wallet-connect-sdk
v0.3.0
Published
C8 Wallet Connect SDK for dApp developers: postMessage API to interact with C8 Wallet.
Maintainers
Readme
C8 Wallet Connect SDK
Lightweight SDK for dApp developers to integrate the C8 Wallet via a popup window and postMessage communication.
Quick Start
Installation
npm i @cantor8/wallet-connect-sdkImport the provider into your dApp:
import { C8WalletProvider } from "./provider";Configuration
Create a new provider instance:
const c8 = new C8WalletProvider({
dappUrl: window.location.href,
dappName: "Cantor8 Wallet Connect SDK Demo",
network: "devnet",
});Events
You can subscribe to wallet events using c8.on():
c8.on("connected", (e) => console.log("Connected", e));
c8.on("disconnected", (e) => console.log("Disconnected", e));
c8.on("accountChanged", (e) => console.log("Account's balance changed", e));
c8.on("txInitiated", (e) => console.log("TX initiated", e));
c8.on("txChanged", (e) => console.log("TX changed", e));Basic Usage
const c8 = new C8WalletProvider({
dappUrl: window.location.href,
dappName: "Cantor8 Wallet Connect SDK Demo",
network: "devnet",
});
await c8.connect();
// Instruments list type: InstrumentListPayload
const instruments = await c8.getInstruments();
console.log("Instruments", instruments);
const firstInstrumentId = instruments.instruments[0]?.instrumentId;
// Accounts list type: AccountListPayload
const accounts = await c8.getAccounts(firstInstrumentId);
console.log("Accounts", accounts);
const senderPartyId = accounts.accounts[0]?.partyId;
const senderHoldingInstrumentId =
accounts.accounts[0]?.holdings[0]?.instrumentId || firstInstrumentId;
// Submit a transfer
// SubmitTransferResultPayload
const res = await c8.send({
senderPartyId,
instrumentId: senderHoldingInstrumentId,
amount: 1.23,
receiverPartyId: "Party::Receiver",
});
console.log("Send result", res); // { txId }API Reference
Public API: C8WalletProvider
| Method | Parameters | Return Type |
| --------------------- | ------------------------------------------ | --------------------------------------------------------- |
| connect() | None | Promise<void> |
| disconnect() | None | Promise<void> |
| status() | None | Promise<{ connected: boolean; walletVersion?: string }> |
| getInstruments() | None | Promise<InstrumentListPayload> |
| getAccounts() | instrumentId?: string | Promise<AccountListPayload> |
| send() | input: SendInput | Promise<SubmitTransferResultPayload> |
| signAndExecute() | input: SignAndExecuteInput | Promise<void> |
| checkTxStatusById() | input: { txId: string } | Promise<TransferStatusResponsePayload> |
| on() | event: T, cb: Listener<C8EventByType<T>> | () => boolean |
send() Input
{
senderPartyId: string;
instrumentId: string;
amount: number;
receiverPartyId: string;
memo?: string;
metadata?: Record<string, string>;
}signAndExecute() Input
{
note: string;
partyId: string;
commandId: string;
commandsJson: string;
disclosedContracts: string;
}Events Reference
Use c8.on(eventType, callback) to listen for changes emitted by the wallet.
| Event Type | Payload Properties | Description |
| ------------------- | --------------------------------------------------------------------------------------- | --------------------------------------------------------------------------- |
| connected | meta?: Json | Fired when the wallet successfully connects. |
| disconnected | reason?: string, meta?: Json | Fired when the session disconnects. |
| txInitiated | txId?: string, meta?: Json | Fired when a transaction request is initiated. |
| txChanged | txId: string, status: "pending" \| "confirmed" \| "failed" \| string, meta?: Json | Fired when a transaction's completion status updates. |
| accountChanged | accounts: AccountInfoPayload[], meta?: Json | Fired when internal account asset holdings scale or modify. |
| themeChanged | theme: "dark" \| "light", meta?: Json | Fired when the wallet interface changes its UI display theme. |
| operationCanceled | reason: string, meta?: Json | Fired when an action or modal interface is explicitly rejected by the user. |
Unsubscribing from Events
The on() method returns an unsubscribe function:
const unsubscribe = c8.on("connected", (event) => {
console.log("Connected", event);
});
unsubscribe();License
MIT
