# StateChannel

# Overview

The contact for Pay-as-you-go service for Indexer and Consumer. The consumer is not only a account, but also a contract

# ChannelStatus

enum ChannelStatus {
  Finalized,
  Open,
  Terminating
}

# ChannelState

struct ChannelState {
  enum StateChannel.ChannelStatus status;
  address indexer;
  address consumer;
  uint256 total;
  uint256 spent;
  uint256 expiredAt;
  uint256 terminatedAt;
  bytes32 deploymentId;
  bool terminateByIndexer;
}

# QueryState

struct QueryState {
  uint256 channelId;
  uint256 spent;
  bool isFinal;
  bytes indexerSign;
  bytes consumerSign;
}

# STATES

# settings

Settings info

contract ISettings settings

# terminateExpiration

The expiration of the terminate. Default is 24 * 60 * 60 = 86400s

uint256 terminateExpiration

# channels

The states of the channels

mapping(uint256 => struct StateChannel.ChannelState) channels

# channelPrice

The price of the channel

mapping(uint256 => uint256) channelPrice

# EVENTS

# ChannelOpen

Emitted when open a channel for Pay-as-you-go service

event ChannelOpen(uint256 channelId, address indexer, address consumer, uint256 total, uint256 price, uint256 expiredAt, bytes32 deploymentId, bytes callback)

# ChannelExtend

Emitted when extend the channel

event ChannelExtend(uint256 channelId, uint256 expiredAt)

# ChannelFund

Emitted when deposit more amount to the channel

event ChannelFund(uint256 channelId, uint256 total)

# ChannelCheckpoint

Emitted when indexer send a checkpoint to claim the part-amount

event ChannelCheckpoint(uint256 channelId, uint256 spent)

# ChannelTerminate

Emitted when consumer start a terminate on channel to finalize in advance

event ChannelTerminate(uint256 channelId, uint256 spent, uint256 terminatedAt, bool terminateByIndexer)

# ChannelFinalize

Emitted when finalize the channel

event ChannelFinalize(uint256 channelId, uint256 total, uint256 remain)

# ChannelLabor

Emitted when Settle the channel with new state

event ChannelLabor(bytes32 deploymentId, address indexer, uint256 amount)

# FUNCTIONS

# initialize

Initialize the contract, setup the terminateExpiration

function initialize(contract ISettings _settings) external
Name Type Description
_settings contract ISettings settings contract address

# setSettings

Update setting state.

function setSettings(contract ISettings _settings) external
Name Type Description
_settings contract ISettings ISettings contract

# setTerminateExpiration

Update the expiration of the terminate

function setTerminateExpiration(uint256 expiration) external
Name Type Description
expiration uint256 terminate expiration time in seconds

# channel

Get the channel info

function channel(uint256 channelId) external view returns (struct StateChannel.ChannelState)
Name Type Description
channelId uint256 channel id

Return: struct StateChannel.ChannelState -> ChannelState channel info

# open

Indexer and Consumer open a channel for Pay-as-you-go service. It will lock the amount of consumer and start a new channel. Need consumer approve amount first. If consumer is contract, use callback to call paid

function open(uint256 channelId, address indexer, address consumer, uint256 amount, uint256 price, uint256 expiration, bytes32 deploymentId, bytes callback, bytes indexerSign, bytes consumerSign) external
Name Type Description
channelId uint256 channel id
indexer address indexer address
consumer address consumer address
amount uint256 SQT amount deposit in channel
price uint256
expiration uint256 channel expiration time in seconds
deploymentId bytes32 deployment id
callback bytes callback info for contract, if consumer not a contract, set null: "0x"
indexerSign bytes indexer's signature
consumerSign bytes consumer's signature

# extend

Extend the channel expiredAt

function extend(uint256 channelId, uint256 preExpirationAt, uint256 expiration, bytes indexerSign, bytes consumerSign) external
Name Type Description
channelId uint256 channel id
preExpirationAt uint256 previous ExpirationAt timestamp
expiration uint256 Extend tiem in seconds
indexerSign bytes indexer's signature
consumerSign bytes consumer's signature

# fund

Deposit more amount to this channel. need consumer approve amount first

function fund(uint256 channelId, uint256 preTotal, uint256 amount, bytes callback, bytes sign) external
Name Type Description
channelId uint256 channel id
preTotal uint256 previous toal amount
amount uint256 SQT amount to deposit
callback bytes callback info for contract
sign bytes the signature of the consumer

# checkpoint

Indexer can send a checkpoint to claim the part-amount. This amount will send to RewardDistributer for staking

function checkpoint(struct StateChannel.QueryState query) external
Name Type Description
query struct StateChannel.QueryState the state of the channel

# terminate

When indexer/consumer what to finalize in advance, can start a terminate. If terminate success, consumer will claim the rest of the locked amount. Indexer can respond to this terminate within the time limit

function terminate(struct StateChannel.QueryState query) external
Name Type Description
query struct StateChannel.QueryState the state of the channel

# respond

Indexer respond the terminate by send the service proof after the terminate

function respond(struct StateChannel.QueryState query) external
Name Type Description
query struct StateChannel.QueryState the state of the channel

# claim

When terminate success (Overdue did not respond) or expiration, consumer can claim the amount

function claim(uint256 channelId) external
Name Type Description
channelId uint256 channel id

PRIVATE FUNCTIONS

# _checkStateSign

Check the signature of the hash with channel info

function _checkStateSign(uint256 channelId, bytes32 payload, bytes indexerSign, bytes consumerSign) private view

# _checkSign

Check the signature of the hash with given addresses

function _checkSign(bytes32 payload, bytes sign, address checkSigner, bool isIndexer) private view

# _settlement

Settlement the new state

function _settlement(struct StateChannel.QueryState query, bool finalize) private

# _finalize

Finalize the channel

function _finalize(uint256 channelId) private

# _isContract

Determine the input address is contract or not

function _isContract(address _addr) private view returns (bool)