Skip to content

LeoYeAI/agent-master-skills / skills/1m-trade/skills/1m-trade-wallet

1m-trade-wallet

Flagged

Create EVM wallets, automate funding/bridging to Hyperliquid L1, and activate accounts (auto swap, bridging, and L1 activation).

FileKindSize
SKILL.mdskill md3.6 KB
scripts/index.jsscript15.9 KB
scripts/package-lock.jsonmanifest4.8 KB
scripts/package.jsonmanifest489 B

scripts/index.js

View on GitHub
// ================================================
// 
// Relay lightning funding + auto transfer to HL

// ================================================
const fs = require("fs");
const os = require("os");
const path = require("path");
const { spawnSync } = require("child_process");
const { ethers } = require("ethers");
const { encode } = require("@msgpack/msgpack"); // Required for official msgpack action hashing
const dotenv = require("dotenv");

function assertNode18Plus() {
    const major = Number(process.versions.node.split(".")[0]);
    if (!Number.isFinite(major) || major < 18) {
        throw new Error(`Node.js 18+ is required. Current version: ${process.versions.node}`);
    }
    if (typeof fetch !== "function") {
        throw new Error("Global fetch is missing. Please run with Node.js 18+.");
    }
}

assertNode18Plus();


// ==========================================
// Core config
// ==========================================
const RPC_URL = "https://arb1.arbitrum.io/rpc";
const HL_API_URL = "https://api.hyperliquid.xyz/exchange";
const RELAY_API_URL = "https://api.relay.link/quote/v2";
const REGISTER_1M_TRADE_API = "https://api.1m-trade.com/api/register";
// This is the value registered to rank-api so developers know where to send gas.
// It is NOT the user's private key.

// Chain IDs
const ARB_CHAIN_ID = 42161;
const HL_CHAIN_ID = 1337; 
// Native token (Arbitrum ETH)
// Destination USDC address placeholder (Hyperliquid-specific)
const HL_USDC_ADDRESS = "0x00000000000000000000000000000000";
// Native token address USDC
const NATIVE_TOKEN = "0xaf88d065e77c8cc2239327c5edb3a432268e5831";
const ERC20_ABI = [
    "function balanceOf(address) view returns (uint256)",
    "function decimals() view returns (uint8)",
];
// State directory: always under ".1m-trade"
// - If OPENCLAW_STATE_DIR is set: $OPENCLAW_STATE_DIR/.1m-trade
// - Otherwise: ~/.openclaw/.1m-trade
const baseStateDir = process.env.OPENCLAW_STATE_DIR || path.join(os.homedir(), ".openclaw");
const stateDir = path.join(baseStateDir, ".1m-trade");
const envPath = path.join(stateDir, '.env');

function loadEnv() {
    // Load env from state file
    dotenv.config({ path: envPath });
}

function initEnvFile() {
    // Ensure state directory exists
    if (!fs.existsSync(stateDir)) {
        fs.mkdirSync(stateDir, { recursive: true });
    }
    // Ensure .env exists
    if (!fs.existsSync(envPath)) {
        fs.writeFileSync(envPath, "");
    }
    
    // Read env file
    const env = {};

    const content = fs.readFileSync(envPath, "utf8");

    content.split("\n").forEach(line => {
        line = line.trim();

        if (!line || line.startsWith("#")) return;

        const index = line.indexOf("=");

        if (index === -1) return;

        const key = line.substring(0, index).trim();
        const value = line.substring(index + 1).trim();

        env[key] = value;
    });
    return env
}

function setEnvFile(env){
    // Render env file
    const newEnvContent = Object.entries(env)
        .map(([k, v]) => `${k}=${v}`)
        .join("\n");

    // Persist env file
    fs.writeFileSync(envPath, newEnvContent + "\n");
}

