Skip to content
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
40 changes: 37 additions & 3 deletions evm-tests/src/contracts/stakeWrap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,32 @@ interface Staking {
) external;

function addStake(bytes32 hotkey, uint256 amount, uint256 netuid) external;

function removeStake(
bytes32 hotkey,
uint256 amount,
uint256 netuid
) external;
}

contract StakeWrap {
constructor() {}
address public owner;
constructor() {
owner = msg.sender;
}

modifier onlyOwner() {
require(msg.sender == owner, "Only owner can call this function");
_;
}

receive() external payable {}

function stake(bytes32 hotkey, uint256 netuid, uint256 amount) external {
function stake(
bytes32 hotkey,
uint256 netuid,
uint256 amount
) external onlyOwner {
// can't call precompile like this way, the call never go to runtime precompile
//Staking(ISTAKING_ADDRESS).addStake(hotkey, amount, netuid);

Expand All @@ -41,7 +60,7 @@ contract StakeWrap {
uint256 limitPrice,
uint256 amount,
bool allowPartial
) external {
) external onlyOwner {
// can't call precompile like this way, the call never go to runtime precompile
// Staking(ISTAKING_ADDRESS).addStakeLimit(
// hotkey,
Expand All @@ -62,4 +81,19 @@ contract StakeWrap {
(bool success, ) = ISTAKING_ADDRESS.call{gas: gasleft()}(data);
require(success, "addStakeLimit call failed");
}

function removeStake(
bytes32 hotkey,
uint256 netuid,
uint256 amount
) external onlyOwner {
bytes memory data = abi.encodeWithSelector(
Staking.removeStake.selector,
hotkey,
amount,
netuid
);
(bool success, ) = ISTAKING_ADDRESS.call{gas: gasleft()}(data);
require(success, "addStake call failed");
}
}
42 changes: 41 additions & 1 deletion evm-tests/src/contracts/stakeWrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,42 @@ export const abi = [
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "owner",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "hotkey",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "netuid",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "removeStake",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
Expand Down Expand Up @@ -59,8 +95,12 @@ export const abi = [
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"stateMutability": "payable",
"type": "receive"
}
];

// compiled with 0.8.20
export const bytecode = "608060405234801561000f575f80fd5b5061069e8061001d5f395ff3fe60806040526004361061002c575f3560e01c80632daedd521461003757806390b9d5341461005f57610033565b3661003357005b5f80fd5b348015610042575f80fd5b5061005d60048036038101906100589190610357565b610087565b005b34801561006a575f80fd5b50610085600480360381019061008091906103dc565b6101b7565b005b5f631fc9b14160e01b8483856040516024016100a593929190610471565b604051602081830303815290604052907bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff838183161783525050505090505f61080573ffffffffffffffffffffffffffffffffffffffff165a8360405161012d9190610512565b5f604051808303815f8787f1925050503d805f8114610167576040519150601f19603f3d011682016040523d82523d5f602084013e61016c565b606091505b50509050806101b0576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016101a790610582565b60405180910390fd5b5050505050565b5f635beb6b7460e01b86848685896040516024016101d99594939291906105af565b604051602081830303815290604052907bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff838183161783525050505090505f61080573ffffffffffffffffffffffffffffffffffffffff165a836040516102619190610512565b5f604051808303815f8787f1925050503d805f811461029b576040519150601f19603f3d011682016040523d82523d5f602084013e6102a0565b606091505b50509050806102e4576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016102db9061064a565b60405180910390fd5b50505050505050565b5f80fd5b5f819050919050565b610303816102f1565b811461030d575f80fd5b50565b5f8135905061031e816102fa565b92915050565b5f819050919050565b61033681610324565b8114610340575f80fd5b50565b5f813590506103518161032d565b92915050565b5f805f6060848603121561036e5761036d6102ed565b5b5f61037b86828701610310565b935050602061038c86828701610343565b925050604061039d86828701610343565b9150509250925092565b5f8115159050919050565b6103bb816103a7565b81146103c5575f80fd5b50565b5f813590506103d6816103b2565b92915050565b5f805f805f60a086880312156103f5576103f46102ed565b5b5f61040288828901610310565b955050602061041388828901610343565b945050604061042488828901610343565b935050606061043588828901610343565b9250506080610446888289016103c8565b9150509295509295909350565b61045c816102f1565b82525050565b61046b81610324565b82525050565b5f6060820190506104845f830186610453565b6104916020830185610462565b61049e6040830184610462565b949350505050565b5f81519050919050565b5f81905092915050565b5f5b838110156104d75780820151818401526020810190506104bc565b5f8484015250505050565b5f6104ec826104a6565b6104f681856104b0565b93506105068185602086016104ba565b80840191505092915050565b5f61051d82846104e2565b915081905092915050565b5f82825260208201905092915050565b7f6164645374616b652063616c6c206661696c65640000000000000000000000005f82015250565b5f61056c601483610528565b915061057782610538565b602082019050919050565b5f6020820190508181035f83015261059981610560565b9050919050565b6105a9816103a7565b82525050565b5f60a0820190506105c25f830188610453565b6105cf6020830187610462565b6105dc6040830186610462565b6105e960608301856105a0565b6105f66080830184610462565b9695505050505050565b7f6164645374616b654c696d69742063616c6c206661696c6564000000000000005f82015250565b5f610634601983610528565b915061063f82610600565b602082019050919050565b5f6020820190508181035f83015261066181610628565b905091905056fea264697066735822122083351bec20bd75de90a1b6e405922bedadf9ff260c02f34ef9dbb5ee1bda11cd64736f6c63430008140033"
export const bytecode = "6080604052348015600e575f5ffd5b50335f5f6101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550610ad08061005b5f395ff3fe608060405260043610610042575f3560e01c80632daedd521461004d5780637d691e30146100755780638da5cb5b1461009d57806390b9d534146100c757610049565b3661004957005b5f5ffd5b348015610058575f5ffd5b50610073600480360381019061006e91906106bd565b6100ef565b005b348015610080575f5ffd5b5061009b600480360381019061009691906106bd565b6102ad565b005b3480156100a8575f5ffd5b506100b161046b565b6040516100be919061074c565b60405180910390f35b3480156100d2575f5ffd5b506100ed60048036038101906100e8919061079a565b61048f565b005b5f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161461017d576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161017490610891565b60405180910390fd5b5f631fc9b14160e01b84838560405160240161019b939291906108cd565b604051602081830303815290604052907bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff838183161783525050505090505f61080573ffffffffffffffffffffffffffffffffffffffff165a836040516102239190610954565b5f604051808303815f8787f1925050503d805f811461025d576040519150601f19603f3d011682016040523d82523d5f602084013e610262565b606091505b50509050806102a6576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161029d906109b4565b60405180910390fd5b5050505050565b5f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161461033b576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161033290610891565b60405180910390fd5b5f637d691e3060e01b848385604051602401610359939291906108cd565b604051602081830303815290604052907bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff838183161783525050505090505f61080573ffffffffffffffffffffffffffffffffffffffff165a836040516103e19190610954565b5f604051808303815f8787f1925050503d805f811461041b576040519150601f19603f3d011682016040523d82523d5f602084013e610420565b606091505b5050905080610464576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161045b906109b4565b60405180910390fd5b5050505050565b5f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b5f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161461051d576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161051490610891565b60405180910390fd5b5f635beb6b7460e01b868486858960405160240161053f9594939291906109e1565b604051602081830303815290604052907bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff838183161783525050505090505f61080573ffffffffffffffffffffffffffffffffffffffff165a836040516105c79190610954565b5f604051808303815f8787f1925050503d805f8114610601576040519150601f19603f3d011682016040523d82523d5f602084013e610606565b606091505b505090508061064a576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161064190610a7c565b60405180910390fd5b50505050505050565b5f5ffd5b5f819050919050565b61066981610657565b8114610673575f5ffd5b50565b5f8135905061068481610660565b92915050565b5f819050919050565b61069c8161068a565b81146106a6575f5ffd5b50565b5f813590506106b781610693565b92915050565b5f5f5f606084860312156106d4576106d3610653565b5b5f6106e186828701610676565b93505060206106f2868287016106a9565b9250506040610703868287016106a9565b9150509250925092565b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f6107368261070d565b9050919050565b6107468161072c565b82525050565b5f60208201905061075f5f83018461073d565b92915050565b5f8115159050919050565b61077981610765565b8114610783575f5ffd5b50565b5f8135905061079481610770565b92915050565b5f5f5f5f5f60a086880312156107b3576107b2610653565b5b5f6107c088828901610676565b95505060206107d1888289016106a9565b94505060406107e2888289016106a9565b93505060606107f3888289016106a9565b925050608061080488828901610786565b9150509295509295909350565b5f82825260208201905092915050565b7f4f6e6c79206f776e65722063616e2063616c6c20746869732066756e6374696f5f8201527f6e00000000000000000000000000000000000000000000000000000000000000602082015250565b5f61087b602183610811565b915061088682610821565b604082019050919050565b5f6020820190508181035f8301526108a88161086f565b9050919050565b6108b881610657565b82525050565b6108c78161068a565b82525050565b5f6060820190506108e05f8301866108af565b6108ed60208301856108be565b6108fa60408301846108be565b949350505050565b5f81519050919050565b5f81905092915050565b8281835e5f83830152505050565b5f61092e82610902565b610938818561090c565b9350610948818560208601610916565b80840191505092915050565b5f61095f8284610924565b915081905092915050565b7f6164645374616b652063616c6c206661696c65640000000000000000000000005f82015250565b5f61099e601483610811565b91506109a98261096a565b602082019050919050565b5f6020820190508181035f8301526109cb81610992565b9050919050565b6109db81610765565b82525050565b5f60a0820190506109f45f8301886108af565b610a0160208301876108be565b610a0e60408301866108be565b610a1b60608301856109d2565b610a2860808301846108be565b9695505050505050565b7f6164645374616b654c696d69742063616c6c206661696c6564000000000000005f82015250565b5f610a66601983610811565b9150610a7182610a32565b602082019050919050565b5f6020820190508181035f830152610a9381610a5a565b905091905056fea2646970667358221220f8ad692d7919fb10f08e5311c64a0aa705a4e665689967633c2ddade5398076664736f6c634300081e0033"
16 changes: 9 additions & 7 deletions evm-tests/test/staking.precompile.wrap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
} from "../src/subtensor";
import { ethers } from "ethers";
import { generateRandomEthersWallet } from "../src/utils";
import { log } from "console";

import { abi, bytecode } from "../src/contracts/stakeWrap";

Expand Down Expand Up @@ -47,14 +46,13 @@ describe("Test staking precompile add from deployed contract", () => {
console.log("will test in subnet: ", netuid);
});

it("Staker add stake", async () => {
it("Staker add and remove stake", async () => {
let netuid = (await api.query.SubtensorModule.TotalNetworks.getValue()) - 1;

const contractFactory = new ethers.ContractFactory(abi, bytecode, wallet1)
const contract = await contractFactory.deploy()
await contract.waitForDeployment()


// stake will remove the balance from contract, need transfer token to deployed contract
const ethTransfer = {
to: contract.target.toString(),
Expand All @@ -64,9 +62,6 @@ describe("Test staking precompile add from deployed contract", () => {
const txResponse = await wallet1.sendTransaction(ethTransfer)
await txResponse.wait();

const balance = await api.query.System.Account.getValue(convertH160ToSS58(contract.target.toString()))
console.log(" == balance is ", balance.data.free)

const deployedContract = new ethers.Contract(
contract.target.toString(),
abi,
Expand All @@ -76,10 +71,17 @@ describe("Test staking precompile add from deployed contract", () => {
const tx = await deployedContract.stake(
hotkey.publicKey,
netuid,
tao(2000),
tao(2),
);
await tx.wait();

const tx2 = await deployedContract.removeStake(
hotkey.publicKey,
netuid,
tao(1),
);
await tx2.wait();

});

it("Staker add stake limit", async () => {
Expand Down
6 changes: 3 additions & 3 deletions pallets/subtensor/src/macros/dispatches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ mod dispatches {
/// - On failure for each failed item in the batch.
///
#[pallet::call_index(80)]
#[pallet::weight((Weight::from_parts(95_160_000, 0)
.saturating_add(T::DbWeight::get().reads(14))
.saturating_add(T::DbWeight::get().writes(2)), DispatchClass::Normal, Pays::No))]
#[pallet::weight((Weight::from_parts(18_910_000, 0)
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().writes(0_u64)), DispatchClass::Normal, Pays::No))]
pub fn batch_set_weights(
origin: OriginFor<T>,
netuids: Vec<Compact<NetUid>>,
Expand Down
6 changes: 3 additions & 3 deletions pallets/subtensor/src/utils/misc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;
use crate::{
Error,
system::{ensure_root, ensure_signed, ensure_signed_or_root, pallet_prelude::BlockNumberFor},
use crate::Error;
use crate::system::{
ensure_root, ensure_signed, ensure_signed_or_root, pallet_prelude::BlockNumberFor,
};
use safe_math::*;
use sp_core::Get;
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 307,
spec_version: 308,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down