add examples

test
Minh Doan 5 years ago
parent 591686a5ce
commit 420a3fe498
  1. 31
      examples/balance.js
  2. 14
      examples/package.json
  3. 75
      examples/staking_test.js
  4. 31
      examples/test.js
  5. 40
      examples/test2.js

@ -0,0 +1,31 @@
const { Harmony } = require('@harmony-js/core');
// import or require settings
const { ChainID, ChainType } = require('@harmony-js/utils');
const URL_TESTNET = `https://api.s0.b.hmny.io`;
const URL_MAINNET = `https://api.s0.t.hmny.io`;
// 1. initialize the Harmony instance
const harmony = new Harmony(
// rpc url
URL_TESTNET,
{
// chainType set to Harmony
chainType: ChainType.Harmony,
// chainType set to HmyLocal
chainId: ChainID.HmyTestnet,
},
);
harmony.blockchain
.getBalance({
address: `one1vjywuur8ckddmc4dsyx6qdgf590eu07ag9fg4a`,
})
.then((res) => {
console.log(new harmony.utils.Unit(res.result).asWei().toEther());
})
.catch((err) => {
console.log(err);
});

@ -0,0 +1,14 @@
{
"name": "example",
"version": "1.0.0",
"description": "",
"main": "test.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@harmony-js/core": "^0.1.32"
}
}

@ -0,0 +1,75 @@
const { Harmony } = require('@harmony-js/core');
const { ChainID, ChainType } = require('@harmony-js/utils');
const {
StakingTransaction,
CreateValidator,
Delegate,
Undelegate,
CollectRewards,
} = require('@harmony-js/staking');
const createMsg = CreateValidator({
validatorAddress: 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9',
description: {
name: 'Alice',
identity: 'alice',
website: 'alice.harmony.one',
securityContact: 'Bob',
details: "Don't mess with me!!!",
},
commissionRates: {
rate: '0.1',
maxRate: '0.9',
maxChangeRate: '0.05',
},
minSelfDelegation: '0xa',
maxTotalDelegation: '0x0bb8',
slotPubKeys: [
'0xb9486167ab9087ab818dc4ce026edb5bf216863364c32e42df2af03c5ced1ad181e7d12f0e6dd5307a73b62247608611',
],
amount: '0x64',
});
const stakingTxn = StakingTransaction({
directive: '0x',
stakeMsg: createMsg,
nonce: '0x2',
gasPrice: '0x',
gasLimit: '0x64',
chainId: 0,
from: 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9',
});
stakingTxn
.sendTransaction()
.then((stakingTxn, res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
});
// const delegateMsg = Delegate({
// delegatorAddress: 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9',
// validatorAddress: 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9',
// amount: '0xa',
// });
// const stakingTxn = StakingTransaction({
// directive: '0x2',
// stakeMsg: delegateMsg,
// nonce: '0x2',
// gasPrice: '0x',
// gasLimit: '0x64',
// chainId: 0,
// from: 'one1a0x3d6xpmr6f8wsyaxd9v36pytvp48zckswvv9',
// });
// stakingTxn
// .sendTransaction()
// .then((stakingTxn, res) => {
// console.log(res);
// })
// .catch((err) => {
// console.log(err);
// });

@ -0,0 +1,31 @@
const { Harmony } = require('@harmony-js/core');
const { ChainID, ChainType } = require('@harmony-js/utils');
const { encryptPhrase, decryptPhrase } = require('@harmony-js/crypto');
const harmony = new Harmony('http://localhost:9500', { chainId: 2, chainType: 'hmy' });
const myPhrase = harmony.wallet.newMnemonic();
const pwd = '1234';
async function encryptThePhrase(phrase, pass) {
const result = await encryptPhrase(phrase, pass);
return result;
}
async function decryptThePhrase(keystore, pass) {
const result = await decryptPhrase(keystore, pass);
return result;
}
async function phraseKeyStore() {
const keyStore = await encryptThePhrase(myPhrase, pwd);
const recoveredPhrase = await decryptThePhrase(JSON.parse(keyStore), pwd);
return { myPhrase, keyStore, recoveredPhrase };
}
phraseKeyStore().then((result) => {
const { myPhrase, keyStore, recoveredPhrase } = result;
harmony.wallet.addByMnemonic(myPhrase);
console.log({ myPhrase, keyStore, recoveredPhrase });
console.log(harmony.wallet);
});

@ -0,0 +1,40 @@
const { Harmony } = require('@harmony-js/core');
const { ChainID, ChainType } = require('@harmony-js/utils');
const {
randomBytes,
generatePrivateKey,
getAddress,
getAddressFromPrivateKey,
encryptPhrase,
decryptPhrase,
} = require('@harmony-js/crypto');
const harmony = new Harmony('http://localhost:9500', { chainId: 2, chainType: 'hmy' });
const myPhrase = harmony.wallet.newMnemonic();
const pwd = '1234';
async function encryptThePhrase(phrase, pass) {
const result = await encryptPhrase(phrase, pass);
return result;
}
async function decryptThePhrase(keystore, pass) {
const result = await decryptPhrase(keystore, pass);
return result;
}
async function phraseKeyStore() {
const keyStore = await encryptThePhrase(myPhrase, pwd);
const recoveredPhrase = await decryptThePhrase(JSON.parse(keyStore), pwd);
return { myPhrase, keyStore, recoveredPhrase };
}
phraseKeyStore().then((result) => {
const { myPhrase, keyStore, recoveredPhrase } = result;
const anotherPhrase = 'wall public vague under poem acid jaguar describe net scene sponsor neck';
harmony.wallet.addByMnemonic(myPhrase);
harmony.wallet.addByMnemonic(anotherPhrase);
console.log({ myPhrase, keyStore, recoveredPhrase });
console.log(harmony.wallet);
});
Loading…
Cancel
Save