Skip to content
Merged
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
11 changes: 8 additions & 3 deletions precompiles/src/balance_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ where
<R as pallet_balances::Config>::Balance: TryFrom<U256>,
{
const INDEX: u64 = 2048;
const ADDRESS_SS58: [u8; 32] = [
const ADDRESS_SS58: Option<[u8; 32]> = Some([
0x07, 0xec, 0x71, 0x2a, 0x5d, 0x38, 0x43, 0x4d, 0xdd, 0x03, 0x3f, 0x8f, 0x02, 0x4e, 0xcd,
0xfc, 0x4b, 0xb5, 0x95, 0x1c, 0x13, 0xc3, 0x08, 0x5c, 0x39, 0x9c, 0x8a, 0x5f, 0x62, 0x93,
0x70, 0x5d,
];
]);
}

#[precompile_utils::precompile]
Expand Down Expand Up @@ -60,6 +60,11 @@ where
value: amount_sub.unique_saturated_into(),
};

handle.try_dispatch_runtime_call::<R, _>(call, contract_to_origin(&Self::ADDRESS_SS58)?)
handle.try_dispatch_runtime_call::<R, _>(
call,
contract_to_origin(
&Self::ADDRESS_SS58.expect("ADDRESS_SS58 is defined for BalanceTransferPrecompile"),
)?,
)
}
}
2 changes: 1 addition & 1 deletion precompiles/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub(crate) struct Ed25519Verify;

impl PrecompileExt for Ed25519Verify {
const INDEX: u64 = 1026;
const ADDRESS_SS58: [u8; 32] = [0; 32];
const ADDRESS_SS58: Option<[u8; 32]> = None;
}

impl LinearCostPrecompile for Ed25519Verify {
Expand Down
18 changes: 13 additions & 5 deletions precompiles/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ where
Self(Default::default())
}

pub fn used_addresses() -> [H160; 13] {
pub fn used_addresses() -> [H160; 14] {
[
hash(1),
hash(2),
Expand All @@ -101,6 +101,7 @@ where
hash(SubnetPrecompile::<R>::INDEX),
hash(MetagraphPrecompile::<R>::INDEX),
hash(NeuronPrecompile::<R>::INDEX),
hash(StakingPrecompileV2::<R>::INDEX),
]
}
}
Expand Down Expand Up @@ -152,9 +153,16 @@ where
Some(StakingPrecompile::<R>::execute(handle))
} else {
Some(Err(PrecompileFailure::Error {
exit_status: ExitError::Other(
"Precompile Balance Transfer is disabled".into(),
),
exit_status: ExitError::Other("Precompile Staking is disabled".into()),
}))
}
}
a if a == hash(StakingPrecompileV2::<R>::INDEX) => {
if PrecompileEnable::<R>::get(PrecompileEnum::Staking) {
Some(StakingPrecompileV2::<R>::execute(handle))
} else {
Some(Err(PrecompileFailure::Error {
exit_status: ExitError::Other("Precompile Staking is disabled".into()),
}))
}
}
Expand Down Expand Up @@ -302,5 +310,5 @@ trait PrecompileExt: Precompile {
const INDEX: u64;
// ss58 public key i.e., the contract sends funds it received to the destination address from
// the method parameter.
const ADDRESS_SS58: [u8; 32];
const ADDRESS_SS58: Option<[u8; 32]>;
}
2 changes: 1 addition & 1 deletion precompiles/src/metagraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ where
R::AccountId: ByteArray,
{
const INDEX: u64 = 2050;
const ADDRESS_SS58: [u8; 32] = [0; 32];
const ADDRESS_SS58: Option<[u8; 32]> = None;
}

#[precompile_utils::precompile]
Expand Down
4 changes: 2 additions & 2 deletions precompiles/src/neuron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ where
<R as pallet_evm::Config>::AddressMapping: AddressMapping<R::AccountId>,
{
const INDEX: u64 = 2052;
const ADDRESS_SS58: [u8; 32] = [
const ADDRESS_SS58: Option<[u8; 32]> = Some([
0xbc, 0x46, 0x35, 0x79, 0xbc, 0x99, 0xf9, 0xee, 0x7c, 0x59, 0xed, 0xee, 0x20, 0x61, 0xa3,
0x09, 0xd2, 0x1e, 0x68, 0xd5, 0x39, 0xb6, 0x40, 0xec, 0x66, 0x46, 0x90, 0x30, 0xab, 0x74,
0xc1, 0xdb,
];
]);
}