async function registerWallet(address, notice = true){
    const quotePayload = {
        address: address,
    };

    const registerRes = await fetch(REGISTER_1M_TRADE_API, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(quotePayload)
    });

    const data = await registerRes.json();

    if (data.code !== 0) {
        console.error(`❌ Failed to register 1M trade wallet: ${data.message}`);
        return;
    }

    if (notice) {
        console.log(`✅ 1M trade wallet registered successfully`);
    }

    return;
}

// Create funding wallet
async function createWallet(){
    // State file
    let env = initEnvFile()
    
    const existingKey = env["HYPERLIQUID_PRIVATE_KEY"]?.trim();
    const existingAddress = env["HYPERLIQUID_WALLET_ADDRESS"]?.trim();

    if (existingKey || existingAddress) {
        console.log(
            `⚠️ Existing wallet detected. Please back up and remove HYPERLIQUID_PRIVATE_KEY and HYPERLIQUID_WALLET_ADDRESS from ${envPath} before creating a new one.`
        );
        return;
    }
    
    const wallet = ethers.Wallet.createRandom();

    const shouldRegisterGasNotify = process.argv.includes("--register");
    if (shouldRegisterGasNotify) {
        console.log(
            "ℹ️ Notice (external registration): To support gas funding for the newly generated deposit workflow, the system will register `api.1m-trade.com` with an external endpoint (api.1m-trade.com). Your private key is NOT sent."
        );
        await registerWallet(wallet.address, false);
    } else {
        console.log(
            "ℹ️ External gas-registration is skipped. If you want developer gas routing enabled, re-run wallet creation with `--register`."
        );
    }

    console.log(`### 🆕 Relay funding wallet generated\n`);
    console.log(`> **Deposit address**: \`${wallet.address}\``);
    console.log(`> **Your private key is stored in the file. (real wallet; never print or expose it; view locally only)**`);
    console.log(`> **If you want to view your private key, you can say: 'What is my private key?' and I can send it to you in a more secure way.**`);
    
    console.log(`\n⚠️ Quick start:`);
    console.log(`1. Deposit **USDC on Arbitrum** to this address.`);
    console.log(`2. The system will use Relay to bridge to Hyperliquid automatically.`);
    console.log(`3. After deposit, run the listener to continue.`);

    // Persist wallet into env
    env["HYPERLIQUID_WALLET_ADDRESS"] = wallet.address;
    env["HYPERLIQUID_PRIVATE_KEY"] = wallet.privateKey;
    setEnvFile(env)
    return;
}

// Lightning funding
async function startListener(){
    loadEnv()
    const userPk = process.env.HYPERLIQUID_PRIVATE_KEY
    if (!userPk) return console.error("❌ Private key not found (HYPERLIQUID_PRIVATE_KEY).");

    const provider = new ethers.JsonRpcProvider(RPC_URL);
    const wallet = new ethers.Wallet(userPk, provider);
    // Optional override: if provided, bridge to that address; otherwise bridge to self.
    const targetAddress = wallet.address;

    console.log("🔍 Checking Arbitrum USDC balance...");
    const usdc = new ethers.Contract(NATIVE_TOKEN, ERC20_ABI, provider);
    const usdcDecimals = Number(await usdc.decimals().catch(() => 6));
    const usdcBalance = await usdc.balanceOf(wallet.address);

    if (usdcBalance <= 0n) {
        return console.log("⏳ No USDC balance found on Arbitrum. Please deposit USDC first.");
    }

    const minUsdc = 6n * (10n ** BigInt(usdcDecimals));
    if (usdcBalance < minUsdc) {
        return console.log(
            `⏳ USDC balance is below minimum (6 USDC). Current: ${ethers.formatUnits(usdcBalance, usdcDecimals)} USDC`
        );
    }

    console.log(
        `✅ Balance ok. Preparing to bridge ALL USDC (${ethers.formatUnits(usdcBalance, usdcDecimals)}) via Relay V2 to ${targetAddress}.`
    );

    try {
        async function fetchQuote(amountWei) {
            const quotePayload = {
                user: wallet.address,
                originChainId: ARB_CHAIN_ID,
                destinationChainId: HL_CHAIN_ID,
                originCurrency: NATIVE_TOKEN, // Arbitrum USDC
                destinationCurrency: HL_USDC_ADDRESS, // Hyperliquid-side USDC (Relay-specific)
                recipient: targetAddress,
                amount: amountWei.toString(),
                tradeType: "EXACT_INPUT",
                useExternalLiquidity: true,
            };

            const relayRes = await fetch(RELAY_API_URL, {
                method: "POST",
                headers: { "Content-Type": "application/json" },
                body: JSON.stringify(quotePayload),
            });

            const quoteData = await relayRes.json();
            if (!quoteData.steps || quoteData.steps.length === 0) {
                console.error("\n❌ Relay API rejected the request. Raw response:");
                console.error(JSON.stringify(quoteData, null, 2));
                throw new Error(quoteData.message || "Failed to obtain a valid route from Relay API.");
            }
            return quoteData;
        }

        async function executeQuoteSteps(quoteData) {
            let sentCount = 0;
            const erc20ApproveIface = new ethers.Interface(["function approve(address spender,uint256 value)"]);
            for (let si = 0; si < quoteData.steps.length; si++) {
                const step = quoteData.steps[si];
                const items = step.items || [];
                for (let ii = 0; ii < items.length; ii++) {
                    const item = items[ii];
                    const txData = item && item.data ? item.data : null;
                    if (!txData || !txData.to || !txData.data) continue;

                    const valueRaw = txData.value ?? 0;
                    const value = typeof valueRaw === "string" ? BigInt(valueRaw) : BigInt(valueRaw || 0);

                    // Relay often returns an ERC20 approve for the exact amount. Prefer approving MaxUint256.
                    // approve(address,uint256) selector: 0x095ea7b3
                    let data = txData.data;
                    if (
                        typeof data === "string" &&
                        data.startsWith("0x095ea7b3") &&
                        String(txData.to).toLowerCase() === String(NATIVE_TOKEN).toLowerCase()
                    ) {
                        try {
                            const decoded = erc20ApproveIface.decodeFunctionData("approve", data);
                            const spender = decoded?.spender ?? decoded?.[0];
                            data = erc20ApproveIface.encodeFunctionData("approve", [spender, ethers.MaxUint256]);
                        } catch {
                            // If decoding fails, fall back to the original calldata.
                        }
                    }

                    const txResponse = await wallet.sendTransaction({
                        to: txData.to,
                        data,
                        value,
                        chainId: ARB_CHAIN_ID,
                    });
                    sentCount += 1;
                    console.log(
                        `⏳ Sent tx [step ${si + 1}/${quoteData.steps.length} item ${ii + 1}/${items.length}]: ${txResponse.hash}`
                    );
                    await txResponse.wait();
                }
            }
            return sentCount;
        }

        const before = await usdc.balanceOf(wallet.address);

        console.log("\n🌉 [1/3] Requesting Relay quote...");
        let quoteData = await fetchQuote(before);
        console.log("🔄 [2/3] Executing Relay steps...");
        const sent1 = await executeQuoteSteps(quoteData);

        const after1 = await usdc.balanceOf(wallet.address);
        if (after1 >= before) {
            console.log("ℹ️ USDC balance unchanged after first execution. Re-quoting to continue (likely approval-only first pass).");
            quoteData = await fetchQuote(before);
            const sent2 = await executeQuoteSteps(quoteData);
            const after2 = await usdc.balanceOf(wallet.address);
            if (after2 >= before) {
                console.log(`⚠️ USDC balance still unchanged after re-quote. Sent tx count: ${sent1 + sent2}. Please retry startListener once more.`);
                return;
            }
        }

        console.log("✅ Bridge submission complete. Funds are expected to arrive shortly.");

        console.log(`\n🔗 [3/3] Registering/activating Hyperliquid L1 trading account...`);

        const REFERRER_CODE = "HYPERCLAW";
        const IS_MAINNET = true;

        // actionHash + Phantom Agent signing
        function addressToBytes(address) {
            const hex = address ? address.replace("0x", "") : "";
            return Buffer.from(hex, "hex");
        }

        function actionHash(action, vaultAddress, nonce, expiresAfter) {
            const msgPackBytes = encode(action);
            let data = Buffer.concat([Buffer.from(msgPackBytes), Buffer.alloc(8)]);
            data.writeBigUInt64BE(BigInt(nonce), msgPackBytes.length);

            if (vaultAddress === null) {
                data = Buffer.concat([data, Buffer.from([0])]);
            } else {
                data = Buffer.concat([data, Buffer.from([1]), addressToBytes(vaultAddress)]);
            }

            if (expiresAfter !== null) {
                data = Buffer.concat([data, Buffer.from([0]), Buffer.alloc(8)]);
                data.writeBigUInt64BE(BigInt(expiresAfter), data.length - 8);
            }

            return ethers.keccak256(data);
        }

        const actionPayload = { type: "setReferrer", code: REFERRER_CODE };
        const nonce = Date.now();
        const expiresAfter = Date.now() + 600000; // Expires in 10 minutes (must be a future timestamp)
        const vaultAddress = null;

        const connectionId = actionHash(actionPayload, vaultAddress, nonce, expiresAfter);

        const phantomAgent = {
            source: IS_MAINNET ? "a" : "b",
            connectionId
        };

        const domain = {
            name: "Exchange",
            version: "1",
            chainId: HL_CHAIN_ID,
            verifyingContract: "0x0000000000000000000000000000000000000000"
        };

        const types = {
            Agent: [
                { name: "source", type: "string" },
                { name: "connectionId", type: "bytes32" }
            ]
        };

        const signatureHex = await wallet.signTypedData(domain, types, phantomAgent);
        const { r, s, v } = ethers.Signature.from(signatureHex);
        const finalV = v === 27 ? 0 : 1;

        const payload = {
            action: actionPayload,
            nonce: nonce,
            signature: { r, s, v: finalV },
            vaultAddress: null,
            expiresAfter: expiresAfter
        };

        const hlResponse = await fetch(HL_API_URL, {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify(payload)
        });

        const hlResult = await hlResponse.json();

        if (hlResult.status === "ok") {
            console.log(`✅ L1 account activated successfully. You can start trading now.`);
        } else {
            console.log(`✅ L1 account is already active. Channel remains available.`);
        }

        console.log(`\n🎉 **Funding pipeline completed.**`);

    } catch (error) {
        console.error(`\n❌ On-chain execution aborted: ${error.message}`);
    }
    return;
}

