Skip to content

add types e2e test for evm #1374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions evm-tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
3 changes: 3 additions & 0 deletions evm-tests/.papi/descriptors/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
!.gitignore
!package.json
24 changes: 24 additions & 0 deletions evm-tests/.papi/descriptors/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"version": "0.1.0-autogenerated.15932613768666598877",
"name": "@polkadot-api/descriptors",
"files": [
"dist"
],
"exports": {
".": {
"types": "./dist/index.d.ts",
"module": "./dist/index.mjs",
"import": "./dist/index.mjs",
"require": "./dist/index.js"
},
"./package.json": "./package.json"
},
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"browser": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"sideEffects": false,
"peerDependencies": {
"polkadot-api": "*"
}
}
Binary file added evm-tests/.papi/metadata/devnet.scale
Binary file not shown.
11 changes: 11 additions & 0 deletions evm-tests/.papi/polkadot-api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": 0,
"descriptorPath": ".papi/descriptors",
"entries": {
"devnet": {
"wsUrl": "ws://localhost:9944",
"metadata": ".papi/metadata/devnet.scale",
"genesis": "0xd4ee169957410f461aada33a817b3800a1521267a1d34d4b8b14836ba4ebcb93"
}
}
}
23 changes: 23 additions & 0 deletions evm-tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# type-test

test with ts

## install papi

npm install polkadot-api

## polkadot api

npx papi add devnet -w ws://10.0.0.11:9944

## get the new metadata

sh get-metadta.sh

## run all tests

yarn test

## update dependence for coding

npm update @polkadot-api/descriptors
3 changes: 3 additions & 0 deletions evm-tests/get-metadata.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
rm -rf .papi
npx papi add devnet -w ws://localhost:9944

53 changes: 53 additions & 0 deletions evm-tests/local.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import * as assert from "assert";
import { getAliceSigner, getClient, getDevnetApi, getRandomSubstrateKeypair } from "../src/substrate"
import { SUB_LOCAL_URL, } from "../src/config";
import { devnet } from "@polkadot-api/descriptors"
import { PolkadotSigner, TypedApi } from "polkadot-api";
import { convertPublicKeyToSs58, convertH160ToSS58 } from "../src/address-utils"
import { ethers } from "ethers"
import { INEURON_ADDRESS, INeuronABI } from "../src/contracts/neuron"
import { generateRandomEthersWallet } from "../src/utils"
import { forceSetBalanceToEthAddress, forceSetBalanceToSs58Address, addNewSubnetwork, burnedRegister } from "../src/subtensor"

describe("Test neuron precompile Serve Axon Prometheus", () => {
// init eth part
// const wallet1 = generateRandomEthersWallet();
// const wallet2 = generateRandomEthersWallet();
// const wallet3 = generateRandomEthersWallet();

// init substrate part

// const coldkey = getRandomSubstrateKeypair();

let api: TypedApi<typeof devnet>

// sudo account alice as signer
let alice: PolkadotSigner;
before(async () => {
// init variables got from await and async
const subClient = await getClient(SUB_LOCAL_URL)
api = await getDevnetApi()
// alice = await getAliceSigner();

// await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(coldkey.publicKey))
// await forceSetBalanceToEthAddress(api, wallet1.address)
// await forceSetBalanceToEthAddress(api, wallet2.address)
// await forceSetBalanceToEthAddress(api, wallet3.address)


let index = 0;
while (index < 30) {
const hotkey = getRandomSubstrateKeypair();
const coldkey = getRandomSubstrateKeypair();
await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(hotkey.publicKey))
await forceSetBalanceToSs58Address(api, convertPublicKeyToSs58(coldkey.publicKey))
let netuid = await addNewSubnetwork(api, hotkey, coldkey)
}


})

it("Serve Axon", async () => {

});
});
31 changes: 31 additions & 0 deletions evm-tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"scripts": {
"test": "mocha --timeout 999999 --require ts-node/register test/eth.sub*test.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@polkadot-api/descriptors": "file:.papi/descriptors",
"@polkadot-labs/hdkd": "^0.0.10",
"@polkadot-labs/hdkd-helpers": "^0.0.11",
"@polkadot/api": "15.1.1",
"crypto": "^1.0.1",
"dotenv": "16.4.7",
"polkadot-api": "^1.9.1",
"ethers": "^6.13.5",
"viem": "2.23.4"
},
"devDependencies": {
"@types/bun": "^1.1.13",
"@types/chai": "^5.0.1",
"@types/mocha": "^10.0.10",
"assert": "^2.1.0",
"chai": "^5.2.0",
"mocha": "^11.1.0",
"prettier": "^3.3.3",
"ts-node": "^10.9.2",
"typescript": "^5.7.2",
"vite": "^5.4.8"
}
}
82 changes: 82 additions & 0 deletions evm-tests/src/address-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Address } from "viem"
import { encodeAddress } from "@polkadot/util-crypto";
import { MultiAddress } from '@polkadot-api/descriptors';
import { ss58Address, KeyPair } from "@polkadot-labs/hdkd-helpers";
import { hexToU8a } from "@polkadot/util";
import { blake2AsU8a, decodeAddress } from "@polkadot/util-crypto";
import { Binary } from "polkadot-api";

