Vesting

Locks up funds for a period of time and allows the owner to withdraw the funds after the lockup period

When a new employee joins an organization, they typically receive a promise of compensation to be disbursed after a specified duration of employment. This arrangement often involves the organization depositing the funds into a vesting contract, with the employee gaining access to the funds upon the completion of a predetermined lockup period. Through the utilization of vesting contracts, organizations establish a mechanism to encourage employee retention by linking financial rewards to tenure.

There are 2 actions (or endpoints) available to interact with this smart contract:

  • deposit asset
  • withdraw asset

Install package

First you can to install the @meshsdk/contracts package:

Initialize the contract

To initialize the contract, we need to initialize a provider, MeshTxBuilder and MeshVestingContract.

Both on-chain and off-chain codes are open-source and available on Mesh Github Repository.

Deposit Fund

After the lockup period has expired, the beneficiary can withdraw the funds from the vesting contract.

withdrawFund() withdraw funds from a vesting contract. The function accepts the following parameters:

  • vestingUtxo (UTxO) - unspent transaction output in the script
Deposit Fund

Deposit funds into a vesting contract with a locking period for a beneficiary

Connect wallet to run this demo

No wallets installed

Withdraw Fund

After the lockup period has expired, the beneficiary can withdraw the funds from the vesting contract.

withdrawFund() withdraw funds from a vesting contract. The function accepts the following parameters:

  • vestingUtxo (UTxO) - unspent transaction output in the script

A successful withdrawal will send the funds to the wallet that signed the transaction to withdraw the funds.

Withdraw Fund

Withdraw funds from a vesting contract

Connect wallet to run this demo

No wallets installed

Full Tutorial

Vesting contract is a smart contract that locks up funds for a period of time and allows the owner to withdraw the funds after the lockup period. Usually, vesting contract defines a beneficiary who can be different from the original owner.

When a new employee joins an organization, they typically receive a promise of compensation to be disbursed after a specified duration of employment. This arrangement often involves the organization depositing the funds into a vesting contract, with the employee gaining access to the funds upon the completion of a predetermined lockup period. Through the utilization of vesting contracts, organizations establish a mechanism to encourage employee retention by linking financial rewards to tenure.

On-Chain code

First, we define the datum's shape, as this datum serves as configuration and contains the different parameters of our vesting operation.

In this example, we define a `VestingDatum` that contains the following fields:

  • `lock_until`: The POSIX timestamp in milliseconds until which the funds are locked.
  • `owner`: The credentials (public key hash) of the owner of the funds.
  • `beneficiary`: The credentials (public key hash) of the beneficiary of the funds.

This datum can be found in `aiken-vesting/aiken-workspace/lib/vesting/types.ak`.

Next, we define the spend validator.

In this example, we define a `vesting` validator that ensures the following conditions are met:

  • The transaction must be signed by owner

Or:

  • The transaction must be signed by beneficiary
  • The transaction must be valid after the lockup period

This validator can be found in `aiken-vesting/aiken-workspace/validators/vesting.ak`.

How it works

The owner of the funds deposits the funds into the vesting contract. The funds are locked up until the lockup period expires.

Transactions can include validity intervals that specify when the transaction is valid, both from and until a certain time. The ledger verifies these validity bounds before executing a script and will only proceed if they are legitimate.

This approach allows scripts to incorporate a sense of time while maintaining determinism within the script's context. For instance, if a transaction has a lower bound `A`, we can infer that the current time is at least `A`.

It's important to note that since we don't control the upper bound, a transaction might be executed even 30 years after the vesting delay. However, from the script's perspective, this is entirely acceptable.

The beneficiary can withdraw the funds after the lockup period expires. The beneficiary can also be different from the owner of the funds.

Testing

To test the vesting contract, we have provided the a comphrehensive test script,you can run tests with `aiken check`.

The test script includes the following test cases:

  • success unlocking
  • success unlocking with only owner signature
  • success unlocking with beneficiary signature and time passed
  • fail unlocking with only beneficiary signature
  • fail unlocking with only time passed

We recommend you to check out `aiken-vesting/aiken-workspace/validators/tests/vesting.ak` to learn more.

Compile and build script

To compile the script, run the following command:

This command will generate a CIP-0057 Plutus blueprint, which you can find in `aiken-vesting/aiken-workspace/plutus.json`.

Off-Chain code

Deposit funds

First, the owner can deposit funds into the vesting contract. The owner can specify the lockup period and the beneficiary of the funds.

In this example, we deposit 10 ADA into the vesting contract. The funds are locked up for 1 minute, and the beneficiary is specified.

Then, we prepare a few variables to be used in the transaction. We get the wallet address and the UTXOs of the wallet. We also get the script address of the vesting contract, to send the funds to the script address. We also get the owner and beneficiary public key hashes.

Next, we construct the transaction to deposit the funds into the vesting contract.

In this example, we construct the transaction to deposit the funds into the vesting contract. We specify the script address of the vesting contract, the amount to deposit, and the lockup period, owner, and beneficiary of the funds.

Finally, we sign and submit the transaction.

To execute this code, ensure you have defined blockfrost key in the `.env` file. You can also define your wallet mnemonic in `aiken-vesting/src/configs.ts` file.

You can run the following command execute the deposit funds code:

Upon successful execution, you will receive a transaction hash. Save this transaction hash for withdrawing the funds.

Example of a successful deposit transaction.

Withdraw funds

After the lockup period expires, the beneficiary can withdraw the funds from the vesting contract. The owner can also withdraw the funds from the vesting contract.

First, let's look for the UTxOs containing the funds locked in the vesting contract.

In this example, we fetch the UTxOs containing the funds locked in the vesting contract. We specify the transaction hash of the deposit transaction.

Like before, we prepare a few variables to be used in the transaction. We get the wallet address and the UTXOs of the wallet. We also get the script address of the vesting contract, to send the funds to the script address. We also get the owner and beneficiary public key hashes.

Next, we prepare the datum and the slot number to set the transaction valid interval to be valid only after the slot.

In this example, we prepare the datum and the slot number to set the transaction valid interval to be valid only after the slot. We get the lockup period from the datum and set the transaction valid interval to be valid only after the lockup period.

Next, we construct the transaction to withdraw the funds from the vesting contract.

In this example, we construct the transaction to withdraw the funds from the vesting contract. We specify the UTxO containing the funds locked in the vesting contract, the script address of the vesting contract, the wallet address to send the funds to, and the transaction valid interval.

Finally, we sign and submit the transaction. Notice that since we are unlocking fund from validator, partial sign has to be specified by passing a `true` parameter into `wallet.signTx`.

To execute this code, update `aiken-vesting/src/withdraw-fund.ts` with the transaction hash from the deposit transaction. Ensure you have defined blockfrost key in the `.env` file. You can also define your wallet mnemonic in `aiken-vesting/src/configs.ts` file.

Run the following command:

Example of a successful withdraw transaction.