Skip to content

Commit 1c8bf91

Browse files
committed
query from contract is ok
1 parent 47fc351 commit 1c8bf91

File tree

2 files changed

+51
-59
lines changed

2 files changed

+51
-59
lines changed

contract-tests/bittensor/lib.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -194,22 +194,22 @@ mod bittensor {
194194
Self::new()
195195
}
196196

197-
// #[ink(message)]
198-
// pub fn get_stake_info_for_hotkey_coldkey_netuid(
199-
// &self,
200-
// hotkey: [u8; 32],
201-
// coldkey: [u8; 32],
202-
// netuid: u16,
203-
// ) -> Result<Option<StakeInfo<ink::primitives::AccountId>>, ReadWriteErrorCode> {
204-
// self.env()
205-
// .extension()
206-
// .get_stake_info_for_hotkey_coldkey_netuid(
207-
// hotkey.into(),
208-
// coldkey.into(),
209-
// netuid.into(),
210-
// )
211-
// .map_err(|_e| ReadWriteErrorCode::ReadFailed)
212-
// }
197+
#[ink(message)]
198+
pub fn get_stake_info_for_hotkey_coldkey_netuid(
199+
&self,
200+
hotkey: [u8; 32],
201+
coldkey: [u8; 32],
202+
netuid: u16,
203+
) -> Result<Option<StakeInfo<ink::primitives::AccountId>>, ReadWriteErrorCode> {
204+
self.env()
205+
.extension()
206+
.get_stake_info_for_hotkey_coldkey_netuid(
207+
hotkey.into(),
208+
coldkey.into(),
209+
netuid.into(),
210+
)
211+
.map_err(|_e| ReadWriteErrorCode::ReadFailed)
212+
}
213213

214214
#[ink(message)]
215215
pub fn add_stake(

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

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
import { getDevnetApi, getRandomSubstrateKeypair, getSignerFromKeypair, waitForTransactionWithRetry } from "../src/substrate"
22
import { devnet, MultiAddress } from "@polkadot-api/descriptors";
3-
import { Binary, PolkadotSigner, TypedApi } from "polkadot-api";
3+
import { Binary, TypedApi } from "polkadot-api";
44
import * as assert from "assert";
55
import { contracts } from "../.papi/descriptors";
6-
7-
import { ETH_LOCAL_URL } from "../src/config";
8-
import { ISTAKING_ADDRESS, ISTAKING_V2_ADDRESS, IStakingABI, IStakingV2ABI } from "../src/contracts/staking"
96
import { getInkClient, InkClient, } from "@polkadot-api/ink-contracts"
107
import fs from "fs"
118
import { convertPublicKeyToSs58 } from "../src/address-utils";
12-
import { addNewSubnetwork, burnedRegister, forceSetBalanceToSs58Address, sendWasmContractExtrinsic, startCall } from "../src/subtensor";
9+
import { addNewSubnetwork, sendWasmContractExtrinsic } from "../src/subtensor";
1310
import { tao } from "../src/balance-math";
1411

1512
const bittensorWasmPath = "./bittensor/target/ink/bittensor.wasm"
1613
const bittensorBytecode = fs.readFileSync(bittensorWasmPath)
1714

18-
/*
19-
The test file is to verify all the functions in the wasm contract are working correctly.
20-
The test call each function defined in the contract extension.
21-
22-
Current issue:
23-
Can't generate the descriptor for the wasm contract if we add the function to return a complicate struct.
24-
https://github.com/polkadot-api/polkadot-api/issues/1207
25-
*/
26-
2715
describe("Test wasm contract", () => {
2816

2917
let api: TypedApi<typeof devnet>
@@ -35,7 +23,6 @@ describe("Test wasm contract", () => {
3523

3624
// set initial netuid to 0 to avoid warning
3725
let netuid: number = 0;
38-
// let inkClient: InkClient<typeof contracts.bittensor>;
3926
let contractAddress: string;
4027
let inkClient: InkClient<typeof contracts.bittensor>;
4128

@@ -83,11 +70,7 @@ describe("Test wasm contract", () => {
8370
netuid = await addNewSubnetwork(api, hotkey, coldkey)
8471
await startCall(api, netuid, coldkey)
8572
console.log("test the case on subnet ", netuid)
86-
await burnedRegister(api, netuid, convertPublicKeyToSs58(hotkey.publicKey), coldkey)
8773
await burnedRegister(api, netuid, convertPublicKeyToSs58(hotkey2.publicKey), coldkey2)
88-
89-
await addNewSubnetwork(api, hotkey, coldkey)
90-
await startCall(api, netuid + 1, coldkey)
9174
})
9275

9376
it("Can instantiate contract", async () => {
@@ -123,30 +106,39 @@ describe("Test wasm contract", () => {
123106
})
124107

125108

126-
// it("Can query stake info from contract", async () => {
127-
// // const signer = getSignerFromKeypair(coldkey);
128-
// const inkClient = getInkClient(contracts.bittensor)
129-
// const queryMessage = inkClient.message("get_stake_info_for_hotkey_coldkey_netuid")
130-
// const data = queryMessage.encode({
131-
// hotkey: Binary.fromBytes(hotkey.publicKey),
132-
// coldkey: Binary.fromBytes(coldkey.publicKey),
133-
// netuid: netuid,
134-
// })
135-
136-
// const response = await api.tx.Contracts.call({
137-
// dest: MultiAddress.Id(contractAddress),
138-
// data: Binary.fromBytes(data.asBytes()),
139-
// value: BigInt(0),
140-
// gas_limit: {
141-
// ref_time: BigInt(1000000000),
142-
// proof_size: BigInt(10000000),
143-
// },
144-
// storage_deposit_limit: BigInt(10000000),
145-
// })
146-
147-
// console.log("===== response", response)
148-
149-
// })
109+
it("Can query stake info from contract", async () => {
110+
const queryMessage = inkClient.message("get_stake_info_for_hotkey_coldkey_netuid")
111+
112+
const data = queryMessage.encode({
113+
hotkey: Binary.fromBytes(hotkey.publicKey),
114+
coldkey: Binary.fromBytes(coldkey.publicKey),
115+
netuid: netuid,
116+
})
117+
118+
const response = await api.apis.ContractsApi.call(
119+
convertPublicKeyToSs58(hotkey.publicKey),
120+
contractAddress,
121+
BigInt(0),
122+
undefined,
123+
undefined,
124+
Binary.fromBytes(data.asBytes()),
125+
)
126+
127+
assert.ok(response.result.success)
128+
const result = queryMessage.decode(response.result.value).value.value
129+
130+
if (typeof result === "object" && "hotkey" in result && "coldkey" in result && "netuid" in result && "stake" in result && "locked" in result && "emission" in result && "tao_emission" in result && "drain" in result && "is_registered" in result) {
131+
assert.equal(result.hotkey, convertPublicKeyToSs58(hotkey.publicKey))
132+
assert.equal(result.coldkey, convertPublicKeyToSs58(coldkey.publicKey))
133+
assert.equal(result.netuid, netuid)
134+
assert.ok(result.stake > 0)
135+
136+
assert.equal(result.is_registered, true)
137+
} else {
138+
throw new Error("result is not an object")
139+
}
140+
141+
})
150142

151143
it("Can add stake to contract", async () => {
152144
await addStakeWhenWithoutStake()

0 commit comments

Comments
 (0)