Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Contract creation with BlockCypher
# The contract API includes the following endpoints (API token always required):
#
# POST /v1/eth/main/contracts
# Creates a new contract. Expects the solidity definition, the private key paying for the publication, the name of the
# contract to be published, the gas amount to use and eventual constructor parameters. The solidity code is compiled
# and the contract creation transaction built and broadcasted. Both the code and the ABI are saved for further
# retrieval. If invoked without contracts to publish, simply compiles and returns the result of compilation.
# Otherwise, along with the compilation results, will return the contract address and creation transaction hash and the
# contract methods will be exposed as endpoints (see below).
#
# GET /v1/eth/main/contracts/{address}
# Returns information about a deployed contract. If the contract was deployed using our API, will return the code and
# ABI as well. Note that the /addrs endpoint also provides regular transactions information.
#
# GET /v1/eth/main/contracts/{address}/{method}
# Invokes the provided contract methods. Accepts method parameters. If the method is declared "constant", it will be
# run right away and the result will be returned. Otherwise, a call transaction is created and broadcasted for
# inclusion in a block.
# Example: using the simple greeter contract often presented as the Ethereum "hello world"
$ cat contract.json
{
"solidity": "contract mortal {\n /* Define variable owner of the type address*/\n address owner;\n /* this function is executed at initialization and sets the owner of the contract */\n function mortal() { owner = msg.sender; }\n /* Function to recover the funds on the contract */\n function kill() { if (msg.sender == owner) suicide(owner); }\n}\n\ncontract greeter is mortal {\n /* define variable greeting of the type string */\n string greeting;\n /* this runs when the contract is executed */\n function greeter(string _greeting) public {\n greeting = _greeting;\n }\n /* main function */\n function greet() constant returns (string) {\n return greeting;\n }\n}",
"params": ["Hello BlockCypher"],
"publish": ["greeter"],
"private": "3ca4...",
"gas_amount": 500000
}
$ curl -d @contract.json https://api.blockcypher.com/v1/eth/main/contracts?token=$TOKEN
[
{
"name": "greeter",
"solidity": "...",
"bin": "...",
"abi": "...",
"creation_tx_hash": "2ee3141cd8f0f65c91150b345ecf77465327741f7028a26e78ba3e8f40cec26b",
"address": "ab9a9ba6b9b0d58d57378c711d7cea540d97d2c9",
"params": ["Hello BlockCypher"]
},
{
"name": "mortal",
...
}
}
$ cat call.json
{
"private": "3ca4...",
"gas_amount": 200000
}
# Calling the greet method is done without involving the network as it's a "constant" method in the ABI
$ curl -d @call.json https://api.blockcypher.com/v1/eth/main/contracts/ab9a9ba6b9b0d58d57378c711d7cea540d97d2c9/greet?token=$TOKEN
{
"gas_amount": 200000,
"address": "ab9a9ba6b9b0d58d57378c711d7cea540d97d2c9",
"results": ["Hello BlockCypher"]
}
# Kill produces a transaction and will cause the contract suicide, creating an internal transaction
# to reimburse the private key that created the contract
$ curl -d @call.json https://api.blockcypher.com/v1/eth/main/contracts/ab9a9ba6b9b0d58d57378c711d7cea540d97d2c9/kill?token=$TOKEN
{
"gas_amount": 200000,
"call_tx_hash": "0ef3e0d0de733461b351e27f9fba054ae0386d24959580dcb2a6e8df17e4e241",
"address": "ab9a9ba6b9b0d58d57378c711d7cea540d97d2c9"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.