1.0.7 • Published 6 years ago

dappsdk v1.0.7

Weekly downloads
8
License
ISC
Repository
-
Last release
6 years ago

STARTEOS DAPP SDK hand book


installation

npm install dappsdk

ES-Module:

import dappSDK from 'dappsdk'

dappSDK.gui.showToast({message: 'Hello World!', delay: 1000})

Use SDK as normal script resource, use node_modules/dappsdk/dist/index.js:

<script type="text/javascript" src="path-to-the-sdk"></script>
window.dappSDK.gui.showToast({message: 'Hello World!', delay: 1000})

Whatever the way you import the SDK is, the window.dappSDK exists.


callback and Promises

First of all, the SDK itself does not contain any Promise library, if you need it, ensure that the window.Promise exists.

All the API could receive a callback function as optional, if callback does not exist, call the API will renturn an instance of Promise:

window.dappSDK.system.getSdkInfo({})
    .then(({version}) => alert(version));

window.dappSDK.system.getSdkInfo({}, function(err, data) {
    if(err) {
        return alert('Error: ' + err.message)
    }
    var version = data.version;
    alert(version)
})

error handling

When using the API with callback style, keep it in mind that the callback function MUST receive two parameters, first of which is an instance of Error , and the second is the result of the call, if err is truthy, the result may be undefined, so you should use error-first-callback to handle with the call:

window.dappSDK.system.getSdkInfo({}, function(err, data) {
    if(err) {
        var code = error.code;
        console.log(code);
        // if error exists, `return` makes the rest of logic not be executed.
        return alert('Error: ' + err.message)
    }
    var version = data.version;
    alert(version)
})

Error

No matter whiche style you use for callback, the error will contain 3 variables, error.message, error.code, error.extra. message is the short information about the error, code is the mapping of message in our definition, extra is the extra information of the error, and it's more detailed than the message, in most case, extra is undefined.

errors and codes

codeerror
-10001invalid namespace, mostly because the app and SDK's version not match
-10002invalid function, mostly because the app and SDK's version not match
-10003parameters error
-10004user canceled the operation
-10005 and lessother type of error, will be listed in the rest of the document

API


exec


function exec(namespace, fnName, params, [callback])

parameters:

nametyperemark
namespaceStringThe namespace of the function to be executed.
fnNameStringFunction name.
paramsObjectParameters to be excuted with.
callbackFunctionOptional parameter, if it exists, the function will be excuted when the native side responses.

e.g.:

window.dappSDK.exec('system', 'getSdkInfo', {}, function(data) {
    alert(data.version) //version
})

SYSTEM

namespace: system


get Native SDK information

function: getSdkInfo

params: null

output:

keyvalueremark
versionStringversion of the native SDK

i.e.:

dappSDK.system.getSdkInfo({}, function(err, data) {
    if(err)
        return console.error(err);
    console.log(data)
}); 

dappSDK.system.getSdkInfo({})
    .then(data => console.log(data))
    .catch(err => console.error(err))

get current language setting

function: getLanguageSetting

params: null

output:

keyvalueremark
languageSettingStringsetting(Chinese,English)

get current currency type

function: getSymbol

params: null

output:

keyvalueremark
symbolStringcurrency type(USD,CNY)

GUI

namespace: gui


full screen switch

function: changeFullScreen

params:

keyvalueremark
fullScreenBooleantrue means set the current full screen state on

output: null


change status bar

function: changeStatusBar

params:

keyvalueremark
titleStringtitle
colorStringcolor(#FFFFFF)
themeString(dark,light)
canGoBackBooleanis able to go back(and display the go back button)

output: null


Toast

function: showToast

params:

keyvalueremark
messageStringcontent
delayIntegerthe time toast stays

output: null


Progress

function: showProgress

params:

keyvalueremark
messageStringcontent
delayNumberHow long will the message display

output: null


function: hideProgress

params: null

output: null


Alert

function: showAlert

params:

keyvalueremark
titleStringtitle
messageStringcontent
btnStringStringtext inside the button

output:

keyvalueremark
clickedIntegerwhen user press the button, this will be received bythe callback

Dialog

function: showDialog

params:

keyvalueremark
titleStringtitle
messageStringcontent
leftBtnStringStringleft btn text
rightBtnStringStringright btn text

output:

keyvalueremark
clickedIntegerwhen user press the button, this value will be received by the callback, 0-left, 1-right.

set to clipboard

function: setClipboard

params:

keyvalueremark
dataStringcontent to be set

output: null


scan QR code

function: scanQRCode

params: null

output:

keyvalueremark
resultStringresult of a qr code

CUSTOMER

namespace: customer


get current wallet type

function: getCurrentWalletType

params: null

output:

keyvalueremark
walletTypeStringwallet type(EOS,ETH)

error:

coderemark
-10005no wallet found

Fingerprint

function: checkFingerprintPayment

params: null

output:

keyvalueremark
statusNumberstatus(-1: does not support due to the hardware, 0: disabled,1: enabled)

EOS

namespace: eos


get current wallet account

function: getCurrentWalletAccount

params: null

output:

keyvalueremark
accountStringaccount
addressStringpublic key

error:

coderemark
-10005no wallet found

get current account's balance

function: getCurrentBalance

params:

keyvalueremark
tokenNameStringtoken name
contractStringcontract

output:

keyvalueremark
accountStringaccount
tokenNameStringtoken name
contractStringcontract
balanceNumberbalance

error:

coderemark
-10005no wallet found

get current account's information

function: getCurrentAccountInfo

params: null

output: the information in the chain

error:

coderemark
-10005no wallet found

get accounts

function: getWalletAccounts

params: null

output:

keyvalueremark
accountsArrayaccounts

item:

keyvalueremark
accountStringaccount
addressStringpublic key

error:

coderemark
-10005no wallet found

get balance

function: getBalance

params:

keyvalueremark
accountStringaccount
contractStringcontract

output:

keyvalueremark
accountStringaccount
contractStringcontract
balanceNumberbalance
symbolStringsymbol

error:

coderemark
-10006search fail(network error)

error:

coderemark
-10006network error
-10007transaction error
-10008target wallet not found

get account's information

function: getAccountInfo

params:

keyvalueremark
accountStringaccount

output: the information in the chain


transfer

function: transfer

params:

keyvalueremark
fromStringthe account transfer from
fromAddressStringthe pub key transfer from
toStringthe account transfer to
amountNumberamount
symbolStringsymbol
contractStringcontract
memoStringmemo
hintStringhint for user

output:

keyvalueremark
transactionIdStringstring that stands a transaction

get transaction record

function: getTransactionRecord

params:

keyvalueremark
accountStringaccount
tokenNameStringtoken
contractStringcontract
\ \ pagination undetermined

output: the information in the chain


ETH

namespace: eth


transaction

undetermined

STARTEOS DAPP SDK使用文档


安装

npm install dappsdk

ES-Module:

import dappSDK from 'dappsdk'

dappSDK.gui.showToast({message: 'Hello World!', delay: 1000})

作为普通资源加载, 使用 node_modules/dappsdk/dist/index.js:

<script type="text/javascript" src="路径"></script>
window.dappSDK.gui.showToast({message: 'Hello World!', delay: 1000})

不管你使用何种方式引入, window.dappSDK始终存在.


回调与Promise

首先, SDK本身并不附带promise, 如果你需要promise, 请确保window.Promise存在.

所有的API均有一个可选的回调函数参数, 如果这个参数不存在, 那调用这个API将返回一个 Promise 实例:

window.dappSDK.system.getSdkInfo({})
    .then(({version}) => alert(version));

window.dappSDK.system.getSdkInfo({}, function(err, data) {
    if(err) {
        return alert('Error: ' + err.message)
    }
    var version = data.version;
    alert(version)
})

错误处理

在使用callback风格调用API的时候, 注意回调函数永远接收两个参数, 第一个为错误, 第二个为请求结果, 如果存在错误, 大部分情况请求结果会为空, 所以在代码中应使用 错误优先回调 :

window.dappSDK.system.getSdkInfo({}, function(err, data) {
    if(err) {
        var code = error.code;
        console.log(code);
        // 如果错误存在, 那么return可以保证之后的代码不执行
        return alert('Error: ' + err.message)
    }
    var version = data.version;
    alert(version)
})

错误

不管使用何种风格处理异步, error 都会包含3个变量, error.message, error.code, error.extra. message 是对error的简单描述, codemessage 的映射, extramessage 额外描述, 在大多数情况下, extraundefined.

错误与状态码

状态码错误
-10001namespace错误, 可能是sdk与native端的版本不对应, 尝试升级app和sdk到最新版本
-10002function错误, 可能是sdk与native端的版本不对应, 尝试升级app和sdk到最新版本
-10003params错误
-10004用户取消了操作
-10005以后其他错误,根据api不同,文档中给于说明

API


exec


function exec(namespace, fnName, params, [callback])

parameters:

名称类型备注
namespaceString被调用函数的命名空间.
fnNameString函数名.
paramsObject调用该函数时的参数.
callbackFunction可选, 如果存在, 那当原生端响应时将调用此函数.

例:

window.dappSDK.exec('system', 'getSdkInfo', {}, function(data) {
    alert(data.version) //version
})

SYSTEM

namespace: system


获取Native SDK信息

function: getSdkInfo

params: null

output:

keyvalueremark
versionString版本号

例:

dappSDK.system.getSdkInfo({}, function(err, data) {
    if(err)
        return console.error(err);
    console.log(data)
}); 

dappSDK.system.getSdkInfo({})
    .then(data => console.log(data))
    .catch(err => console.error(err))

获取当前语言设置

function: getLanguageSetting

params: null

output:

keyvalueremark
languageSettingString语言设置(Chinese,English)

获取当前显示币种

function: getSymbol

params: null

output:

keyvalueremark
symbolString币种(USD,CNY)

GUI

namespace: gui


全屏切换

function: changeFullScreen

params:

keyvalueremark
fullScreenBoolean是否全屏

output: null


更改状态栏显示

function: changeStatusBar

params:

keyvalueremark
titleString标题
colorString十六进制颜色(#FFFFFF)
themeString使用暗色或者白色的图标以及文字(dark,light)
canGoBackBoolean是否能够返回上一级(并且显示返回按钮)

output: null


Toast

function: showToast

params:

keyvalueremark
messageString内容
delayInteger显示时长

output: null


进度

function: showProgress

params:

keyvalueremark
messageString内容
delayNumber最大显示时长(毫秒),默认最大3000ms

output: null


function: hideProgress

params: null

output: null


Alert

function: showAlert

params:

keyvalueremark
titleString标题
messageString内容
btnStringString按钮文字

output:

keyvalueremark
clickedInteger接收到回调时,代表用户按下了按钮

Dialog

function: showDialog

params:

keyvalueremark
titleString标题
messageString内容
leftBtnStringString左边按钮文字
rightBtnStringString右边按钮文字

output:

keyvalueremark
clickedInteger接收到回调时,代表用户按下了按钮,0代表左边,1代表右边

复制到剪切板

function: setClipboard

params:

keyvalueremark
dataString复制的内容

output: null


扫描二维码

function: scanQRCode

params: null

output:

keyvalueremark
resultString扫描到的内容

CUSTOMER

namespace: customer


获取当前钱包类型

function: getCurrentWalletType

params: null

output:

keyvalueremark
walletTypeString钱包类型(EOS,ETH)

error:

coderemark
-10005当前没有钱包

检查当前是否开启了指纹支付

function: checkFingerprintPayment

params: null

output:

keyvalueremark
statusNumber状态(-1:硬件不支持,0:未开启,1:已开启)

EOS

namespace: eos


获取当前账户

function: getCurrentWalletAccount

params: null

output:

keyvalueremark
accountString账户名
addressString公钥

error:

coderemark
-10005当前没有钱包

获取当前账户余额

function: getCurrentBalance

params:

keyvalueremark
tokenNameString代币名称
contractString合约地址

output:

keyvalueremark
accountString账户名
tokenNameString代币名称
contractString合约地址
balanceNumber余额

error:

coderemark
-10005当前没有钱包

获取当前账户信息

function: getCurrentAccountInfo

params: null

output: 链上返回的原始数据

error:

coderemark
-10005当前没有钱包

获取账户列表

function: getWalletAccounts

params: null

output:

keyvalueremark
accountsArray账户名列表

item:

keyvalueremark
accountString账户名
addressString公钥

error:

coderemark
-10005当前没有钱包

获取余额

function: getBalance

params:

keyvalueremark
accountString账户名
contractString合约地址

output:

keyvalueremark
accountString账户名
contractString合约地址
balanceNumber余额
symbolString单位

error:

coderemark
-10006查询失败(网络错误)

获取账户信息

function: getAccountInfo

params:

keyvalueremark
accountString账户名

output: 链上返回的原始数据


转账

function: transfer

params:

keyvalueremark
fromString转出账户名
fromAddressString转出账户公钥
toString转账接受账户名
amountNumber金额
symbolString代币单位
contractString合约地址
memoString备注
hintString提示,仅用于展示给用户

output:

keyvalueremark
transactionIdString交易ID

error:

coderemark
-10006网络错误
-10007交易失败
-10008没有找到转出的钱包

获取交易记录

function: getTransactionRecord

params:

keyvalueremark
accountString账户名
tokenNameString代币名称
contractString合约地址
\ \ 翻页方式待定

output: 链上返回的原始数据


ETH

namespace: eth


转账

待定

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago