Skip to main content

Allowance | How to Enable Spend Permissions

The Safe Allowance Module is a powerful tool designed to enable users to automate token transfers securely. It provides a flexible framework that allows account owners to delegate specific rights and permissions to other accounts or contracts, ensuring that funds can be transferred or utilized according to predefined conditions.

This module does not manage allowances in the traditional sense of ERC20 token contracts. Instead, it provides a method for granting authorization of specific token amounts to a delegate: (an account or a smart contract), allowing them to use the token amounts on behalf of the user. It ensures a limited access to specific tokens while facilitating automated transactions.

By leveraging the Safe Allowance Module, wallet developers can implement sophisticated financial operations with ease. Whether it's managing delegated funds for multiple users, automating payment processes or delegating to smart contracts for various usecases.

Use Cases

Recurring Transfers: Automate periodic transactions for reccuring transfers. Users can define the amount, frequency, duration, and receipients.

Subscriptions: Set up automatic payments for subscriptions, utility bills, or membership fees.

Dollar-Cost Averaging (DCA): Facilitate DCA by delegating allowances to swap contracts.

And More.. by delegating to smart contracts for specific needs.

Audits

Usage

Install

npm i abstractionkit

Import

import { AllowanceModule } from "abstractionkit";

How to use

AbstractionKit provides two types of methods for the Allowance Module

  1. The recommended methods that offer simplified interfaces. These are under Methods.
  2. Those prefixed with createBase, which match the exact contract method names and paramaters. These are under Advanced Methods.

While createBase methods allow direct interaction with the contract, they can be confusing for developers due to the multiple options and paramaters involved. The recommended methods improves usability by providing clearer and more intuitive method names and input parameters.

tip

If you're looking for a code example, take a look at Allowance Module tests

1. Initialize the Module

const allowanceModule = new AllowanceModule();

2. Enable the Module

const enableModuleMetaTransaction = allowanceModule.createEnableModuleMetaTransaction(sourceAccountAddress);

3. Add Delegate

const addDelegateMetaTransaction = 
allowanceModule.createAddDelegateMetaTransaction(
delegateAddress,
);

4. Set Allowance

In the example below, we show how to set a reccuring allowance through createRecurringAllowanceMetaTransaction. You can also use one time allowences via createOneTimeAllowanceMetaTransaction

const setAllowanceMetaTransaction = 
allowanceModule.createRecurringAllowanceMetaTransaction(
delegateAddress, // The address of the delegate to whom the recurring allowance is given.
allowanceToken, // The address of the token for which the allowance is set.
1, // The amount of the token allowed for the delegate.
3, // The time period (in minutes) after which the allowance resets.
0 // The delay in minutes before the allowance can be used.
);

5. Create UserOperation

We batch all three MetaTransactions into a single UserOperation with createUserOperation. From there, you can continue the normal flow of signing and submiting the UserOperation onchain.

const userOperation =
await allowanceSourceAccount.createUserOperation(
[enableModuleMetaTransaction, addDelegateMetaTransaction, setAllowanceMetaTransaction],
jsonRpcNodeProvider,
bundlerUrl,
);

6. The Delegate execute the Allowance

Once the allowance is set and the conditions are met, the delegate can execute the token transfer. The delegate can be an automated service, an EOA (Externally Owned Account), or a smart contract.

const allowanceTransferMetaTransaction =
allowanceModule.createAllowanceTransferMetaTransaction(
sourceAccountAddress, // The safe address from which the allowance is being transferred
allowanceToken,
transferRecipient, // The recipient address of the allowance transfer.
2, // The amount of tokens to be transferred.
delegateAddress, // The delegate address managing the transfer.
);
info

Candide Atelier offers an automation tool to execute delegated allowances. Contact us directly for Beta access.

Methods

createEnableModuleMetaTransaction

Creates a MetaTransaction to enable a specified module for a Safe account. This transaction allows the specified account to interact with the module.

keytypedescription
accountAddressstringThe target Safe Account address to enable the module for.

createAddDelegateMetaTransaction

Creates a MetaTransaction that allows adding a delegate to the account. This enables the specified delegate to perform actions on behalf of the account.

keytypedescription
delegatestringThe delegate address that should be added.

createOneTimeAllowanceMetaTransaction

Creates a MetaTransaction that sets a one-time allowance for a delegate to use specified tokens. This transaction can be scheduled to start after a certain period.

keytypedescription
delegatestringThe address of the delegate to whom the allowance is given.
tokenstringThe address of the token for which the allowance is set.
allowanceAmountbigintThe amount of the token allowed for the delegate.
startAfterInMinutesbigintThe delay in minutes before the allowance can be used.

createRecurringAllowanceMetaTransaction

Creates a MetaTransaction that sets a recurring allowance for a delegate to use specified tokens. This transaction can be scheduled to start after a certain period and will automatically reset after the specified validity period.

