Skip to content
ThatOtherZach edited this page Jan 2, 2018 · 3 revisions

A script that gets runs a function call in a smart contract on Ethereum. Currently this script is setup to call the "Ebola on Ethereum" smart contract. For more info on that please visit the Github repo.

Note: This script will likely only work for call-able ("constant") functions in a smart contract. This script will probably not work on payable or not constant solidity functions. Use at your own risk.

For this example we're going to use the script located here.

Whats Happening Here?

var Web3 = require('web3');

web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/YOUR-API-TOKEN-HERE'));

Please read the Basic Required Setup page for this first bit, you'll see it a lot.

console.log('Contract-ing Ebola.....');

This is a joke... I like to think I'm funny. Moving on...

var abi = [{"constant":true,"inputs":[],"name":"getEbola","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"kill","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getInfo","outputs":[{"name":"","type":"string"},{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"tipCreator","outputs":[{"name":"","type":"string"},{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"}];

This is a bit verbose, but all we're doing here is defining the ABI (Application Binary Interface). Now I'm not a computer scientist, but basically we're are providing the functions of the contract as an array variable so we can reference them later on.

var addr = "0xe16f391e860420e65c659111c9e1601c0f8e2818";

var EbolaContract = new web3.eth.Contract(abi, addr);

Then we provide a contract address to use. In this case 0xe16f391e860420e65c659111c9e1601c0f8e2818 is the ebola smart contract on the main Ethereum blockchain. Then we define a new variable EbolaContract which is comprised of the web3 function to interact and it includes the ebola smart contract ABI and address.

// FUNCTION can be "getEbola", "getInfo", "tipCreator" and "kill"
EbolaContract.methods.FUNCTION().call().then(console.log);

Now the fun part! We use EbolaContract combined with methods and a function. The function can be any one of the options in the ABI array we defined previously. Keep in mind this can only be a call-able function. If you swap FUNCTION out for one of the other functions in the comment and run the script you'll get a different result.

Run the script from your console and you should get a response from the smart contract. I'm not going to spoil the surprise here ;)

Going Further

Try calling all the functions on that contract and see what happens! Then try using it on another contract with call-able functions. If you're feeling really ambitious, try writing up your own smart contract, deploying it, and then running this script to interact with it!