Skip to content

Commit f5ddec8

Browse files
committed
add test file
1 parent c513a4e commit f5ddec8

File tree

3 files changed

+77
-52
lines changed

3 files changed

+77
-52
lines changed

evm-tests/src/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export const ETH_LOCAL_URL = 'http://localhost:9944'
22
export const SUB_LOCAL_URL = 'ws://localhost:9944'
33
export const SS58_PREFIX = 42;
44
// set the tx timeout as 2 second when eable the fast-blocks feature.
5-
export const TX_TIMEOUT = 2000;
5+
export const TX_TIMEOUT = 3000;
66

77
export const IED25519VERIFY_ADDRESS = "0x0000000000000000000000000000000000000402";
88
export const IEd25519VerifyABI = [

evm-tests/src/contracts/staking.ts

+48
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,30 @@ export const IStakingV2ABI = [
137137
"stateMutability": "payable",
138138
"type": "function"
139139
},
140+
{
141+
"inputs": [
142+
{
143+
"internalType": "bytes32",
144+
"name": "hotkey",
145+
"type": "bytes32"
146+
},
147+
{
148+
"internalType": "uint256",
149+
"name": "netuid",
150+
"type": "uint256"
151+
}
152+
],
153+
"name": "getAlphaStakedValidators",
154+
"outputs": [
155+
{
156+
"internalType": "uint256[]",
157+
"name": "",
158+
"type": "uint256[]"
159+
}
160+
],
161+
"stateMutability": "view",
162+
"type": "function"
163+
},
140164
{
141165
"inputs": [
142166
{
@@ -166,6 +190,30 @@ export const IStakingV2ABI = [
166190
"stateMutability": "view",
167191
"type": "function"
168192
},
193+
{
194+
"inputs": [
195+
{
196+
"internalType": "bytes32",
197+
"name": "hotkey",
198+
"type": "bytes32"
199+
},
200+
{
201+
"internalType": "uint256",
202+
"name": "netuid",
203+
"type": "uint256"
204+
}
205+
],
206+
"name": "getTotalAlphaStaked",
207+
"outputs": [
208+
{
209+
"internalType": "uint256",
210+
"name": "",
211+
"type": "uint256"
212+
}
213+
],
214+
"stateMutability": "view",
215+
"type": "function"
216+
},
169217
{
170218
"inputs": [
171219
{

evm-tests/test/staking.precompile.stake-get.test.ts

+28-51
Original file line numberDiff line numberDiff line change
@@ -5,76 +5,53 @@ import { TypedApi } from "polkadot-api";
55
import { convertPublicKeyToSs58 } from "../src/address-utils"
66
import { tao } from "../src/balance-math"
77
import {
8-
forceSetBalanceToSs58Address, addNewSubnetwork, burnedRegister,
9-
setTxRateLimit, setTempo, setWeightsSetRateLimit, setSubnetOwnerCut, setMaxAllowedUids,
10-
setMinDelegateTake, becomeDelegate, setActivityCutoff, addStake, setWeight, rootRegister
8+
forceSetBalanceToSs58Address, addNewSubnetwork, addStake,
119
} from "../src/subtensor"
12-
import { PublicClient } from "viem";
13-
import { generateRandomEthersWallet, getPublicClient } from "../src/utils"
14-
import { ISTAKING_ADDRESS, ISTAKING_V2_ADDRESS, IStakingABI, IStakingV2ABI } from "../src/contracts/staking"
15-
import { ETH_LOCAL_URL } from "../src/config";
10+
import { ethers } from "ethers";
11+
import { generateRandomEthersWallet } from "../src/utils"
12+
import { ISTAKING_V2_ADDRESS, IStakingV2ABI } from "../src/contracts/staking"
13+
import { log } from "console";
1614

17-
describe("Test neuron precompile reveal weights", () => {
15+
describe("Test staking precompile get methods", () => {
1816
const hotkey = getRandomSubstrateKeypair();
1917
const coldkey = getRandomSubstrateKeypair();
20-
let publicClient: PublicClient;
21-
22-
// const validator = getRandomSubstrateKeypair();
23-
// const miner = getRandomSubstrateKeypair();
24-
// const nominator = getRandomSubstrateKeypair();
18+
const wallet1 = generateRandomEthersWallet();
2519

2620
let api: TypedApi<typeof devnet>
2721

2822
before(async () => {
29-
const root_netuid = 0;
30-
const root_tempo = 1; // neet root epoch to happen before subnet tempo
31-
const subnet_tempo = 1;
32-
publicClient = await getPublicClient(ETH_LOCAL_URL)
3323
api = await getDevnetApi()
34-
35-
// await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(alice.publicKey))
3624
await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(hotkey.publicKey))
3725
await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(coldkey.publicKey))
38-
// await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(validator.publicKey))
39-
// await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(miner.publicKey))
40-
// await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(nominator.publicKey))
41-
// await forceSetBalanceToEthAddress(api, wallet1.address)
42-
// await forceSetBalanceToEthAddress(api, wallet2.address)
4326
let netuid = await addNewSubnetwork(api, hotkey, coldkey)
44-
45-
console.log("test the case on subnet ", netuid)
46-
47-
await setTxRateLimit(api, BigInt(0))
48-
await setTempo(api, root_netuid, root_tempo)
49-
await setTempo(api, netuid, subnet_tempo)
50-
await setWeightsSetRateLimit(api, netuid, BigInt(0))
51-
52-
// await burnedRegister(api, netuid, convertPublicKeyToSs58(validator.publicKey), coldkey)
53-
// await burnedRegister(api, netuid, convertPublicKeyToSs58(miner.publicKey), coldkey)
54-
// await burnedRegister(api, netuid, convertPublicKeyToSs58(nominator.publicKey), coldkey)
55-
await setSubnetOwnerCut(api, 0)
56-
await setActivityCutoff(api, netuid, 65535)
57-
await setMaxAllowedUids(api, netuid, 65535)
58-
await setMinDelegateTake(api, 0)
59-
// await becomeDelegate(api, convertPublicKeyToSs58(validator.publicKey), coldkey)
60-
// await becomeDelegate(api, convertPublicKeyToSs58(miner.publicKey), coldkey)
27+
console.log("will test in subnet: ", netuid)
6128
})
6229

6330
it("Staker receives rewards", async () => {
6431
let netuid = (await api.query.SubtensorModule.TotalNetworks.getValue()) - 1
6532

6633
await addStake(api, netuid, convertPublicKeyToSs58(hotkey.publicKey), tao(1), coldkey)
6734

68-
const value = await publicClient.readContract({
69-
address: ISTAKING_ADDRESS,
70-
abi: IStakingABI,
71-
functionName: "getStake",
72-
args: [hotkey.publicKey, // Convert to bytes32 format
73-
hotkey.publicKey,
74-
netuid]
75-
})
76-
77-
console.log(value)
35+
const contract = new ethers.Contract(
36+
ISTAKING_V2_ADDRESS,
37+
IStakingV2ABI,
38+
wallet1
39+
);
40+
41+
const stake = BigInt(
42+
await contract.getStake(hotkey.publicKey, coldkey.publicKey, netuid)
43+
);
44+
45+
// validator returned as bigint now.
46+
const validators =
47+
await contract.getAlphaStakedValidators(hotkey.publicKey, netuid)
48+
49+
const alpha = BigInt(
50+
await contract.getTotalAlphaStaked(hotkey.publicKey, netuid)
51+
);
52+
assert.ok(stake > 0)
53+
assert.equal(validators.length, 1)
54+
assert.ok(alpha > 0)
7855

7956
})
8057
})

0 commit comments

Comments
 (0)