Skip to content

Commit 35a8d12

Browse files
committed
initiate is ok
1 parent bb59c61 commit 35a8d12

File tree

7 files changed

+71
-79
lines changed

7 files changed

+71
-79
lines changed

evm-tests/bittensor/lib.rs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -239,29 +239,22 @@ mod bittensor {
239239
Self::new()
240240
}
241241

242-
/// A message that can be called on instantiated contracts.
243-
/// This one flips the value of the stored `bool` from `true`
244-
/// to `false` and vice versa.
245242
#[ink(message)]
246-
pub fn a(&self) -> bool {
247-
true
243+
pub fn get_stake_info_for_hotkey_coldkey_netuid(
244+
&mut self,
245+
hotkey: [u8; 32],
246+
coldkey: [u8; 32],
247+
netuid: u16,
248+
) -> Result<Option<StakeInfo<ink::primitives::AccountId>>, ReadWriteErrorCode> {
249+
self.env()
250+
.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)
248257
}
249-
250-
// pub fn get_stake_info_for_hotkey_coldkey_netuid(
251-
// &mut self,
252-
// hotkey: [u8; 32],
253-
// coldkey: [u8; 32],
254-
// netuid: u16,
255-
// ) -> Result<Option<StakeInfo<ink::primitives::AccountId>>, ReadWriteErrorCode> {
256-
// self.env()
257-
// .extension()
258-
// .get_stake_info_for_hotkey_coldkey_netuid(
259-
// hotkey.into(),
260-
// coldkey.into(),
261-
// netuid.into(),
262-
// )
263-
// .map_err(|_e| ReadWriteErrorCode::ReadFailed)
264-
// }
265258
}
266259

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

evm-tests/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"scripts": {
3-
"test": "mocha --timeout 999999 --retries 3 --file src/setup.ts --require ts-node/register test/wasm*test.ts"
3+
"test": "mocha --timeout 999999 --file src/setup.ts --require ts-node/register test/wasm*test.ts"
44
},
55
"keywords": [],
66
"author": "",

evm-tests/src/config.ts

Lines changed: 1 addition & 1 deletion
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-runtime feature.
5-
export const TX_TIMEOUT = 3000;
5+
export const TX_TIMEOUT = 2000;
66

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

evm-tests/src/setup.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { createClient, TypedApi, PolkadotClient, Binary } from 'polkadot-api';
33
import { SUB_LOCAL_URL } from "./config"
44
import { getWsProvider } from 'polkadot-api/ws-provider/web';
5+
import { withPolkadotSdkCompat } from "polkadot-api/polkadot-sdk-compat"
56

67
let client: PolkadotClient | undefined = undefined
78

@@ -13,6 +14,15 @@ export async function getClient() {
1314
return client;
1415
}
1516

17+
export async function getSdkClient() {
18+
const client = createClient(
19+
withPolkadotSdkCompat(
20+
getWsProvider(SUB_LOCAL_URL),
21+
),
22+
)
23+
return client
24+
}
25+
1626
after(() => {
1727
client?.destroy()
1828
});

evm-tests/src/substrate.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { devnet, MultiAddress, contracts } from '@polkadot-api/descriptors';
1+
import { devnet, MultiAddress } from '@polkadot-api/descriptors';
22
import { TypedApi, Transaction, PolkadotSigner, Binary } from 'polkadot-api';
33
import { sr25519CreateDerive } from "@polkadot-labs/hdkd"
44
import { DEV_PHRASE, entropyToMiniSecret, mnemonicToEntropy, KeyPair } from "@polkadot-labs/hdkd-helpers"
@@ -7,10 +7,8 @@ import { randomBytes } from 'crypto';
77
import { Keyring } from '@polkadot/keyring';
88
import { SS58_PREFIX, TX_TIMEOUT } from "./config";
99
import { getClient } from "./setup"
10-
import { getInkClient, InkClient } from "@polkadot-api/ink-contracts"
1110

1211
let api: TypedApi<typeof devnet> | undefined = undefined
13-
let inkClient: InkClient<contracts> | undefined = undefined
1412

1513
// define url string as type to extend in the future
1614
// export type ClientUrlType = 'ws://localhost:9944' | 'wss://test.finney.opentensor.ai:443' | 'wss://dev.chain.opentensor.ai:443' | 'wss://archive.chain.opentensor.ai';
@@ -19,18 +17,12 @@ export type ClientUrlType = 'ws://localhost:9944'
1917
export async function getDevnetApi() {
2018
if (api === undefined) {
2119
let client = await getClient()
20+
2221
api = client.getTypedApi(devnet)
2322
}
2423
return api
2524
}
2625

27-
export async function gettInkClient() {
28-
if (inkClient === undefined) {
29-
inkClient = getInkClient(contracts.bittensor)
30-
}
31-
return inkClient
32-
}
33-
3426
export function getKeypairFromPath(path: string) {
3527
const entropy = mnemonicToEntropy(DEV_PHRASE)
3628
const miniSecret = entropyToMiniSecret(entropy)
@@ -152,7 +144,7 @@ export async function waitForTransactionWithRetry(
152144
.catch((error) => {
153145
console.log(`transaction error ${error}`);
154146
});
155-
await new Promise((resolve) => setTimeout(resolve, 1000));
147+
await new Promise((resolve) => setTimeout(resolve, 2000));
156148
retries += 1;
157149
}
158150

evm-tests/src/subtensor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as assert from "assert";
22
import { devnet, MultiAddress } from '@polkadot-api/descriptors';
3-
import { TypedApi, TxCallData } from 'polkadot-api';
3+
import { TypedApi, TxCallData, Binary, Enum } from 'polkadot-api';
44
import { KeyPair } from "@polkadot-labs/hdkd-helpers"
55
import { getAliceSigner, waitForTransactionCompletion, getSignerFromKeypair, waitForTransactionWithRetry } from './substrate'
66
import { convertH160ToSS58, convertPublicKeyToSs58, ethAddressToH160 } from './address-utils'
Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,69 @@
11
import * as assert from "assert";
2-
import { getDevnetApi, getRandomSubstrateKeypair, gettInkClient } from "../src/substrate"
3-
import { devnet } from "@polkadot-api/descriptors"
4-
import { Binary, Enum, PolkadotSigner, TypedApi } from "polkadot-api";
5-
import { convertPublicKeyToSs58, convertH160ToSS58 } from "../src/address-utils"
6-
import { raoToEth, tao } from "../src/balance-math"
7-
import { ethers } from "ethers"
8-
import { generateRandomEthersWallet, getPublicClient } from "../src/utils"
9-
import { convertH160ToPublicKey } from "../src/address-utils"
10-
import {
11-
forceSetBalanceToEthAddress, forceSetBalanceToSs58Address, addNewSubnetwork, burnedRegister,
12-
sendProxyCall,
13-
startCall,
14-
} from "../src/subtensor"
2+
import { getDevnetApi, getAliceSigner, getRandomSubstrateKeypair } from "../src/substrate"
3+
import { devnet, contracts, MultiAddress } from "@polkadot-api/descriptors"
4+
import { Binary, PolkadotSigner, TypedApi } from "polkadot-api";
5+
156
import { ETH_LOCAL_URL } from "../src/config";
167
import { ISTAKING_ADDRESS, ISTAKING_V2_ADDRESS, IStakingABI, IStakingV2ABI } from "../src/contracts/staking"
17-
import { PublicClient } from "viem";
188
import { getInkClient, InkClient } from "@polkadot-api/ink-contracts"
19-
import { contracts } from "@polkadot-api/descriptors"
209
import fs from "fs"
10+
import { convertPublicKeyToSs58 } from "../src/address-utils";
11+
import { forceSetBalanceToSs58Address } from "../src/subtensor";
2112

22-
const Determinism = {
23-
Enforced: Enum('Enforced'),
24-
Relaxed: Enum('Relaxed')
25-
} as const;
13+
const bittensorWasmPath = "./bittensor/target/ink/bittensor.wasm"
14+
const bittensorBytecode = fs.readFileSync(bittensorWasmPath)
15+
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
2616

27-
describe("Test neuron precompile add remove stake", () => {
17+
describe("Test wasm contract", () => {
2818

19+
let api: TypedApi<typeof devnet>
2920
const hotkey = getRandomSubstrateKeypair();
3021
const coldkey = getRandomSubstrateKeypair();
31-
const proxy = getRandomSubstrateKeypair();
32-
33-
let api: TypedApi<typeof devnet>
3422

35-
let inkClient: InkClient<contracts>;
23+
let inkClient: InkClient<typeof contracts.bittensor>;
24+
let contractAddress: string;
3625

3726
// sudo account alice as signer
3827
let alice: PolkadotSigner;
3928
before(async () => {
4029
// init variables got from await and async
4130
api = await getDevnetApi()
42-
inkClient = await gettInkClient()
43-
44-
// await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(alice.publicKey))
31+
alice = await getAliceSigner();
4532
await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(hotkey.publicKey))
4633
await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(coldkey.publicKey))
47-
await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(proxy.publicKey))
48-
let netuid = await addNewSubnetwork(api, hotkey, coldkey)
49-
await startCall(api, netuid, coldkey)
50-
51-
console.log("test the case on subnet ", netuid)
5234
})
5335

54-
it("Can upload contract", async () => {
55-
let netuid = (await api.query.SubtensorModule.TotalNetworks.getValue()) - 1
56-
if (api === undefined) {
57-
throw new Error("api is undefined")
36+
it("Can instantiate contract", async () => {
37+
const signer = await getAliceSigner();
38+
inkClient = getInkClient(contracts.bittensor)
39+
const constructor = inkClient.constructor('new')
40+
const data = constructor.encode()
41+
const instantiate_with_code = await api.tx.Contracts.instantiate_with_code({
42+
code: Binary.fromBytes(bittensorBytecode),
43+
storage_deposit_limit: BigInt(10000000),
44+
value: BigInt(0),
45+
gas_limit: {
46+
ref_time: BigInt(1000000000),
47+
proof_size: BigInt(1000000),
48+
},
49+
data: Binary.fromBytes(data.asBytes()),
50+
salt: Binary.fromHex("0x"),
51+
}).signAndSubmit(signer)
52+
53+
54+
let codeStoredEvents = await api.event.Contracts.Instantiated.filter(instantiate_with_code.events)
55+
if (codeStoredEvents.length === 0) {
56+
throw new Error("No events found")
5857
}
59-
const bytecode = fs.readFileSync("bittensor.wasm")
60-
const upload = await api.tx.Contracts.upload_code({
61-
code: Binary.fromBytes(bytecode),
62-
storage_deposit_limit: BigInt(0),
63-
determinism: Determinism.Enforced
64-
})
65-
// const contract = await inkClient.upload(netuid, "bittensor", "bittensor.json")
66-
// assert.ok(contract !== undefined)
58+
contractAddress = codeStoredEvents[0].contract
59+
60+
console.log("===== contractAddress", contractAddress)
6761
})
6862

6963

64+
it("Can query stake info from contract", async () => {
65+
66+
})
7067

7168

7269
});

0 commit comments

Comments
 (0)