diff options
Diffstat (limited to 'ethpipe/js_pipe.go')
-rw-r--r-- | ethpipe/js_pipe.go | 130 |
1 files changed, 30 insertions, 100 deletions
diff --git a/ethpipe/js_pipe.go b/ethpipe/js_pipe.go index b32e94a10..96990b671 100644 --- a/ethpipe/js_pipe.go +++ b/ethpipe/js_pipe.go @@ -3,12 +3,10 @@ package ethpipe import ( "bytes" "encoding/json" - "fmt" "sync/atomic" "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethcrypto" - "github.com/ethereum/eth-go/ethreact" "github.com/ethereum/eth-go/ethstate" "github.com/ethereum/eth-go/ethutil" ) @@ -87,10 +85,6 @@ func (self *JSPipe) CoinBase() string { return ethutil.Bytes2Hex(self.obj.KeyManager().Address()) } -func (self *JSPipe) BalanceAt(addr string) string { - return self.World().SafeGet(ethutil.Hex2Bytes(addr)).Balance.String() -} - func (self *JSPipe) NumberToHuman(balance string) string { b := ethutil.Big(balance) @@ -99,13 +93,22 @@ func (self *JSPipe) NumberToHuman(balance string) string { func (self *JSPipe) StorageAt(addr, storageAddr string) string { storage := self.World().SafeGet(ethutil.Hex2Bytes(addr)).Storage(ethutil.Hex2Bytes(storageAddr)) - return storage.BigInt().String() + + return ethutil.Bytes2Hex(storage.Bytes()) +} + +func (self *JSPipe) BalanceAt(addr string) string { + return self.World().SafeGet(ethutil.Hex2Bytes(addr)).Balance.String() } func (self *JSPipe) TxCountAt(address string) int { return int(self.World().SafeGet(ethutil.Hex2Bytes(address)).Nonce) } +func (self *JSPipe) CodeAt(address string) string { + return ethutil.Bytes2Hex(self.World().SafeGet(ethutil.Hex2Bytes(address)).Code) +} + func (self *JSPipe) IsContract(address string) bool { return len(self.World().SafeGet(ethutil.Hex2Bytes(address)).Code) > 0 } @@ -119,6 +122,18 @@ func (self *JSPipe) SecretToAddress(key string) string { return ethutil.Bytes2Hex(pair.Address()) } +func (self *JSPipe) Execute(addr, value, gas, price, data string) (string, error) { + ret, err := self.ExecuteObject(&Object{ + self.World().safeGet(ethutil.Hex2Bytes(addr))}, + ethutil.Hex2Bytes(data), + ethutil.NewValue(value), + ethutil.NewValue(gas), + ethutil.NewValue(price), + ) + + return ethutil.Bytes2Hex(ret), err +} + type KeyVal struct { Key string `json:"key"` Value string `json:"value"` @@ -224,6 +239,12 @@ func (self *JSPipe) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr return NewJSReciept(contractCreation, tx.CreationAddress(), tx.Hash(), keyPair.Address()), nil } +func (self *JSPipe) PushTx(txStr string) (*JSReceipt, error) { + tx := ethchain.NewTransactionFromBytes(ethutil.Hex2Bytes(txStr)) + self.obj.TxPool().QueueTransaction(tx) + return NewJSReciept(tx.CreatesContract(), tx.CreationAddress(), tx.Hash(), tx.Sender()), nil +} + func (self *JSPipe) CompileMutan(code string) string { data, err := self.Pipe.CompileMutan(code) if err != nil { @@ -233,102 +254,11 @@ func (self *JSPipe) CompileMutan(code string) string { return ethutil.Bytes2Hex(data) } -func (self *JSPipe) Watch(object map[string]interface{}) *JSFilter { - return NewJSFilterFromMap(object, self.Pipe.obj) - /*} else if str, ok := object.(string); ok { - println("str") - return NewJSFilterFromString(str, self.Pipe.obj) - */ -} - -func (self *JSPipe) Messages(object map[string]interface{}) string { - filter := self.Watch(object) - filter.Uninstall() - - return filter.Messages() - -} - -type JSFilter struct { - eth ethchain.EthManager - *ethchain.Filter - quit chan bool - - BlockCallback func(*ethchain.Block) - MessageCallback func(ethstate.Messages) -} - -func NewJSFilterFromMap(object map[string]interface{}, eth ethchain.EthManager) *JSFilter { - filter := &JSFilter{eth, ethchain.NewFilterFromMap(object, eth), make(chan bool), nil, nil} - - go filter.mainLoop() - - return filter -} - -func NewJSFilterFromString(str string, eth ethchain.EthManager) *JSFilter { - return nil -} - -func (self *JSFilter) MessagesToJson(messages ethstate.Messages) string { +func ToJSMessages(messages ethstate.Messages) *ethutil.List { var msgs []JSMessage for _, m := range messages { msgs = append(msgs, NewJSMessage(m)) } - // Return an empty array instead of "null" - if len(msgs) == 0 { - return "[]" - } - - b, err := json.Marshal(msgs) - if err != nil { - return "{\"error\":" + err.Error() + "}" - } - - return string(b) -} - -func (self *JSFilter) Messages() string { - return self.MessagesToJson(self.Find()) -} - -func (self *JSFilter) mainLoop() { - blockChan := make(chan ethreact.Event, 5) - messageChan := make(chan ethreact.Event, 5) - // Subscribe to events - reactor := self.eth.Reactor() - reactor.Subscribe("newBlock", blockChan) - reactor.Subscribe("messages", messageChan) -out: - for { - select { - case <-self.quit: - break out - case block := <-blockChan: - if block, ok := block.Resource.(*ethchain.Block); ok { - if self.BlockCallback != nil { - self.BlockCallback(block) - } - } - case msg := <-messageChan: - if messages, ok := msg.Resource.(ethstate.Messages); ok { - if self.MessageCallback != nil { - println("messages!") - msgs := self.FilterMessages(messages) - if len(msgs) > 0 { - self.MessageCallback(msgs) - } - } - } - } - } -} - -func (self *JSFilter) Changed(object interface{}) { - fmt.Printf("%T\n", object) -} - -func (self *JSFilter) Uninstall() { - self.quit <- true + return ethutil.NewList(msgs) } |