The Enclave SDK provides a set of tools to interact with the Enclave API, allowing you to manage smart accounts, build and submit transactions, calculate gas fees, and retrieve smart balances.
- Installation
- Getting Started
- Create a Smart Account
- Fund Your Smart Account
- Get Smart Balance
- Compute Quote
- Session Key Management
- Cross-Network USDC Transfer Example
To use the Enclave SDK, you need to have Node.js installed. You can install the SDK via npm:
npm install enclavemoney
To get started with the Enclave SDK, you need to initialize an instance of the Enclave
class with your API key.
import { Enclave, SignMode } from 'enclavemoney';
const API_KEY = 'your-api-key-here';
const enclave = new Enclave(API_KEY);
To create a smart account using the Enclave SDK, you can use the createSmartAccount
method. Here's an example:
// Assuming you have already initialized the Enclave instance as shown above
const privateKey = 'your-private-key-here'; // Replace with the user's private key
const wallet = new ethers.Wallet(privateKey);
const account = await enclave.createSmartAccount(wallet.address);
console.log('Smart Account created:', account.wallet.scw_address);
To fund your smart account, you can deposit USDC to the wallet address on any supported network. Follow these steps:
-
Retrieve Your Wallet Address: Use the
createSmartAccount
method to get your wallet address if you haven't already. -
Deposit USDC: Transfer USDC to your smart account's wallet address using any wallet or exchange that supports the network you are using.
-
Verify the Deposit: After the transfer, you can verify the deposit by checking the balance using the
getSmartBalance
method.
To retrieve the balance of a smart account, use the getSmartBalance
method. Here's an example:
// Assuming you have already initialized the Enclave instance as shown above
const balance = await enclave.getSmartBalance(account.wallet.scw_address);
console.log('Smart Account Balance:', balance);
To compute a quote using the Enclave SDK, you can use the computeQuote
method. Here's an example:
// Assuming you have already initialized the Enclave instance as shown above
const network = 10;
const amount = 100; // Amount to compute the quote for
const limit = 102; // Limit for the quote (optional)
const type = 'AMOUNT_OUT';
// User wants 100 USDC on OP Mainnet and is willing to spend at most 102 USDC from their balance
const quote = await enclave.computeQuote(account.wallet.scw_address, network, amount, type, limit);
console.log('Quote computed:', quote);
// Assuming you have already initialized the Enclave instance as shown above
const network = 10;
const amount = 100; // Amount to compute the quote for
const limit = 99; // Limit for the quote (optional)
const type = 'AMOUNT_IN';
// User is willing to spend at 100 USDC from their balance and wants at least 99 USDC on OP Mainnet
const quote = await enclave.computeQuote(account.wallet.scw_address, network, amount, type, limit);
console.log('Quote computed:', quote);
-
Order Types:
- AMOUNT_OUT: This type indicates that the user needs a specific amount on the target network. The user will be charged the specified amount plus any applicable fees.
- AMOUNT_IN: This type indicates that the user is willing to spend a specific amount, and they will receive the specified amount minus any applicable fees on the target network.
The Enclave SDK provides functionality to enable and disable session keys for your smart account. Session keys allow for temporary delegation of transaction signing capabilities.
To enable a session key for your smart account, use the enableSessionKey
method:
import { ethers, getBytes } from 'ethers';
// Assuming you have already initialized the Enclave instance as shown above
const validAfter = Math.floor(Date.now() / 1000); // Unix timestamp when the session key becomes valid
const validUntil = validAfter + (24 * 60 * 60); // Valid for 24 hours
const network = 10; // Network ID (e.g., 10 for Optimism)
const builtTxn = await enclave.enableSessionKey(
account.wallet.scw_address,
validAfter,
validUntil,
network
);
// Sign the message
const signature = await wallet.signMessage(getBytes(builtTxn.messageToSign));
// Submit the transaction
const response = await enclave.submitTransaction(
signature,
builtTxn.userOp,
10, // Target network
account.wallet.scw_address,
builtTxn.signMode
);
console.log('Transaction submitted successfully:', response);
To disable a previously enabled session key, use the disableSessionKey
method:
// Assuming you have already initialized the Enclave instance as shown above
const network = 10; // Network ID (e.g., 10 for Optimism)
const builtTxn = await enclave.disableSessionKey(
account.wallet.scw_address,
network
);
// Sign the message
const signature = await wallet.signMessage(getBytes(builtTxn.messageToSign));
// Submit the transaction
const response = await enclave.submitTransaction(
signature,
builtTxn.userOp,
10, // Target network
account.wallet.scw_address,
builtTxn.signMode
);
console.log('Transaction submitted successfully:', response);
To execute delegated transactions through an account once session keys are enabled:
// Submit the transaction
const response = await enclave.delegateAction(
transactionDetails,
10, // Target network
account.wallet.scw_address
);
console.log('Transaction submitted successfully:', response);
In this example, we'll demonstrate how to deposit USDC on Arbitrum and then transfer it to an address on Optimism using the Enclave SDK.
First, ensure you have deposited USDC into your smart account on the Arbitrum network. You can verify the deposit using the getSmartBalance
method.
To transfer USDC to a wallet on Optimism, you'll need to construct the calldata for the transfer and build the transaction. Here's how you can do it:
// Define the transaction details
// Define the USDC contract address on Optimism
const usdcContractAddress = '0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85';
// Define the recipient address and amount to transfer
const recipientAddress = '0x...'; // Replace with the recipient's address
const amount = ethers.utils.parseUnits('1', 6); // Amount of USDC to transfer (1 USDC)
// Create the call data for the ERC20 transfer
const erc20Interface = new ethers.utils.Interface([
'function transfer(address to, uint256 amount)'
]);
const encodedData = erc20Interface.encodeFunctionData('transfer', [recipientAddress, amount]);
const transactionDetails = [{
encodedData,
targetContractAddress: usdcContractAddress,
value: 0 // Assuming no ETH is being transferred, only USDC
}];
// Define the order data - Describes how much the user wants to spend from their chain-abstracted balances
const orderData = {
amount: amount.toString(), // Amount of USDC required for the transfer
type: 'AMOUNT_OUT'
// Understanding order type
// AMOUNT_OUT: User needs 100 USDC on the target network. The user will be charged 100 + fees.
// AMOUNT_IN : User is charged 100 USDC and receives (100 - fees) on target network
};
// Build the transaction
const builtTxn = await enclave.buildTransaction(
transactionDetails,
10, // Target network
account.wallet.scw_address, // User's smart account address
orderData,
undefined,
SignMode.ECDSA // Sign mode (Pass SignMode.SimpleSessionKey for sessionKey transaction)
);
console.log('Transaction built successfully:', builtTxn);
- Transaction Details: This includes the encoded data for the USDC transfer and the target contract address on Optimism.
- Order Data: Specifies the amount of USDC to transfer and the type of operation.
-
Build Transaction: The
buildTransaction
method constructs the transaction for the specified network (Optimism in this case).
After building the transaction, you need to sign the messageToSign
with the user's EOA (Externally Owned Account) and then submit the transaction using the Enclave SDK.
To sign the transaction, you can use the ethers
library to sign the messageToSign
with the user's private key.
import { ethers, getBytes } from 'ethers';
// Sign the message
const signature = await wallet.signMessage(getBytes(builtTxn.messageToSign));
// Submit the transaction
const response = await enclave.submitTransaction(
signature,
builtTxn.userOp,
10, // Target network
account.wallet.scw_address,
SignMode.ECDSA// Signature type (Pass SignMode.SimpleSessionKey if the signature is being generated from a sessionKey instead of the user's default EOA)
);
console.log('Transaction submitted successfully:', response);
-
Sign the Message: Use the
ethers
library to sign themessageToSign
with the user's private key. -
Submit the Transaction: Call the
submitTransaction
method on the Enclave SDK object, passing the signature and other required parameters.