【SERO TECH】手机轻钱包DApp接入教程

介绍

SERO Popup 钱包是一款去中心化的手机端钱包,分为IOS、Android、浏览器三个版本。可以管理SERO生态中丰富的资产形式,并且支持Pullup钱包的助记词导入。Popup 钱包将是今后SERO上各种去中心化应用的主要入口。

[](https://wiki.sero.cash/zh/ind...

[](https://wiki.sero.cash/zh/ind...

[](https://wiki.sero.cash/zh/ind.... 配置开发环境

[](https://wiki.sero.cash/zh/ind.... 开发智能合约

pragma solidity ^0.4.16;

contract HelloDapp {

    string public name;
    string public message;

    constructor() public{
        name = "Tom";
        message = "Hello";
    }

    function say(string _name,string _msg) public {
        name = _name;
        message = _msg;
    }

    function sayHello() public view returns (string){
        var a = stringAdd(name," say ");
        return stringAdd(a,message);
    }

    function stringAdd(string a, string b) returns(string){
        bytes memory _a = bytes(a);
        bytes memory _b = bytes(b);
        bytes memory res = new bytes(_a.length + _b.length);
        for(uint i = 0;i < _a.length;i++)
            res[i] = _a[i];
        for(uint j = 0;j < _b.length;j++)
            res[_a.length+j] = _b[j];  
        return string(res);
    }
}

[](https://wiki.sero.cash/zh/ind.... 新建DApp工程 (本教程使用react开发)

  • 安装开发组件
create-react-app hello-dapp
cd hello-dapp
npm install
npm install sero-pp serojs bignumber.js sero-web3
npm run start
  • 修改App.js,导入组件
import serojs from 'serojs'
import seropp from 'sero-pp'
import Web3 from 'sero-web3'
import BigNumber from 'bignumber.js'
  • 初始化DApp信息,

contractAddress是智能合约地址

const dapp = {
            name: "Hello DApp",
            contractAddress: contractAddress,
            github: "https://github.com/sero-cash/sero-pp/example",
            author: "tom",
            url: "http://127.0.0.1:3000",
            logo: "http://127.0.0.1:3000/logo192.png",
        }

seropp.init(dapp,function (rest) {
    console.log("init result >>> " , rest);
})

[](https://wiki.sero.cash/zh/ind.... 调试

  • 在Popup钱包中,打开“应用” ,输入DApp的地址

image.png

  • 核心方法
// 调用智能合约的方法(不发送交易)
callMethod(_method, _args, callback) {
        let that = this;
        // let param = [ABI, contractAddress, _method, args]

        let packData = contract.packData(_method, _args);
        let callParams = {
            from: that.state.accounts[0].MainPKr,
            to: contractAddress,
            data: packData
        }

        seropp.call(callParams,function (callData) {
            if(callData !== "0x"){
                let res = contract.unPackData(_method, callData);
                if (callback){
                    callback(res);
                }
            }else{
                callback("0x0");
            }
        });
    }

// 执行智能合约的方法(发送交易)
executeMethod(_method, args, value, cy, callback) {
        let that = this;

        let packData = contract.packData(_method, args);
        let executeData = {
            from: that.state.accounts[0].PK,
            to: contractAddress,
            value: "0x"+value,//SERO
            data: packData,
            gasPrice: "0x"+new BigNumber("1000000000").toString(16),
            cy:cy,
        };
        let estimateParam = {
            from: that.state.accounts[0].MainPKr,
            to: contractAddress,
            value: "0x"+value,//SERO
            data: packData,
            gasPrice: "0x"+new BigNumber("1000000000").toString(16),
            cy:cy,
        }
        seropp.estimateGas(estimateParam,function (gas) {
            executeData["gas"] = gas;
            seropp.executeContract(executeData,function (res) {
                if (callback) {
                    callback(res)
                }
            })
        });
    }
  • 加载账户
getAccountList = ()=>{
        let that = this;
        seropp.getAccountList(function (rest) {
            that.setState({
                accounts:rest
            })
        })
    }
  • 查询智能合约
load = () => {
        let that = this;
        this.callMethod("name","",function (rest) {
            that.setState({
                name:rest
            })
        })

        this.callMethod("msg","",function (rest) {
            that.setState({
                msg:rest
            })
        })

        this.callMethod("sayHello","",function (rest) {
            that.setState({
                say:rest
            })
        })

        let balance = web3.sero.getBalance(contractAddress, "latest");
        console.log("balance:: ",balance);
    }
  • 执行智能合约
execute = () => {
        let name = document.getElementById("name").value;
        let msg = document.getElementById("msg").value;
        this.executeMethod("say",[name,msg],0,"SERO",function (res) {
            alert("transaction submit successful. hash :"+res);
        })
    }

[](https://wiki.sero.cash/zh/ind...://github.com/sero-cash/sero-pp/tree/master/example/hello-dapp)5. DEMO下载https://github.com/sero-cash/sero-pp/tree/master/example/hello-dapp

  • Hello Dapp页面

image.png

  • 加载账户

image.png

  • 查询和执行智能合约

image.png

  • 确认交易信息(SERO Popup的组件,无需单独开发)

image.png

  • 弹出密码输入框(SERO Popup的组件,无需单独开发)

image.png

[](https://wiki.sero.cash/zh/ind...


开发者帮助文档:SERO remixIDE /SERO WiKi/SERO Github

http://remix.web.sero.cash/

https://wiki.sero.cash/zh/index.html?file=home-Home

https://github.com/sero-cash/

评论

暂无评论