11import * 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+
156import { ETH_LOCAL_URL } from "../src/config" ;
167import { ISTAKING_ADDRESS , ISTAKING_V2_ADDRESS , IStakingABI , IStakingV2ABI } from "../src/contracts/staking"
17- import { PublicClient } from "viem" ;
188import { getInkClient , InkClient } from "@polkadot-api/ink-contracts"
19- import { contracts } from "@polkadot-api/descriptors"
209import 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