eosjs 文档(浏览器)

7次阅读

共计 1414 个字符,预计需要花费 4 分钟才能阅读完成。

浏览器
用法
npm run build-web 或 yarn build-web。
为所有交易重用 api 对象,它缓存 ABI 以减少网络使用,只调用一次 new eosjs_api.default(…)。
<pre style=”width: 100%; height: 100%; margin:0px; “></pre>

<script src=’dist-web/eosjs-api.js’></script>
<script src=’dist-web/eosjs-jsonrpc.js’></script>
<script src=’dist-web/eosjs-jssig.js’></script>
<script>
let pre = document.getElementsByTagName(‘pre’)[0];
const defaultPrivateKey = “5JtUScZK2XEp3g9gh7F8bwtPTRAkASmNrrftmx4AxDKD5K4zDnr”; // useraaaaaaaa
const rpc = new eosjs_jsonrpc.default(‘http://127.0.0.1:8888’);
const signatureProvider = new eosjs_jssig.default([defaultPrivateKey]);
const api = new eosjs_api.default({rpc, signatureProvider});

(async () => {
try {
const result = await api.transact({
actions: [{
account: ‘eosio.token’,
name: ‘transfer’,
authorization: [{
actor: ‘useraaaaaaaa’,
permission: ‘active’,
}],
data: {
from: ‘useraaaaaaaa’,
to: ‘useraaaaaaab’,
quantity: ‘0.0001 SYS’,
memo: ”,
},
}]
}, {
blocksBehind: 3,
expireSeconds: 30,
});
pre.textContent += ‘\n\nTransaction pushed!\n\n’ + JSON.stringify(result, null, 2);
} catch (e) {
pre.textContent = ‘\nCaught exception: ‘ + e;
if (e instanceof eosjs_jsonrpc.RpcError)
pre.textContent += ‘\n\n’ + JSON.stringify(e.json, null, 2);
}
})();
</script>
调试
如果你想要可读的源文件进行调试,请将文件引用更改为 dist-web/debug 目录下的 -debug.js 文件,这些文件只用于开发,因为它们的大小是缩小了 10 倍多的版本,导入调试版本将增加最终用户的加载时间。
IE11 和 Edge 支持
如果你需要支持 IE11 或 Edge,你还需要安装文本编码的 polyfill,因为 eosjs 签名依赖于 IE11 和 Edge 不提供的 TextEncoder。将 TextEncoder 和 TextDecoder 传递给 API 构造函数,请参阅 https://github.com/inexorabletash/text-encoding 中的文档,以确定将其包含在项目中的最佳方法。

上一篇:介绍

正文完
 0