11import * 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" ;
44import { Binary , PolkadotSigner , TypedApi } from "polkadot-api" ;
55
6+ import { contracts } from "../.papi/descriptors" ;
7+
68import { ETH_LOCAL_URL } from "../src/config" ;
79import { 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"
911import fs from "fs"
1012import { convertPublicKeyToSs58 } from "../src/address-utils" ;
11- import { forceSetBalanceToSs58Address } from "../src/subtensor" ;
13+ import { addNewSubnetwork , burnedRegister , forceSetBalanceToSs58Address , startCall } from "../src/subtensor" ;
1214
1315const bittensorWasmPath = "./bittensor/target/ink/bittensor.wasm"
1416const 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