Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/vesting contract #56

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions token-vesting/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# EditorConfig is awesome: https://EditorConfig.org
root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = false

[*.ral]
indent_size = 4
2 changes: 2 additions & 0 deletions token-vesting/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/dist/
**/templates/
16 changes: 16 additions & 0 deletions token-vesting/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": [
"prettier",
"plugin:prettier/recommended",
"plugin:@typescript-eslint/recommended"
],
"rules": {
"header/header": ["off"]
},
"parserOptions": {
"project": "tsconfig.json",
"ecmaVersion": 2020,
"sourceType": "module"
},
"parser": "@typescript-eslint/parser"
}
2 changes: 2 additions & 0 deletions token-vesting/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
artifacts/
25 changes: 25 additions & 0 deletions token-vesting/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
node_modules/

# Dotfiles
.vscode
.DS_Store

# Files generated by devnet
/dev/*
!dev/user.conf

# Files generated by executing smart contracts
/artifacts/

# Config files
license-header.js
/configs/

# Tests
coverage/

# GitHub Actions
.github/

src/**/*.test.ts
**/fixtures/
68 changes: 68 additions & 0 deletions token-vesting/.project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"fullNodeVersion": "v3.10.0",
"compilerOptionsUsed": {
"ignoreUnusedConstantsWarnings": false,
"ignoreUnusedVariablesWarnings": false,
"ignoreUnusedFieldsWarnings": false,
"ignoreUnusedPrivateFunctionsWarnings": false,
"ignoreUpdateFieldsCheckWarnings": false,
"ignoreCheckExternalCallerWarnings": false,
"ignoreUnusedFunctionReturnWarnings": false
},
"infos": {
"AddVestingSchedule": {
"sourceFile": "TxScripts/addVestingSchedule.ral",
"sourceCodeHash": "52c2469bcf24d3be8856e1fa80405681395991efa71bab5d380d9d43af078e03",
"bytecodeDebugPatch": "",
"codeHashDebug": ""
},
"AddVestingScheduleWithPercentage": {
"sourceFile": "TxScripts/addVestingSchedulePercentage.ral",
"sourceCodeHash": "e796233125a44621d2daf59559e958904fdbf8fd0ee37b570d59a29ce9ab3e2b",
"bytecodeDebugPatch": "",
"codeHashDebug": ""
},
"Claim": {
"sourceFile": "TxScripts/claim.ral",
"sourceCodeHash": "78d8049ae83cc5c4595465434eef220963b67cd96d2cc73241f794f04be2271c",
"bytecodeDebugPatch": "",
"codeHashDebug": ""
},
"EndVesting": {
"sourceFile": "TxScripts/endVesting.ral",
"sourceCodeHash": "d07e59b64534ca68991f0f6dff4b9523920781ca60f0d268243356f8babdb4c8",
"bytecodeDebugPatch": "",
"codeHashDebug": ""
},
"IFungibleToken": {
"sourceFile": "../node_modules/@alephium/web3/std/fungible_token_interface.ral",
"sourceCodeHash": "62910bf11e1eeb6cb2fd468626ff606a9b06306b2b81590c3b10f6deb5966bde",
"bytecodeDebugPatch": "",
"codeHashDebug": ""
},
"Metadata": {
"sourceFile": "metadata.ral",
"sourceCodeHash": "2626cfea6c9e4d249c993c3f0065cbe129e22a1f229903606d0dbdb0da5b76a4",
"bytecodeDebugPatch": "",
"codeHashDebug": "13cc81d4d36c016bdf5376d4c9c6b917841189f137dc5009eddafe86bd53225e"
},
"Token": {
"sourceFile": "token.ral",
"sourceCodeHash": "d577ddd6751732ca003a1dfecc282fed09c96b37d6b15d4649f416a7672f43cf",
"bytecodeDebugPatch": "",
"codeHashDebug": "3ef6b0e92af5358d2f9872825f6e90387fea4a832864748d71e43437329e37e0"
},
"UserVestingSchedule": {
"sourceFile": "vesting.ral",
"sourceCodeHash": "324955185f5c4b7740bbb86603810d275f12c97b01f3b249e422214c378baeff",
"bytecodeDebugPatch": "",
"codeHashDebug": ""
},
"Vesting": {
"sourceFile": "vesting.ral",
"sourceCodeHash": "324955185f5c4b7740bbb86603810d275f12c97b01f3b249e422214c378baeff",
"bytecodeDebugPatch": "",
"codeHashDebug": "491b42d3d5349aa0a370297981f79047b6f9bd9ed85ca957c3a695caedc01147"
}
}
}
3 changes: 3 additions & 0 deletions token-vesting/.ralph-lsp/ralph.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"contractPath": "contracts"
}
25 changes: 25 additions & 0 deletions token-vesting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# My dApp Template

## Install

```
npm install
```

## Start a local devnet for testing and development

Please refer to the documentation here: https://wiki.alephium.org/full-node/devnet

## Compile

Compile the TypeScript files into JavaScript:

```
npm run compile
```

## Testing

```
npm run test
```
50 changes: 50 additions & 0 deletions token-vesting/alephium.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Configuration } from '@alephium/cli'
import { Number256 } from '@alephium/web3'

// Settings are usually for configuring
export type Settings = {
issueTokenAmount: Number256
openaiAPIKey?: string
ipfs?: {
infura?: {
projectId: string
projectSecret: string
}
}
}

const defaultSettings: Settings = {
issueTokenAmount: 100n,
openaiAPIKey: process.env.OPENAI_API_KEY || '',
ipfs: {
infura: {
projectId: process.env.IPFS_INFURA_PROJECT_ID || '',
projectSecret: process.env.IPFS_INFURA_PROJECT_SECRET || ''
}
}
}

