types.go 2.54 KB
Newer Older
1
package javascript
obscuren's avatar
obscuren committed
2 3 4

import (
	"fmt"
5

6
	"github.com/ethereum/go-ethereum/eth"
7
	"github.com/ethereum/go-ethereum/ethutil"
obscuren's avatar
obscuren committed
8
	"github.com/ethereum/go-ethereum/state"
9
	"github.com/ethereum/go-ethereum/ui"
10
	"github.com/ethereum/go-ethereum/xeth"
11
	"github.com/obscuren/otto"
obscuren's avatar
obscuren committed
12 13
)

14
type JSStateObject struct {
15
	*xeth.Object
16
	eth *JSEthereum
obscuren's avatar
obscuren committed
17 18
}

19 20
func (self *JSStateObject) EachStorage(call otto.FunctionCall) otto.Value {
	cb := call.Argument(0)
obscuren's avatar
obscuren committed
21

22
	it := self.Object.Trie().Iterator()
obscuren's avatar
obscuren committed
23 24 25
	for it.Next() {
		cb.Call(self.eth.toVal(self), self.eth.toVal(ethutil.Bytes2Hex(it.Key)), self.eth.toVal(ethutil.Bytes2Hex(it.Value)))
	}
obscuren's avatar
obscuren committed
26

27
	return otto.UndefinedValue()
obscuren's avatar
obscuren committed
28 29 30 31
}

// The JSEthereum object attempts to wrap the PEthereum object and returns
// meaningful javascript objects
32
type JSBlock struct {
33
	*xeth.Block
34 35 36 37
	eth *JSEthereum
}

func (self *JSBlock) GetTransaction(hash string) otto.Value {
38
	return self.eth.toVal(self.Block.GetTransaction(hash))
39 40
}

obscuren's avatar
obscuren committed
41 42 43 44 45
type JSLog struct {
	Address string   `json:address`
	Topics  []string `json:topics`
	Number  int32    `json:number`
	Data    string   `json:data`
46 47
}

obscuren's avatar
obscuren committed
48 49 50 51 52 53
func NewJSLog(log state.Log) JSLog {
	return JSLog{
		Address: ethutil.Bytes2Hex(log.Address()),
		Topics:  nil, //ethutil.Bytes2Hex(log.Address()),
		Number:  0,
		Data:    ethutil.Bytes2Hex(log.Data()),
54 55 56
	}
}

obscuren's avatar
obscuren committed
57
type JSEthereum struct {
58
	*xeth.XEth
59 60
	vm       *otto.Otto
	ethereum *eth.Ethereum
obscuren's avatar
obscuren committed
61 62
}

63 64
func (self *JSEthereum) Block(v interface{}) otto.Value {
	if number, ok := v.(int64); ok {
65
		return self.toVal(&JSBlock{self.XEth.BlockByNumber(int32(number)), self})
66
	} else if hash, ok := v.(string); ok {
67
		return self.toVal(&JSBlock{self.XEth.BlockByHash(hash), self})
68 69 70
	}

	return otto.UndefinedValue()
obscuren's avatar
obscuren committed
71 72
}

obscuren's avatar
obscuren committed
73
func (self *JSEthereum) GetStateObject(addr string) otto.Value {
74
	return self.toVal(&JSStateObject{self.XEth.State().SafeGet(addr), self})
75 76
}

obscuren's avatar
obscuren committed
77
func (self *JSEthereum) Transact(key, recipient, valueStr, gasStr, gasPriceStr, dataStr string) otto.Value {
78
	r, err := self.XEth.Transact(recipient, valueStr, gasStr, gasPriceStr, dataStr)
obscuren's avatar
obscuren committed
79 80 81 82 83 84 85 86 87 88 89 90 91
	if err != nil {
		fmt.Println(err)

		return otto.UndefinedValue()
	}

	return self.toVal(r)
}

func (self *JSEthereum) toVal(v interface{}) otto.Value {
	result, err := self.vm.ToValue(v)

	if err != nil {
92
		fmt.Println("Value unknown:", err)
obscuren's avatar
obscuren committed
93 94 95 96 97 98

		return otto.UndefinedValue()
	}

	return result
}
99 100

func (self *JSEthereum) Messages(object map[string]interface{}) otto.Value {
101
	filter := ui.NewFilterFromMap(object, self.ethereum)
102

obscuren's avatar
obscuren committed
103 104 105 106
	logs := filter.Find()
	var jslogs []JSLog
	for _, m := range logs {
		jslogs = append(jslogs, NewJSLog(m))
107 108
	}

obscuren's avatar
obscuren committed
109
	v, _ := self.vm.ToValue(jslogs)
110 111 112

	return v
}