SubscribeLogs creates a subscription that will write all logs matching the given criteria to the given logs channel. Default value for the from and to block is "latest". If the fromBlock > toBlock an error is returned.
(crit cpchain.FilterQuery, logs chan []*types.Log)
| 196 | // given criteria to the given logs channel. Default value for the from and to |
| 197 | // block is "latest". If the fromBlock > toBlock an error is returned. |
| 198 | func (es *EventSystem) SubscribeLogs(crit cpchain.FilterQuery, logs chan []*types.Log) (*Subscription, error) { |
| 199 | var from, to rpc.BlockNumber |
| 200 | if crit.FromBlock == nil { |
| 201 | from = rpc.LatestBlockNumber |
| 202 | } else { |
| 203 | from = rpc.BlockNumber(crit.FromBlock.Int64()) |
| 204 | } |
| 205 | if crit.ToBlock == nil { |
| 206 | to = rpc.LatestBlockNumber |
| 207 | } else { |
| 208 | to = rpc.BlockNumber(crit.ToBlock.Int64()) |
| 209 | } |
| 210 | |
| 211 | // only interested in pending logs |
| 212 | if from == rpc.PendingBlockNumber && to == rpc.PendingBlockNumber { |
| 213 | return es.subscribePendingLogs(crit, logs), nil |
| 214 | } |
| 215 | // only interested in new mined logs |
| 216 | if from == rpc.LatestBlockNumber && to == rpc.LatestBlockNumber { |
| 217 | return es.subscribeLogs(crit, logs), nil |
| 218 | } |
| 219 | // only interested in mined logs within a specific block range |
| 220 | if from >= 0 && to >= 0 && to >= from { |
| 221 | return es.subscribeLogs(crit, logs), nil |
| 222 | } |
| 223 | // interested in mined logs from a specific block number, new logs and pending logs |
| 224 | if from >= rpc.LatestBlockNumber && to == rpc.PendingBlockNumber { |
| 225 | return es.subscribeMinedPendingLogs(crit, logs), nil |
| 226 | } |
| 227 | // interested in logs from a specific block number to new mined blocks |
| 228 | if from >= 0 && to == rpc.LatestBlockNumber { |
| 229 | return es.subscribeLogs(crit, logs), nil |
| 230 | } |
| 231 | return nil, fmt.Errorf("invalid from and to block combination: from > to") |
| 232 | } |
| 233 | |
| 234 | // subscribeMinedPendingLogs creates a subscription that returned mined and |
| 235 | // pending logs that match the given criteria. |