|
| 1 | +// This file is part of Substrate. |
| 2 | + |
| 3 | +// Copyright (C) Parity Technologies (UK) Ltd. |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | + |
| 18 | +#![no_std] |
| 19 | + |
| 20 | +extern crate alloc; |
| 21 | + |
| 22 | +use alloc::vec::Vec; |
| 23 | +use alloy_core::sol_types::SolValue; |
| 24 | +use core::{marker::PhantomData, num::NonZero}; |
| 25 | +use frame_support::{ |
| 26 | + dispatch::GetDispatchInfo, |
| 27 | + traits::{Get, VestingSchedule}, |
| 28 | +}; |
| 29 | +use pallet_revive::{ |
| 30 | + Config, |
| 31 | + precompiles::{AddressMatcher, Error, Ext, H160, Precompile, RuntimeCosts, U256}, |
| 32 | +}; |
| 33 | +use pallet_revive_uapi::precompiles::vesting::IVesting; |
| 34 | +use sp_runtime::traits::StaticLookup; |
| 35 | + |
| 36 | +pub struct Vesting<T>(PhantomData<T>); |
| 37 | + |
| 38 | +/// The balance type used by `pallet-vesting`'s currency. |
| 39 | +type VestingBalance<T> = |
| 40 | + <<T as pallet_vesting::Config>::Currency as frame_support::traits::Currency< |
| 41 | + <T as frame_system::Config>::AccountId, |
| 42 | + >>::Balance; |
| 43 | + |
| 44 | +impl<T: Config + pallet_vesting::Config> Precompile for Vesting<T> |
| 45 | +where |
| 46 | + VestingBalance<T>: Into<U256>, |
| 47 | + // Weak proxy for type identity: mutual From bounds do not guarantee the types are the same |
| 48 | + // (e.g. From<u64> for u128 exists in core), but they are the best constraint expressible |
| 49 | + // in stable Rust without a custom sealed trait. A misconfigured runtime that satisfies |
| 50 | + // these bounds with distinct types will compile but return wrong-denomination values. |
| 51 | + VestingBalance<T>: From<<T as Config>::Balance>, |
| 52 | + <T as Config>::Balance: From<VestingBalance<T>>, |
| 53 | +{ |
| 54 | + type T = T; |
| 55 | + type Interface = IVesting::IVestingCalls; |
| 56 | + const MATCHER: AddressMatcher = AddressMatcher::Fixed(NonZero::new(0x0902).unwrap()); |
| 57 | + const HAS_CONTRACT_INFO: bool = false; |
| 58 | + |
| 59 | + fn call( |
| 60 | + _address: &[u8; 20], |
| 61 | + input: &Self::Interface, |
| 62 | + env: &mut impl Ext<T = Self::T>, |
| 63 | + ) -> Result<Vec<u8>, Error> { |
| 64 | + use IVesting::IVestingCalls; |
| 65 | + match input { |
| 66 | + IVestingCalls::vest(_) if env.is_read_only() => { |
| 67 | + Err(pallet_revive::Error::<T>::StateChangeDenied.into()) |
| 68 | + }, |
| 69 | + IVestingCalls::vest(IVesting::vestCall {}) => { |
| 70 | + if env.is_delegate_call() { |
| 71 | + return Err(Error::Revert( |
| 72 | + "vesting precompile cannot be called via delegate call".into(), |
| 73 | + )); |
| 74 | + } |
| 75 | + // Derive the beneficiary from the immediate caller (not the tx origin). |
| 76 | + let account_id = env |
| 77 | + .caller() |
| 78 | + .account_id() |
| 79 | + .map_err(|e| { |
| 80 | + Error::Revert( |
| 81 | + alloc::format!("vest: caller has no account id: {:?}", e).into(), |
| 82 | + ) |
| 83 | + })? |
| 84 | + .clone(); |
| 85 | + |
| 86 | + // Determine and charge the dispatch weight before calling. |
| 87 | + let dispatch_weight = |
| 88 | + pallet_vesting::Call::<T>::vest {}.get_dispatch_info().call_weight; |
| 89 | + env.frame_meter_mut() |
| 90 | + .charge_weight_token(RuntimeCosts::Precompile(dispatch_weight))?; |
| 91 | + |
| 92 | + // Construct a signed RuntimeOrigin and dispatch vest(). |
| 93 | + let origin = frame_system::RawOrigin::Signed(account_id).into(); |
| 94 | + pallet_vesting::Pallet::<T>::vest(origin) |
| 95 | + .map_err(|e| Error::Revert(alloc::format!("vest failed: {:?}", e).into()))?; |
| 96 | + Ok(Vec::new()) |
| 97 | + }, |
| 98 | + IVestingCalls::vestOther(_) if env.is_read_only() => { |
| 99 | + Err(pallet_revive::Error::<T>::StateChangeDenied.into()) |
| 100 | + }, |
| 101 | + IVestingCalls::vestOther(IVesting::vestOtherCall { target }) => { |
| 102 | + if env.is_delegate_call() { |
| 103 | + return Err(Error::Revert( |
| 104 | + "vesting precompile cannot be called via delegate call".into(), |
| 105 | + )); |
| 106 | + } |
| 107 | + let caller_account = env |
| 108 | + .caller() |
| 109 | + .account_id() |
| 110 | + .map_err(|e| { |
| 111 | + Error::Revert( |
| 112 | + alloc::format!("vestOther: caller has no account id: {:?}", e).into(), |
| 113 | + ) |
| 114 | + })? |
| 115 | + .clone(); |
| 116 | + |
| 117 | + let target_account = env.to_account_id(&H160::from_slice(target.as_slice())); |
| 118 | + let target_lookup = T::Lookup::unlookup(target_account); |
| 119 | + |
| 120 | + let dispatch_weight = |
| 121 | + pallet_vesting::Call::<T>::vest_other { target: target_lookup.clone() } |
| 122 | + .get_dispatch_info() |
| 123 | + .call_weight; |
| 124 | + env.frame_meter_mut() |
| 125 | + .charge_weight_token(RuntimeCosts::Precompile(dispatch_weight))?; |
| 126 | + |
| 127 | + let origin = frame_system::RawOrigin::Signed(caller_account).into(); |
| 128 | + pallet_vesting::Pallet::<T>::vest_other(origin, target_lookup).map_err(|e| { |
| 129 | + Error::Revert(alloc::format!("vestOther failed: {:?}", e).into()) |
| 130 | + })?; |
| 131 | + Ok(Vec::new()) |
| 132 | + }, |
| 133 | + // View function to query the currently locked (unvested) balance for the caller. |
| 134 | + // vesting_balance() returns Option<Balance>: None means no schedule exists, |
| 135 | + // Some(0) means a schedule exists but all funds are already unlocked. Both |
| 136 | + // collapse to 0 here — in either case there is nothing left to vest. |
| 137 | + IVestingCalls::vestingBalance(IVesting::vestingBalanceCall {}) => { |
| 138 | + let account_id = env |
| 139 | + .caller() |
| 140 | + .account_id() |
| 141 | + .map_err(|e| { |
| 142 | + Error::Revert( |
| 143 | + alloc::format!("vestingBalance: caller has no account id: {:?}", e) |
| 144 | + .into(), |
| 145 | + ) |
| 146 | + })? |
| 147 | + .clone(); |
| 148 | + |
| 149 | + // Charge upfront for the worst case: Vesting map read + free_balance read. |
| 150 | + // If no schedule exists only the Vesting map is read; refund the unused read. |
| 151 | + let charged = env.frame_meter_mut().charge_weight_token( |
| 152 | + RuntimeCosts::Precompile(<T as frame_system::Config>::DbWeight::get().reads(2)), |
| 153 | + )?; |
| 154 | + |
| 155 | + let maybe_locked = |
| 156 | + <pallet_vesting::Pallet<T> as VestingSchedule<T::AccountId>>::vesting_balance( |
| 157 | + &account_id, |
| 158 | + ); |
| 159 | + |
| 160 | + if maybe_locked.is_none() { |
| 161 | + env.frame_meter_mut().adjust_weight( |
| 162 | + charged, |
| 163 | + RuntimeCosts::Precompile( |
| 164 | + <T as frame_system::Config>::DbWeight::get().reads(1), |
| 165 | + ), |
| 166 | + ); |
| 167 | + } |
| 168 | + |
| 169 | + let locked = maybe_locked.unwrap_or_default(); |
| 170 | + Ok(U256::from(locked.into()).to_big_endian().abi_encode()) |
| 171 | + }, |
| 172 | + IVestingCalls::vestingBalanceOf(IVesting::vestingBalanceOfCall { target }) => { |
| 173 | + let account_id = env.to_account_id(&H160::from_slice(target.as_slice())); |
| 174 | + |
| 175 | + // Same worst-case weight as vestingBalance(): Vesting map read + free_balance read. |
| 176 | + // Refund one read if no schedule exists (only the map was accessed). |
| 177 | + let charged = env.frame_meter_mut().charge_weight_token( |
| 178 | + RuntimeCosts::Precompile(<T as frame_system::Config>::DbWeight::get().reads(2)), |
| 179 | + )?; |
| 180 | + |
| 181 | + let maybe_locked = |
| 182 | + <pallet_vesting::Pallet<T> as VestingSchedule<T::AccountId>>::vesting_balance( |
| 183 | + &account_id, |
| 184 | + ); |
| 185 | + |
| 186 | + if maybe_locked.is_none() { |
| 187 | + env.frame_meter_mut().adjust_weight( |
| 188 | + charged, |
| 189 | + RuntimeCosts::Precompile( |
| 190 | + <T as frame_system::Config>::DbWeight::get().reads(1), |
| 191 | + ), |
| 192 | + ); |
| 193 | + } |
| 194 | + |
| 195 | + let locked = maybe_locked.unwrap_or_default(); |
| 196 | + Ok(U256::from(locked.into()).to_big_endian().abi_encode()) |
| 197 | + }, |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments