-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from 0xsharma/shivam/add-send-tx
add : send-tx cmd, fix downloader
- Loading branch information
Showing
15 changed files
with
873 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"math/big" | ||
"net/rpc" | ||
|
||
"github.com/0xsharma/compact-chain/types" | ||
"github.com/0xsharma/compact-chain/util" | ||
) | ||
|
||
type sendTxConfig struct { | ||
PrivateKey string | ||
To string | ||
Value int64 | ||
RPCAddr string | ||
Nonce int64 | ||
} | ||
|
||
func SendTx(sendTxCfg *sendTxConfig) { | ||
ua := util.NewUnlockedAccount(util.HexToPrivateKey(sendTxCfg.PrivateKey)) | ||
from := ua.Address() | ||
|
||
tx := &types.Transaction{ | ||
From: *from, | ||
To: *util.StringToAddress(sendTxCfg.To), | ||
Value: big.NewInt(sendTxCfg.Value), | ||
Msg: []byte("hello"), | ||
Fee: big.NewInt(1000), | ||
Nonce: big.NewInt(sendTxCfg.Nonce), | ||
} | ||
tx.Sign(ua) | ||
|
||
fmt.Printf("%+v\n", tx) | ||
res, err := SendRpcRequest("TxPool.AddTx_RPC", tx, sendTxCfg.RPCAddr) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Println(res) | ||
} | ||
|
||
func SendRpcRequest(method string, params interface{}, rpcAddr string) (interface{}, error) { | ||
client, err := rpc.DialHTTP("tcp", rpcAddr) | ||
if err != nil { | ||
log.Fatal("dialing: ", err) | ||
} | ||
|
||
var reply types.RPCResponse | ||
|
||
err = client.Call(method, params, &reply) | ||
if err != nil { | ||
log.Fatal("error: ", err) | ||
} | ||
|
||
return reply, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.