This commit is contained in:
RJ 2016-12-21 23:37:33 +00:00 committed by GitHub
commit c8c884cad1
3 changed files with 19 additions and 9 deletions

View file

@ -348,6 +348,7 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
Type string Type string
Name string Name string
Constant bool Constant bool
Payable bool
Indexed bool Indexed bool
Inputs []Argument Inputs []Argument
Outputs []Argument Outputs []Argument
@ -364,12 +365,14 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
case "constructor": case "constructor":
abi.Constructor = Method{ abi.Constructor = Method{
Inputs: field.Inputs, Inputs: field.Inputs,
Payable: field.Payable,
} }
// empty defaults to function according to the abi spec // empty defaults to function according to the abi spec
case "function", "": case "function", "":
abi.Methods[field.Name] = Method{ abi.Methods[field.Name] = Method{
Name: field.Name, Name: field.Name,
Const: field.Constant, Const: field.Constant,
Payable: field.Payable,
Inputs: field.Inputs, Inputs: field.Inputs,
Outputs: field.Outputs, Outputs: field.Outputs,
} }

View file

@ -425,8 +425,8 @@ const jsondata = `
const jsondata2 = ` const jsondata2 = `
[ [
{ "type" : "function", "name" : "balance", "constant" : true }, { "type" : "function", "name" : "balance", "constant" : true, "payable" : false},
{ "type" : "function", "name" : "send", "constant" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }, { "type" : "function", "name" : "send", "constant" : false, "payable": true, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] },
{ "type" : "function", "name" : "test", "constant" : false, "inputs" : [ { "name" : "number", "type" : "uint32" } ] }, { "type" : "function", "name" : "test", "constant" : false, "inputs" : [ { "name" : "number", "type" : "uint32" } ] },
{ "type" : "function", "name" : "string", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "string" } ] }, { "type" : "function", "name" : "string", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "string" } ] },
{ "type" : "function", "name" : "bool", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "bool" } ] }, { "type" : "function", "name" : "bool", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "bool" } ] },
@ -446,10 +446,10 @@ func TestReader(t *testing.T) {
exp := ABI{ exp := ABI{
Methods: map[string]Method{ Methods: map[string]Method{
"balance": Method{ "balance": Method{
"balance", true, nil, nil, "balance", true, false, nil, nil,
}, },
"send": Method{ "send": Method{
"send", false, []Argument{ "send", false, true, []Argument{
Argument{"amount", Uint256, false}, Argument{"amount", Uint256, false},
}, nil, }, nil,
}, },
@ -549,7 +549,7 @@ func TestTestSlice(t *testing.T) {
func TestMethodSignature(t *testing.T) { func TestMethodSignature(t *testing.T) {
String, _ := NewType("string") String, _ := NewType("string")
m := Method{"foo", false, []Argument{Argument{"bar", String, false}, Argument{"baz", String, false}}, nil} m := Method{"foo", false, false, []Argument{Argument{"bar", String, false}, Argument{"baz", String, false}}, nil}
exp := "foo(string,string)" exp := "foo(string,string)"
if m.Sig() != exp { if m.Sig() != exp {
t.Error("signature mismatch", exp, "!=", m.Sig()) t.Error("signature mismatch", exp, "!=", m.Sig())
@ -561,7 +561,7 @@ func TestMethodSignature(t *testing.T) {
} }
uintt, _ := NewType("uint") uintt, _ := NewType("uint")
m = Method{"foo", false, []Argument{Argument{"bar", uintt, false}}, nil} m = Method{"foo", false, false, []Argument{Argument{"bar", uintt, false}}, nil}
exp = "foo(uint256)" exp = "foo(uint256)"
if m.Sig() != exp { if m.Sig() != exp {
t.Error("signature mismatch", exp, "!=", m.Sig()) t.Error("signature mismatch", exp, "!=", m.Sig())

View file

@ -31,10 +31,14 @@ import (
// from the storage and therefor requires no Tx to be send to the // from the storage and therefor requires no Tx to be send to the
// network. A method such as `Transact` does require a Tx and thus will // network. A method such as `Transact` does require a Tx and thus will
// be flagged `true`. // be flagged `true`.
// If the method is `Payable` then ether can be sent along to the contract
// with the method call. In the event that it is not `Payable` and ether
// is sent along with the call, the method will throw and not execute the call.
// Input specifies the required input parameters for this gives method. // Input specifies the required input parameters for this gives method.
type Method struct { type Method struct {
Name string Name string
Const bool Const bool
Payable bool
Inputs []Argument Inputs []Argument
Outputs []Argument Outputs []Argument
} }
@ -106,11 +110,14 @@ func (m Method) String() string {
} }
outputs[i] += output.Type.String() outputs[i] += output.Type.String()
} }
constant := "" constant, payable := "", ""
if m.Const { if m.Const {
constant = "constant " constant = "constant "
} }
return fmt.Sprintf("function %v(%v) %sreturns(%v)", m.Name, strings.Join(inputs, ", "), constant, strings.Join(outputs, ", ")) if m.Payable {
payable = "payable "
}
return fmt.Sprintf("function %v(%v)%s %sreturns(%v)", m.Name, strings.Join(inputs, ", "), constant, payable, strings.Join(outputs, ", "))
} }
func (m Method) Id() []byte { func (m Method) Id() []byte {