Skip to content

Commit e7fa965

Browse files
committed
add stake is ok
1 parent 35a8d12 commit e7fa965

File tree

3 files changed

+136
-42
lines changed

3 files changed

+136
-42
lines changed

evm-tests/bittensor/lib.rs

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ use parity_scale_codec::{Compact, CompactAs, Error as CodecError};
55
#[derive(Debug, Clone)]
66
pub struct CustomEnvironment;
77

8-
// pub enum FunctionId {
9-
// GetStakeInfoForHotkeyColdkeyNetuidV1 = 0,
10-
// AddStakeV1 = 1,
11-
// RemoveStakeV1 = 2,
12-
// UnstakeAllV1 = 3,
13-
// UnstakeAllAlphaV1 = 4,
14-
// MoveStakeV1 = 5,
15-
// TransferStakeV1 = 6,
16-
// SwapStakeV1 = 7,
17-
// AddStakeLimitV1 = 8,
18-
// RemoveStakeLimitV1 = 9,
19-
// SwapStakeLimitV1 = 10,
20-
// RemoveStakeFullLimitV1 = 11,
21-
// SetColdkeyAutoStakeHotkeyV1 = 12,
22-
// AddProxyV1 = 13,
23-
// RemoveProxyV1 = 14,
24-
// }
8+
pub enum FunctionId {
9+
GetStakeInfoForHotkeyColdkeyNetuidV1 = 0,
10+
AddStakeV1 = 1,
11+
RemoveStakeV1 = 2,
12+
UnstakeAllV1 = 3,
13+
UnstakeAllAlphaV1 = 4,
14+
MoveStakeV1 = 5,
15+
TransferStakeV1 = 6,
16+
SwapStakeV1 = 7,
17+
AddStakeLimitV1 = 8,
18+
RemoveStakeLimitV1 = 9,
19+
SwapStakeLimitV1 = 10,
20+
RemoveStakeFullLimitV1 = 11,
21+
SetColdkeyAutoStakeHotkeyV1 = 12,
22+
AddProxyV1 = 13,
23+
RemoveProxyV1 = 14,
24+
}
2525