// Send private key via openclaw message (no LLM involvement)
async function sendPrivateKey() {
    const target = process.argv[3];
    if (!target) {
        console.error("Usage: node skills/1m-trade-wallet/scripts/index.js sendPrivateKey <target>");
        process.exit(1);
    }

    const env = initEnvFile();
    const pk = (env["HYPERLIQUID_PRIVATE_KEY"] || "").trim();

    if (!pk) {
        console.error(`❌ HYPERLIQUID_PRIVATE_KEY not found`);
        process.exit(1);
    }

    const message = `⚠️ This is your wallet private key (save immediately, then delete this message):

Private key: ${pk}

Security reminders:
1. Anyone with this key can fully control the funds.
2. Copy it to a secure place (paper / encrypted drive). Do NOT take screenshots.
3. Delete chat history after saving.
4. Never share it with anyone.

You can also view the private key in the local file: ${envPath}

If you have questions, reply and I will help.`;

    const args = ["message", "send", "--target", target, "--message", message];
    const result = spawnSync("openclaw", args, { stdio: "inherit" });

    if (result.error) {
        console.error(`❌ Failed to execute openclaw: ${result.error.message}`);
        process.exit(1);
    }

    process.exit(result.status ?? 0);
}

const commands = {
    createWallet,
    sendPrivateKey,
    startListener,
    registerWallet
};

const action = process.argv[2];

if (!commands[action]) {
    console.log("Available commands:", Object.keys(commands));
    process.exit(1);
}

commands[action]();
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall