Create delegated account

import { toAccount } from 'brewit';
import { createDelegatedAccount } from 'brewit/delegation';


const mainAccount = await toAccount({
  chainId: 8453,
  rpcEndpoint: 'https://mainnet.base.org',
  signer: privateKeyToAccount('0x...'),
  type: 'main',
  config: { validator: 'ownable' },
});



// Define the validator and policy for the delegated account
const validator = {
  address: '0xValidatorAddress', // Validator contract address
  initData: '0x...',             // Validator initialization data
  salt: '0x...',                 // Unique salt for the delegated account
};

const policyParams = {
  policy: 'spendlimit', // or 'sudo'
  tokenLimits: [{ token: '0xTokenAddress', amount: 1000000000000000000n }],
};

const txs = await createDelegatedAccount(mainAccount, validator, policyParams);

Use a delegated account

import { toAccount } from 'brewit/account';

const delegatedAccount = await toAccount({
  chainId: 8453,
  safeAddress: mainAccount.address,
  rpcEndpoint: 'https://mainnet.base.org',
  signer: privateKeyToAccount('0x...'),
  type: 'delegated',
  config: { validator: 'ownable', validatorInitData: '0x...', salt: '0x...' },
});

Send a transaction

import { createAccountClient } from 'brewit';

const client = createAccountClient(delegatedAccount, bundlerUrl);

const tx = await client.sendTransaction({
  account: account,
  to: '0x...',
  value: '0x...',   
});

Get delegated account details

import { getDelegatedAccount } from 'brewit/delegation';

// Fetch delegated account info
const delegatedInfo = await getDelegatedAccount(
  publicClient, // viem PublicClient instance
  tokens,       // Array of tokens to check
  mainAccount.address,
  validator,
  'spendlimit'  // or 'sudo'
);

Update a delegated account

import { updateDelegatedAccount } from 'brewit/delegation';

// Update the policy or validator for the delegated account
const updatedPolicyParams = {
  policy: 'spendlimit',
  tokenLimits: [{ token: '0xTokenAddress', amount: 2000000000000000000n }],
};

const updateTxs = await updateDelegatedAccount(mainAccount, updatedPolicyParams, validator);
// Send these transactions using your preferred method

Remove a delegated account

import { removeDelegatedAccount } from 'brewit/delegation';

const removeTxs = await removeDelegatedAccount(mainAccount, validator);
// Send these transactions using your preferred method