Hot answers tagged

114

The ABI, Application Binary Interface, is basically how you call functions in a contract and get data back. An ABI determines such details as how functions are called and in which binary format information should be passed from one program component to the next... An Ethereum smart contract is bytecode deployed on the Ethereum blockchain. There could be ...


105

ABI stands for application binary interface. In general, an ABI is the interface between two program modules, one of which is often at the level of machine code. The interface is the de facto method for encoding/decoding data into/out of the machine code. In Ethereum, it's basically how you can encode Solidity contract calls for the EVM and, backwards, ...


70

Summary Your original contract code would not work correctly until I added a constant to the function definition to indicate that this function does not modify the blockchain. I had to use the method signature of double(int256) instead of double(int) to get the eth_call JSON-RPC to work. The Details Run Your geth Instance You can use the Testnet ...


33

The JSON is called an ABI. You do need the source code, as you have, and one way to get the ABI is to paste it in Remix IDE, compile it, and then click the 'ABI' button to copy the ABI into your clipboard.


30

Thank you K.Fichter for the help, I just want to fully clarify the answer for anyone else who may run into this issue. Let's say you want to get the balance of a token at an Ethereum Address. Using Web3 you would do the following: var tokenContract = web3.eth.contract(contractABI).at(contractAddress) var decimal = tokenContract.decimals() var balance = ...


25

Here is an example from What is an ABI and why is it needed to interact with contracts? contract Foo { function baz(uint32 x, bool y) returns (bool r) { r = x > 32 || y; } } If we wanted to call baz() with the parameters 69 and true, we would pass 68 bytes in total, which can be broken down into: 0xcdcd77c0: the Method ID. This is derived as the first ...


23

Mainly due to optimizations carried out when the source code is compiled it's not feasible to get the source code from the bytecode/opcode itself. Refer this question. Compilation back to the original source code is impossible because all variable names, type names and even function names are removed. It might be technically possible to arrive at some ...


23

It means that now you need to change your code to: uint256 _unixTimestamp; uint256 _timeExpired; bytes32 output = keccak256( abi.encodePacked(msg.sender, _unixTimestamp, _timeExpired) );


19

Contract Defintion: Formal definition in high-level code (e.g. solidity). Compiled Contract: The contract converted to byte-code to run on the Ethereum Virtual Machine (EVM), adhering to the specification. Note the function names and input parameters are hashed during compilation. Therefore, for another account to call a function, it must first be given the ...


19

Solidity's call is a low-level interface for sending a message to a contract. It returns false if the subcall encounters an exception, otherwise it returns true. There is no notion of a legal call, if it compiles, it's valid Solidity. nameReg.call("register", "MyName") is a message that passes certain bytes to nameReg. For the bytes, ...


19

It is quite complicated to get this correct, but here's some information on how to work it out. I'm using a simple contract as an example: pragma solidity ^0.4.2; contract Test { uint256 public v1; string public v2; function Test(uint256 _v1, string _v2) { v1 = _v1; v2 = _v2; } } Here is my deployment to a --dev blockchain ...


17

Functions (aka Methods) are specified by the ABI, and have a Method ID, which is the first 4 bytes of the sha3 (Keccak-256) of the method's signature. Here's an example of invoking someFunction on contract: contract.call(bytes4(sha3("someFunction()"))) Here is a tested function with passing a methodId as a parameter: contract C1 { uint public _n; // ...


17

I know the question is old, but I had some problems with this so I will share what helped me. To import the ABI from a JSON file, you can use the following code (assuming you already have your web3 Object): var fs = require('fs'); var jsonFile = "pathToYourJSONFile/project.json"; var parsed= JSON.parse(fs.readFileSync(jsonFile)); var abi = parsed.abi; var ...


16

a) you know the source code of smart-contract programmatically: see the link to gist from Ivan's answer manually: with Remix b) you don't know the source code but smart-contract was verified in one of the explorers Then, use block explorer search to get ABI of smart-contract. For example, you can read the verified source code of smart-contracts in BlockScout,...


16

The page "Verify Contract" ask for "Constructor Arguments ABI-encoded". In your case the constructor has no parameters, so you can leave such field empty. In the general case when your constrctor has parameters you can proceed as follow: You can use ethereumjs-abi to encode/decode the parameters for transaction calls. Let's suppose your constructor is ...


