Skip to main content

Account API Methods

The @layerg-ua-sdk/aa-sdk package provides comprehensive APIs for managing Universal Accounts in the LayerG Account Abstraction system. These APIs allow developers to create, manage, and interact with accounts across different blockchain networks.

BaseAccountAPI

The base class for all account-related APIs, providing core functionality.

Methods

getCounterFactualAddress

getCounterFactualAddress(owner: string): Promise<string>

Calculates the counterfactual address for a Universal Account before it's deployed.

Parameters:

  • owner: string - Ethereum address of the account owner

Returns:

  • Promise<string> - The calculated counterfactual address

Example:

import { BaseAccountAPI } from '@layerg-ua-sdk/aa-sdk';

const baseAccountAPI = new BaseAccountAPI(provider);
const futureAddress = await baseAccountAPI.getCounterFactualAddress('0xOwnerAddress');
console.log(`Account will be deployed at: ${futureAddress}`);

getNonce

getNonce(address: string): Promise<number>

Gets the current nonce for the specified account address.

Parameters:

  • address: string - Account address

Returns:

  • Promise<number> - Current nonce value

Example:

const nonce = await baseAccountAPI.getNonce(accountAddress);
console.log(`Current nonce: ${nonce}`);

GAccountAPI

API for managing G-type Universal Accounts, which provide advanced features like multi-signature and permissions.

Methods

createAccount

createAccount(options: CreateAccountOptions): Promise<AccountDetails>

Creates a new G-type Universal Account.

Parameters:

  • options: CreateAccountOptions - Options including:
    • owner: string - Address of the account owner
    • salt?: string - Optional salt for address generation
    • factoryAddress?: string - Optional custom factory address

Returns:

  • Promise<AccountDetails> - Details of the created account:
    • address: string - The account address
    • deployed: boolean - Whether the account is deployed on-chain
    • owner: string - Owner address

Example:

import { GAccountAPI } from '@layerg-ua-sdk/aa-sdk';

const gAccountAPI = new GAccountAPI(provider);
const account = await gAccountAPI.createAccount({
owner: '0xOwnerAddress',
salt: '0x123'
});

console.log(`Account created at: ${account.address}`);
console.log(`Deployed: ${account.deployed}`);

addPermission

addPermission(options: AddPermissionOptions): Promise<string>

Adds a new permission to the account.

Parameters:

  • options: AddPermissionOptions - Options including:
    • accountAddress: string - Account address
    • delegate: string - Address receiving permission
    • permissions: PermissionType[] - Array of permission types
    • validUntil?: number - Optional timestamp when permission expires

Returns:

  • Promise<string> - Transaction hash

Example:

const result = await gAccountAPI.addPermission({
accountAddress: '0xAccountAddress',
delegate: '0xDelegateAddress',
permissions: ['CALL_CONTRACT', 'TRANSFER_ASSET'],
validUntil: Math.floor(Date.now() / 1000) + 86400 // Valid for 24 hours
});

console.log(`Permission added, transaction: ${result}`);

revokePermission

revokePermission(options: RevokePermissionOptions): Promise<string>

Revokes a permission from the account.

Parameters:

  • options: RevokePermissionOptions - Options including:
    • accountAddress: string - Account address
    • delegate: string - Address losing permission
    • permissions: PermissionType[] - Array of permission types to revoke

Returns:

  • Promise<string> - Transaction hash

Example:

const result = await gAccountAPI.revokePermission({
accountAddress: '0xAccountAddress',
delegate: '0xDelegateAddress',
permissions: ['CALL_CONTRACT']
});

console.log(`Permission revoked, transaction: ${result}`);

getPermissions

getPermissions(accountAddress: string, delegate?: string): Promise<Permission[]>

Gets all permissions for an account, optionally filtered by delegate.

Parameters:

  • accountAddress: string - Account address
  • delegate?: string - Optional delegate address to filter by

Returns:

  • Promise<Permission[]> - Array of permission objects

Example:

