Skip to main content

API Reference

Overview

The Atlas SDK provides a comprehensive set of methods to interact with the Atlas smart contracts deployed on various Ethereum-compatible blockchain networks. It facilitates operations such as creating and managing user and solver operations, signing transactions, sorting solver bids, and executing meta-calls. This documentation outlines the available methods, their parameters, return types, and descriptions to help developers effectively utilize the SDK in their applications.

Initialization

AtlasSdk Constructor

Initializes a new instance of the Atlas SDK with the provided Ethereum provider, chain ID, and backend configuration.

Parameters

NameTypeDescription
providerAbstractProviderAn Ethereum provider (e.g., JsonRpcProvider from ethers).
chainIdnumberThe chain ID of the blockchain network.
backendIBackendAn instance of the backend client implementing the IBackend interface.
hooksControllersIHooksControllerConstructable[]Optional array of hooks controllers for custom processing.

Returns

TypeDescription
AtlasSdkThe initialized Atlas SDK instance.

Example

import { JsonRpcProvider } from "ethers";
import { AtlasSdk } from "atlas-sdk";
import { MockBackend } from "atlas-sdk/backend";

const chainId = 11155111; // Replace with your network's chain ID
const provider = new JsonRpcProvider("https://rpc.sepolia.org/", chainId);
const backend = new MockBackend(); // Replace with your backend implementation

const sdk = new AtlasSdk(provider, chainId, backend);

Contract Retrieval

getAtlasContract

Retrieves the Atlas contract instance for a specific blockchain network.

Parameters

NameTypeDescription
chainIdnumberThe chain ID of the blockchain network.

Returns

TypeDescription
AtlasContractThe Atlas contract instance associated with chain ID.
ErrorIf the contract retrieval fails.

Example

const atlasContract = sdk.getAtlasContract(chainId);
if (!atlasContract) {
throw new Error("Failed to retrieve Atlas contract.");
}

getAtlasVerificationContract

Retrieves the Atlas Verification contract instance for a specific blockchain network.

Parameters

NameTypeDescription
chainIdnumberThe chain ID of the blockchain network.

Returns

TypeDescription
AtlasVerificationContractThe Atlas Verification contract instance.
ErrorIf the contract retrieval fails.

Example

const atlasVerifContract = sdk.getAtlasVerificationContract(chainId);
if (!atlasVerifContract) {
throw new Error("Failed to retrieve Atlas Verification contract.");
}

getSimulatorContract

Retrieves the Simulator contract instance for a specific blockchain network.

Parameters

NameTypeDescription
chainIdnumberThe chain ID of the blockchain network.

Returns

TypeDescription
SimulatorContractThe Simulator contract instance associated with chain ID.
ErrorIf the contract retrieval fails.

Example

const simulatorContract = sdk.getSimulatorContract(chainId);
if (!simulatorContract) {
throw new Error("Failed to retrieve Simulator contract.");
}

getSorterContract

Retrieves the Sorter contract instance for a specific blockchain network.

Parameters

NameTypeDescription
chainIdnumberThe chain ID of the blockchain network.

Returns

TypeDescription
SorterContractThe Sorter contract instance associated with chain ID.
ErrorIf the contract retrieval fails.

Example

const sorterContract = sdk.getSorterContract(chainId);
if (!sorterContract) {
throw new Error("Failed to retrieve Sorter contract.");
}

Simulation Methods

simulateUserOperation

Simulates a user operation by invoking the simulation function of the Simulator contract.

Parameters

NameTypeDescription
chainIdnumberThe chain ID of the blockchain network.
userOpUserOperationThe user operation to be simulated.

Returns

TypeDescription
voidIf the simulation succeeds.
ErrorIf the simulation fails.

Example

try {
await sdk.simulateUserOperation(chainId, userOp);
console.log("User operation simulation succeeded.");
} catch (error) {
console.error("User operation simulation failed:", error);
}

simulateSolverOperation

Simulates a solver operation in the context of a user operation by invoking the simulation function of the Simulator contract.

Parameters

NameTypeDescription
chainIdnumberThe chain ID of the blockchain network.
userOpUserOperationThe user operation associated with the solver operation.
solverOpSolverOperationThe solver operation to be simulated.

Returns

TypeDescription
voidIf the simulation succeeds.
ErrorIf the simulation fails.

Example

