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
18 changes: 18 additions & 0 deletions contract-tests/src/subtensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,22 @@ export async function setNetworkLastLockCost(api: TypedApi<typeof devnet>, defau

const valueOnChain = await api.query.SubtensorModule.NetworkLastLockCost.getValue()
assert.equal(defaultNetworkLastLockCost, valueOnChain)
}


export async function getStake(api: TypedApi<typeof devnet>, hotkey: string, coldkey: string, netuid: number): Promise<bigint> {
const value = (await api.query.SubtensorModule.AlphaV2.getValue(hotkey, coldkey, netuid));

const mantissa = value.mantissa;
const exponent = value.exponent;

let result: bigint;

if (exponent >= 0) {
result = mantissa * BigInt(10) ** exponent;
} else {
result = mantissa / BigInt(10) ** -exponent;
}

return result;
}
12 changes: 6 additions & 6 deletions contract-tests/test/alphaPool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { PublicClient } from "viem";
import { TypedApi } from "polkadot-api";
import { ALPHA_POOL_CONTRACT_ABI, ALPHA_POOL_CONTRACT_BYTECODE } from "../src/contracts/alphaPool";
import { convertH160ToPublicKey, convertH160ToSS58, convertPublicKeyToSs58, toViemAddress } from "../src/address-utils";
import { forceSetBalanceToEthAddress, disableWhiteListCheck, addNewSubnetwork, forceSetBalanceToSs58Address, startCall, burnedRegister } from "../src/subtensor";
import { forceSetBalanceToEthAddress, disableWhiteListCheck, addNewSubnetwork, forceSetBalanceToSs58Address, startCall, burnedRegister, getStake } from "../src/subtensor";
import { ethers } from "ethers"
import { tao } from "../src/balance-math";
import { ISTAKING_V2_ADDRESS, IStakingV2ABI } from "../src/contracts/staking";
Expand Down Expand Up @@ -46,7 +46,7 @@ describe("bridge token contract deployment", () => {
let netuid = (await api.query.SubtensorModule.TotalNetworks.getValue()) - 1
// the unit in V2 is RAO, not ETH
let stakeBalance = tao(20)
const stakeBefore = await api.query.SubtensorModule.Alpha.getValue(convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(wallet.address), netuid)
const stakeBefore = await getStake(api, convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(wallet.address), netuid)
const contract = new ethers.Contract(ISTAKING_V2_ADDRESS, IStakingV2ABI, wallet);
const tx = await contract.addStake(hotkey.publicKey, stakeBalance.toString(), netuid)
await tx.wait()
Expand All @@ -56,7 +56,7 @@ describe("bridge token contract deployment", () => {
);

assert.ok(stakeFromContract > stakeBefore)
const stakeAfter = await api.query.SubtensorModule.Alpha.getValue(convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(wallet.address), netuid)
const stakeAfter = await getStake(api, convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(wallet.address), netuid)
assert.ok(stakeAfter > stakeBefore)
assert.ok(stakeFromContract > tao(20))
})
Expand All @@ -66,7 +66,7 @@ describe("bridge token contract deployment", () => {
let netuid = (await api.query.SubtensorModule.TotalNetworks.getValue()) - 1
const stakingPrecompile = new ethers.Contract(ISTAKING_V2_ADDRESS, IStakingV2ABI, wallet);

const stakeBeforeDeposit = await api.query.SubtensorModule.Alpha.getValue(convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(wallet.address), netuid)
const stakeBeforeDeposit = await getStake(api, convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(wallet.address), netuid)

const contractFactory = new ethers.ContractFactory(ALPHA_POOL_CONTRACT_ABI, ALPHA_POOL_CONTRACT_BYTECODE, wallet)
const contract = await contractFactory.deploy(hotkey.publicKey)
Expand Down Expand Up @@ -103,11 +103,11 @@ describe("bridge token contract deployment", () => {
await depositAlphaTx.wait()

// compare wallet stake
const stakeAftereDeposit = await api.query.SubtensorModule.Alpha.getValue(convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(wallet.address), netuid)
const stakeAftereDeposit = await getStake(api, convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(wallet.address), netuid)
assert.ok(stakeAftereDeposit < stakeBeforeDeposit)

// check the contract stake
const ContractStake = await api.query.SubtensorModule.Alpha.getValue(convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(contractAddress), netuid)
const ContractStake = await getStake(api, convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(contractAddress), netuid)
assert.ok(ContractStake > 0)

// check the wallet alpha balance in contract, the actual swapped alpha could be less than alphaAmount in deposit call
Expand Down
13 changes: 9 additions & 4 deletions contract-tests/test/precompileWrapper.direct-call.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
startCall,
disableWhiteListCheck,
forceSetBalanceToEthAddress,
getStake,

} from "../src/subtensor";
import { ethers } from "ethers";
Expand Down Expand Up @@ -147,7 +148,8 @@ describe("PrecompileWrapper - Direct Call Tests", () => {

it("Should add stake via wrapper", async () => {
const stakeAmount = tao(2);
const stakeBefore = await api.query.SubtensorModule.Alpha.getValue(
const stakeBefore = await getStake(
api,
convertPublicKeyToSs58(hotkey.publicKey),
convertH160ToSS58(wrapperAddress),
netuid
Expand All @@ -161,7 +163,8 @@ describe("PrecompileWrapper - Direct Call Tests", () => {
);
await addStakeTx.wait();

const stakeAfter = await api.query.SubtensorModule.Alpha.getValue(
const stakeAfter = await getStake(
api,
convertPublicKeyToSs58(hotkey.publicKey),
convertH160ToSS58(wrapperAddress),
netuid
Expand All @@ -171,7 +174,8 @@ describe("PrecompileWrapper - Direct Call Tests", () => {

it("Should remove stake via wrapper", async () => {
const removeAmount = tao(1);
const stakeBefore = await api.query.SubtensorModule.Alpha.getValue(
const stakeBefore = await getStake(
api,
convertPublicKeyToSs58(hotkey.publicKey),
convertH160ToSS58(wrapperAddress),
netuid
Expand All @@ -184,7 +188,8 @@ describe("PrecompileWrapper - Direct Call Tests", () => {
);
await removeStakeTx.wait();

const stakeAfter = await api.query.SubtensorModule.Alpha.getValue(
const stakeAfter = await getStake(
api,
convertPublicKeyToSs58(hotkey.publicKey),
convertH160ToSS58(wrapperAddress),
netuid
Expand Down
29 changes: 17 additions & 12 deletions contract-tests/test/staking.precompile.add-remove.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
forceSetBalanceToEthAddress, forceSetBalanceToSs58Address, addNewSubnetwork, burnedRegister,
sendProxyCall,
startCall,
getStake,
} from "../src/subtensor"
import { ETH_LOCAL_URL } from "../src/config";
import { ISTAKING_ADDRESS, ISTAKING_V2_ADDRESS, IStakingABI, IStakingV2ABI } from "../src/contracts/staking"
Expand Down Expand Up @@ -54,7 +55,7 @@ describe("Test neuron precompile add remove stake", () => {
let netuid = (await api.query.SubtensorModule.TotalNetworks.getValue()) - 1
// ETH unit
let stakeBalance = raoToEth(tao(20))
const stakeBefore = await api.query.SubtensorModule.Alpha.getValue(convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(wallet1.address), netuid)
const stakeBefore = await getStake(api, convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(wallet1.address), netuid)
const contract = new ethers.Contract(ISTAKING_ADDRESS, IStakingABI, wallet1);
const tx = await contract.addStake(hotkey.publicKey, netuid, { value: stakeBalance.toString() })
await tx.wait()
Expand All @@ -64,15 +65,15 @@ describe("Test neuron precompile add remove stake", () => {
);

assert.ok(stakeFromContract > stakeBefore)
const stakeAfter = await api.query.SubtensorModule.Alpha.getValue(convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(wallet1.address), netuid)
const stakeAfter = await getStake(api, convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(wallet1.address), netuid)
assert.ok(stakeAfter > stakeBefore)
})

it("Can add stake V2", async () => {
let netuid = (await api.query.SubtensorModule.TotalNetworks.getValue()) - 1
// the unit in V2 is RAO, not ETH
let stakeBalance = tao(20)
const stakeBefore = await api.query.SubtensorModule.Alpha.getValue(convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(wallet2.address), netuid)
const stakeBefore = await getStake(api, convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(wallet2.address), netuid)
const contract = new ethers.Contract(ISTAKING_V2_ADDRESS, IStakingV2ABI, wallet2);
const tx = await contract.addStake(hotkey.publicKey, stakeBalance.toString(), netuid)
await tx.wait()
Expand All @@ -82,15 +83,15 @@ describe("Test neuron precompile add remove stake", () => {
);

assert.ok(stakeFromContract > stakeBefore)
const stakeAfter = await api.query.SubtensorModule.Alpha.getValue(convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(wallet2.address), netuid)
const stakeAfter = await getStake(api, convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(wallet2.address), netuid)
assert.ok(stakeAfter > stakeBefore)
})

it("Can not add stake if subnet doesn't exist", async () => {
// wrong netuid
let netuid = 12345;
let stakeBalance = raoToEth(tao(20))
const stakeBefore = await api.query.SubtensorModule.Alpha.getValue(convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(wallet1.address), netuid)
const stakeBefore = await getStake(api, convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(wallet1.address), netuid)
const contract = new ethers.Contract(ISTAKING_ADDRESS, IStakingABI, wallet1);
try {
const tx = await contract.addStake(hotkey.publicKey, netuid, { value: stakeBalance.toString() })
Expand All @@ -104,7 +105,7 @@ describe("Test neuron precompile add remove stake", () => {
await contract.getStake(hotkey.publicKey, convertH160ToPublicKey(wallet1.address), netuid)
);
assert.equal(stakeFromContract, stakeBefore)
const stakeAfter = await api.query.SubtensorModule.Alpha.getValue(convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(wallet1.address), netuid)
const stakeAfter = await getStake(api, convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(wallet1.address), netuid)
assert.equal(stakeAfter, stakeBefore)
});

Expand All @@ -113,7 +114,7 @@ describe("Test neuron precompile add remove stake", () => {
let netuid = 12345;
// the unit in V2 is RAO, not ETH
let stakeBalance = tao(20)
const stakeBefore = await api.query.SubtensorModule.Alpha.getValue(convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(wallet2.address), netuid)
const stakeBefore = await getStake(api, convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(wallet2.address), netuid)
const contract = new ethers.Contract(ISTAKING_V2_ADDRESS, IStakingV2ABI, wallet2);

try {
Expand All @@ -128,7 +129,7 @@ describe("Test neuron precompile add remove stake", () => {
await contract.getStake(hotkey.publicKey, convertH160ToPublicKey(wallet2.address), netuid)
);
assert.equal(stakeFromContract, stakeBefore)
const stakeAfter = await api.query.SubtensorModule.Alpha.getValue(convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(wallet2.address), netuid)
const stakeAfter = await getStake(api, convertPublicKeyToSs58(hotkey.publicKey), convertH160ToSS58(wallet2.address), netuid)
assert.equal(stakeAfter, stakeBefore)
})

Expand Down Expand Up @@ -248,7 +249,8 @@ describe("Test neuron precompile add remove stake", () => {

assert.equal(proxiesAfterAdd[0][0].delegate, convertPublicKeyToSs58(proxy.publicKey))

let stakeBefore = await api.query.SubtensorModule.Alpha.getValue(
let stakeBefore = await getStake(
api,
convertPublicKeyToSs58(hotkey.publicKey),
ss58Address,
netuid
Expand All @@ -261,7 +263,8 @@ describe("Test neuron precompile add remove stake", () => {
})
await sendProxyCall(api, call.decodedCall, ss58Address, proxy)

let stakeAfter = await api.query.SubtensorModule.Alpha.getValue(
let stakeAfter = await getStake(
api,
convertPublicKeyToSs58(hotkey.publicKey),
ss58Address,
netuid
Expand Down Expand Up @@ -306,7 +309,8 @@ describe("Test neuron precompile add remove stake", () => {

assert.equal(proxiesAfterAdd[0][0].delegate, convertPublicKeyToSs58(proxy.publicKey))

let stakeBefore = await api.query.SubtensorModule.Alpha.getValue(
let stakeBefore = await getStake(
api,
convertPublicKeyToSs58(hotkey.publicKey),
ss58Address,
netuid
Expand All @@ -320,7 +324,8 @@ describe("Test neuron precompile add remove stake", () => {

await sendProxyCall(api, call.decodedCall, ss58Address, proxy)

let stakeAfter = await api.query.SubtensorModule.Alpha.getValue(
let stakeAfter = await getStake(
api,
convertPublicKeyToSs58(hotkey.publicKey),
ss58Address,
netuid
Expand Down
4 changes: 3 additions & 1 deletion contract-tests/test/staking.precompile.burn-alpha.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { convertH160ToPublicKey } from "../src/address-utils"
import {
forceSetBalanceToEthAddress, forceSetBalanceToSs58Address, addNewSubnetwork, burnedRegister,
startCall,
getStake,
} from "../src/subtensor"
import { ISTAKING_V2_ADDRESS, IStakingV2ABI } from "../src/contracts/staking"

Expand Down Expand Up @@ -72,7 +73,8 @@ describe("Test staking precompile burn alpha", () => {
let netuid = (await api.query.SubtensorModule.TotalNetworks.getValue()) - 1

// Get current stake
const currentStake = await api.query.SubtensorModule.Alpha.getValue(
const currentStake = await getStake(
api,
convertPublicKeyToSs58(hotkey.publicKey),
convertH160ToSS58(wallet1.address),
netuid
Expand Down
25 changes: 17 additions & 8 deletions contract-tests/test/staking.precompile.full-limit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
addStake,
forceSetBalanceToEthAddress,
forceSetBalanceToSs58Address,
getStake,
startCall,
} from "../src/subtensor";
import { ethers } from "ethers";
Expand Down Expand Up @@ -51,7 +52,8 @@ describe("Test staking precompile add remove limit methods", () => {
let netuid = (await api.query.SubtensorModule.TotalNetworks.getValue()) - 1;
let ss58Address = convertH160ToSS58(wallet1.address);

const alpha = await api.query.SubtensorModule.Alpha.getValue(
const alpha = await getStake(
api,
convertPublicKeyToSs58(hotkey.publicKey),
ss58Address,
netuid,
Expand All @@ -72,7 +74,8 @@ describe("Test staking precompile add remove limit methods", () => {
);
await tx.wait();

const alphaAfterAddStake = await api.query.SubtensorModule.Alpha.getValue(
const alphaAfterAddStake = await getStake(
api,
convertPublicKeyToSs58(hotkey.publicKey),
ss58Address,
netuid,
Expand All @@ -85,7 +88,8 @@ describe("Test staking precompile add remove limit methods", () => {
let netuid = (await api.query.SubtensorModule.TotalNetworks.getValue()) - 1;
let ss58Address = convertH160ToSS58(wallet1.address);

const alpha = await api.query.SubtensorModule.Alpha.getValue(
const alpha = await getStake(
api,
convertPublicKeyToSs58(hotkey.publicKey),
ss58Address,
netuid,
Expand All @@ -104,7 +108,8 @@ describe("Test staking precompile add remove limit methods", () => {
);
await tx.wait();

const alphaAfterRemoveStake = await api.query.SubtensorModule.Alpha.getValue(
const alphaAfterRemoveStake = await getStake(
api,
convertPublicKeyToSs58(hotkey.publicKey),
ss58Address,
netuid,
Expand All @@ -120,7 +125,8 @@ describe("Test staking precompile add remove limit methods", () => {
let netuid = (await api.query.SubtensorModule.TotalNetworks.getValue()) - 1;
let ss58Address = convertH160ToSS58(wallet2.address);

const alpha = await api.query.SubtensorModule.Alpha.getValue(
const alpha = await getStake(
api,
convertPublicKeyToSs58(hotkey.publicKey),
ss58Address,
netuid,
Expand All @@ -141,7 +147,8 @@ describe("Test staking precompile add remove limit methods", () => {
);
await tx.wait();

const alphaAfterAddStake = await api.query.SubtensorModule.Alpha.getValue(
const alphaAfterAddStake = await getStake(
api,
convertPublicKeyToSs58(hotkey.publicKey),
ss58Address,
netuid,
Expand All @@ -154,7 +161,8 @@ describe("Test staking precompile add remove limit methods", () => {
let netuid = (await api.query.SubtensorModule.TotalNetworks.getValue()) - 1;
let ss58Address = convertH160ToSS58(wallet2.address);

const alpha = await api.query.SubtensorModule.Alpha.getValue(
const alpha = await getStake(
api,
convertPublicKeyToSs58(hotkey.publicKey),
ss58Address,
netuid,
Expand All @@ -172,7 +180,8 @@ describe("Test staking precompile add remove limit methods", () => {
);
await tx.wait();

const alphaAfterRemoveStake = await api.query.SubtensorModule.Alpha.getValue(
const alphaAfterRemoveStake = await getStake(
api,
convertPublicKeyToSs58(hotkey.publicKey),
ss58Address,
netuid,
Expand Down
13 changes: 9 additions & 4 deletions contract-tests/test/staking.precompile.limit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
addStake,
forceSetBalanceToEthAddress,
forceSetBalanceToSs58Address,
getStake,
startCall,
} from "../src/subtensor";
import { ethers } from "ethers";
Expand Down Expand Up @@ -47,7 +48,8 @@ describe("Test staking precompile add remove limit methods", () => {
let netuid = (await api.query.SubtensorModule.TotalNetworks.getValue()) - 1;
let ss58Address = convertH160ToSS58(wallet1.address);

const alpha = await api.query.SubtensorModule.Alpha.getValue(
const alpha = await getStake(
api,
convertPublicKeyToSs58(hotkey.publicKey),
ss58Address,
netuid,
Expand All @@ -68,7 +70,8 @@ describe("Test staking precompile add remove limit methods", () => {
);
await tx.wait();

const alphaAfterAddStake = await api.query.SubtensorModule.Alpha.getValue(
const alphaAfterAddStake = await getStake(
api,
convertPublicKeyToSs58(hotkey.publicKey),
ss58Address,
netuid,
Expand All @@ -81,7 +84,8 @@ describe("Test staking precompile add remove limit methods", () => {
let netuid = (await api.query.SubtensorModule.TotalNetworks.getValue()) - 1;
let ss58Address = convertH160ToSS58(wallet1.address);

const alpha = await api.query.SubtensorModule.Alpha.getValue(
const alpha = await getStake(
api,
convertPublicKeyToSs58(hotkey.publicKey),
ss58Address,
netuid,
Expand All @@ -102,7 +106,8 @@ describe("Test staking precompile add remove limit methods", () => {
);
await tx.wait();

const alphaAfterRemoveStake = await api.query.SubtensorModule.Alpha.getValue(
const alphaAfterRemoveStake = await getStake(
api,
convertPublicKeyToSs58(hotkey.publicKey),
ss58Address,
netuid,
Expand Down
Loading
Loading