export function toViemAddress(address: string): Address {
let addressNoPrefix = address.replace("0x", "")
return `0x${addressNoPrefix}`
}

export function convertSs58ToMultiAddress(ss58Address: string) {
const address = MultiAddress.Id(ss58Address)
return address
}

export function convertH160ToSS58(ethAddress: string) {
// get the public key
const hash = convertH160ToPublicKey(ethAddress);

// Convert the hash to SS58 format
const ss58Address = encodeAddress(hash, 42); // Assuming network ID 42
return ss58Address;
}

export function convertPublicKeyToSs58(publickey: Uint8Array) {
return ss58Address(publickey, 42);
}

export function convertH160ToPublicKey(ethAddress: string) {
const prefix = "evm:";
const prefixBytes = new TextEncoder().encode(prefix);
const addressBytes = hexToU8a(
ethAddress.startsWith("0x") ? ethAddress : `0x${ethAddress}`
);
const combined = new Uint8Array(prefixBytes.length + addressBytes.length);

// Concatenate prefix and Ethereum address
combined.set(prefixBytes);
combined.set(addressBytes, prefixBytes.length);

// Hash the combined data (the public key)
const hash = blake2AsU8a(combined);
return hash;
}

export function ss58ToEthAddress(ss58Address: string) {
// Decode the SS58 address to a Uint8Array public key
const publicKey = decodeAddress(ss58Address);

// Take the first 20 bytes of the hashed public key for the Ethereum address
const ethereumAddressBytes = publicKey.slice(0, 20);

// Convert the 20 bytes into an Ethereum H160 address format (Hex string)
const ethereumAddress = '0x' + Buffer.from(ethereumAddressBytes).toString('hex');

return ethereumAddress;
}

export function ss58ToH160(ss58Address: string): Binary {
// Decode the SS58 address to a Uint8Array public key
const publicKey = decodeAddress(ss58Address);

// Take the first 20 bytes of the hashed public key for the Ethereum address
const ethereumAddressBytes = publicKey.slice(0, 20);


return new Binary(ethereumAddressBytes);
}

export function ethAddressToH160(ethAddress: string): Binary {
// Decode the SS58 address to a Uint8Array public key
const publicKey = hexToU8a(ethAddress);

// Take the first 20 bytes of the hashed public key for the Ethereum address
// const ethereumAddressBytes = publicKey.slice(0, 20);


return new Binary(publicKey);
}
26 changes: 26 additions & 0 deletions evm-tests/src/balance-math.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import assert from "assert"

export const TAO = BigInt(1000000000) // 10^9
export const ETHPerRAO = BigInt(1000000000) // 10^9
export const GWEI = BigInt(1000000000) // 10^9
export const MAX_TX_FEE = BigInt(21000000) * GWEI // 100 times EVM to EVM transfer fee

export function bigintToRao(value: bigint) {
return TAO * value
}

export function tao(value: number) {
return TAO * BigInt(value)
}

export function raoToEth(value: bigint) {
return ETHPerRAO * value
}

export function compareEthBalanceWithTxFee(balance1: bigint, balance2: bigint) {
if (balance1 > balance2) {
assert((balance1 - balance2) < MAX_TX_FEE)
} else {
assert((balance2 - balance1) < MAX_TX_FEE)
}
}
Loading
Loading