2

I want to access and interact with a deployed contract. My restriction: Knowing the API (functions, fields), but not the implementation.

I was looking at the Ethereum documentation "Accessing Contracts and Transactions" (http://ethdocs.org/en/latest/contracts-and-transactions/accessing-contracts-and-transactions.html).

They have this Web3.js

var source = 'contract Multiply7 { event Print(uint); function multiply(uint input) returns (uint) { Print(input * 7); return input * 7; } }';
var compiled = web3.eth.compile.solidity(source);
var Multiply7 = web3.eth.contract(compiled.Multiply7.info.abiDefinition);
var multi = Multiply7.at("0x0ab60714033847ad7f0677cc7514db48313976e2")
multi.multiply.sendTransaction(6, {from: "0xeb85a5557e5bdc18ee1934a89d8bb402398ee26a"})

However, this requires to know the implementation of the contract, which does not make sense to me.

Thanks for any help.

1

All you need is the address of the contract and the JSON representation of the Application Binary Interface.

To get the ABI:

> var source = 'contract Multiply7 { event Print(uint); function multiply(uint input) returns (uint) { Print(input * 7); return input * 7; } }';
undefined
> var compiled = web3.eth.compile.solidity(source);
undefined
> compiled.Multiply7.info.abiDefinition
[{
    constant: false,
    inputs: [{
        name: "input",
        type: "uint256"
    }],
    name: "multiply",
    outputs: [{
        name: "",
        type: "uint256"
    }],
    payable: false,
    type: "function"
}, {
    anonymous: false,
    inputs: [{
        indexed: false,
        name: "",
        type: "uint256"
    }],
    name: "Print",
    type: "event"
}]

So, without knowing the implementation but only the address and the ABI, you should be able to execute the following command:

var Multiply7 = web3.eth.contract([{constant: false, inputs: [{name: "input", type: "uint256"}], name: "multiply", outputs: [{name: "", type: "uint256"}], payable: false, type: "function"}, {anonymous: false, inputs: [{indexed: false, name: "", type: "uint256"}], name: "Print", type: "event"}]);
var multi = Multiply7.at("0x0ab60714033847ad7f0677cc7514db48313976e2")
multi.multiply.sendTransaction(6, {from: "0xeb85a5557e5bdc18ee1934a89d8bb402398ee26a"})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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