Skip to content

Commit d237a0a

Browse files
committed
add types e2e test for evm
1 parent 4b2a266 commit d237a0a

40 files changed

+5790
-0
lines changed

evm-tests/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.env
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*
2+
!.gitignore
3+
!package.json
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"version": "0.1.0-autogenerated.15932613768666598877",
3+
"name": "@polkadot-api/descriptors",
4+
"files": [
5+
"dist"
6+
],
7+
"exports": {
8+
".": {
9+
"types": "./dist/index.d.ts",
10+
"module": "./dist/index.mjs",
11+
"import": "./dist/index.mjs",
12+
"require": "./dist/index.js"
13+
},
14+
"./package.json": "./package.json"
15+
},
16+
"main": "./dist/index.js",
17+
"module": "./dist/index.mjs",
18+
"browser": "./dist/index.mjs",
19+
"types": "./dist/index.d.ts",
20+
"sideEffects": false,
21+
"peerDependencies": {
22+
"polkadot-api": "*"
23+
}
24+
}
218 KB
Binary file not shown.

evm-tests/.papi/polkadot-api.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version": 0,
3+
"descriptorPath": ".papi/descriptors",
4+
"entries": {
5+
"devnet": {
6+
"wsUrl": "ws://localhost:9944",
7+
"metadata": ".papi/metadata/devnet.scale",
8+
"genesis": "0xd4ee169957410f461aada33a817b3800a1521267a1d34d4b8b14836ba4ebcb93"
9+
}
10+
}
11+
}

evm-tests/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# type-test
2+
3+
test with ts
4+
5+
## install papi
6+
7+
npm install polkadot-api
8+
9+
## polkadot api
10+
11+
npx papi add devnet -w ws://10.0.0.11:9944
12+
13+
## get the new metadata
14+
15+
sh get-metadta.sh
16+
17+
## run all tests
18+
19+
yarn test
20+
21+
## update dependence for coding
22+
23+
npm update @polkadot-api/descriptors

evm-tests/get-metadata.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
rm -rf .papi
2+
npx papi add devnet -w ws://localhost:9944
3+

evm-tests/local.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import * as assert from "assert";
2+
import { getAliceSigner, getClient, getDevnetApi, getRandomSubstrateKeypair } from "../src/substrate"
3+
import { SUB_LOCAL_URL, } from "../src/config";
4+
import { devnet } from "@polkadot-api/descriptors"
5+
import { PolkadotSigner, TypedApi } from "polkadot-api";
6+
import { convertPublicKeyToSs58, convertH160ToSS58 } from "../src/address-utils"
7+
import { ethers } from "ethers"
8+
import { INEURON_ADDRESS, INeuronABI } from "../src/contracts/neuron"
9+
import { generateRandomEthersWallet } from "../src/utils"
10+
import { forceSetBalanceToEthAddress, forceSetBalanceToSs58Address, addNewSubnetwork, burnedRegister } from "../src/subtensor"
11+
12+
describe("Test neuron precompile Serve Axon Prometheus", () => {
13+
// init eth part
14+
// const wallet1 = generateRandomEthersWallet();
15+
// const wallet2 = generateRandomEthersWallet();
16+
// const wallet3 = generateRandomEthersWallet();
17+
18+
// init substrate part
19+
20+
// const coldkey = getRandomSubstrateKeypair();
21+
22+
let api: TypedApi<typeof devnet>
23+
24+
// sudo account alice as signer
25+
let alice: PolkadotSigner;
26+
before(async () => {
27+
// init variables got from await and async
28+
const subClient = await getClient(SUB_LOCAL_URL)
29+
api = await getDevnetApi()
30+
// alice = await getAliceSigner();
31+
32+
// await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(coldkey.publicKey))
33+
// await forceSetBalanceToEthAddress(api, wallet1.address)
34+
// await forceSetBalanceToEthAddress(api, wallet2.address)
35+
// await forceSetBalanceToEthAddress(api, wallet3.address)
36+
37+
38+
let index = 0;
39+
while (index < 30) {
40+
const hotkey = getRandomSubstrateKeypair();
41+
const coldkey = getRandomSubstrateKeypair();
42+
await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(hotkey.publicKey))
43+
await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(coldkey.publicKey))
44+
let netuid = await addNewSubnetwork(api, hotkey, coldkey)
45+
}
46+
47+
48+
})
49+
50+
it("Serve Axon", async () => {
51+
52+
});
53+
});

evm-tests/package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"scripts": {
3+
"test": "mocha --timeout 999999 --require ts-node/register test/eth.sub*test.ts"
4+
},
5+
"keywords": [],
6+
"author": "",
7+
"license": "ISC",
8+
"dependencies": {
9+
"@polkadot-api/descriptors": "file:.papi/descriptors",
10+
"@polkadot-labs/hdkd": "^0.0.10",
11+
"@polkadot-labs/hdkd-helpers": "^0.0.11",
12+
"@polkadot/api": "15.1.1",
13+
"crypto": "^1.0.1",
14+
"dotenv": "16.4.7",
15+
"polkadot-api": "^1.9.1",
16+
"ethers": "^6.13.5",
17+
"viem": "2.23.4"
18+
},
19+
"devDependencies": {
20+
"@types/bun": "^1.1.13",
21+
"@types/chai": "^5.0.1",
22+
"@types/mocha": "^10.0.10",
23+
"assert": "^2.1.0",
24+
"chai": "^5.2.0",
25+
"mocha": "^11.1.0",
26+
"prettier": "^3.3.3",
27+
"ts-node": "^10.9.2",
28+
"typescript": "^5.7.2",
29+
"vite": "^5.4.8"
30+
}
31+
}

evm-tests/src/address-utils.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { Address } from "viem"
2+
import { encodeAddress } from "@polkadot/util-crypto";
3+
import { MultiAddress } from '@polkadot-api/descriptors';
4+
import { ss58Address, KeyPair } from "@polkadot-labs/hdkd-helpers";
5+
import { hexToU8a } from "@polkadot/util";
6+
import { blake2AsU8a, decodeAddress } from "@polkadot/util-crypto";
7+
import { Binary } from "polkadot-api";
8+
9+
export function toViemAddress(address: string): Address {
10+
let addressNoPrefix = address.replace("0x", "")
11+
return `0x${addressNoPrefix}`
12+
}
13+
14+
export function convertSs58ToMultiAddress(ss58Address: string) {
15+
const address = MultiAddress.Id(ss58Address)
16+
return address
17+
}
18+
19+
export function convertH160ToSS58(ethAddress: string) {
20+
// get the public key
21+
const hash = convertH160ToPublicKey(ethAddress);
22+
23+
// Convert the hash to SS58 format
24+
const ss58Address = encodeAddress(hash, 42); // Assuming network ID 42
25+
return ss58Address;
26+
}
27+
28+
export function convertPublicKeyToSs58(publickey: Uint8Array) {
29+
return ss58Address(publickey, 42);
30+
}
31+
32+
export function convertH160ToPublicKey(ethAddress: string) {
33+
const prefix = "evm:";
34+
const prefixBytes = new TextEncoder().encode(prefix);
35+
const addressBytes = hexToU8a(
36+
ethAddress.startsWith("0x") ? ethAddress : `0x${ethAddress}`
37+
);
38+
const combined = new Uint8Array(prefixBytes.length + addressBytes.length);
39+
40+
// Concatenate prefix and Ethereum address
41+
combined.set(prefixBytes);
42+
combined.set(addressBytes, prefixBytes.length);
43+
44+
// Hash the combined data (the public key)
45+
const hash = blake2AsU8a(combined);
46+
return hash;
47+
}
48+
49+
export function ss58ToEthAddress(ss58Address: string) {
50+
// Decode the SS58 address to a Uint8Array public key
51+
const publicKey = decodeAddress(ss58Address);
52+
53+
// Take the first 20 bytes of the hashed public key for the Ethereum address
54+
const ethereumAddressBytes = publicKey.slice(0, 20);
55+
56+
// Convert the 20 bytes into an Ethereum H160 address format (Hex string)
57+
const ethereumAddress = '0x' + Buffer.from(ethereumAddressBytes).toString('hex');
58+
59+
return ethereumAddress;
60+
}
61+
62+
export function ss58ToH160(ss58Address: string): Binary {
63+
// Decode the SS58 address to a Uint8Array public key
64+
const publicKey = decodeAddress(ss58Address);
65+
66+
// Take the first 20 bytes of the hashed public key for the Ethereum address
67+
const ethereumAddressBytes = publicKey.slice(0, 20);
68+
69+
70+
return new Binary(ethereumAddressBytes);
71+
}
72+
73+
export function ethAddressToH160(ethAddress: string): Binary {
74+
// Decode the SS58 address to a Uint8Array public key
75+
const publicKey = hexToU8a(ethAddress);
76+
77+
// Take the first 20 bytes of the hashed public key for the Ethereum address
78+
// const ethereumAddressBytes = publicKey.slice(0, 20);
79+
80+
81+
return new Binary(publicKey);
82+
}

0 commit comments

Comments
 (0)