try {
await sdk.simulateSolverOperation(chainId, userOp, solverOp);
console.log("Solver operation simulation succeeded.");
} catch (error) {
console.error("Solver operation simulation failed:", error);
}

Configuration Methods

getDAppCallConfig

Retrieves the call configuration for a decentralized application (dApp) from the dApp Control contract.

Parameters

NameTypeDescription
chainIdnumberThe chain ID of the blockchain network.
dAppControlAddrstringThe Ethereum address of the dApp Control contract.

Returns

TypeDescription
bigintThe call configuration flags for the dApp.
ErrorIf retrieval fails.

Example

try {
const callConfig = await sdk.getDAppCallConfig(chainId, dAppControlAddress);
console.log("DApp Call Config:", callConfig);
} catch (error) {
console.error("Failed to get DApp call config:", error);
}

getDAppConfig

Retrieves the configuration settings for a decentralized application (dApp) based on a user operation.

Parameters

NameTypeDescription
chainIdnumberThe chain ID of the blockchain network.
userOpUserOperationThe user operation associated with the dApp configuration.
dAppControlAddrstringThe Ethereum address of the dApp Control contract.

Returns

TypeDescription
DAppConfigThe configuration settings of the dApp.
ErrorIf retrieval fails.

Example

try {
const dAppConfig = await sdk.getDAppConfig(chainId, userOp, dAppControlAddress);
console.log("DApp Config:", dAppConfig);
} catch (error) {
console.error("Failed to get DApp config:", error);
}

Nonce Management

setUserNonce

Sets the nonce for a user operation based on the current state of the blockchain.

Parameters

NameTypeDescription
chainIdnumberThe chain ID of the blockchain network.
userOpUserOperationThe user operation for which the nonce is set.

Returns

TypeDescription
voidIf the nonce is set successfully.
ErrorIf setting the nonce fails.

Example

try {
await sdk.setUserNonce(chainId, userOp);
console.log("User nonce set successfully.");
} catch (error) {
console.error("Failed to set user nonce:", error);
}

getUserNextNonce

Retrieves the next nonce for a user based on the call configuration.

Parameters

NameTypeDescription
userOpUserOperationThe user operation to determine the next nonce.

Returns

TypeDescription
bigintThe next nonce value for the user.
ErrorIf retrieval fails.

Example

try {
const nextNonce = await sdk.getUserNextNonce(userOp);
console.log("Next User Nonce:", nextNonce);
} catch (error) {
console.error("Failed to get user next nonce:", error);
}

getDAppNextNonce

Retrieves the next nonce for a decentralized application (dApp) based on the call configuration.

Parameters

NameTypeDescription
dAppOpDAppOperationThe dApp operation to determine the next nonce.

Returns

TypeDescription
bigintThe next nonce value for the dApp.
ErrorIf retrieval fails.

Example

try {
const dAppNonce = await sdk.getDAppNextNonce(dAppOp);
console.log("Next DApp Nonce:", dAppNonce);
} catch (error) {
console.error("Failed to get DApp next nonce:", error);
}

Operation Sorting

sortSolverOperations

Sorts solver operations using the Sorter contract to determine the order in which solvers should act.

Parameters

NameTypeDescription
userOpUserOperationThe user operation associated with the solver operations.
solverOpsSolverOperation[]An array of solver operations to be sorted.

Returns

TypeDescription
SolverOperation[]The sorted array of solver operations.
ErrorIf sorting fails or the Sorter contract is not found.

Example

try {
const sortedSolverOps = await sdk.sortSolverOperations(userOp, solverOps);
console.log("Sorted Solver Operations:", sortedSolverOps);
} catch (error) {
console.error("Failed to sort solver operations:", error);
}

Meta-call Execution

metacall

Executes a meta-call that bundles user operations, solver operations, and dApp operations into a single transaction.

Parameters

NameTypeDescription
userOpUserOperationThe user operation to be included in the meta-call.
solverOpsSolverOperation[]An array of solver operations to be included in the meta-call.
dAppOpDAppOperationThe dApp operation to be included in the meta-call.

Returns

TypeDescription
TransactionThe Ethereum transaction object representing the meta-call.
ErrorIf the meta-call execution fails.

Example

try {
const atlasTxHash = await sdk.metacall(userOp, sortedSolverOps, dAppOp);
console.log("Meta-call Transaction Hash:", atlasTxHash);
} catch (error) {
console.error("Failed to execute meta-call:", error);
}

