mirror of https://github.com/status-im/op-geth.git
commit
c27c2be584
|
@ -0,0 +1,65 @@
|
|||
package natspec
|
||||
|
||||
import (
|
||||
"github.com/obscuren/otto"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
type NatSpec struct {
|
||||
jsvm *otto.Otto
|
||||
}
|
||||
|
||||
func NewNATSpec(transaction string) (self *NatSpec, err error) {
|
||||
|
||||
self = new(NatSpec)
|
||||
self.jsvm = otto.New()
|
||||
code, err := ioutil.ReadFile("natspec.js")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = self.jsvm.Run(string(code))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = self.jsvm.Run("var natspec = require('natspec');")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
self.jsvm.Run("var transaction = " + transaction + ";")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (self *NatSpec) SetDescription(desc string) (err error) {
|
||||
|
||||
_, err = self.jsvm.Run("var expression = \"" + desc + "\";")
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
func (self *NatSpec) SetABI(abi string) (err error) {
|
||||
|
||||
_, err = self.jsvm.Run("var abi = " + abi + ";")
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
func (self *NatSpec) SetMethod(method string) (err error) {
|
||||
|
||||
_, err = self.jsvm.Run("var method = '" + method + "';")
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
func (self *NatSpec) Parse() string {
|
||||
|
||||
self.jsvm.Run("var call = {method: method,abi: abi,transaction: transaction};")
|
||||
value, err := self.jsvm.Run("natspec.evaluateExpression(expression, call);")
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
return value.String()
|
||||
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,51 @@
|
|||
package natspec
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNotice(t *testing.T) {
|
||||
|
||||
ns, err := NewNATSpec(`
|
||||
{
|
||||
"jsonrpc": "2.0",
|
||||
"method": "eth_call",
|
||||
"params": [{
|
||||
"to": "0x8521742d3f456bd237e312d6e30724960f72517a",
|
||||
"data": "0xc6888fa1000000000000000000000000000000000000000000000000000000000000007a"
|
||||
}],
|
||||
"id": 6
|
||||
}
|
||||
`)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("NewNATSpec error %v", err)
|
||||
}
|
||||
|
||||
ns.SetABI(`
|
||||
[{
|
||||
"name": "multiply",
|
||||
"constant": false,
|
||||
"type": "function",
|
||||
"inputs": [{
|
||||
"name": "a",
|
||||
"type": "uint256"
|
||||
}],
|
||||
"outputs": [{
|
||||
"name": "d",
|
||||
"type": "uint256"
|
||||
}]
|
||||
}]
|
||||
`)
|
||||
ns.SetDescription("Will multiply `a` by 7 and return `a * 7`.")
|
||||
ns.SetMethod("multiply")
|
||||
|
||||
notice := ns.Parse()
|
||||
|
||||
expected := "Will multiply 122 by 7 and return 854."
|
||||
if notice != expected {
|
||||
t.Errorf("incorrect notice. expected %v, got %v", expected, notice)
|
||||
} else {
|
||||
t.Logf("returned notice \"%v\"", notice)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue