Skip to content

Commit

Permalink
[feat] modify README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
neeboo committed May 21, 2019
1 parent cc95986 commit cc55172
Show file tree
Hide file tree
Showing 14 changed files with 1,895 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,54 @@
# harmony-sdk-examples

# Install

```bash
yarn install
```


# Test local wallet

1. open `nodejs`
2. run `node testWallet.js`
3. you can see `mnemonic` and `simple password` and 10 accounts imported


# Test with Harmony node

First you have to run harmony's test node.

1. git clone

``` bash
git clone git@github.com:harmony-one/harmony.git
```

2. follow the `Build all executables` instruction, [here](https://github.com/harmony-one/harmony/tree/master)
3. open your editor, inside `core/resharding.go` , edit `GenesisShardSize = 50` to `GenesisShardSize = 5`
4. use this script to run

```bash
./test/deploy.sh ./test/configs/ten-oneshard.txt
```

Wait for the test-node running for 30 seconds,

Then **open another console** , go back to our `nodejs` folder,

Run:

``` bash
node testNode.js
```


# Test with `ganache-cli`
** ganache-cli runs in js file **,

In this case, we use ganache and ethereum's setting to simulate the result
We don't need harmony's testnode running.
1. open `nodejs`
2. run `node testGanache.js`
19 changes: 19 additions & 0 deletions nodejs/contracts/Calc.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pragma solidity ^0.5.1;

contract Calc {

uint count;

function getCount() public returns (uint) {

return count;

}

function add(uint a, uint b) public returns (uint) {
count++;
return a + b;
}


}
8 changes: 8 additions & 0 deletions nodejs/contracts/MyContract.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pragma solidity ^0.5.1;


contract MyContract {
function myFunction() public returns(uint256 myNumber, string memory myString) {
return (23456, "Hello!%");
}
}
23 changes: 23 additions & 0 deletions nodejs/contracts/SimpleStorage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pragma solidity ^0.5.1;

contract SimpleStorage {

event ValueChanged(address indexed author, string oldValue, string newValue);

string _value;

constructor(string memory value) public {
emit ValueChanged(msg.sender, _value, value);
_value = value;
}

function getValue() view public returns (string memory) {
return _value;
}

function setValue(string memory value) public {
emit ValueChanged(msg.sender, _value, value);
_value = value;
}

}
52 changes: 52 additions & 0 deletions nodejs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const { Harmony } = require('@harmony-js/core')
const { ChainID, ChainType } = require('@harmony-js/utils')

const Settings = {
Ropsten: {
http: 'https://ropsten.infura.io/v3/4f3be7f5bbe644b7a8d95c151c8f52ec',
ws: 'wss://ropsten.infura.io/ws/v3/4f3be7f5bbe644b7a8d95c151c8f52ec',
type: ChainType.Ethereum,
id: ChainID.Ropsten
},
Rinkeby: {
http: 'https://rinkeby.infura.io/v3/4f3be7f5bbe644b7a8d95c151c8f52ec',
ws: 'wss://rinkeby.infura.io/ws/v3/4f3be7f5bbe644b7a8d95c151c8f52ec',
type: ChainType.Ethereum,
id: ChainID.Ropsten
},
Ganache: {
http: 'http://localhost:18545',
ws: 'ws://localhost:18545',
type: ChainType.Ethereum,
id: ChainID.Ganache
}
}

// a function that will map the setting to harmony class constructor inputs
function useSetting(setting, providerType) {
return [setting[providerType], setting.type, setting.id]
}

// simply change `Ropsten` to `Rinkeby` to test with different testnet
// and switch `ws` or `http` as RPC provider

const harmony = new Harmony(...useSetting(Settings.Ropsten, 'ws'))

// import our preset mnes
const mne =
'food response winner warfare indicate visual hundred toilet jealous okay relief tornado'

// now we have the mnes added to wallet
const acc1 = harmony.wallet.addByMnemonic(mne, 0)

// now we create contract using extracted abi
// const myContract = harmony.contracts.createContract(abi)

// first we get the account's balance to see if we have enough token on the testnet
acc1.getBalance().then(res => {
console.log(`-- hint: account balance of ${acc1.address}`)
console.log(``)
console.log({ account: res })
console.log(``)
console.log(``)
})
76 changes: 76 additions & 0 deletions nodejs/temp.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="format-detection" content="telephone=no" />
<meta name="format-detection" content="email=no" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"
/>
<title></title>
</head>
<style>
#nodeUrl,
#address {
width: 250px;
height: 50px;
font-size: 16px;
}

#setProviderButton,
#getBlanceButton {
width: 100px;
height: 50px;
margin-left: 16px;
background: rgba(0, 0, 0, 1);
color: #ffffff;
}
</style>

<body>
<div id="root">

</div>

<script src="../dist/HarmonyJs.browser.js"></script>
<script>

const harmony= new HarmonyJs.Harmony('ws://localhost:9128',0);

// console.log()




const biubiubiu = async () => {
const p= await harmony.blockchain.newBlockHeaders()
// harmony.blockchain.newBlockHeaders()


p.onData(res=>{
console.log(res)
}).on('error',error=>{
console.log(error)
})
setTimeout(()=>{
if(p.subscriptions!=={}){
p.clearSubscriptions('eth_unsubscribe').then(res=>{
if(res){
console.log('Successfully unsubscribed!')
}
})
}
},50000)

}

setTimeout(biubiubiu, 100);



</script>
</body>
</html>
Loading

0 comments on commit cc55172

Please sign in to comment.