Skip to content

dantenetwork/ink-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ink! SDK

web3 foundation_grants_badge_white

Ink! SDK makes it easy for ink developers in the Polkadot Ecosystem to use Dante Network to interact with other chains, such as Near, Ethereum, Avalanche, Flow.

Click Use this template to start your multi-ecosystem dApp.

  • Create your project in ./project. You can find more details there.
  • The examples are here

Or you can use this SDK as a library by adding

ink_sdk = { path = "<local path of ink_sdk>/contracts/", default-features = false, features = ["ink-as-dependency"] }

into Cargo.toml of your project.

Library

The library that is contained in contracts is used to develop Ink! application contracts. The library provides two functional modules, practical traits and cross-chain interacting module.

Practical Traits

This trait can be used when a contract needs to communicate with more than one other chain.

This trait can be used when a contract has custom SQoS demands.

The cross-chain interaction module is contained in the file cross_chain_helper.rs, which mainly provides functions to make cross-contract calls to cross-chain contract, as well as to make cross-chain interaction with other chains.

CrossChainBase is a trait, which must be implemented by the contract struct to use the Ink! SDK.
You can use the default implemantation of CrossChainBase::get_cross_chain_contract_address like this:

impl cross_chain_helper::CrossChainBase for Flip {
}

Or you can rewrite the method if you want to use another cross-chain contract address.

The function cross_chain_send_message sends a cross-chain message, and returns the message id recorded in the cross-chain contract.

Example is shown below, or you can refer it in the example greeting.

#[ink(message)]
pub fn send_greeting(&mut self, chain_name: String, greeting: Vec<String>) -> Result<(), Error> {
    ...
    let message = IRequestMessage::new(chain_name, sqos, content);

    cross_chain_helper::cross_chain_send_message(self, message);

    Ok(())
}

The function cross_chain_call sends a cross-chain message, and returns the message id recorded in the cross-chain contract. Later a callback in the application contract will be called.

Example is shown below, or you can refer it in the example osComputing.

#[ink(message)]
pub fn send_computing_task(&mut self, chain_name: String, nums: Vec<u32>) -> Result<(), Error> {
    ...
    let message = IRequestMessage::new(chain_name, sqos, content);

    cross_chain_helper::cross_chain_call(self, message, 2_u32.to_be_bytes());

    Ok(())
}

The function cross_chain_respond responds a cross-chain request, and returns the message id recorded inthe cross-chain contract.

Example is shown below, or you can refer it in the example osComputing.

#[ink(message)]
pub fn receive_computing_task(&mut self, payload: MessagePayload) -> String {
    ...
    let message = IResponseMessage::new(sqos, content);
    cross_chain_helper::cross_chain_respond(self, message);

    String::try_from("Ok").unwrap()
}

The function set_sqos is used to set the type of SQoS when the contract receive cross-chain messages from other chains.

Example is shown below, or you can refer it in the example greeting.

#[ink(message)]
fn set_sqos(&mut self, sqos_item: ISQoS) {
    ...

    let account_id = Self::env().account_id();
    cross_chain_helper::set_sqos(self, sqos_item, account_id);
}

The function get_sqos is used to view the contract's SQoS type.

Example is shown below, or you can refer it in the example greeting.

#[ink(message)]
#[ink(message)]
fn get_sqos(&self) -> Option<ISQoS> {
    let account_id = Self::env().account_id();
    cross_chain_helper::get_sqos(self, account_id)
}

Other information

The meaning of session_type in Session: *1: send out without callback;

  • 2: call out with callback;
  • 3: callback message;
  • 104: local error message;
  • 105: remote error message.

There are two examples in the repo, one is greeting, the other is osComputing.

The example shows how to send greeting messages to, and receive greeting messages from other chains with the Ink! SDK.

The example shows a scenario in which sb. want to send a outsource computing task to another chain, and receive the result.

Usage

Use Examples

You can use either of the examples as a template, it is the recommended way.

  • Copy the example.
  • Change the package name and lib name in Cargo.toml.
  • Write your code.

New Project

You can use the library in a totally new ink! project.

  • Create a new ink! project, you can refer it here https://docs.substrate.io/tutorials/v3/ink-workshop/pt1/.
  • Change dependencies
    ink = { version = "4.0.0-alpha.3", default-features = false }
    
    scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
    scale-info = { version = "2", default-features = false, features = ["derive", "serde", "decode"] }
    
    payload = { path = "../../../message-ink/payload/", default-features = false, features = ["ink-as-dependency"] }
    ink_sdk = { path = "../../contracts/", default-features = false, features = ["ink-as-dependency"] }
  • Use modules in lib.rs, use ink_sdk::{cross_chain_helper}, and other modules if you need.
  • Implement the trait cross_chain_helper::CrossChainBase, the method get_cross_chain_contract_address has default implementation.
  • Write your code.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages