import { IContract } from "../chain/contract"; import { Address, Hash, PublicClient } from "viem"; import { IRelayer } from "../chain/relayer"; import { ShielderTransaction } from "../shielder/state"; import { InjectedStorageInterface } from "../shielder/state/storageSchema"; import { Calldata } from "../shielder/actions"; import { CustomError } from "ts-custom-error"; export type ShielderOperation = "shield" | "withdraw" | "sync"; export declare class OutdatedSdkError extends CustomError { constructor(); } export interface ShielderCallbacks { /** * Fired after the calldata is generated. * @param calldata - calldata generated for the operation with some metadata * @param {ShielderOperation} operation - operation type */ onCalldataGenerated?: (calldata: Calldata, operation: ShielderOperation) => unknown; /** * Fired after the calldata is sent to chain/relayer. * @param txHash - transaction hash * @param {ShielderOperation} operation - operation type */ onCalldataSent?: (txHash: `0x${string}`, operation: ShielderOperation) => unknown; /** * Fired after the new transaction is found and tracked by shielder client. * This is the intended way to track the new transactions. * Note, that this callback may be called multiple times for the same transaction. * @param tx - new transaction */ onNewTransaction?: (tx: ShielderTransaction) => unknown; /** * Fired when an error occurs during the generation or sending of the calldata, or during syncing. */ onError?: (error: unknown, stage: "generation" | "sending" | "syncing", operation: ShielderOperation) => unknown; } export type SendShielderTransaction = (params: { data: `0x${string}`; to: `0x${string}`; value: bigint; }) => Promise; export type QuotedFees = { baseFee: bigint; relayFee: bigint; totalFee: bigint; }; /** * Factory method to create ShielderClient with the original configuration * @param {`0x${string}`} accountPrivateKey - private key of the account, in 32-byte hex format of ethereum's private key * @param {`0x${string}`} shielderSeedPrivateKey - seed private key for the shielder account, in 32-byte hex format of ethereum's private key * @param {number} chainId - chain id of the blockchain * @param {string} rpcHttpEndpoint - rpc http endpoint of the blockchain * @param {Address} contractAddress - address of the shielder contract * @param {Address} relayerAddress - address of the relayer * @param {string} relayerUrl - url of the relayer * @param {InjectedStorageInterface} storage - storage interface to manage the shielder state, must be isolated per shielder account * @param {ShielderCallbacks} callbacks - callbacks for the shielder actions */ export declare const createShielderClient: (shielderSeedPrivateKey: `0x${string}`, chainId: number, rpcHttpEndpoint: string, contractAddress: Address, relayerAddress: Address, relayerUrl: string, storage: InjectedStorageInterface, callbacks?: ShielderCallbacks) => ShielderClient; export declare class ShielderClient { private stateManager; private stateSynchronizer; private newAccountAction; private depositAction; private withdrawAction; private callbacks; private relayer; private publicClient?; /** * Creates a new ShielderClient instance. * Please use the factory method `create` to create the instance. This constructor is not meant to be used directly. * @param {`0x${string}`} shielderSeedPrivateKey - seed private key for the shielder account, in 32-byte hex format of ethereum's private key * @param {IContract} contract - shielder contract, initialized with the public account actions * @param {IRelayer} relayer - relayer instance * @param {InjectedStorageInterface} storage - storage interface to manage the shielder state, must be isolated per shielder account * @param {ShielderCallbacks} callbacks - callbacks for the shielder actions * @param {PublicClient} [publicClient] - viem's public client instance, used for waiting for the transaction receipt */ constructor(shielderSeedPrivateKey: `0x${string}`, contract: IContract, relayer: IRelayer, storage: InjectedStorageInterface, publicClient: PublicClient, callbacks?: ShielderCallbacks); /** * Get the fees for the withdraw operation. * @returns quoted fees for the withdraw operation */ getWithdrawFees(): Promise; /** * Syncs the shielder state with the blockchain. * Emits callbacks for the newly synced transaction. * Might have side effects, as it mutates the shielder state. * For the fresh storage and existing account being imported, it goes through the whole * shielder transactions history and updates the state, so it might be slow. * @returns new transactions, which were not yet synced * @throws {OutdatedSdkError} if cannot sync state due to unsupported contract version */ syncShielder(): Promise; /** * Get the current account state. * @returns the current account state */ accountState(): Promise; /** * Get the whole shielder transactions history. * Note, this method should be used with caution, * as it may fetch and return a large amount of data. * Instead, consider using callback `onNewTransaction` to track the new transactions. * @returns the shielder transactions * @throws {OutdatedSdkError} if cannot sync state due to unsupported contract version */ scanChainForShielderTransactions(): AsyncGenerator; /** * Shield `amount` to the shielder account. Under the hood, it either creates a new account or deposits to the existing account. * Emits callbacks for the shielder actions. * Mutates the shielder state. * @param {bigint} amount - amount to shield, in wei * @param {SendShielderTransaction} sendShielderTransaction - function to send the shielder transaction to the blockchain * @param {`0x${string}`} from - public address of the sender * @returns transaction hash of the shield transaction * @throws {OutdatedSdkError} if cannot call the contract due to unsupported contract version */ shield(amount: bigint, sendShielderTransaction: SendShielderTransaction, from: `0x${string}`): Promise<`0x${string}`>; /** * Withdraw `amount` to the `address` from the shielder account. * Emits callbacks for the shielder actions. * Mutates the shielder state. * @param {bigint} amount - amount to withdraw, in wei * @param {bigint} totalFee - total fee that is deducted from amount, in wei, supposedly a sum of base fee and relay fee * @param {`0x${string}`} address - public address of the recipient * @returns transaction hash of the withdraw transaction * @throws {OutdatedSdkError} if cannot call the relayer due to unsupported contract version */ withdraw(amount: bigint, totalFee: bigint, address: Address): Promise<`0x${string}`>; private newAccount; private deposit; private handleCalldata; private handleVersionErrors; }