15

Seems like the contract address should be enough. You're right, it would be better if the ABI could be obtained by just looking at the contract. So why do contract writers need to also provide the ABI, in addition to the contract address? One reason is that the function Method IDs in a contract are computed using a hash function. So it is infeasible for ...


14

think of "ABI" as an "API" at a low level. Think of it as the compiled version of an API (or as an API on the low level). As you know the contracts are stored as bytecodes in a binary format into the blockchain under a specific address. No one understands binary, so the ABI defines the structures and methods that you will use to interact with that binary ...


14

You can use simple online tool to get abi-encoded constructor parameters https://abi.hashex.org. Just paste in abi and enter parameter values. Abi-encoded parameters would be automatically calculated. Then just copy and paste result into etherscan.io constructor parameters input. Here is an example of using this tool


13

You don't need the source code, just the ABI. The ABI is produced when the code is compiled, but can be, and usually is, distributed separately. The ABI is essentially just a list of the function names in a contract and what types of arguments they take. You don't even need the whole ABI, just the parts that you want to interact with. For example, to ...


13

you can also do import { AbiItem } from 'web3-utils' import Abi from './abi.json' new web3.eth.Contract(Abi as AbiItem[], contractAddress)


12

when you run $ truffle compile truffle creates and saves a json file per contract in /yourProjectPath/build/contracts. In this json file you'll find the abi, the bytecode, the topics (if you have any event in your smart contract), the function definitions etc (you get the point I guess, I encourage you to spend some time understanding that file - just to ...


11

Here is how you get the abi by using your local solc compiler: solc filename.sol --abi You also need the bin code to deploy the contract, which you can get as follows: solc filename.sol --bin If you want a solc combined output (e.g. for parity), where bin is part of the abi json string, use this: solc filename.sol --combined-json abi,bin


11

It's the job of the compiler to produce the correct EVM bytecode so that as you say the MethodID "piece of data will end up jumping to the appropriate address for that function in the bytecode of the contract". Since the MethodID is only 4 bytes, hash collisions (between 2 different functions) are possible. The Solidity compiler gives an error in ...


11

Here are instructions to find contract ABI using online remix solidity compiler. In the "Compile" tab, click on the "Details" button. On the modal, go down to the "Interface" section. Click the copy button.


11

Most proxy contracts typically have a public variable defined as a: address public implementation; Which defines the address of the implementation contract. You could then call it as a view function in python, with something like: proxy_contract = web3.eth.contract(address=address, abi=abi_string) implementation_contract_address = proxy_contract.functions....


10

If the question is about abi params, you can take a look at these two web3.js files: https://github.com/ChainSafe/web3.js/blob/0.20.7/test/coder.encodeParam.js https://github.com/ChainSafe/web3.js/blob/0.20.7/test/coder.decodeParam.js Although these functions are not a part of public web3.js interface. Mostly because they need a proper refactor. There is ...


10

If the contract is verified in Etherscan, the following script fetches the ABI and saves it to a JSON file. Use it like $ python fetch_abi.py <contract address> -o <target JSON file> fetch_abi.py #!/usr/bin/python import argparse import requests import json # Exports contract ABI in JSON ABI_ENDPOINT = 'https://api.etherscan.io/api?module=...


10

I cannot find the contract details to get the json interface anymore. If you select the compile tab, there you will find a button called details. When the details is clicked, a pop-up window will appear. You can find the ABI at under the metadata section, and there output you will see the ABI (json interface). OR under the interface section Click green ...


10

calldata is where data from external calls to functions is stored. Functions can be called internally, e.g. from within the contract, or externally. When a function's visibility is external, only external contracts can call that function. When such an external call happens, the data of that call is stored in calldata. Read also here: `external` vs `public`...


10

A function selector is the first 4 bytes in the hash of the function's prototype. A function prototype is defined as the function's name and its argument types by order. It allows you, for example, to call a function without knowing its exact return-value type: bytes4 private constant FUNC_SELECTOR = bytes4(keccak256("someFunc(address,uint256)")); ...


Only top voted, non community-wiki answers of a minimum length are eligible