|
| 1 | +--- |
| 2 | +description: Learn how to use the spending limit scopes for a delegation |
| 3 | +sidebar_position: 1 |
| 4 | +sidebar_label: Spending limit scopes |
| 5 | +--- |
| 6 | + |
| 7 | +# Spending limit scopes |
| 8 | + |
| 9 | +Spending limit scopes let you define how much a delegate can spend in native, ERC-20, or ERC-721 tokens. |
| 10 | +The toolkit provides multiple types of spending limit scopes tailored for different use cases. |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +- [Install and set up the Delegation Toolkit.](../../get-started/install) |
| 15 | +- [Configure the Delegation Toolkit](../../configure). |
| 16 | +- [Create a MetaMask smart account](../execute-on-smart-accounts-behalf#3-create-a-delegator-account) to create delegations. |
| 17 | +- [Create a delegate account](../execute-on-smart-accounts-behalf#4-create-a-delegate-account) |
| 18 | + |
| 19 | +## ERC-20 periodic scope |
| 20 | + |
| 21 | +This scope ensures that ERC-20 token transfers remain within a predefined limit during a specified time |
| 22 | +window. At the start of each new period, the transfer allowance resets. For example, Alice can create a delegation |
| 23 | +that allows Bob to spend 10 USDC on her behalf each day, week, or month. |
| 24 | + |
| 25 | +When this scope is applied, the toolkit automatically sets the native token transfer limit |
| 26 | +to `0`, meaning native token transfers are disabled. |
| 27 | + |
| 28 | +Internally, the scope uses combination of [`erc20PeriodTransfer`](../../../reference/caveats#erc20periodtransfer), and [`valueLte`](../../../reference/caveats#valuelte) caveat enforcers. |
| 29 | + |
| 30 | +```typescript |
| 31 | +import { createDelegation } from "@metamask/delegatino-toolkit"; |
| 32 | + |
| 33 | +const delegation = createDelegation({ |
| 34 | + scope: { |
| 35 | + type: "erc20PeriodTransfer", |
| 36 | + tokenAddress: "0xb4aE654Aca577781Ca1c5DE8FbE60c2F423f37da", |
| 37 | + periodAmount: 1000000000000000000n, |
| 38 | + periodDuration: 86400, |
| 39 | + startDate: 1743763600, |
| 40 | + }, |
| 41 | + to: delegateaAccount, |
| 42 | + from: delegatorAccount, |
| 43 | +}); |
| 44 | +``` |
| 45 | + |
| 46 | +## ERC-20 streaming scope |
| 47 | + |
| 48 | +This scopes ensures a linear streaming transfer limit for ERC-20 tokens. Token transfers are blocked until the defined |
| 49 | +start timestamp. At the start, a specified initial amount is released, after which tokens accrue linearly at the |
| 50 | +configured rate, up to the maximum allowed amount. For example, Alice can create delegation that allows Bob to spend |
| 51 | +0.1 USDC per second, starting with an initial amount of 10 USDC, up to a maximum of 100 USDC. |
| 52 | + |
| 53 | +When this scope is applied, the toolkit automatically sets the native token transfer limit |
| 54 | +to `0`, meaning native token transfers are disabled. |
| 55 | + |
| 56 | +Internally, the scope uses combination of [`erc20Streaming`](../../../reference/caveats#erc20streaming), and [`valueLte`](../../../reference/caveats#valuelte) caveat enforcers. |
| 57 | + |
| 58 | +```typescript |
| 59 | + |
| 60 | +import { createDelegation } from "@metamask/delegatino-toolkit"; |
| 61 | + |
| 62 | +const delegation = createDelegation({ |
| 63 | + scope: { |
| 64 | + type: "erc20Streaming", |
| 65 | + tokenAddress: "0xc11F3a8E5C7D16b75c9E2F60d26f5321C6Af5E92", |
| 66 | + amountPerSecond: 100n, |
| 67 | + initialAmount: 1000000n, |
| 68 | + maxAmount: 10000000n, |
| 69 | + startTime: 1703980800, |
| 70 | + }, |
| 71 | + to: delegateaAccount, |
| 72 | + from: delegatorAccount, |
| 73 | +}); |
| 74 | +``` |
| 75 | + |
| 76 | +## ERC-20 transfer scope |
| 77 | + |
| 78 | +This scope ensures that ERC-20 token transfers are limited to a predefined maximum amount. This scope is useful for |
| 79 | +setting simple, fixed transfer limits without any time based or streaming conditions. For example, Alice can create |
| 80 | +a delegation that allows Bob to spend up to 10 USDC without any conditions. Bob may use the 10 USDC in a single |
| 81 | +transaction or make multiple transactions, as long as the total does not exceed 10 USDC. |
| 82 | + |
| 83 | +When this scope is applied, the toolkit automatically sets the native token transfer limit |
| 84 | +to `0`, meaning native token transfers are disabled. |
| 85 | + |
| 86 | +Internally, the scope uses combination of [`erc20TransferAmount`](../../../reference/caveats#erc20transferamount), and [`valueLte`](../../../reference/caveats#valuelte) caveat enforcers. |
| 87 | + |
| 88 | +```typescript |
| 89 | +import { createDelegation } from "@metamask/delegatino-toolkit"; |
| 90 | + |
| 91 | +const delegation = createDelegation({ |
| 92 | + scope: { |
| 93 | + type: "erc20TransferAmount", |
| 94 | + tokenAddress: "0xc11F3a8E5C7D16b75c9E2F60d26f5321C6Af5E92", |
| 95 | + maxAmount: 10000n, |
| 96 | + }, |
| 97 | + to: delegateaAccount, |
| 98 | + from: delegatorAccount, |
| 99 | +}); |
| 100 | +``` |
| 101 | + |
| 102 | +## ERC-721 scope |
| 103 | + |
| 104 | +This scope limits the delegation to ERC-721 token transfers only. Internally, the scope uses [`erc721Transfer`](../../../reference/caveats#erc721transfer) caveat enforcer. |
| 105 | +For example, Alice can create a delegation that allows Bob to transfer an NFT she owns on her behalf. |
| 106 | + |
| 107 | +```typescript |
| 108 | +import { createDelegation } from "@metamask/delegatino-toolkit"; |
| 109 | + |
| 110 | +const delegation = createDelegation({ |
| 111 | + scope: { |
| 112 | + type: "erc721Transfer", |
| 113 | + tokenAddress: "0x3fF528De37cd95b67845C1c55303e7685c72F319", |
| 114 | + tokenId: 1n, |
| 115 | + }, |
| 116 | + to: delegateaAccount, |
| 117 | + from: delegatorAccount, |
| 118 | +}); |
| 119 | +``` |
| 120 | + |
| 121 | +## Native token periodic scope |
| 122 | + |
| 123 | +This scope ensures that native token transfers remain within a predefined limit during a specified time |
| 124 | +window. At the start of each new period, the transfer allowance resets. For example, Alice can create a delegation |
| 125 | +that allows Bob to spend 0.01 ETH on her behalf each day, week, or month. |
| 126 | + |
| 127 | +When this scope is applied, the toolkit automatically sets the allowed calldata to `0x`, disabling |
| 128 | +the ERC-20 and ERC-721 token transfers. |
| 129 | + |
| 130 | +Internally, the scope uses combination of [`exactCalldata`](../../../reference/caveats#exactcalldata), and [`nativeTokenPeriodTransfer`](../../../reference/caveats#nativetokenperiodtransfer) caveat enforcers. |
| 131 | + |
| 132 | +```typescript |
| 133 | +import { createDelegation } from "@metamask/delegatino-toolkit"; |
| 134 | + |
| 135 | +const delegation = createDelegation({ |
| 136 | + scope: { |
| 137 | + type: "nativeTokenPeriodTransfer", |
| 138 | + periodAmount: 1000000000000000000n, |
| 139 | + periodDuration: 86400, |
| 140 | + startDate: 1743763600, |
| 141 | + }, |
| 142 | + to: delegateaAccount, |
| 143 | + from: delegatorAccount, |
| 144 | +}); |
| 145 | +``` |
| 146 | + |
| 147 | +## Native token streaming scope |
| 148 | + |
| 149 | +This scopes ensures a linear streaming transfer limit for native tokens. Token transfers are blocked until the defined |
| 150 | +start timestamp. At the start, a specified initial amount is released, after which tokens accrue linearly at the |
| 151 | +configured rate, up to the maximum allowed amount. For example, Alice can create delegation that allows Bob to spend |
| 152 | +0.001 ETH per second, starting with an initial amount of 0.01 ETH, up to a maximum of 0.1 ETH. |
| 153 | + |
| 154 | +When this scope is applied, the toolkit automatically sets the allowed calldata to `0x`, disabling |
| 155 | +the ERC-20 and ERC-721 token transfers. |
| 156 | + |
| 157 | +Internally, the scope uses combination of [`exactCalldata`](../../../reference/caveats#exactcalldata), and [`nativeTokenStreaming`](../../../reference/caveats#nativetokenstreaming) caveat enforcers. |
| 158 | + |
| 159 | +```typescript |
| 160 | +import { createDelegation } from "@metamask/delegatino-toolkit"; |
| 161 | + |
| 162 | +const delegation = createDelegation({ |
| 163 | + scope: { |
| 164 | + type: "nativeTokenStreaming", |
| 165 | + amountPerSecond: 100n, |
| 166 | + initialAmount: 1000000n, |
| 167 | + maxAmount: 10000000n, |
| 168 | + startTime: 1703980800, |
| 169 | + }, |
| 170 | + to: delegateaAccount, |
| 171 | + from: delegatorAccount, |
| 172 | +}); |
| 173 | +``` |
| 174 | + |
| 175 | +## Native token transfer scope |
| 176 | + |
| 177 | +This scope ensures that native token transfers are limited to a predefined maximum amount. This scope is useful for setting |
| 178 | +simple, fixed transfer limits without any time based or streaming conditions. For example, Alice can create |
| 179 | +a delegation that allows Bob to spend up to 0.1 ETH without any conditions. Bob may use the 0.1 ETH in a single |
| 180 | +transaction or make multiple transactions, as long as the total does not exceed 0.1 ETH. |
| 181 | + |
| 182 | +When this scope is applied, the toolkit automatically sets the allowed calldata to `0x`, disabling |
| 183 | +the ERC-20 and ERC-721 token transfers. |
| 184 | + |
| 185 | +Internally, the scope uses combination of [`exactCalldata`](../../../reference/caveats#exactcalldata), and [`nativeTokenTransferAmount`](../../../reference/caveats#nativetokentransferamount) caveat enforcers. |
| 186 | + |
| 187 | +```typescript |
| 188 | +import { createDelegation } from "@metamask/delegatino-toolkit"; |
| 189 | + |
| 190 | +const delegation = createDelegation({ |
| 191 | + scope: { |
| 192 | + type: "nativeTokenTransferAmount", |
| 193 | + maxAmount: 1000000n, |
| 194 | + }, |
| 195 | + to: delegateaAccount, |
| 196 | + from: delegatorAccount, |
| 197 | +}); |
| 198 | +``` |
| 199 | + |
| 200 | +## Next steps |
| 201 | + |
| 202 | +- See [Restrict a delegation](../restrict-delegation) to learn how to further limit the authority of a delegation. |
0 commit comments