Smart Account SDK Guide
Building on the Protocol-Enforced L2
The OCS SDK allows you to interact with the OCS Network's native Account Abstraction layer. Unlike standard EVM chains, you must handle Protocol-Level Rejections when a transaction violates user policy.
1. Client Initialization
Connect to the OCS L2 using the user's existing L1 signer (e.g., Metamask/Wagmi). The SDK automatically resolves their L2 Smart Account address.
initialization.ts
import { OCSClient } from '@ocs-network/sdk';
// Initialize with standard L1 signer
const ocsClient = new OCSClient({
rpcUrl: 'https://rpc.testnet.ocs.network',
signer: window.ethereum.signer,
});
// Get the user's deterministic L2 Smart Account
const smartAccount = await ocsClient.getSmartAccountAddress();
console.log("L2 Vault Address:", smartAccount);2. Setting On-Chain Policy (P0)
Write immutable rules directly to the Smart Account storage. The Sequencer must enforce these rules for all future transactions.
set-policy.ts
import { PolicyType } from '@ocs-network/sdk';
// Example: Set an Address Whitelist (B2)
const tx = await ocsClient.policy.setPolicy({
type: PolicyType.AddressWhitelist,
data: {
action: 'ADD',
address: '0xRecipientAddress...',
label: 'Trusted Exchange Withdrawal',
requireHardwareCoApproval: true // Optional P0 Feature
}
});
await tx.wait();
console.log("Policy Enforced on L2.");3. Handling Native Rejection
On the OCS Network, a transaction can fail before execution if it violates policy. You must handle the specific POLICY_REJECTED status.
execute-transaction.ts
try {
const tx = await ocsClient.transfer({
to: '0xSuspiciousAddress...',
value: parseEther('10.0')
});
// Wait for Sequencer Policy Check
const receipt = await tx.waitWithPolicyStatus();
if (receipt.status === 'POLICY_REJECTED') {
// The transaction was dropped from the mempool
alert(`Blocked by OCS Protocol: ${receipt.policyViolationReason}`);
}
} catch (error) {
console.error("Standard EVM Error:", error);
}Ready to test your integration?
Get Testnet Tokens