1

I have a following simple contract that stores file details:

pragma solidity ^0.4.17;

contract FileDetail {
    uint fileSize;
    string fileName;
    string fileType;

 function set(uint fileS,string fileN, string fileT) {
        fileSize = fileS;
        fileName = fileN;
        fileType = fileT;
    }
}

How do I access the set method from web3 js and pass the needed parameters ?

2 Answers 2

1

This code works for a local testrpc. For mainnet, replace the httpProvider.

var Web3 = require(‘web3’);
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider(“http://localhost:8545"));

var abiFileContract = web3.eth.contract(PASTE ABI HERE!);
var fileContract = abiFileContract.at('PASTE CONTRACT ADDRESS HERE');

fileContract.set(12, ‘Stephen Hawkings’, ‘Stephen Hawkings’);
1

After deploying your contract you can call set function as follow :

var MContract = web3.eth.contract(“your contract’s ABI”);
var contractInstance= MContract.at(“contract address”);
var fileS=...;
var fileN=...;
var fileT=...;
contractInstance.set(fileS,fileN,fileT,{from: eth.accounts[0],  gas: 200000}, function(error, result){...});

read more here : https://github.com/ethereum/wiki/wiki/JavaScript-API

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.