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

import (
	"fmt"
5
	"github.com/ethereum/go-ethereum/ethutil"
obscuren's avatar
obscuren committed
6
	"github.com/ethereum/go-ethereum/state"
7
	"github.com/ethereum/go-ethereum/xeth"
8
	"github.com/obscuren/otto"
obscuren's avatar
obscuren committed
9 10
)

11
type JSStateObject struct {
12
	*xeth.Object
13
	eth *JSEthereum
obscuren's avatar
obscuren committed
14 15
}

16 17
func (self *JSStateObject) EachStorage(call otto.FunctionCall) otto.Value {
	cb := call.Argument(0)
obscuren's avatar
obscuren committed
18

19
	it := self.Object.Trie().Iterator()
obscuren's avatar
obscuren committed
20 21 22
	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
23

24
	return otto.UndefinedValue()
obscuren's avatar
obscuren committed
25 26 27 28
}

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

func (self *JSBlock) GetTransaction(hash string) otto.Value {
35
	return self.eth.toVal(self.Block.GetTransaction(hash))
36 37
}

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

obscuren's avatar
obscuren committed
45 46 47 48 49 50
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()),
51 52 53
	}
}

obscuren's avatar
obscuren committed
54
type JSEthereum struct {
55
	*xeth.XEth
obscuren's avatar
obscuren committed
56
	vm *otto.Otto
obscuren's avatar
obscuren committed
57 58
}

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

	return otto.UndefinedValue()
obscuren's avatar
obscuren committed
67 68
}

obscuren's avatar
obscuren committed
69
func (self *JSEthereum) GetStateObject(addr string) otto.Value {
70
	return self.toVal(&JSStateObject{self.XEth.State().SafeGet(addr), self})
71 72
}

obscuren's avatar
obscuren committed
73
func (self *JSEthereum) Transact(key, recipient, valueStr, gasStr, gasPriceStr, dataStr string) otto.Value {
74
	r, err := self.XEth.Transact(recipient, valueStr, gasStr, gasPriceStr, dataStr)
obscuren's avatar
obscuren committed
75 76 77 78 79 80 81 82 83 84 85 86 87
	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 {
88
		fmt.Println("Value unknown:", err)
obscuren's avatar
obscuren committed
89 90 91 92 93 94

		return otto.UndefinedValue()
	}

	return result
}