Skip to content

Instantly share code, notes, and snippets.

@aalu1418
Last active November 20, 2020 18:05
Show Gist options
  • Save aalu1418/cfdfb97fd7bffed16aaa84de20a474db to your computer and use it in GitHub Desktop.
Save aalu1418/cfdfb97fd7bffed16aaa84de20a474db to your computer and use it in GitHub Desktop.
Final planet mission (Neptune) code

Mission 8: Neptune (Javascript SDK)

These instructions have been created for use in a terminal/command line. Please ask questions if you run into issues!

Steps (Linux commands)

If you are not using linux, please adjust accordingly.

Create a new folder and navigate into it

mkdir sendTokens
cd sendTokens

Install the Javascript SDK

npm install js-conflux-sdk

Create a new file

touch main.js

Paste the following into the main.js file and make the necessary changes to the private key and contract address:

// import the Conflux javascript package into the file
const { Conflux, Drip } = require("js-conflux-sdk"); //importing the Conflux instance constructor + a unit conversion utility

//main function that is called at the end of the file (no inputs)
const main = async () => {
  // create the conflux instance
  const cfx = new Conflux({
    url: "http://test.confluxrpc.org", //specify the conflux endpoint
    logger: console //specify to show JSON-RPC log calls (can be removed if desired)
  });

  // CHANGE HERE
  //create the account instance from a private key (use the private key that deployed the custom tokenn in the previous mission)
  const account = cfx.wallet.addPrivateKey(
    "0x<insert private key>" //insert your private key here. do not share this code with your private key in it!!
  );

  // CHANGE HERE
  //create the contract instance
  const contract = cfx.Contract({
    abi, //ABI will be discussed below
    address: "<insert contract address>" //contract address for the custom token you deployed in the previous mission
  });

  //create and send the transaction instance
  const tx = await contract
    .transfer("0x15fd1E4F13502b1a8BE110F100EC001d0270552d", Drip.fromCFX(1)) //call the transfer function from the ABI (sending 1 token to the verifier)
    .sendTransaction({ from: account }) //sign and send the transaction using the account instance
    .executed(); //wait for the transaction to be executed on chain to complete

  //print out URL for confluxscan of transaction
  console.log(
    "TX link: https://testnet.confluxscan.io/transaction/" + tx.transactionHash
  );
};

//ABI is short for application binary interface. It specifies how you can interact with a smart contract.
//This is a simplified version to only include the transfer function of the contract
const abi = [
  {
    inputs: [
      {
        internalType: "address",
        name: "receiver",
        type: "address"
      },
      {
        internalType: "uint256",
        name: "amount",
        type: "uint256"
      }
    ],
    name: "transfer",
    outputs: [
      {
        internalType: "bool",
        name: "",
        type: "bool"
      }
    ],
    stateMutability: "nonpayable",
    type: "function"
  }
];

//run the main function when the file is run and catch + print errors that may happen
main().catch(e => console.log(e));

From the command line in the folder where main.js is, run the file:

node main.js

It should return the URL for confluxscan needed for submission.

Additional Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment