@@ -14,77 +14,76 @@ import { keccak256 } from 'ethers';
1414import { addNewSubnetwork , forceSetBalanceToSs58Address , startCall } from "../src/subtensor" ;
1515
1616describe ( "Test the UID Lookup precompile" , ( ) => {
17- const hotkey = getRandomSubstrateKeypair ( ) ;
18- const coldkey = getRandomSubstrateKeypair ( ) ;
19- const evmWallet = generateRandomEthersWallet ( ) ;
20- let publicClient : PublicClient ;
17+ const hotkey = getRandomSubstrateKeypair ( ) ;
18+ const coldkey = getRandomSubstrateKeypair ( ) ;
19+ const evmWallet = generateRandomEthersWallet ( ) ;
20+ let publicClient : PublicClient ;
2121
22- let api : TypedApi < typeof devnet >
22+ let api : TypedApi < typeof devnet >
2323
24- let alice : PolkadotSigner ;
24+ let alice : PolkadotSigner ;
2525
26- let uid : number ;
27- let blockNumber : number ;
28- let netuid : number ;
29- let blockNumberAssociated : bigint ;
26+ let uid : number ;
27+ let blockNumber : number ;
28+ let netuid : number ;
29+ let blockNumberAssociated : bigint ;
3030
31- before ( async ( ) => {
32- publicClient = await getPublicClient ( ETH_LOCAL_URL )
33- api = await getDevnetApi ( )
34- alice = await getAliceSigner ( ) ;
31+ before ( async ( ) => {
32+ publicClient = await getPublicClient ( ETH_LOCAL_URL )
33+ api = await getDevnetApi ( )
34+ alice = await getAliceSigner ( ) ;
3535
36- await forceSetBalanceToSs58Address ( api , convertPublicKeyToSs58 ( alice . publicKey ) )
37- await forceSetBalanceToSs58Address ( api , convertPublicKeyToSs58 ( hotkey . publicKey ) )
38- await forceSetBalanceToSs58Address ( api , convertPublicKeyToSs58 ( coldkey . publicKey ) )
36+ await forceSetBalanceToSs58Address ( api , convertPublicKeyToSs58 ( alice . publicKey ) )
37+ await forceSetBalanceToSs58Address ( api , convertPublicKeyToSs58 ( hotkey . publicKey ) )
38+ await forceSetBalanceToSs58Address ( api , convertPublicKeyToSs58 ( coldkey . publicKey ) )
3939
40- netuid = await addNewSubnetwork ( api , hotkey , coldkey )
41- await startCall ( api , netuid , coldkey )
40+ netuid = await addNewSubnetwork ( api , hotkey , coldkey )
41+ await startCall ( api , netuid , coldkey )
4242
43- const maybeUid = await api . query . SubtensorModule . Uids . getValue ( netuid , convertPublicKeyToSs58 ( hotkey . publicKey ) )
43+ const maybeUid = await api . query . SubtensorModule . Uids . getValue ( netuid , convertPublicKeyToSs58 ( hotkey . publicKey ) )
4444
45- if ( maybeUid === undefined ) {
46- throw new Error ( "UID should be defined" )
47- }
48- uid = maybeUid
45+ if ( maybeUid === undefined ) {
46+ throw new Error ( "UID should be defined" )
47+ }
48+ uid = maybeUid
4949
50- // Associate EVM key
51- blockNumber = await api . query . System . Number . getValue ( ) ;
52- const blockNumberBytes = u64 . enc ( BigInt ( blockNumber ) ) ;
53- const blockNumberHash = hexToU8a ( keccak256 ( blockNumberBytes ) ) ;
54- const concatenatedArray = new Uint8Array ( [ ...hotkey . publicKey , ...blockNumberHash ] ) ;
55- const signature = await evmWallet . signMessage ( concatenatedArray ) ;
56- const associateEvmKeyTx = api . tx . SubtensorModule . associate_evm_key ( {
57- netuid : netuid ,
58- hotkey : convertPublicKeyToSs58 ( hotkey . publicKey ) ,
59- evm_key : convertToFixedSizeBinary ( evmWallet . address , 20 ) ,
60- block_number : BigInt ( blockNumber ) ,
61- signature : convertToFixedSizeBinary ( signature , 65 )
62- } ) ;
63- const signer = getSignerFromKeypair ( coldkey ) ;
64- await waitForTransactionCompletion ( api , associateEvmKeyTx , signer )
65- . then ( ( ) => { } )
66- . catch ( ( error ) => { console . log ( `transaction error ${ error } ` ) } ) ;
50+ // Associate EVM key
51+ blockNumber = await api . query . System . Number . getValue ( ) ;
52+ const blockNumberBytes = u64 . enc ( BigInt ( blockNumber ) ) ;
53+ const blockNumberHash = hexToU8a ( keccak256 ( blockNumberBytes ) ) ;
54+ const concatenatedArray = new Uint8Array ( [ ...hotkey . publicKey , ...blockNumberHash ] ) ;
55+ const signature = await evmWallet . signMessage ( concatenatedArray ) ;
56+ const associateEvmKeyTx = api . tx . SubtensorModule . associate_evm_key ( {
57+ netuid : netuid ,
58+ evm_key : convertToFixedSizeBinary ( evmWallet . address , 20 ) ,
59+ block_number : BigInt ( blockNumber ) ,
60+ signature : convertToFixedSizeBinary ( signature , 65 )
61+ } ) ;
62+ const signer = getSignerFromKeypair ( hotkey ) ;
63+ await waitForTransactionCompletion ( api , associateEvmKeyTx , signer )
64+ . then ( ( ) => { } )
65+ . catch ( ( error ) => { console . log ( `transaction error ${ error } ` ) } ) ;
6766
68- const storedEvmKey = await api . query . SubtensorModule . AssociatedEvmAddress . getValue ( netuid , uid )
69- assert . notEqual ( storedEvmKey , undefined , "storedEvmKey should be defined" )
70- if ( storedEvmKey !== undefined ) {
71- assert . equal ( storedEvmKey [ 0 ] . asHex ( ) , convertToFixedSizeBinary ( evmWallet . address , 20 ) . asHex ( ) )
72- blockNumberAssociated = storedEvmKey [ 1 ]
73- }
74- } )
75-
76- it ( "UID lookup via precompile contract works correctly" , async ( ) => {
77- // Get UID for the EVM address
78- const uidArray = await publicClient . readContract ( {
79- abi : IUIDLookupABI ,
80- address : toViemAddress ( IUID_LOOKUP_ADDRESS ) ,
81- functionName : "uidLookup" ,
82- args : [ netuid , evmWallet . address , 1024 ]
83- } )
67+ const storedEvmKey = await api . query . SubtensorModule . AssociatedEvmAddress . getValue ( netuid , uid )
68+ assert . notEqual ( storedEvmKey , undefined , "storedEvmKey should be defined" )
69+ if ( storedEvmKey !== undefined ) {
70+ assert . equal ( storedEvmKey [ 0 ] . asHex ( ) , convertToFixedSizeBinary ( evmWallet . address , 20 ) . asHex ( ) )
71+ blockNumberAssociated = storedEvmKey [ 1 ]
72+ }
73+ } )
8474
85- assert . notEqual ( uidArray , undefined , "UID should be defined" )
86- assert . ok ( Array . isArray ( uidArray ) , `UID should be an array, got ${ typeof uidArray } ` )
87- assert . ok ( uidArray . length > 0 , "UID array should not be empty" )
88- assert . deepStrictEqual ( uidArray [ 0 ] , { uid : uid , block_associated : blockNumberAssociated } )
75+ it ( "UID lookup via precompile contract works correctly" , async ( ) => {
76+ // Get UID for the EVM address
77+ const uidArray = await publicClient . readContract ( {
78+ abi : IUIDLookupABI ,
79+ address : toViemAddress ( IUID_LOOKUP_ADDRESS ) ,
80+ functionName : "uidLookup" ,
81+ args : [ netuid , evmWallet . address , 1024 ]
8982 } )
83+
84+ assert . notEqual ( uidArray , undefined , "UID should be defined" )
85+ assert . ok ( Array . isArray ( uidArray ) , `UID should be an array, got ${ typeof uidArray } ` )
86+ assert . ok ( uidArray . length > 0 , "UID array should not be empty" )
87+ assert . deepStrictEqual ( uidArray [ 0 ] , { uid : uid , block_associated : blockNumberAssociated } )
88+ } )
9089} ) ;
0 commit comments