Skip to content

Instantly share code, notes, and snippets.

@ColinGilbert
Last active July 11, 2023 19:29
Show Gist options
  • Select an option

  • Save ColinGilbert/e1906ea35c789c6065f13dc323c4c499 to your computer and use it in GitHub Desktop.

Select an option

Save ColinGilbert/e1906ea35c789c6065f13dc323c4c499 to your computer and use it in GitHub Desktop.

Revisions

  1. ColinGilbert revised this gist Jul 11, 2023. No changes.
  2. ColinGilbert created this gist Jul 11, 2023.
    123 changes: 123 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,123 @@
    const HELP = "Usage example: \n\nnode transfer.js receiver amount";
    const Pact = require("pact-lang-api");
    require("dotenv").config();
    const NETWORK_ID = "testnet04";
    const CHAIN_ID = "1";
    const API_HOST = `https://api.testnet.chainweb.com/chainweb/0.0/${NETWORK_ID}/chain/${CHAIN_ID}/pact`;
    const KEY_PAIR = {
    publicKey: process.env.SENDER_PUBKEY,
    secretKey: process.env.SENDER_SECRET_KEY,
    };

    const creationTime = () => Math.round(new Date().getTime() / 1000 - 15);

    if (process.argv.length !== 4) {
    console.info(HELP);
    process.exit(1);
    }

    if (KEY_PAIR.publicKey === "" || KEY_PAIR.secretKey === "") {
    console.error("Please set a key pair");
    process.exit(1);
    }

    transferCreate(process.env.SENDER_NAME, process.argv[2], process.argv[3]);

    async function transferCreate(sender, receiver, amount) {
    const cmd = {
    networkId: NETWORK_ID,
    keyPairs: [
    Object.assign(KEY_PAIR, {
    clist: [
    Pact.lang.mkCap(
    "GAS",
    "Capability to allow buying gas",
    "coin.GAS",
    []
    ).cap,
    Pact.lang.mkCap(
    "Transfer",
    "Capability to allow coin transfer",
    "coin.TRANSFER",
    [sender, receiver, { decimal: amount }]
    ).cap,
    ],
    }),
    ],
    pactCode: `(coin.transfer "${sender}" "${receiver}" ${amount})`,
    envData: {},
    meta: {
    creationTime: creationTime(),
    ttl: 28000,
    gasLimit: 800,
    chainId: CHAIN_ID,
    gasPrice: 0.0000001,
    sender: sender,
    },
    };

    const response = await Pact.fetch.send(cmd, API_HOST);
    //console.log(response);
    console.log(`Request key: ${response.requestKeys[0]}`);
    console.log("Transaction pending...");

    const txResult = await Pact.fetch.listen(
    { listen: response.requestKeys[0] },
    API_HOST
    );
    console.log("Transaction mined!");
    console.log(txResult);
    }

    if (KEY_PAIR.publicKey === "" || KEY_PAIR.secretKey === "") {
    console.error("Please set a key pair");
    process.exit(1);
    }

    transferCreate(process.env.FIRST_ACCT_NAME, process.argv[2], process.argv[3]);

    async function transferCreate(sender, receiver, amount) {
    const cmd = {
    networkId: NETWORK_ID,
    keyPairs: [
    Object.assign(KEY_PAIR, {
    clist: [
    Pact.lang.mkCap(
    "GAS",
    "Capability to allow buying gas",
    "coin.GAS",
    []
    ).cap,
    Pact.lang.mkCap(
    "Transfer",
    "Capability to allow coin transfer",
    "coin.TRANSFER",
    [sender, receiver, { decimal: amount }]
    ).cap,
    ],
    }),
    ],
    pactCode: `(coin.transfer "${sender}" "${newAccount}" ${amount})`,
    envData: {},
    meta: {
    creationTime: creationTime(),
    ttl: 28000,
    gasLimit: 800,
    chainId: CHAIN_ID,
    gasPrice: 0.0000001,
    sender: sender,
    },
    };

    const response = await Pact.fetch.send(cmd, API_HOST);
    //console.log(response);
    console.log(`Request key: ${response.requestKeys[0]}`);
    console.log("Transaction pending...");

    const txResult = await Pact.fetch.listen(
    { listen: response.requestKeys[0] },
    API_HOST
    );
    console.log("Transaction mined!");
    console.log(txResult);
    }