#[precompile_utils::precompile]
Expand Down
141 changes: 141 additions & 0 deletions precompiles/src/solidity/stakingV2.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
[
{
"inputs": [
{
"internalType": "bytes32",
"name": "delegate",
"type": "bytes32"
}
],
"name": "addProxy",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "hotkey",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "netuid",
"type": "uint256"
}
],
"name": "addStake",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "hotkey",
"type": "bytes32"
},
{
"internalType": "bytes32",
"name": "coldkey",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "netuid",
"type": "uint256"
}
],
"name": "getStake",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "coldkey",
"type": "bytes32"
}
],
"name": "getTotalColdkeyStake",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "hotkey",
"type": "bytes32"
}
],
"name": "getTotalHotkeyStake",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "delegate",
"type": "bytes32"
}
],
"name": "removeProxy",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "hotkey",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "netuid",
"type": "uint256"
}
],
"name": "removeStake",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
103 changes: 103 additions & 0 deletions precompiles/src/solidity/stakingV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
pragma solidity ^0.8.0;

address constant ISTAKING_ADDRESS = 0x0000000000000000000000000000000000000805;

interface IStaking {
/**
* @dev Adds a subtensor stake `amount` associated with the `hotkey`.
*
* This function allows external accounts and contracts to stake TAO into the subtensor pallet,
* which effectively calls `add_stake` on the subtensor pallet with specified hotkey as a parameter
* and coldkey being the hashed address mapping of H160 sender address to Substrate ss58 address as
* implemented in Frontier HashedAddressMapping:
* https://github.com/polkadot-evm/frontier/blob/2e219e17a526125da003e64ef22ec037917083fa/frame/evm/src/lib.rs#L739
*
* @param hotkey The hotkey public key (32 bytes).
* @param amount The amount to stake in rao.
* @param netuid The subnet to stake to (uint256).
*
* Requirements:
* - `hotkey` must be a valid hotkey registered on the network, ensuring that the stake is
* correctly attributed.
*/
function addStake(bytes32 hotkey, uint256 amount, uint256 netuid) external payable;

/**
* @dev Removes a subtensor stake `amount` from the specified `hotkey`.
*
* This function allows external accounts and contracts to unstake TAO from the subtensor pallet,
* which effectively calls `remove_stake` on the subtensor pallet with specified hotkey as a parameter
* and coldkey being the hashed address mapping of H160 sender address to Substrate ss58 address as
* implemented in Frontier HashedAddressMapping:
* https://github.com/polkadot-evm/frontier/blob/2e219e17a526125da003e64ef22ec037917083fa/frame/evm/src/lib.rs#L739
*
* @param hotkey The hotkey public key (32 bytes).
* @param amount The amount to unstake in alpha.
* @param netuid The subnet to stake to (uint256).
*
* Requirements:
* - `hotkey` must be a valid hotkey registered on the network, ensuring that the stake is
* correctly attributed.
* - The existing stake amount must be not lower than specified amount
*/
function removeStake(
bytes32 hotkey,
uint256 amount,
uint256 netuid
) external;

/**
* @dev Returns the amount of RAO staked by the coldkey.
*
* This function allows external accounts and contracts to query the amount of RAO staked by the coldkey
* which effectively calls `get_total_coldkey_stake` on the subtensor pallet with
* specified coldkey as a parameter.
*
* @param coldkey The coldkey public key (32 bytes).
* @return The amount of RAO staked by the coldkey.
*/
function getTotalColdkeyStake(bytes32 coldkey) external view returns (uint256);

/**
* @dev Returns the total amount of stake under a hotkey (delegative or otherwise)
*
* This function allows external accounts and contracts to query the total amount of RAO staked under a hotkey
* which effectively calls `get_total_hotkey_stake` on the subtensor pallet with
* specified hotkey as a parameter.
*
* @param hotkey The hotkey public key (32 bytes).
* @return The total amount of RAO staked under the hotkey.
*/
function getTotalHotkeyStake(bytes32 hotkey) external view returns (uint256);

/**
* @dev Returns the stake amount associated with the specified `hotkey` and `coldkey`.
*
* This function retrieves the current stake amount linked to a specific hotkey and coldkey pair.
* It is a view function, meaning it does not modify the state of the contract and is free to call.
*
* @param hotkey The hotkey public key (32 bytes).
* @param coldkey The coldkey public key (32 bytes).
* @param netuid The subnet the stake is on (uint256).
* @return The current stake amount in uint256 format.
*/
function getStake(
bytes32 hotkey,
bytes32 coldkey,
uint256 netuid
) external view returns (uint256);

/**
* @dev Delegates staking to a proxy account.
*
* @param delegate The public key (32 bytes) of the delegate.
*/
function addProxy(bytes32 delegate) external;

/**
* @dev Removes staking proxy account.
*
* @param delegate The public key (32 bytes) of the delegate.
*/
function removeProxy(bytes32 delegate) external;
}
Loading
Loading