Home

Awesome

BTCRelay-Solidity Tools

Tools for interacting with the BTC Relay in Solidity

This contract provides an interface with BTC Relay which accepts and returns intuitive Solidity types, and provides additional functionality like fetching block headers by block height and caching results in a manner that is fair to the original relayers.

Contract Address:

ABI:

API


Transaction Verification

Functions for validating transactions. Insert example here

verifyTx(rawTransaction, transactionIndex, merkleSibling, blockHash)

Verifies the presence of a transaction on the Bitcoin blockchain, primarily that the transaction is on Bitcoin's main chain and has at least 6 confirmations.

Arguments:

Returns: uint256


relayTx(rawTransaction, transactionIndex, merkleSibling, blockHash, contractAddress)

Verifies a Bitcoin transaction per verifyTx() and relays the verified transaction to the specified Ethereum contract.

Arguments:

The processor contract at contractAddress should have a function of signature processTransaction(bytes rawTransaction, uint256 transactionHash) returns (int256) and is what will be invoked by relayTx if the transaction passes verification. For examples, see BitcoinProcessor.sol and testnetSampleRelayTx.html.

Returns: int256

Note: Callers cannot be 100% certain when an relay error occurs because -1 may also have been returned by processTransaction(). Callers should be aware of the contract that they are relaying transactions to, and understand what the processor contract's processTransaction method returns.


Block Lookup

Functions for finding particular blocks

getBlockHash(blockHeight)

Get the block hash for a given blockHeight.

Arguments:

Returns: (bytes32, uint256)

NOTE: Due to the mechanics of BTC Relay, in order to fetch a block hash we must iterate over all of the successive blocks in the chain, paying the required fee for each. This is only true the first time a given block header is fetched, so before calling this function make sure to call getFeeAmount() and check if you are willing to pay the current price.


getBlockHeight(blockHash)

Get the block height of a block by its hash

Arguments:

Returns: uint


Block Header Fields

You must pay a small fee for these, i.e.

uint fee = relay.getFeeAmount(blockHash);
bytes32 merkleRoot = relay.getMerkleRoot.value(fee)(blockHash);

getBlockVersion(blockHash)

Get the Bitcoin block version bytes

Arguments:

Returns: bytes4


getParentHash(blockHash)

Get the hash of the block immediately previous

Arguments:

Returns: bytes32


getMerkleRoot(blockHash)

Get the Merkle root for the block

Arguments:

Returns: bytes32


getTimestamp(blockHash)

Get the timestamp of the block, as a bytes4

Arguments:

Returns: bytes4


getBits(blockHash)

Get the abbreviated difficulty

Arguments:

Returns: bytes4


getNonce(blockHash)

Get the nonce of the block

Arguments:

Returns: bytes4


Chain Info

These functions are free and provide general information about the state of the blockchain

getLastBlockHeight()

Returns: uint256


getBlockchainHead()

Returns: bytes32


getAverageChainWork()

Returns: uint

This is provided in case an Ethereum contract wants to use the chainWork or Bitcoin network difficulty (which can be derived) as a data feed.


Fee Estimators

These estimates are guaranteed to be enough for execution, although your contract may receive a refund to a function called _relay_refund(), or your fallback function if that does not exist.

getFeeAmount(blockHash|blockHeight)

uint is interpreted as a block height, bytes32 is interpreted as a block hash. For less ambiguity, you may use the more verbose functions

Arguments:

Returns: uint


getFeeAmountByBlockHash(blockHeight)

Returns: uint


getMaxFeeAmountByBlockHeight(blockHeight)

Returns: uint