const configuration: Configuration<Settings> = {
networks: {
devnet: {
nodeUrl: 'http://127.0.0.1:22973',
// here we could configure which address groups to deploy the contract
privateKeys: ['a642942e67258589cd2b1822c631506632db5a12aabcf413604e785300d762a5'],
settings: defaultSettings
},

testnet: {
nodeUrl: process.env.NODE_URL as string,
privateKeys: process.env.PRIVATE_KEYS === undefined ? [] : process.env.PRIVATE_KEYS.split(','),
settings: defaultSettings
},

mainnet: {
nodeUrl: process.env.NODE_URL as string,
privateKeys: process.env.PRIVATE_KEYS === undefined ? [] : process.env.PRIVATE_KEYS.split(','),
settings: defaultSettings
}
}
}

export default configuration
3 changes: 3 additions & 0 deletions token-vesting/contracts/TxScripts/addVestingSchedule.ral
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
TxScript AddVestingSchedule(vesting: Vesting, tokenId: ByteVec, recipient: Address, startTime: U256, cliffTime: U256, endTime: U256, totalAmount: U256) {
vesting.addVestingSchedule{callerAddress!() -> ALPH: minimalContractDeposit!(), tokenId: totalAmount}(recipient, startTime, cliffTime, endTime, totalAmount)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
TxScript AddVestingScheduleWithPercentage(vesting: Vesting, tokenId: ByteVec, recipient: Address, startTime: U256, cliffTime: U256, endTime: U256, totalAmount: U256, percentage: U256) {
vesting.addVestingScheduleWithPercentage{callerAddress!() -> ALPH: minimalContractDeposit!(), tokenId: totalAmount}(recipient, startTime, cliffTime, endTime, totalAmount, percentage)
}
3 changes: 3 additions & 0 deletions token-vesting/contracts/TxScripts/claim.ral
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
TxScript Claim(vesting: Vesting) {
vesting.claim()
}
3 changes: 3 additions & 0 deletions token-vesting/contracts/TxScripts/endVesting.ral
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
TxScript EndVesting(vesting: Vesting, addressToEnd: Address, refundAddress: Address) {
vesting.endVesting(addressToEnd, refundAddress)
}
107 changes: 107 additions & 0 deletions token-vesting/contracts/metadata.ral
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
Contract Metadata(
vesting: Vesting,
address: Address,
startTime: U256,
cliffTime: U256,
endTime: U256,
totalAmount: U256,
mut totalClaimed: U256
) {

const CliffTimeNotReached = 8
const InvalidContractCaller = 9

pub fn getAddress() -> Address {
return address
}

@using(assetsInContract = true, updateFields = true)
pub fn unlockToken() -> () {

checkCaller!(callerContractId!() == contractId!(vesting), InvalidContractCaller)

assert!(blockTimeStamp!() > cliffTime, CliffTimeNotReached)

let totalAmountVested = getTotalAmountVested()
let amountWithdrawable = totalAmountVested - totalClaimed

totalClaimed = totalAmountVested

let tokenId = vesting.getTokenId()

if (amountWithdrawable > 0){
transferTokenFromSelf!(address, tokenId, amountWithdrawable)
}
}

pub fn getTotalAmountLocked() -> U256 {
return totalAmount
}

pub fn getTotalAmountClaimed() -> U256 {
return totalClaimed
}

pub fn getTotalAmountVested() -> U256 {
let now = blockTimeStamp!()

if(now < startTime){
return 0
}

if(now >= endTime){
return totalAmount
}

let timeSinceStart = now - startTime
let totalVestingTime = endTime - startTime
let totalAmountVested = (timeSinceStart * totalAmount) / totalVestingTime
return totalAmountVested
}

pub fn getStartTime() -> U256 {
return startTime
}

pub fn getCliffTime() -> U256 {
return cliffTime
}

pub fn getEndTime() -> U256 {
return endTime
}

@using(assetsInContract = true)
pub fn removeVestingSchedule() -> () {
checkCaller!(callerContractId!() == contractId!(vesting), InvalidContractCaller)
destroySelf!(address)
}

@using(assetsInContract = true)
pub fn endVesting(refundAddress: Address) -> (U256, U256) {
checkCaller!(callerContractId!() == contractId!(vesting), InvalidContractCaller)

let mut amountRefundable = 0
let mut amountWithdrawable = 0

if(blockTimeStamp!() < cliffTime) {
amountRefundable = getTotalAmountLocked()
}else{
let totalAmountVested = getTotalAmountVested()
amountWithdrawable = totalAmountVested - getTotalAmountClaimed()
amountRefundable = getTotalAmountLocked() - totalAmountVested
}

let tokenId = vesting.getTokenId()

if(amountWithdrawable > 0){
transferTokenFromSelf!(address, tokenId, amountWithdrawable)
}

if(amountRefundable > 0){
transferTokenFromSelf!(refundAddress, tokenId, amountRefundable)
}

return amountWithdrawable, amountRefundable
}
}
19 changes: 19 additions & 0 deletions token-vesting/contracts/token.ral
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import "std/fungible_token_interface"

Contract Token(totalSupply: U256) implements IFungibleToken {
pub fn getTotalSupply() -> U256 {
return totalSupply
}

pub fn getSymbol() -> ByteVec {
return b`TTK`
}

pub fn getName() -> ByteVec {
return b`Token`
}

pub fn getDecimals() -> U256 {
return 18
}
}
Loading