const permissions = await gAccountAPI.getPermissions('0xAccountAddress');
console.log('Account Permissions:', permissions);

// Filter by specific delegate
const delegatePermissions = await gAccountAPI.getPermissions('0xAccountAddress', '0xDelegateAddress');
console.log('Delegate Permissions:', delegatePermissions);

ERCAccountAPI

API for managing ERC-4337 compatible Universal Accounts.

Methods

createAccount

createAccount(options: CreateERC4337AccountOptions): Promise<AccountDetails>

Creates a new ERC-4337 compatible Universal Account.

Parameters:

  • options: CreateERC4337AccountOptions - Options including:
    • owner: string - Address of the account owner
    • salt?: string - Optional salt for address generation
    • factoryAddress?: string - Optional custom factory address
    • implementation?: string - Optional implementation address

Returns:

  • Promise<AccountDetails> - Details of the created account

Example:

import { ERCAccountAPI } from '@layerg-ua-sdk/aa-sdk';

const ercAccountAPI = new ERCAccountAPI(provider);
const account = await ercAccountAPI.createAccount({
owner: '0xOwnerAddress'
});

console.log(`ERC-4337 account created at: ${account.address}`);

validateSignature

validateSignature(userOp: UserOperation): Promise<boolean>

Validates if a user operation has a valid signature.

Parameters:

  • userOp: UserOperation - User operation object

Returns:

  • Promise<boolean> - Whether the signature is valid

Example:

const isValid = await ercAccountAPI.validateSignature(userOp);
console.log(`Signature is valid: ${isValid}`);

SimpleAccountAPI

A simplified API for basic Universal Account operations, suitable for most dApps.

Methods

createAndFundAccount

createAndFundAccount(options: CreateAccountOptions, fundAmount?: string): Promise<AccountDetails>

Creates a new account and funds it with a specified amount.

Parameters:

  • options: CreateAccountOptions - Account creation options
  • fundAmount?: string - Optional amount to fund the account with (in wei)

Returns:

  • Promise<AccountDetails> - Details of the created account

Example:

import { SimpleAccountAPI } from '@layerg-ua-sdk/aa-sdk';

const simpleAccountAPI = new SimpleAccountAPI(provider);
const account = await simpleAccountAPI.createAndFundAccount(
{ owner: '0xOwnerAddress' },
'1000000000000000' // 0.001 ETH
);

console.log(`Account created and funded at: ${account.address}`);

transferAsset

transferAsset(options: TransferOptions): Promise<UserOperationResponse>

Transfers native or ERC-20 assets from the Universal Account.

Parameters:

  • options: TransferOptions - Options including:
    • from: string - Sender account address
    • to: string - Recipient address
    • amount: string - Amount to transfer in wei
    • tokenAddress?: string - Optional ERC-20 token address (omit for native token)

Returns:

  • Promise<UserOperationResponse> - User operation response

Example:

// Native token transfer
const nativeTransfer = await simpleAccountAPI.transferAsset({
from: accountAddress,
to: recipientAddress,
amount: '1000000000000000' // 0.001 ETH
});

// ERC-20 token transfer
const tokenTransfer = await simpleAccountAPI.transferAsset({
from: accountAddress,
to: recipientAddress,
amount: '1000000', // 1 USDC (assuming 6 decimals)
tokenAddress: '0xUSDCTokenAddress'
});

// Wait for confirmation
const receipt = await nativeTransfer.wait();
console.log(`Transfer confirmed in block ${receipt.blockNumber}`);

Error Handling

All account API methods throw standardized errors that include error codes and descriptive messages. Always implement proper error handling:

try {
const account = await gAccountAPI.createAccount({
owner: ownerAddress
});
// Process account creation
} catch (error) {
if (error.code === 'INSUFFICIENT_FUNDS') {
// Handle specific error
console.error('Not enough funds to deploy account');
} else {
console.error('Account API error:', error.message);
}
}

Next Steps

  • Learn about Contract Calls for interacting with smart contracts
  • See Providers for information on provider classes