test smart contract

pull/254/head
Lutty 4 years ago
parent 5e13282019
commit 04d6def83c
  1. 23
      README.md
  2. 16
      pkg/console/console.go
  3. 53
      pkg/console/hmy.go
  4. 2
      pkg/console/jsre/deps/bindata.go
  5. 8
      pkg/console/jsre/deps/web3.js

@ -153,6 +153,9 @@ body: |
25. Vote Proposal In Space Of Governance 25. Vote Proposal In Space Of Governance
./hmy governance vote-proposal --proposal=[proposal hash] --choice=[your choice text, eg: yes] --key=[key name] ./hmy governance vote-proposal --proposal=[proposal hash] --choice=[your choice text, eg: yes] --key=[key name]
PS: key must first use (hmy keys import-private-key) to import PS: key must first use (hmy keys import-private-key) to import
26. Enter Console
./hmy command --net=testnet --shard=0
``` ```
# Sending batched transactions # Sending batched transactions
@ -266,3 +269,23 @@ based environment variables.
```bash ```bash
HMY_RPC_DEBUG=true HMY_TX_DEBUG=true ./hmy blockchain protocol-version HMY_RPC_DEBUG=true HMY_TX_DEBUG=true ./hmy blockchain protocol-version
``` ```
# Contract Deploy
You can deploy the contract use the command;
```bash
./hmy command --net=testnet --shard=0
```
Then you can test this code to deploy a smart contract
```js
var abi = [{"constant":false,"inputs":[],"name":"changeIt","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"number","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]
var code = '0x60806040526212345660005534801561001757600080fd5b5060c8806100266000396000f3006080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806327b5c17514604e5780638381f58a146062575b600080fd5b348015605957600080fd5b506060608a565b005b348015606d57600080fd5b5060746096565b6040518082815260200191505060405180910390f35b62654321600081905550565b600054815600a165627a7a72305820294db9eed647c48762212fd43bf9ed7733c9d9f43b1ce5583bb4c2bae97fae770029'
var publicKey = '0x34BB31FBE8E7F0F480983B7690Ea4977259534b5' // change this to your public key
var myContract=eth.contract(abi)
contract = myContract.new({from:publicKey,data:code})
```
Wait for a few seconds and it's ready to use

@ -231,13 +231,13 @@ func (c *Console) initPersonal(vm *goja.Runtime, bridge *bridge) {
return return
} }
personal.Set("getListAccounts", jsre.MakeCallback(vm, bridge.HmyGetListAccounts)) personal.Set("getListAccounts", jsre.MakeCallback(vm, bridge.HmyGetListAccounts))
personal.Set("signTransaction", jsre.MakeCallback(vm, bridge.HmySignTransaction)) personal.Set("signTransaction", jsre.MakeCallback(vm, bridge.callbackProtected(bridge.HmySignTransaction)))
personal.Set("sendTransaction", jsre.MakeCallback(vm, bridge.HmySendTransaction)) personal.Set("sendTransaction", jsre.MakeCallback(vm, bridge.callbackProtected(bridge.HmySendTransaction)))
personal.Set("lockAccount", jsre.MakeCallback(vm, bridge.HmyLockAccount)) personal.Set("lockAccount", jsre.MakeCallback(vm, bridge.callbackProtected(bridge.HmyLockAccount)))
personal.Set("importRawKey", jsre.MakeCallback(vm, bridge.HmyImportRawKey)) personal.Set("importRawKey", jsre.MakeCallback(vm, bridge.HmyImportRawKey))
personal.Set("unlockAccount", jsre.MakeCallback(vm, bridge.HmyUnlockAccount)) personal.Set("unlockAccount", jsre.MakeCallback(vm, bridge.callbackProtected(bridge.HmyUnlockAccount)))
personal.Set("newAccount", jsre.MakeCallback(vm, bridge.HmyNewAccount)) personal.Set("newAccount", jsre.MakeCallback(vm, bridge.callbackProtected(bridge.HmyNewAccount)))
personal.Set("sign", jsre.MakeCallback(vm, bridge.HmySign)) personal.Set("sign", jsre.MakeCallback(vm, bridge.callbackProtected(bridge.HmySign)))
_, err := vm.RunString(`Object.defineProperty(personal, "listAccounts", {get: personal.getListAccounts});`) _, err := vm.RunString(`Object.defineProperty(personal, "listAccounts", {get: personal.getListAccounts});`)
if err != nil { if err != nil {
@ -251,8 +251,8 @@ func (c *Console) initEth(vm *goja.Runtime, bridge *bridge) {
return return
} }
eth.Set("sendTransaction", jsre.MakeCallback(vm, bridge.HmySendTransaction)) eth.Set("sendTransaction", jsre.MakeCallback(vm, bridge.callbackProtected(bridge.HmySendTransaction)))
eth.Set("signTransaction", jsre.MakeCallback(vm, bridge.HmySignTransaction)) eth.Set("signTransaction", jsre.MakeCallback(vm, bridge.callbackProtected(bridge.HmySignTransaction)))
} }
func (c *Console) clearHistory() { func (c *Console) clearHistory() {

@ -20,6 +20,39 @@ import (
"time" "time"
) )
func getStringFromJsObjWithDefault(o *goja.Object, key string, def string) string {
get := o.Get(key)
if get == nil {
return def
} else {
return get.String()
}
}
func (b *bridge) callbackProtected(protectedFunc func(call jsre.Call) (goja.Value, error)) func(call jsre.Call) (goja.Value, error) {
return func(call jsre.Call) (goja.Value, error) {
var availableCB goja.Callable = nil
for i, args := range call.Arguments {
if cb, ok := goja.AssertFunction(args); ok {
availableCB = cb
call.Arguments = call.Arguments[:i] // callback must be last
break
}
}
value, err := protectedFunc(call)
jsErr := goja.Undefined()
if err != nil {
jsErr = call.VM.NewGoError(err)
}
if availableCB != nil {
_, _ = availableCB(nil, jsErr, value)
}
return value, err
}
}
func (b *bridge) HmyGetListAccounts(call jsre.Call) (goja.Value, error) { func (b *bridge) HmyGetListAccounts(call jsre.Call) (goja.Value, error) {
var accounts = []string{} var accounts = []string{}
@ -38,11 +71,11 @@ func (b *bridge) HmySignTransaction(call jsre.Call) (goja.Value, error) {
txObj := call.Arguments[0].ToObject(call.VM) txObj := call.Arguments[0].ToObject(call.VM)
password := call.Arguments[1].String() password := call.Arguments[1].String()
from := txObj.Get("from").String() from := getStringFromJsObjWithDefault(txObj, "from", "")
to := txObj.Get("to").String() to := getStringFromJsObjWithDefault(txObj, "to", "")
gasLimit := txObj.Get("gas").String() gasLimit := getStringFromJsObjWithDefault(txObj, "gas", "1000000")
amount := txObj.Get("value").String() amount := getStringFromJsObjWithDefault(txObj, "value", "0")
gasPrice := txObj.Get("gasPrice").String() gasPrice := getStringFromJsObjWithDefault(txObj, "gasPrice", "1")
networkHandler := rpc.NewHTTPHandler(b.console.nodeUrl) networkHandler := rpc.NewHTTPHandler(b.console.nodeUrl)
chanId, err := common.StringToChainID(b.console.net) chanId, err := common.StringToChainID(b.console.net)
@ -120,11 +153,11 @@ func (b *bridge) HmySendTransaction(call jsre.Call) (goja.Value, error) {
password = call.Arguments[1].String() password = call.Arguments[1].String()
} }
from := txObj.Get("from").String() from := getStringFromJsObjWithDefault(txObj, "from", "")
to := txObj.Get("to").String() to := getStringFromJsObjWithDefault(txObj, "to", "")
gasLimit := txObj.Get("gas").String() gasLimit := getStringFromJsObjWithDefault(txObj, "gas", "1000000")
amount := txObj.Get("value").String() amount := getStringFromJsObjWithDefault(txObj, "value", "0")
gasPrice := txObj.Get("gasPrice").String() gasPrice := getStringFromJsObjWithDefault(txObj, "gasPrice", "1")
networkHandler := rpc.NewHTTPHandler(b.console.nodeUrl) networkHandler := rpc.NewHTTPHandler(b.console.nodeUrl)
chanId, err := common.StringToChainID(b.console.net) chanId, err := common.StringToChainID(b.console.net)

File diff suppressed because one or more lines are too long

@ -3005,10 +3005,8 @@ require = (function e(t, n, r) {
filter.stopWatching(function () { filter.stopWatching(function () {
}); });
callbackFired = true; callbackFired = true;
if (code) {
if (code.length > 3) { console.log('Contract code deployed to ' + receipt.contractAddress);
// console.log('Contract code deployed!');
contract.address = receipt.contractAddress; contract.address = receipt.contractAddress;
@ -3106,6 +3104,8 @@ require = (function e(t, n, r) {
var hash = this.eth.sendTransaction(options); var hash = this.eth.sendTransaction(options);
// add the transaction hash // add the transaction hash
contract.transactionHash = hash; contract.transactionHash = hash;
console.log('check for contract address, please wait...');
checkForContractAddress(contract); checkForContractAddress(contract);
} }

Loading…
Cancel
Save