2626
#[ink::chain_extension(extension = 0x1000)]
2727
pub trait RuntimeReadWrite {
@@ -34,8 +34,12 @@ pub trait RuntimeReadWrite {
3434
netuid: u16,
3535
) -> Option<StakeInfo<ink::primitives::AccountId>>;
3636

37-
// #[ink(function = 1)]
38-
// fn add_stake(hotkey: &[u8], netuid: &[u8], amount: &[u8]);
37+
#[ink(function = 1)]
38+
fn add_stake(
39+
hotkey: <CustomEnvironment as ink::env::Environment>::AccountId,
40+
netuid: NetUid,
41+
amount: AlphaCurrency,
42+
);
3943

4044
// #[ink(function = 2)]
4145
// fn remove_stake(hotkey: &[u8], netuid: &[u8], amount: &[u8]);
@@ -240,21 +244,39 @@ mod bittensor {
240244
}
241245

242246
#[ink(message)]
243-
pub fn get_stake_info_for_hotkey_coldkey_netuid(
244-
&mut self,
247+
pub fn dummy(&self) -> Result<bool, ReadWriteErrorCode> {
248+
Ok(true)
249+
}
250+
251+
#[ink(message)]
252+
pub fn add_stake(
253+
&self,
245254
hotkey: [u8; 32],
246-
coldkey: [u8; 32],
247255
netuid: u16,
248-
) -> Result<Option<StakeInfo<ink::primitives::AccountId>>, ReadWriteErrorCode> {
256+
amount: u64,
257+
) -> Result<(), ReadWriteErrorCode> {
249258
self.env()
250259
.extension()
251-
.get_stake_info_for_hotkey_coldkey_netuid(
252-
hotkey.into(),
253-
coldkey.into(),
254-
netuid.into(),
255-
)
256-
.map_err(|_e| ReadWriteErrorCode::ReadFailed)
260+
.add_stake(hotkey.into(), netuid.into(), amount.into())
261+
.map_err(|_e| ReadWriteErrorCode::WriteFailed)
257262
}
263+
264+
// #[ink(message)]
265+
// pub fn get_stake_info_for_hotkey_coldkey_netuid(
266+
// &mut self,
267+
// hotkey: [u8; 32],
268+
// coldkey: [u8; 32],
269+
// netuid: u16,
270+
// ) -> Result<Option<StakeInfo<ink::primitives::AccountId>>, ReadWriteErrorCode> {
271+
// self.env()
272+
// .extension()
273+
// .get_stake_info_for_hotkey_coldkey_netuid(
274+
// hotkey.into(),
275+
// coldkey.into(),
276+
// netuid.into(),
277+
// )
278+
// .map_err(|_e| ReadWriteErrorCode::ReadFailed)
279+
// }
258280
}
259281

260282
/// Unit tests in Rust are normally defined within such a `#[cfg(test)]`

evm-tests/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@
66
"author": "",
77
"license": "ISC",
88
"dependencies": {
9+
"@localpapi/descriptors": "file:.papi/descriptors",
910
"@polkadot-api/descriptors": "file:.papi/descriptors",
11+
"@polkadot-api/ink-contracts": "^0.4.1",
12+
"@polkadot-api/sdk-ink": "^0.5.1",
1013
"@polkadot-labs/hdkd": "^0.0.23",
1114
"@polkadot-labs/hdkd-helpers": "^0.0.23",
1215
"@polkadot/api": "^16.4.9",
1316
"@types/mocha": "^10.0.10",
1417
"dotenv": "17.2.1",
1518
"ethers": "^6.13.5",
1619
"mocha": "^11.1.0",
17-
"polkadot-api": "^1.9.5",
20+
"polkadot-api": "^1.20.0",
1821
"rxjs": "^7.8.2",
1922
"scale-ts": "^1.6.1",
2023
"viem": "2.23.4",
21-
"ws": "^8.18.2",
22-
"@polkadot-api/ink-contracts": "^0.4.1",
23-
"@polkadot-api/sdk-ink": "^0.5.1"
24+
"ws": "^8.18.2"
2425
},
2526
"devDependencies": {
2627
"@types/bun": "^1.1.13",

evm-tests/test/wasm.contract.test.ts

Lines changed: 80 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import * as assert from "assert";
2-
import { getDevnetApi, getAliceSigner, getRandomSubstrateKeypair } from "../src/substrate"
3-
import { devnet, contracts, MultiAddress } from "@polkadot-api/descriptors"
2+
import { getDevnetApi, getAliceSigner, getRandomSubstrateKeypair, getSignerFromKeypair } from "../src/substrate"
3+
import { devnet, MultiAddress } from "@polkadot-api/descriptors";
44
import { Binary, PolkadotSigner, TypedApi } from "polkadot-api";
55

6+
import { contracts } from "../.papi/descriptors";
7+
68
import { ETH_LOCAL_URL } from "../src/config";
79
import { ISTAKING_ADDRESS, ISTAKING_V2_ADDRESS, IStakingABI, IStakingV2ABI } from "../src/contracts/staking"
8-
import { getInkClient, InkClient } from "@polkadot-api/ink-contracts"
10+
import { getInkClient, InkClient, } from "@polkadot-api/ink-contracts"
911
import fs from "fs"
1012
import { convertPublicKeyToSs58 } from "../src/address-utils";
11-
import { forceSetBalanceToSs58Address } from "../src/subtensor";
13+
import { addNewSubnetwork, burnedRegister, forceSetBalanceToSs58Address, startCall } from "../src/subtensor";
1214

1315
const bittensorWasmPath = "./bittensor/target/ink/bittensor.wasm"
1416
const bittensorBytecode = fs.readFileSync(bittensorWasmPath)
@@ -20,7 +22,7 @@ describe("Test wasm contract", () => {
2022
const hotkey = getRandomSubstrateKeypair();
2123
const coldkey = getRandomSubstrateKeypair();
2224

23-
let inkClient: InkClient<typeof contracts.bittensor>;
25+
// let inkClient: InkClient<typeof contracts.bittensor>;
2426
let contractAddress: string;
2527

2628
// sudo account alice as signer
@@ -29,13 +31,20 @@ describe("Test wasm contract", () => {
2931
// init variables got from await and async
3032
api = await getDevnetApi()
3133
alice = await getAliceSigner();
32-
await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(hotkey.publicKey))
34+
3335
await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(coldkey.publicKey))
36+
let netuid = await addNewSubnetwork(api, hotkey, coldkey)
37+
await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(hotkey.publicKey))
38+
await startCall(api, netuid, coldkey)
39+
40+
console.log("test the case on subnet ", netuid)
41+
await burnedRegister(api, netuid, convertPublicKeyToSs58(hotkey.publicKey), coldkey)
42+
3443
})
3544

3645
it("Can instantiate contract", async () => {
37-
const signer = await getAliceSigner();
38-
inkClient = getInkClient(contracts.bittensor)
46+
const signer = getSignerFromKeypair(coldkey);
47+
const inkClient = getInkClient(contracts.bittensor)
3948
const constructor = inkClient.constructor('new')
4049
const data = constructor.encode()
4150
const instantiate_with_code = await api.tx.Contracts.instantiate_with_code({
@@ -50,7 +59,6 @@ describe("Test wasm contract", () => {
5059
salt: Binary.fromHex("0x"),
5160
}).signAndSubmit(signer)
5261

53-
5462
let codeStoredEvents = await api.event.Contracts.Instantiated.filter(instantiate_with_code.events)
5563
if (codeStoredEvents.length === 0) {
5664
throw new Error("No events found")
@@ -62,8 +70,71 @@ describe("Test wasm contract", () => {
6270

6371

6472
it("Can query stake info from contract", async () => {
73+
let netuid = (await api.query.SubtensorModule.TotalNetworks.getValue()) - 1
74+
const signer = getSignerFromKeypair(coldkey);
75+
const inkClient = getInkClient(contracts.bittensor)
76+
const query = inkClient.message("dummy")
77+
const data = query.encode() // No parameters needed
78+
// const queryTx = await api.tx.Contracts.call({
79+
// dest: MultiAddress.Id(contractAddress),
80+
// data: Binary.fromBytes(data.asBytes()),
81+
// value: BigInt(0),
82+
// gas_limit: {
83+
// ref_time: BigInt(1000000000),
84+
// proof_size: BigInt(10000000),
85+
// },
86+
// storage_deposit_limit: BigInt(10000000),
87+
// }).signAndSubmit(signer)
88+
89+
const response = await api.apis.ContractsApi.call(
90+
convertPublicKeyToSs58(coldkey.publicKey),
91+
contractAddress,
92+
BigInt(0),
93+
{
94+
ref_time: BigInt(1000000000),
95+
proof_size: BigInt(10000000),
96+
},
97+
BigInt(1000000000),
98+
Binary.fromBytes(data.asBytes()),
99+
undefined,
100+
)
101+
102+
// console.log("===== response", response.result.asBytes().toString())
65103

66104
})
67105

106+
it("Can add stake to contract", async () => {
107+
console.log("===== Can add stake to contract")
108+
let netuid = (await api.query.SubtensorModule.TotalNetworks.getValue()) - 1
109+
let amount = BigInt(100000000)
110+
111+
const balance = await api.query.System.Account.getValue(convertPublicKeyToSs58(coldkey.publicKey))
112+
console.log("===== balance", balance.data.free)
113+
114+
const signer = getSignerFromKeypair(coldkey);
115+
const inkClient = getInkClient(contracts.bittensor)
116+
const message = inkClient.message("add_stake")
117+
const data = message.encode({
118+
hotkey: Binary.fromBytes(hotkey.publicKey),
119+
netuid: netuid,
120+
amount: amount,
121+
})
122+
123+
const tx = await api.tx.Contracts.call({
124+
value: BigInt(0),
125+
dest: MultiAddress.Id(contractAddress),
126+
data: Binary.fromBytes(data.asBytes()),
127+
gas_limit: {
128+
ref_time: BigInt(10000000000),
129+
proof_size: BigInt(10000000),
130+
},
131+
storage_deposit_limit: BigInt(1000000000)
132+
}).signAndSubmit(signer)
68133

134+
// const response = await api.event.Contracts.Call.filter(tx.events)
135+
// if (response.length === 0) {
136+
// throw new Error("No events found")
137+
// }
138+
console.log("===== response", tx.events)
139+
})
69140
});

0 commit comments

Comments
 (0)