Skip to content
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

add types e2e test for evm #1374

Merged
merged 8 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions evm-tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.papi
.env
27 changes: 27 additions & 0 deletions evm-tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# type-test

test with ts

## polkadot api

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

## get the new metadata

```bash
sh get-metadata.sh
```

## run all tests

```bash
yarn run test
```

## To run a particular test case, you can pass an argument with the name or part of the name. For example:

```bash
yarn run test -- -g "Can set subnet parameter"
```
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/*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",
"ethers": "^6.13.5",
"polkadot-api": "^1.9.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"
}
}
77 changes: 77 additions & 0 deletions evm-tests/src/address-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Address } from "viem"
import { encodeAddress } from "@polkadot/util-crypto";
import { ss58Address } from "@polkadot-labs/hdkd-helpers";
import { hexToU8a } from "@polkadot/util";
import { blake2AsU8a, decodeAddress } from "@polkadot/util-crypto";
import { Binary } from "polkadot-api";
import { SS58_PREFIX } from "./config"

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

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

// Convert the hash to SS58 format
const ss58Address = encodeAddress(hash, SS58_PREFIX);
return ss58Address;
}

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

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 ETH_PER_RAO = 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 ETH_PER_RAO * value
}

export function compareEthBalanceWithTxFee(balance1: bigint, balance2: bigint) {
if (balance1 > balance2) {
assert((balance1 - balance2) < MAX_TX_FEE)
} else {
assert((balance2 - balance1) < MAX_TX_FEE)
}
}
38 changes: 38 additions & 0 deletions evm-tests/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export const ETH_LOCAL_URL = 'http://localhost:9944'
export const SUB_LOCAL_URL = 'ws://localhost:9944'
export const SS58_PREFIX = 42;
// set the tx timeout as 2 second when eable the fast-blocks feature.
export const TX_TIMEOUT = 2000;

export const IED25519VERIFY_ADDRESS = "0x0000000000000000000000000000000000000402";
export const IEd25519VerifyABI = [
{
inputs: [
{ internalType: "bytes32", name: "message", type: "bytes32" },
{ internalType: "bytes32", name: "publicKey", type: "bytes32" },
{ internalType: "bytes32", name: "r", type: "bytes32" },
{ internalType: "bytes32", name: "s", type: "bytes32" },
],
name: "verify",
outputs: [{ internalType: "bool", name: "", type: "bool" }],
stateMutability: "pure",
type: "function",
},
];

export const IBALANCETRANSFER_ADDRESS = "0x0000000000000000000000000000000000000800";
export const IBalanceTransferABI = [
{
inputs: [
{
internalType: "bytes32",
name: "data",
type: "bytes32",
},
],
name: "transfer",
outputs: [],
stateMutability: "payable",
type: "function",
},
];
Loading
Loading