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

Add vesting precompile #3263

Merged
merged 17 commits into from
Feb 21, 2025
25 changes: 25 additions & 0 deletions parachain/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions parachain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ members = [
'precompiles/omni-bridge',
'precompiles/parachain-staking',
'precompiles/score-staking',
'precompiles/vesting',
'runtime/litentry',
'runtime/paseo',
'runtime/common',
Expand Down Expand Up @@ -297,6 +298,7 @@ pallet-evm-precompile-bridge-transfer = { path = "precompiles/bridge-transfer",
pallet-evm-precompile-omni-bridge = { path = "precompiles/omni-bridge", default-features = false }
pallet-evm-precompile-parachain-staking = { path = "precompiles/parachain-staking", default-features = false }
pallet-evm-precompile-score-staking = { path = "precompiles/score-staking", default-features = false }
pallet-evm-precompile-vesting = { path = "precompiles/vesting", default-features = false }

pallet-evm-precompile-aiusd-convertor = { path = "precompiles/collab-ai/aiusd-convertor", default-features = false }
pallet-evm-precompile-curator = { path = "precompiles/collab-ai/curator", default-features = false }
Expand Down
51 changes: 51 additions & 0 deletions parachain/precompiles/vesting/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[package]
authors = ["Trust Computing GmbH <info@litentry.com>"]
edition = '2021'
name = "pallet-evm-precompile-vesting"
version = '0.1.0'

[dependencies]
precompile-utils = { workspace = true }

frame-support = { workspace = true }
frame-system = { workspace = true }
pallet-vesting = { workspace = true }
parity-scale-codec = { workspace = true }
scale-info = { workspace = true, features = ["derive"] }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }

fp-evm = { workspace = true }
pallet-evm = { workspace = true }

[dev-dependencies]
derive_more = { workspace = true }
hex-literal = { workspace = true }
libsecp256k1 = { workspace = true, features = ["std"] }
serde = { workspace = true }
sha3 = { workspace = true }
precompile-utils = { workspace = true, features = ["std", "testing"] }
pallet-timestamp = { workspace = true, features = ["std"] }
parity-scale-codec = { workspace = true, features = ["std"] }
sp-runtime = { workspace = true, features = ["std"] }

[features]
default = ["std"]
std = [
"fp-evm/std",
"frame-support/std",
"frame-system/std",
"libsecp256k1/std",
"pallet-vesting/std",
"pallet-evm/std",
"pallet-timestamp/std",
"parity-scale-codec/std",
"precompile-utils/std",
"scale-info/std",
"serde/std",
"sha3/std",
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
]
9 changes: 9 additions & 0 deletions parachain/precompiles/vesting/VestingInterface.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: GPL-3.0-only
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to add a test case in parachain ts-tests?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I will add one.

pragma solidity >=0.8.3;

interface IVesting {
/// @notice Used to unlock vest.
/// @custom:selector 0x458efde3
/// vest()
function vest() external;
}
46 changes: 46 additions & 0 deletions parachain/precompiles/vesting/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2020-2024 Trust Computing GmbH.
// This file is part of Litentry.
//
// Litentry is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Litentry is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Litentry. If not, see <https://www.gnu.org/licenses/>.
#![cfg_attr(not(feature = "std"), no_std)]

use fp_evm::PrecompileHandle;

use frame_support::dispatch::{GetDispatchInfo, PostDispatchInfo};
use pallet_evm::AddressMapping;
use precompile_utils::prelude::*;
use sp_runtime::traits::Dispatchable;

use sp_std::marker::PhantomData;

pub struct VestingPrecompile<Runtime>(PhantomData<Runtime>);

#[precompile_utils::precompile]
impl<Runtime> VestingPrecompile<Runtime>
where
Runtime: pallet_vesting::Config + pallet_evm::Config,
Runtime::RuntimeCall: Dispatchable<PostInfo = PostDispatchInfo> + GetDispatchInfo,
Runtime::RuntimeCall: From<pallet_vesting::Call<Runtime>>,
<Runtime::RuntimeCall as Dispatchable>::RuntimeOrigin: From<Option<Runtime::AccountId>>,
{
#[precompile::public("vest()")]
fn vest(handle: &mut impl PrecompileHandle) -> EvmResult {
let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);

let call = pallet_vesting::Call::<Runtime>::vest {};
RuntimeHelper::<Runtime>::try_dispatch(handle, Some(origin).into(), call)?;

Ok(())
}
}
2 changes: 2 additions & 0 deletions parachain/runtime/litentry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ pallet-evm-precompile-bridge-transfer = { workspace = true }
pallet-evm-precompile-omni-bridge = { workspace = true }
pallet-evm-precompile-parachain-staking = { workspace = true }
pallet-evm-precompile-score-staking = { workspace = true }
pallet-evm-precompile-vesting = { workspace = true }

