|
| 1 | +--- |
| 2 | +title: Finance |
| 3 | +--- |
| 4 | + |
| 5 | +import { UMBRELLA_VERSION } from "../utils/constants.js"; |
| 6 | + |
| 7 | +This crate includes primitives for financial systems. |
| 8 | + |
| 9 | +## Interfaces |
| 10 | + |
| 11 | +### `IVesting` [toc] [#IVesting] |
| 12 | + |
| 13 | +<APIGithubLinkHeader |
| 14 | + moduleName="IVesting" |
| 15 | + link={`https://github.com/OpenZeppelin/cairo-contracts/blob/${UMBRELLA_VERSION}/packages/finance/src/vesting/interface.cairo`} |
| 16 | +/> |
| 17 | + |
| 18 | +```rust |
| 19 | +use openzeppelin_finance::vesting::interface::IVesting; |
| 20 | +``` |
| 21 | + |
| 22 | +Common interface for contracts implementing the vesting functionality. |
| 23 | + |
| 24 | +Functions |
| 25 | + |
| 26 | +- [`start()`](#IVesting-start) |
| 27 | +- [`cliff()`](#IVesting-cliff) |
| 28 | +- [`duration()`](#IVesting-duration) |
| 29 | +- [`end()`](#IVesting-end) |
| 30 | +- [`released(token)`](#IVesting-released) |
| 31 | +- [`releasable(token)`](#IVesting-releasable) |
| 32 | +- [`vested_amount(token, timestamp)`](#IVesting-vested_amount) |
| 33 | +- [`release(token)`](#IVesting-release) |
| 34 | + |
| 35 | +Events |
| 36 | + |
| 37 | +- [`AmountReleased(token, amount)`](#IVesting-AmountReleased) |
| 38 | + |
| 39 | +#### Functions [!toc] [#IVesting-Functions] |
| 40 | + |
| 41 | +<APIItem |
| 42 | + functionSignature="start() → u64" |
| 43 | + id="IVesting-start" |
| 44 | + kind="external" |
| 45 | +> |
| 46 | +Returns the timestamp marking the beginning of the vesting period. |
| 47 | +</APIItem> |
| 48 | + |
| 49 | +<APIItem |
| 50 | + functionSignature="cliff() → u64" |
| 51 | + id="IVesting-cliff" |
| 52 | + kind="external" |
| 53 | +> |
| 54 | +Returns the timestamp marking the end of the cliff period. |
| 55 | +</APIItem> |
| 56 | + |
| 57 | +<APIItem |
| 58 | + functionSignature="duration() → u64" |
| 59 | + id="IVesting-duration" |
| 60 | + kind="external" |
| 61 | +> |
| 62 | +Returns the total duration of the vesting period. |
| 63 | +</APIItem> |
| 64 | + |
| 65 | +<APIItem |
| 66 | + functionSignature="end() → u64" |
| 67 | + id="IVesting-end" |
| 68 | + kind="external" |
| 69 | +> |
| 70 | +Returns the timestamp marking the end of the vesting period. |
| 71 | +</APIItem> |
| 72 | + |
| 73 | +<APIItem |
| 74 | + functionSignature="released(token: ContractAddress) → u256" |
| 75 | + id="IVesting-released" |
| 76 | + kind="external" |
| 77 | +> |
| 78 | +Returns the already released amount for a given `token`. |
| 79 | +</APIItem> |
| 80 | + |
| 81 | +<APIItem |
| 82 | + functionSignature="releasable(token: ContractAddress) → u256" |
| 83 | + id="IVesting-releasable" |
| 84 | + kind="external" |
| 85 | +> |
| 86 | +Returns the amount of a given `token` that can be released at the time of the call. |
| 87 | +</APIItem> |
| 88 | + |
| 89 | +<APIItem |
| 90 | + functionSignature="vested_amount(token: ContractAddress, timestamp: u64) → u256" |
| 91 | + id="IVesting-vested_amount" |
| 92 | + kind="external" |
| 93 | +> |
| 94 | +Returns the total vested amount of a specified `token` at a given `timestamp`. |
| 95 | +</APIItem> |
| 96 | + |
| 97 | +<APIItem |
| 98 | + functionSignature="release(token: ContractAddress) → u256" |
| 99 | + id="IVesting-release" |
| 100 | + kind="external" |
| 101 | +> |
| 102 | +Releases the amount of a given `token` that has already vested and returns that amount. |
| 103 | + |
| 104 | +May emit an [AmountReleased](#IVesting-AmountReleased) event. |
| 105 | +</APIItem> |
| 106 | + |
| 107 | +#### Events [!toc] [#IVesting-Events] |
| 108 | + |
| 109 | +<APIItem |
| 110 | + functionSignature="AmountReleased(token: ContractAddress, amount: u256)" |
| 111 | + id="IVesting-AmountReleased" |
| 112 | + kind="event" |
| 113 | +> |
| 114 | +Emitted when vested tokens are released to the beneficiary. |
| 115 | +</APIItem> |
| 116 | + |
| 117 | +## [](#vesting)Vesting |
| 118 | + |
| 119 | +### `VestingComponent` [toc] [#VestingComponent] |
| 120 | + |
| 121 | +<APIGithubLinkHeader |
| 122 | + moduleName="VestingComponent" |
| 123 | + link={`https://github.com/OpenZeppelin/cairo-contracts/blob/${UMBRELLA_VERSION}/packages/finance/src/vesting/vesting.cairo`} |
| 124 | +/> |
| 125 | + |
| 126 | +```rust |
| 127 | +use openzeppelin_finance::vesting::VestingComponent; |
| 128 | +``` |
| 129 | + |
| 130 | +Vesting component implementing the [`IVesting`](#IVesting) interface. |
| 131 | + |
| 132 | +Vesting Schedule Trait Implementations |
| 133 | + |
| 134 | +#### functions [!toc] [#VestingComponent-Vesting-Schedule-functions] |
| 135 | + |
| 136 | +- [`calculate_vested_amount(self, token, total_allocation, timestamp, start, duration, cliff)`](#VestingComponent-calculate_vested_amount) |
| 137 | + |
| 138 | +Embeddable Implementations |
| 139 | + |
| 140 | +#### VestingImpl [!toc] [#VestingComponent-Embeddable-Impls-VestingImpl] |
| 141 | + |
| 142 | +- [`start(self)`](#VestingComponent-start) |
| 143 | +- [`cliff(self)`](#VestingComponent-cliff) |
| 144 | +- [`duration(self)`](#VestingComponent-duration) |
| 145 | +- [`end(self)`](#VestingComponent-end) |
| 146 | +- [`released(self, token)`](#VestingComponent-released) |
| 147 | +- [`releasable(self, token)`](#VestingComponent-releasable) |
| 148 | +- [`vested_amount(self, token, timestamp)`](#VestingComponent-vested_amount) |
| 149 | +- [`release(self, token)`](#VestingComponent-release) |
| 150 | + |
| 151 | +Internal implementations |
| 152 | + |
| 153 | +#### InternalImpl [!toc] [#VestingComponent-InternalImpl] |
| 154 | + |
| 155 | +- [`initializer(self, start, duration, cliff_duration)`](#VestingComponent-initializer) |
| 156 | +- [`resolve_vested_amount(self, token, timestamp)`](#VestingComponent-resolve_vested_amount) |
| 157 | + |
| 158 | +A trait that defines the logic for calculating the vested amount based on a given timestamp. |
| 159 | + |
| 160 | +You can read more about the trait's purpose and how to use it [here](../finance#vesting-schedule). |
| 161 | + |
| 162 | +<APIItem |
| 163 | + functionSignature="calculate_vested_amount(self: @ContractState, token: ContractAddress, total_allocation: u256, timestamp: u64, start: u64, duration: u64, cliff: u64) → u256" |
| 164 | + id="VestingComponent-calculate_vested_amount" |
| 165 | + kind="internal" |
| 166 | +> |
| 167 | +Calculates and returns the vested amount at a given `timestamp` based on the core vesting parameters. |
| 168 | +</APIItem> |
| 169 | + |
| 170 | +#### Functions [!toc] [#VestingComponent-Functions] |
| 171 | + |
| 172 | +<APIItem |
| 173 | + functionSignature="start(self: @ContractState) → u64" |
| 174 | + id="VestingComponent-start" |
| 175 | + kind="external" |
| 176 | +> |
| 177 | +Returns the timestamp marking the beginning of the vesting period. |
| 178 | +</APIItem> |
| 179 | + |
| 180 | +<APIItem |
| 181 | + functionSignature="cliff(self: @ContractState) → u64" |
| 182 | + id="VestingComponent-cliff" |
| 183 | + kind="external" |
| 184 | +> |
| 185 | +Returns the timestamp marking the end of the cliff period. |
| 186 | +</APIItem> |
| 187 | + |
| 188 | +<APIItem |
| 189 | + functionSignature="duration(self: @ContractState) → u64" |
| 190 | + id="VestingComponent-duration" |
| 191 | + kind="external" |
| 192 | +> |
| 193 | +Returns the total duration of the vesting period. |
| 194 | +</APIItem> |
| 195 | + |
| 196 | +<APIItem |
| 197 | + functionSignature="end(self: @ContractState) → u64" |
| 198 | + id="VestingComponent-end" |
| 199 | + kind="external" |
| 200 | +> |
| 201 | +Returns the timestamp marking the end of the vesting period. |
| 202 | +</APIItem> |
| 203 | + |
| 204 | +<APIItem |
| 205 | + functionSignature="released(self: @ContractState, token: ContractAddress) → u256" |
| 206 | + id="VestingComponent-released" |
| 207 | + kind="external" |
| 208 | +> |
| 209 | +Returns the already released amount for a given `token`. |
| 210 | +</APIItem> |
| 211 | + |
| 212 | +<APIItem |
| 213 | + functionSignature="releasable(self: @ContractState, token: ContractAddress) → u256" |
| 214 | + id="VestingComponent-releasable" |
| 215 | + kind="external" |
| 216 | +> |
| 217 | +Returns the amount of a given `token` that can be released at the time of the call. |
| 218 | +</APIItem> |
| 219 | + |
| 220 | +<APIItem |
| 221 | + functionSignature="vested_amount(self: @ContractState, token: ContractAddress, timestamp: u64) → u256" |
| 222 | + id="VestingComponent-vested_amount" |
| 223 | + kind="external" |
| 224 | +> |
| 225 | +Returns the total vested amount of a specified `token` at a given `timestamp`. |
| 226 | +</APIItem> |
| 227 | + |
| 228 | +<APIItem |
| 229 | + functionSignature="release(ref self: ContractState, token: ContractAddress) → u256" |
| 230 | + id="VestingComponent-release" |
| 231 | + kind="external" |
| 232 | +> |
| 233 | +Releases the amount of a given `token` that has already vested and returns that amount. |
| 234 | + |
| 235 | +If the releasable amount is zero, this function won't emit the event or attempt to transfer the tokens. |
| 236 | + |
| 237 | +Requirements: |
| 238 | + |
| 239 | +- `transfer` call to the `token` must return `true` indicating a successful transfer. |
| 240 | + |
| 241 | +May emit an [AmountReleased](#IVesting-AmountReleased) event. |
| 242 | +</APIItem> |
| 243 | + |
| 244 | +#### Internal functions [!toc] [#VestingComponent-Internal-Functions] |
| 245 | + |
| 246 | +<APIItem |
| 247 | + functionSignature="initializer(ref self: ContractState, start: u64, duration: u64, cliff_duration: u64)" |
| 248 | + id="VestingComponent-initializer" |
| 249 | + kind="internal" |
| 250 | +> |
| 251 | +Initializes the component by setting the vesting `start`, `duration` and `cliff_duration`. To prevent reinitialization, this should only be used inside of a contract's constructor. |
| 252 | + |
| 253 | +Requirements: |
| 254 | + |
| 255 | +- `cliff_duration` must be less than or equal to `duration`. |
| 256 | +</APIItem> |
| 257 | + |
| 258 | +<APIItem |
| 259 | + functionSignature="resolve_vested_amount(self: @ContractState, token: ContractAddress, timestamp: u64) → u256" |
| 260 | + id="VestingComponent-resolve_vested_amount" |
| 261 | + kind="internal" |
| 262 | +> |
| 263 | +Returns the vested amount that's calculated using the [VestingSchedule](#VestingComponent-Vesting-Schedule) trait implementation. |
| 264 | +</APIItem> |
| 265 | + |
| 266 | +### `LinearVestingSchedule` [toc] [#LinearVestingSchedule] |
| 267 | + |
| 268 | +<APIGithubLinkHeader |
| 269 | + moduleName="LinearVestingSchedule" |
| 270 | + link={`https://github.com/OpenZeppelin/cairo-contracts/blob/${UMBRELLA_VERSION}/packages/finance/src/vesting/vesting.cairo`} |
| 271 | +/> |
| 272 | + |
| 273 | +```rust |
| 274 | +use openzeppelin_finance::vesting::LinearVestingSchedule; |
| 275 | +``` |
| 276 | + |
| 277 | +Defines the logic for calculating the vested amount, incorporating a cliff period. It returns 0 before the cliff ends. After the cliff period, the vested amount returned is directly proportional to the time passed since the start of the vesting schedule. |
| 278 | + |
| 279 | +## [](#presets)Presets |
| 280 | + |
| 281 | +### `VestingWallet` [toc] [#VestingWallet] |
| 282 | + |
| 283 | +<APIGithubLinkHeader |
| 284 | + moduleName="VestingWallet" |
| 285 | + link={`https://github.com/OpenZeppelin/cairo-contracts/blob/${UMBRELLA_VERSION}/packages/presets/src/vesting.cairo`} |
| 286 | +/> |
| 287 | + |
| 288 | +```rust |
| 289 | +use openzeppelin::presets::VestingWallet; |
| 290 | +``` |
| 291 | + |
| 292 | +A non-upgradable contract leveraging [VestingComponent](#VestingComponent) and [OwnableComponent](./access#OwnableComponent). |
| 293 | + |
| 294 | +The contract is intentionally designed to be non-upgradable to ensure that neither the vesting initiator nor the vesting beneficiary can modify the vesting schedule without the consent of the other party. |
| 295 | + |
| 296 | +[Sierra class hash](../presets) |
| 297 | + |
| 298 | +```text |
| 299 | +{{VestingWalletClassHash}} |
| 300 | +``` |
| 301 | + |
| 302 | +Constructor |
| 303 | + |
| 304 | +- [`constructor(self, beneficiary, start, duration, cliff_duration)`](#VestingWallet-constructor) |
| 305 | + |
| 306 | +Embedded Implementations |
| 307 | + |
| 308 | +VestingComponent |
| 309 | + |
| 310 | +- [`VestingImpl`](#VestingComponent-Embeddable-Impls-VestingImpl) |
| 311 | + |
| 312 | +OwnableComponent |
| 313 | + |
| 314 | +- [`OwnableMixinImpl`](./access#OwnableComponent-Mixin-Impl) |
| 315 | + |
| 316 | +#### Constructor [!toc] [#VestingWallet-constructor-section] |
| 317 | + |
| 318 | +<APIItem |
| 319 | + functionSignature="constructor(ref self: ContractState, beneficiary: ContractAddress, start: u64, duration: u64, cliff_duration: u64)" |
| 320 | + id="VestingWallet-constructor" |
| 321 | + kind="constructor" |
| 322 | +> |
| 323 | +Initializes the vesting component by setting the vesting `start`, `duration` and `cliff_duration`. Assigns `beneficiary` as the contract owner and the vesting beneficiary. |
| 324 | + |
| 325 | +Requirements: |
| 326 | + |
| 327 | +- `cliff_duration` must be less than or equal to `duration`. |
| 328 | +</APIItem> |
0 commit comments