sal/examples/hero_vault/add_custom_network.rhai

126 lines
3.8 KiB
Plaintext

// Example script demonstrating how to add a custom network and use it
// This script shows the new network-independent wallet design
// Load a key space (or create one if it doesn't exist)
if (!load_key_space("demo", "password123")) {
print("Creating new key space...");
create_key_space("demo", "password123");
}
// Always create a keypair (will be a no-op if it already exists)
print("Creating keypair...");
create_keypair("demo_key", "password123");
// Select the keypair
print("Selecting keypair...");
select_keypair("demo_key");
// Create an Ethereum wallet (network-independent)
print("Creating Ethereum wallet...");
create_ethereum_wallet();
// Get the wallet address (same for all networks)
let address = get_ethereum_address();
print(`Your Ethereum address: ${address}`);
// List the built-in networks
print("\nBuilt-in networks:");
let networks = list_supported_networks();
for network in networks {
print(`- ${network}`);
}
// Register a custom network (Sepolia testnet)
print("\nRegistering Sepolia testnet...");
let success = register_network(
"Sepolia",
11155111,
"https://sepolia.blast.io", // Using Blast.io's public RPC endpoint
"https://sepolia.etherscan.io",
"ETH",
18
);
if (success) {
print("Sepolia testnet registered successfully!");
} else {
print("Failed to register Sepolia testnet!");
}
// List networks again to confirm Sepolia was added
print("\nUpdated networks list:");
networks = list_supported_networks();
for network in networks {
print(`- ${network}`);
}
// Get network details
print("\nSepolia network details:");
print(`Token symbol: ${get_network_token_symbol("sepolia")}`);
print(`Explorer URL: ${get_network_explorer_url("sepolia")}`);
// Check balance on different networks
print("\nChecking balances on different networks...");
print(`NOTE: These will likely show zero unless you've funded the address`);
print(`NOTE: Balance checks may fail if the RPC endpoints are not accessible`);
// Check balance on Sepolia (this might fail if the RPC endpoint is not accessible)
print("\nTrying to get balance on Sepolia...");
let sepolia_balance = get_balance("sepolia", address);
if (sepolia_balance == "") {
print("Failed to get balance on Sepolia (this is expected if you don't have access to the Sepolia RPC)");
} else {
print(`Balance on Sepolia: ${sepolia_balance}`);
}
// Check balance on Gnosis
print("\nTrying to get balance on Gnosis...");
let gnosis_balance = get_balance("gnosis", address);
if (gnosis_balance == "") {
print("Failed to get balance on Gnosis");
} else {
print(`Balance on Gnosis: ${gnosis_balance}`);
}
// Check balance on Peaq
print("\nTrying to get balance on Peaq...");
let peaq_balance = get_balance("peaq", address);
if (peaq_balance == "") {
print("Failed to get balance on Peaq");
} else {
print(`Balance on Peaq: ${peaq_balance}`);
}
// Demonstrate sending a transaction (commented out to prevent accidental execution)
// To execute this, uncomment the following lines and make sure your wallet has funds
/*
print("\nSending 0.001 ETH on Sepolia...");
let recipient = "0x742d35Cc6634C0532925a3b844Bc454e4438f44e"; // Example address
let amount = "1000000000000000"; // 0.001 ETH in wei
let tx_hash = send_eth("sepolia", recipient, amount);
if (tx_hash != "") {
print(`Transaction sent! Hash: ${tx_hash}`);
print(`View on Sepolia Explorer: ${get_network_explorer_url("sepolia")}/tx/${tx_hash}`);
} else {
print("Transaction failed!");
}
*/
// Remove the custom network
print("\nRemoving Sepolia network...");
success = remove_network("Sepolia");
if (success) {
print("Sepolia network removed successfully!");
} else {
print("Failed to remove Sepolia network!");
}
// List networks again to confirm Sepolia was removed
print("\nFinal networks list:");
networks = list_supported_networks();
for network in networks {
print(`- ${network}`);
}
print("\nDone!");