You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
neeboo
311709dab1
|
5 years ago | |
---|---|---|
docs | 5 years ago | |
e2e | 5 years ago | |
packages | 5 years ago | |
scripts | 5 years ago | |
typings | 6 years ago | |
.babelrc | 6 years ago | |
.env | 5 years ago | |
.eslintignore | 6 years ago | |
.gitignore | 6 years ago | |
.prettierignore | 6 years ago | |
.prettierrc | 5 years ago | |
.travis.yml | 5 years ago | |
.yarnrc | 5 years ago | |
LICENSE | 6 years ago | |
README.md | 5 years ago | |
gulpfile.js | 5 years ago | |
lerna.json | 5 years ago | |
package.json | 5 years ago | |
tsconfig.base.json | 6 years ago | |
tsconfig.e2e.json | 5 years ago | |
tsconfig.json | 5 years ago | |
tsconfig.test.json | 6 years ago | |
tslint.json | 6 years ago |
README.md
Harmony-SDK-Core
A Harmony's blockchain javascript library
It's a mono-repo library, not yet published to npm.
Install from npm/yarn
Note: we added a @next tag to npm package, please use the following command to install with npm/yarn
# npm
npm install @harmony-js/core@next
# yarn
yarn add @harmony-js/core@next
# tslib may be required, we'd better install it as well
npm install tslib
yarn add tslib
Examples with tutorials
Packages
- @harmony-js/core
- @harmony-js/account
- @harmony-js/crypto
- @harmony-js/network
- @harmony-js/utils
- @harmony-js/transaction
- @harmony-js/contract
Hacking from source files
- install
lerna
andtypescript
globally (if you have these, you can skip)
yarn global add lerna && yarn global add typescript
- bootstrap repostory
yarn install && yarn bootstrap
- run watcher before editing any source file
yarn watch
- if you are ready to build/test/bundle, please refer to the following section:
Manually Build/bundle
Build
yarn build
Bundle
There are 2 ways bundling files.
-
building the
es5
version bundled javascript for each sub-packages, which can be run in Browser directly.yarn dist
All files are exported in
/dist
folder -
build
umd
andesm
version javascript for each sub-packages, which can be accessed byimport
orrequire
yarn bundle
All files are exported in
packages/dist
folder, use**.esm.js
or**.umd.js
format
E2E tests
Contantly updating now, please get back later
- edit
.env
file if you have custom setting - run harmony node locally(this fork currently : https://github.com/mikedoan/harmony/tree/enable_tx)
- wait for 1-2 mins, and run this:
yarn build && yarn test:e2e
Cross-Shard
async function crossShard() {
// manually set the shardingStructure
// 手动设置 sharding
const shardingArray = [
{
shardID: 0,
http: 'http://localhost:9500',
ws: 'ws://localhost:9800',
},
{
shardID: 1,
http: 'http://localhost:9501',
ws: 'ws://localhost:9801',
},
];
// get shardingStructure from rpc
// 通过api获取sharding设置
const res = await harmony.blockchain.getShardingStructure();
if (res.result) {
// if we can get from network use `harmony.shardingStructures` to set the structure to all sub module
//如果网络获取成功,通过`harmony.shardingStructures`进行设置
harmony.shardingStructures(res.result);
} else {
// or set it using local setting
// 否则加载本地的设置
harmony.shardingStructures(shardingArray);
}
// each account should update it's own shard
// 每个帐号更新自己的sharding
await acc1.updateBalances();
await acc2.updateBalances();
// to get sharded address, the format goes `bech32-{shardID}`
// 获得分片地址,返回格式为 `bech32-{shardID}`
const from = acc1.getAddressFromShardID(0);
const to = acc2.getAddressFromShardID(1);
// use `getShardBalance(shardID:number)` to get balance in shardID
// 可以获得指定shard的余额
acc1.getShardBalance(0).then(console.log);
// you can print the sharding map for each account
// 打印每个帐号的sharding结构
console.log({ acc1: acc1.shards, acc2: acc2.shards });
// now construct a ShardingTransaction, use `harmony.transactions.newTx(obj,true)`
// because `from` and `to` here ,are sharded addresses, like `bech32-{shardID}`
// you dont have to specify `shardID` and `toShardID`, it will handle it automatically.
// 构建一个sharding transaction,注意,true这里需要标志好
// 因为from,和 to已经被认定为带后缀的分片地址
// 格式为`bech32-{shardID}`
// 在这里不需要指定 shardID和toShardID了,因为这个txn会自动把 shardID和toShardID填进去
const txn = harmony.transactions.newTx(
{
from,
to,
value: '10000',
gasLimit: '210000',
gasPrice: new harmony.utils.Unit('1000000000').asWei().toWei(),
},
true,
);
// now use acc1 to sign, it will use specific shardID to get the nonce of sharded address
// 正常使用acc1进行签名,这里会自动根据shardID来取指定分片的nonce
const signed = await acc1.signTransaction(txn, true);
// send the transaction, it will also use speicific shardID to send the transaction
// 发送交易,同样会根据shardID来进行交易的发送
const [sent, id] = await signed.sendTransaction();
// print the id(transactionHash), and the class that return as well
// 打印返回的Transaction Class,和transaction ID
console.log({ sent, id });
// you can use Transaction.confirm() to confirm the transaction is on the blockchain or not
//使用confirm进行transactionReceipt的获取以确认
await sent.confirm(id);
// of course you can use blockchain.getTransactionReceipt to get the receipt
// you have to specify the shardID here.
// 同样可以通过blockchain类来获取transaction Receipt
// 注意你需要指定shardID
const receipt = await harmony.blockchain.getTransactionReceipt({
txnHash: id,
shardID: sent.txParams.shardID,
});
console.log({ receipt: receipt.result });
}