moonbeam-evm-tracer = { workspace = true }
moonbeam-rpc-primitives-debug = { workspace = true }
Expand Down Expand Up @@ -249,6 +250,7 @@ std = [
"pallet-evm-precompile-score-staking/std",
"pallet-evm-precompile-sha3fips/std",
"pallet-evm-precompile-simple/std",
"pallet-evm-precompile-vesting/std",
"pallet-evm/std",
"pallet-extrinsic-filter/std",
"pallet-group/std",
Expand Down
7 changes: 7 additions & 0 deletions parachain/runtime/litentry/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use pallet_evm_precompile_parachain_staking::ParachainStakingPrecompile;
use pallet_evm_precompile_score_staking::ScoreStakingPrecompile;
use pallet_evm_precompile_sha3fips::Sha3FIPS256;
use pallet_evm_precompile_simple::{ECRecover, ECRecoverPublicKey, Identity, Ripemd160, Sha256};
use pallet_evm_precompile_vesting::VestingPrecompile;
use precompile_utils::precompile_set::*;
use sp_std::fmt::Debug;

Expand Down Expand Up @@ -118,6 +119,12 @@ pub type PrecompilesSetAt<R> = (
PrecompileAt<AddressU64<1026>, ECRecoverPublicKey, (CallableByContract, CallableByPrecompile)>,
PrecompileAt<AddressU64<1027>, Ed25519Verify, (CallableByContract, CallableByPrecompile)>,
// Litentry precompiles (starts from 0x5000):
// Vesting: pallet_vesting = 11 + 20480
PrecompileAt<
AddressU64<20491>,
VestingPrecompile<R>,
(CallableByContract, CallableByPrecompile),
>,
// ParachainStaking: pallet_parachain_staking = 45 + 20480
PrecompileAt<
AddressU64<20525>,
Expand Down
2 changes: 2 additions & 0 deletions parachain/runtime/paseo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pallet-evm-precompile-bridge-transfer = { workspace = true }
pallet-evm-precompile-omni-bridge = { workspace = true }
pallet-evm-precompile-parachain-staking = { workspace = true }
pallet-evm-precompile-score-staking = { workspace = true }
pallet-evm-precompile-vesting = { workspace = true }

pallet-evm-precompile-aiusd-convertor = { workspace = true }
pallet-evm-precompile-curator = { workspace = true }
Expand Down Expand Up @@ -280,6 +281,7 @@ std = [
"pallet-evm-precompile-score-staking/std",
"pallet-evm-precompile-sha3fips/std",
"pallet-evm-precompile-simple/std",
"pallet-evm-precompile-vesting/std",
"pallet-evm/std",
"pallet-extrinsic-filter/std",
"pallet-group/std",
Expand Down
7 changes: 7 additions & 0 deletions parachain/runtime/paseo/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use pallet_evm_precompile_parachain_staking::ParachainStakingPrecompile;
use pallet_evm_precompile_score_staking::ScoreStakingPrecompile;
use pallet_evm_precompile_sha3fips::Sha3FIPS256;
use pallet_evm_precompile_simple::{ECRecover, ECRecoverPublicKey, Identity, Ripemd160, Sha256};
use pallet_evm_precompile_vesting::VestingPrecompile;
use precompile_utils::precompile_set::*;
use sp_std::fmt::Debug;

Expand Down Expand Up @@ -125,6 +126,12 @@ pub type PrecompilesSetAt<R> = (
PrecompileAt<AddressU64<1026>, ECRecoverPublicKey, (CallableByContract, CallableByPrecompile)>,
PrecompileAt<AddressU64<1027>, Ed25519Verify, (CallableByContract, CallableByPrecompile)>,
// Litentry precompiles (starts from 0x5000):
// Vesting: pallet_vesting = 11 + 20480
PrecompileAt<
AddressU64<20491>,
VestingPrecompile<R>,
(CallableByContract, CallableByPrecompile),
>,
// ParachainStaking: pallet_parachain_staking = 45 + 20480
PrecompileAt<
AddressU64<20525>,
Expand Down
9 changes: 9 additions & 0 deletions parachain/ts-tests/common/abi/precompile/Vesting.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"inputs": [],
"name": "vest",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
57 changes: 56 additions & 1 deletion parachain/ts-tests/integration-tests/precompile-contract.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from 'chai';
import { step } from 'mocha-steps';
import {
sleep,
signAndSend,
describeLitentry,
loadConfig,
Expand All @@ -11,8 +12,9 @@ import {
import precompileStakingContractAbi from '../common/abi/precompile/Staking.json';
import precompileBridgeContractAbi from '../common/abi/precompile/Bridge.json';
import precompileOmniBridgeContractAbi from '../common/abi/precompile/OmniBridge.json';
import precompileVestingContractAbi from '../common/abi/precompile/Vesting.json';
const BN = require('bn.js');
import { evmToAddress } from '@polkadot/util-crypto';
import { encodeAddress, evmToAddress } from '@polkadot/util-crypto';
import { KeyringPair } from '@polkadot/keyring/types';
import { HexString } from '@polkadot/util/types';
import { ethers } from 'ethers';
Expand All @@ -29,6 +31,7 @@ describeLitentry('Test Parachain Precompile Contract', ``, (context) => {
const precompileStakingContractAddress = '0x000000000000000000000000000000000000502d';
const precompileBridgeContractAddress = '0x000000000000000000000000000000000000503d';
const precompileOmniBridgeContractAddress = '0x0000000000000000000000000000000000005055';
const precompileVestingContractAddress = '0x000000000000000000000000000000000000500b';
const evmAccountRaw = {
privateKey: '0x01ab6e801c06e59ca97a14fc0a1978b27fa366fc87450e0b65459dd3515b7391',
address: '0xaaafB3972B05630fCceE866eC69CdADd9baC2771',
Expand Down Expand Up @@ -58,6 +61,11 @@ describeLitentry('Test Parachain Precompile Contract', ``, (context) => {
precompileOmniBridgeContractAbi,
provider
);
const precompileVestingContract = new ethers.Contract(
precompileVestingContractAddress,
precompileVestingContractAbi,
provider
);

const executeTransaction = async (delegateTransaction: any, contractAddress: HexString, label = '') => {
console.log(`=== Executing ${label} ===`);
Expand Down Expand Up @@ -317,6 +325,53 @@ describeLitentry('Test Parachain Precompile Contract', ``, (context) => {
console.timeEnd('Test precompile staking contract');
});

step('Test precompile vesting contract', async function () {
console.time('Test precompile vesting contract');

let balance = (await context.api.query.system.account(evmAccountRaw.mappedAddress)).data;
printBalance('initial balance', balance);

// top up LITs if insufficient amount for staking or they are not reserved (require: 50 LITs minimum)
// Add vesting balance
if (
parseInt(balance.free.toString()) < parseInt('60000000000000000000') &&
Number(balance.reserved.toString()) === 0
) {
console.log('transferring more tokens');

await transferTokens(context.alice, evmAccountRaw);

balance = (await context.api.query.system.account(evmAccountRaw.mappedAddress)).data;
printBalance('balance after transferring', balance);
}

// Add an almost immediate-unlocked vesting
const targetBlock = parseInt((await context.api.query.system.number()).toString()) + 2;
const vestedTransferTx = context.api.tx.vesting.vestedTransfer(evmAccountRaw.mappedAddress, { locked: '60000000000000000000', perBlock: '60000000000000000000', startingBlock: targetBlock.toString()});
await signAndSend(vestedTransferTx, context.alice);

// Set timeout to 60 seconds (5 blocks on parachain)
sleep(60);

// Precompile vest
const vestTx = precompileVestingContract.interface.encodeFunctionData('vest', []);

await executeTransaction(vestTx, precompileVestingContractAddress, 'vest');
const eventsPromise = subscribeToEvents('vesting', 'VestingCompleted', context.api);
const events = (await eventsPromise).map(({ event }) => event);

expect(events.length).to.eq(1);
const event_data = events[0].toHuman().data! as {
account: string;
};
console.log(`Print Event data: ${JSON.stringify(event_data)}`);

// VestingCompleted Event
expect(encodeAddress(event_data.account, 42)).to.eq(evmToAddress(evmAccountRaw.address, 42));

console.timeEnd('Test precompile vesting contract');
});

step('Set ExtrinsicFilter mode to Normal', async function () {
let extrinsic = await sudoWrapperTC(context.api, context.api.tx.extrinsicFilter.setMode('Normal'));
await signAndSend(extrinsic, context.alice);
Expand Down