keytypedescription
delegatestringThe address of the delegate to whom the recurring allowance is given.
tokenstringThe address of the token for which the allowance is set.
allowanceAmountbigintThe amount of the token allowed for the delegate.
recurringAllowanceValidityPeriodInMinutesbigintThe time period (in minutes) after which the allowance resets.
startAfterInMinutesbigintThe delay in minutes before the allowance can be used.

createAllowanceTransferMetaTransaction

Creates a MetaTransaction that allows using the allowance to perform a transfer. This transaction enables the specified delegate to transfer tokens from the account's allowance.

keytypedescription
allowanceSourceSafeAddressstringThe safe address from which the allowance is being transferred.
tokenstringThe token address being transferred.
tostringThe recipient address of the allowance transfer.
amountbigintThe amount of tokens to be transferred.
delegatestringThe delegate address managing the transfer.
overrides
keytypedescription
delegateSignaturestringThe signature of the delegate. Optional.
paymentTokenstringAn optional payment token address. Defaults to the zero address.
paymentAmountbigintThe amount of the payment token to be transferred. Required if paymentToken is specified.
Optional overrides including delegate signature, payment token, and payment amount.

createRenewAllowanceMetaTransaction

Creates a MetaTransaction that allows renewing (resetting) the allowance for a specific delegate and token. This transaction enables the account to extend the delegate's allowance without having to delete and recreate it.

keytypedescription
delegatestringThe address of the delegate whose allowance should be reset.
tokenstringThe address of the token for which the allowance is set.

createDeleteAllowanceMetaTransaction

Creates a MetaTransaction that allows removing the allowance for a specific delegate and token. This transaction sets all values except the nonce to 0, effectively revoking the delegate's permission to use the allowance.

keytypedescription
delegatestringThe address of the delegate whose allowance should be removed.
tokenstringThe address of the token for which the allowance is set.

Helpers

getDelegates

Retrieves a list of delegates associated with a specified Safe account. This function allows pagination to manage large sets of delegates.

keytypedescription
nodeRpcUrlstringThe JSON-RPC API URL for the target chain.
safeAddressstringThe target account address.
overrides
keytypedescription
startbigintThe starting point for fetching delegates. Defaults to 0.
maxNumberOfResultsbigintThe maximum number of results to return. If specified, overrides pagination.
Optional parameters for pagination.

getTokensAllowance

Retrieves the allowance for a specific delegate and token associated with a specified Safe account. This function returns an object containing details about the allowance, including the amount, spent, reset time, last reset time, and nonce.

keytypedescription
nodeRpcUrlstringThe JSON-RPC API URL for the target chain.
safeAddressstringThe target account address.
delegatestringThe address of the target delegate.
tokenstringThe address of the token for which the allowance is being queried.

getTokens

Retrieves the delegated tokens for a specific delegate associated with a Safe account.

keytypedescription
nodeRpcUrlstringThe JSON-RPC API URL for the target chain.
safeAddressstringThe target account address.
delegatestringThe address of the target delegate.

IsModuleEnabled

Checks if a specified module is enabled for a given Safe account. This function returns a boolean indicating the module's status.

See the method IsModuleEnabled in the Safe Account.

createRemoveDelegateMetaTransaction

Creates a MetaTransaction that allows removing a delegate from the Safe account. It can also remove allowances if specified.

keytypedescription
delegatestringThe delegate address that should be removed.
removeAllowancesbooleanIndicator if allowances should also be removed. Set to true unless this causes an out of gas.

Advanced Methods

createBaseSetAllowanceMetaTransaction

Creates a MetaTransaction to update the allowance for a specified token. This can only be done via a Safe transaction.

keytypedescription
delegatestringThe address of the delegate whose allowance should be updated.
tokenstringThe address of the token for which the allowance is set.
allowanceAmountbigintThe amount of the token allowed for the delegate.
resetTimeMinbigintTime in minutes after which the allowance resets.
resetBaseMinbigintTime interval to increase the reset time.

createBaseExecuteAllowanceTransferMetaTransaction

Creates a MetaTransaction that allows using the allowance to perform a transfer.

keytypedescription
safeAddressstringThe Safe address whose funds should be used.
tokenstringThe token contract address being transferred.
tostringThe recipient address of the token transfer.
amountbigintThe amount of tokens to be transferred.
paymentTokenstringThe token that should be used to pay for the execution of the transfer.
paymentbigintThe amount to be paid for executing the transfer.
delegatestringThe delegate whose allowance should be updated.
delegateSignaturestringSignature generated by the delegate to authorize the transfer.

Advanced Helpers

baseGetDelegates

Retrieves a list of delegates associated with a specific Safe account. It supports pagination to manage large results.

keytypedescription
nodeRpcUrlstringThe JSON-RPC API URL for the target chain.
safeAddressstringThe target account address.
startbigintThe starting point for fetching delegates.
pageSizebigintThe number of delegates to return in one call.