Utility Functions

validateAddress

Validates whether a given string is a valid Ethereum address.

Parameters

NameTypeDescription
addressstringThe Ethereum address to validate.

Returns

TypeDescription
booleantrue if valid, false otherwise.

Example

const isValid = validateAddress("0xYourAddressHere");
console.log("Is Valid Address:", isValid);

abiEncodeUserOperation

Encodes a UserOperation using ABI encoding standards.

Parameters

NameTypeDescription
userOpUserOperationThe user operation to encode.

Returns

TypeDescription
stringThe ABI-encoded user operation as a hex string.

Example

const encodedUserOp = abiEncodeUserOperation(userOp);
console.log("Encoded User Operation:", encodedUserOp);

getCallChainHash

Computes the call chain hash for a sequence of user and solver operations to ensure operation integrity.

Parameters

NameTypeDescription
userOpUserOperationThe user operation for which the chain hash is calculated.
solverOpsSolverOperation[]An array of solver operations involved in the chain hash.
requirePreOpsbooleanWhether pre-operations are required.
dAppControlstringThe dApp control address.

Returns

TypeDescription
stringThe resulting Keccak256 hash of the operation chain as a hex string.
ErrorIf encoding fails.

Example

try {
const callChainHash = getCallChainHash(userOp, solverOps, true, dAppControlAddress);
console.log("Call Chain Hash:", callChainHash);
} catch (error) {
console.error("Failed to calculate call chain hash:", error);
}

Error Handling

All methods in the Atlas SDK throw errors when operations fail. It is essential to handle these errors appropriately to ensure the robustness of your application. Always use try-catch blocks or handle promise rejections when invoking SDK methods.

Example

try {
const atlasTxHash = await sdk.metacall(userOp, sortedSolverOps, dAppOp);
console.log("Meta-call Transaction Hash:", atlasTxHash);
} catch (error) {
console.error("Failed to execute meta-call:", error);
// Implement additional error handling logic as needed
}

Best Practices for Error Handling

  1. Log Errors: Always log errors with sufficient context to aid in debugging.
try {
// Some SDK method
} catch (error) {
console.error("Error in methodName:", error);
}
  1. Categorize Errors: Implement custom error types to categorize different kinds of errors.
class NetworkError extends Error {
constructor(message: string) {
super(message);
this.name = "NetworkError";
}
}

// Usage
try {
// Some SDK method
} catch (error) {
if (error instanceof NetworkError) {
// Handle network-specific error
} else {
// Handle other errors
}
}
  1. Retry Mechanisms: For transient errors, implement a retry mechanism with exponential backoff.
async function retryOperation(operation: () => Promise<any>, retries: number = 3): Promise<any> {
let lastError;
for (let i = 0; i < retries; i++) {
try {
return await operation();
} catch (error) {
lastError = error;
await new Promise(res => setTimeout(res, 2 ** i * 1000)); // Exponential backoff
}
}
throw lastError;
}

// Usage
try {
const result = await retryOperation(() => sdk.someMethod(params));
console.log("Operation succeeded:", result);
} catch (error) {
console.error("Operation failed after retries:", error);
}
  1. Meaningful Error Messages: Ensure that error messages are clear and actionable.
try {
// Some SDK method
} catch (error) {
throw new Error(`Failed to execute someMethod: ${error.message}`);
}
  1. Graceful Degradation: When possible, implement fallback mechanisms to provide partial functionality.
try {
const result = await sdk.primaryMethod();
} catch (error) {
console.warn("Primary method failed, falling back to secondary method:", error);
const result = await sdk.secondaryMethod();
}
  1. Context-aware Error Handling: Use context to provide more information about where and why an error occurred.
import { createLogger } from "some-logging-library";

const logger = createLogger();

try {
// Some SDK method
} catch (error) {
logger.error("Failed to execute someMethod", { error, context: "Initializing user operation" });
throw error; // or handle accordingly
}

By implementing these error handling strategies, you can create more robust applications that gracefully handle failures and provide better debugging information when issues occur.

Summary

This Methods documentation provides detailed information about the primary functions available in the Atlas SDK, including their usage, parameters, and return types. By following the examples and understanding the method functionalities, developers can effectively integrate the Atlas SDK into their applications to interact with the Atlas protocol.

For more detailed explanations and additional methods, refer to the Quickstart section.