Contract documentation

Repository structure

All source code is located in the src folder. Files calls.rs and views.rs are about interacting with the contract. Other files contains of helpers and primitives.

Build

Run build.sh. It will put the compiled contract into the res folder named xyiming.wasm.

Tests

Run compile_and_test.sh. It will build the contract followed by running simulation tests from tests/general.rs.

Scripts

Some useful scripts are located in init directory. They are mostly related to the automatic creation of several streams for testing purposes.

Methods of the contract

Calls

The contract supports the following calls:

Views

The contract supports the following views:

Private calls

Calls related to fungible tokens processing are private. These ones cannot be called directly and must be executed only by ft_on_transfer fungible token callback.

API and integration

For integrating the contract into your project using the methods described above. The complete specification can be found below.

Create a stream

pub fn create_stream(
    &mut self,
    description: Option<String>,
    owner_id: ValidAccountId,
    receiver_id: ValidAccountId,
    token_name: String,
    tokens_per_tick: WrappedBalance,
    auto_deposit_enabled: bool,
) -> Base58CryptoHash;

Deposit

pub fn deposit(&mut self, stream_id: Base58CryptoHash);

Update an account

pub fn update_account(&mut self, account_id: ValidAccountId) -> Vec<Promise>;

Start a stream

pub fn start_stream(&mut self, stream_id: Base58CryptoHash);

Change the auto-deposit flag of a stream

pub fn change_auto_deposit(&mut self, stream_id: Base58CryptoHash, auto_deposit: bool);

Pause a stream

pub fn pause_stream(&mut self, stream_id: Base58CryptoHash);

Stop a stream

pub fn stop_stream(&mut self, stream_id: Base58CryptoHash);

Start a cron job

pub fn start_cron(&mut self) -> Promise;

Takes no args in the current version of the contract. Will be updated later.

Get account view

pub fn get_account(&self, account_id: ValidAccountId) -> Option<AccountView>;

Get stream view

pub fn get_stream(&self, stream_id: Base58CryptoHash) -> Option<StreamView>;

Get stream history

pub fn get_stream_history(
    &self,
    stream_id: Base58CryptoHash,
    from: u64,
    to: u64,
) -> Vec<ActionView>;

View structs

AccountView

pub struct AccountView {
    pub account_id: String,
    pub inputs: Vec<Base58CryptoHash>,
    pub outputs: Vec<Base58CryptoHash>,
    pub last_action: WrappedTimestamp,
    pub total_received: Vec<(String, WrappedBalance)>,
    pub total_incoming: Vec<(String, WrappedBalance)>,
    pub total_outgoing: Vec<(String, WrappedBalance)>,
    pub cron_calls_enabled: bool,
}

StreamView

pub struct StreamView {
    pub stream_id: Option<Base58CryptoHash>,
    pub description: Option<String>,
    pub owner_id: String,
    pub receiver_id: String,
    pub token_name: String,
    pub timestamp_created: WrappedTimestamp,
    pub balance: WrappedBalance,
    pub tokens_per_tick: WrappedBalance,
    pub auto_deposit_enabled: bool,
    pub status: String,
    pub tokens_total_withdrawn: WrappedBalance,
    pub available_to_withdraw: WrappedBalance,
    pub history_len: u64,
}

ActionView

pub struct ActionView {
    pub actor: String,
    pub action_type: String,
    pub amount: Option<WrappedBalance>,
    pub timestamp: WrappedTimestamp,
}

Stream states

Any stream may be in one of the following states: - initialized. Stream is created but not started yet. This state is reachable if creates a stream with zero deposit. - active. Stream is actively streaming tokens. Can be paused or stopped. - paused. Stream is paused and not streaming any tokens. Can be restarted or stopped completely. - interrupted. Stream is stopped by the owner. Cannot be restarted. - finished. Stream is stopped by the receiver, manually or naturally, by withdrawing all tokens from the stream. Cannot be restarted.

Stream history

Stream history is the complete list of operations that happened with any stream. Getting stream history is possible with view method get_stream_history, see above.

Actions

Several actions may be applied in one transaction due to following the invariant of keeping the valid state of the accounts.