API
General
new Subspace(web3 [, options])
Constructor.
Parameters
web3
-Object
: aweb3.js
object.options
-Object
(optional): Options used to initialize SubspacedbFilename
-String
(optional): Name of the database where the information will be stored (default'subspace.db'
)callInterval
- Interval of time in milliseconds to query a contract/address to determine changes in state or balance. It’s only used with HttpProviders (default:undefined
. Obtains data every block using the average block time as an interval).refreshLastNBlocks
- Ignores last N blocks (from current block), stored in the local db and refresh them via a web3 subscription. Useful for possible reorgs (default: 12),disableSubscriptions
- Subspace by default will attempt to use websocket subscriptions if the current provider supports them, otherwise it will use polling because it asumes the provider is an HttpProvider. This functionality can be disabled by passing true to this option. (default: undefined)
init()
Initializes Subspace
ReturnsPromise
that once it’s resolved, will mean that Subspace is available to use
close()
Dispose and perform the cleanup necessary to remove the internal subscriptions and interval timers created by Subspace during its normal execution.
contract(instance|{abi,address})
Adds a track
method to the web3 contract objects. You can obtain this functionality by passing a web3.eth.Contract
instance, or the abi
and address
of your contract
Returnsweb3.eth.Contract
object enhanced with .track()
functions for methods and events.
Contract methods
myContract.events.MyEvent.track([options])
Track a contract event.
Parameters
options
-Object
(optional): web3 filter options object to limit the number of events based on a block number range, or indexed filtersfilter
-Object
(optional): Lets you filter events by indexed parameters, e.g.{filter: {myNumber: [12,13]}}
means all events where"myNumber"
is12
or13
.fromBlock
-Number
(optional): The block number from which to get events on.toBlock
-Number
(optional): The block number to get events up to (Defaults to"latest"
)topics
-Array
(optional): This allows you to manually set the topics for the event filter. If given the filter property and event signature, (topic[0]
) will not be set automatically.saveToDb
-Boolean
(optional): Store events into a local database for faster data retrievals. (default:true
)
ReturnsRxJS Observable
which will stream the event returnValues
.
myContract.methods.myMethod([param1[, ...]]).track([callOptions])
Track a constant function / contract state variable on each block mined, or depending on the callInterval
option used during Subspace initialization.
Parameters
callOptions
-Object
(optional): The options used for calling.from
-String
(optional): The address the call “transaction” should be made from.gasPrice
-String
(optional): The gas price in wei to use for this call “transaction”.gas
-Number
(optional): The maximum gas provided for this call “transaction” (gas limit).
ReturnsRxJS Observable
which will stream the function / variable values. Data type will depend on the contract function invoked.
myContract.trackBalance(address [, tokenAddress])
Track a contract’s balance changes for an address on each block mined, or depending on the callInterval
option used during Subspace initialization.
Parameters
address
-String
: The address to get the balance of.tokenAddress
-String
(optional): If you want to track the balance for an ERC20 contract, here you can specify the token address. Otherwise, Only ETH balances will be returned.
ReturnsRxJS Observable
which will stream a string containing the address balance.
Blocks, gas price and block time
trackBlock()
Receive the block information for any new block. It’s the reactive equivalent to web3.eth.getBlock("latest")
.
ReturnsRxJS Observable
which will stream a block object for the latest block received
trackBlockNumber()
Returns the latest block number. It’s the reactive equivalent to web3.eth.getBlockNumber
.
ReturnsRxJS Observable
with the latest block number
trackGasPrice()
Returns the current gas price oracle. It’s the reactive equivalent to web3.eth.getGasPrice
.
ReturnsRxJS Observable
with the average gas price in wei.
trackAverageBlocktime()
Average block time of the last 10 blocks.
ReturnsRxJS Observable
with the moving average block time of the last 10 blocks. The time is returned in milliseconds:
Low level API for data tracking
These are used in case you don’t want to decorate your web3 contract objects, or if you want to track the balance for an specific address.
trackEvent(contractObject, eventName [, options])
Track a contract event.
Parameters
contractObject
-web3.eth.Contract
: An already initialized contract object pointing to an address and containing a valid ABI.eventName
-String
: The name of the event to subscribe.options
-Object
(optional): web3 filter options object to limit the number of events based on a block number range, or indexed filtersfilter
-Object
(optional): Lets you filter events by indexed parameters, e.g.{filter: {myNumber: [12,13]}}
means all events where"myNumber"
is12
or13
.fromBlock
-Number
(optional): The block number from which to get events on.toBlock
-Number
(optional): The block number to get events up to (Defaults to"latest"
)topics
-Array
(optional): This allows you to manually set the topics for the event filter. If given the filter property and event signature, (topic[0]
) will not be set automatically.\saveToDb
-Boolean
(optional): Store events into a local database for faster data retrievals. (default:true
)
ReturnsRxJS Observable
which will stream the event returnValues
.
trackProperty(contractObject, functionName [, functionArgs] [, callOptions])
Track a constant function / contract state variable on each block mined, or depending on the callInterval
option used during Subspace initialization.
Parameters
contractObject
-web3.eth.Contract
: An already initialized contract object pointing to an address and containing a valid ABI.functionName
-String
: Name of the function or variable whose values will be tracked.functionArgs
-Array
(optional): Array of arguments that the tracked function receivescallOptions
-Object
(optional): The options used for calling.from
-String
(optional): The address the call “transaction” should be made from.gasPrice
-String
(optional): The gas price in wei to use for this call “transaction”.gas
-Number
(optional): The maximum gas provided for this call “transaction” (gas limit).
ReturnsRxJS Observable
which will stream the function / variable values. Data type will depend on the contract function invoked.
trackBalance(address [, tokenAddress])
Track balance changes for an address on each block mined, or depending on the callInterval
option used during Subspace initialization.
Parameters
address
-String
: The address to get the balance of.tokenAddress
-String
(optional): If you want to track the balance for an ERC20 contract, here you can specify the token address. Otherwise, Only ETH balances will be returned.
ReturnsRxJS Observable
which will stream a string containing the address balance.
trackLogs(options [, abi])
Tracks incoming logs, filtered by the given options.
Parameters
options
-Object
(optional): web3 filter options object to limit the number of logsaddress
-String|Array
(optional): An address or a list of addresses to only get logs from particular account(s).fromBlock
-Number
(optional): The block number from which to get events on.topics
-Array
(optional): An array of values which must each appear in the log entries. The order is important, if you want to leave topics out use null, e.g. [null, ‘0x00…’]. You can also pass another array for each topic with options for that topic e.g. [null, [‘option1’, ‘option2’]].
abi
-Array
(optional): Array containing the ABI for the inputs of the logs received. It will automatically decode the logs using this ABI instead of returning the hexadecimal data.
ReturnsRxJS Observable
which will stream the logs. If the inputs ABI is included in the call, the logs will be automatically decoded.