1. Install agw-client

To install the agw-client package, run the following command:

npm install @abstract-foundation/agw-client viem

2. Instantiate a client

Use the createAbstractClient function to instantiate a new isntance of the AbstractClient.

The signer field is the EOA that owns the Abstract Global Wallet smart contract. If this account has not yet created an Abstract Global Wallet, the first transaction they send will first deploy one, set the account as the owner and then continue with the transaction.

import { createAbstractClient } from "@abstract-foundation/agw-client";
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
import { abstractTestnet } from "viem/chains";

// As an example, we are creating a new dummy account here for the signer.
const randomPrivateKey = generatePrivateKey(); // Make a random private key
const account = privateKeyToAccount(randomPrivateKey); // Use that to create a signer

// Create a wallet client where:
// - Transactions are signed by the provided account in the signer field.
// - Transactions are sent from the AGW smart contract wallet (i.e. tx.from)
const agwClient = await createAbstractClient({
  chain: abstractTestnet,
  signer: account, // This is the AGW "owner" (the initial approved signer) of the smart contract wallet.
});

3. Use the client

Once the client is instantiated, you can use it to send transactions from the AGW smart contract wallet. The transactions are signed by the EOA account and sent from the AGW smart contract wallet.

import { getGeneralPaymasterInput } from "viem/zksync";

// This transaction does the following:
// 1. Deploys the AGW smart contract (if not already deployed), from the EOA created above.
//   - The gas fee to deploy the AGW is sponsored by a paymaster.
//   - The EOA in "account" is set as the initial approved signer of the AGW smart contract.
// 2. Uses the AGW smart contract to send a transaction
//   - Note: The first transaction occurs inside the deployment initializer inside an internal transaction.
const hash = await agwClient.sendTransaction({
  to: "0x273B3527BF5b607dE86F504fED49e1582dD2a1C6",
  data: "0x69",
  // (Optional) Paymaster to abstract gas fees
  paymaster: "0x5407B5040dec3D339A9247f3654E59EEccbb6391",
  paymasterInput: getGeneralPaymasterInput({
    innerInput: "0x",
  }),
});

console.log("Transaction sent:", `https://explorer.testnet.abs.xyz/tx/${hash}`);