From 2b811b4a1892707057e879e8208678a996cb2133 Mon Sep 17 00:00:00 2001 From: VoR0220 Date: Thu, 8 Dec 2016 15:10:55 -0600 Subject: [PATCH 1/2] accounts/abi: added ability for abi to keep note of the payable field Signed-off-by: VoR0220 --- accounts/abi/abi.go | 5 ++++- accounts/abi/abi_test.go | 12 ++++++------ accounts/abi/method.go | 11 +++++++++-- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/accounts/abi/abi.go b/accounts/abi/abi.go index 2efac1307d..7c69a3a26f 100644 --- a/accounts/abi/abi.go +++ b/accounts/abi/abi.go @@ -348,6 +348,7 @@ func (abi *ABI) UnmarshalJSON(data []byte) error { Type string Name string Constant bool + Payable bool Indexed bool Inputs []Argument Outputs []Argument @@ -363,13 +364,15 @@ func (abi *ABI) UnmarshalJSON(data []byte) error { switch field.Type { case "constructor": abi.Constructor = Method{ - Inputs: field.Inputs, + Inputs: field.Inputs, + Payable: field.Payable, } // empty defaults to function according to the abi spec case "function", "": abi.Methods[field.Name] = Method{ Name: field.Name, Const: field.Constant, + Payable: field.Payable, Inputs: field.Inputs, Outputs: field.Outputs, } diff --git a/accounts/abi/abi_test.go b/accounts/abi/abi_test.go index 85e25f9ea8..099411b3ed 100644 --- a/accounts/abi/abi_test.go +++ b/accounts/abi/abi_test.go @@ -425,8 +425,8 @@ const jsondata = ` const jsondata2 = ` [ - { "type" : "function", "name" : "balance", "constant" : true }, - { "type" : "function", "name" : "send", "constant" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }, + { "type" : "function", "name" : "balance", "constant" : true, "payable" : false}, + { "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" : "string", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "string" } ] }, { "type" : "function", "name" : "bool", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "bool" } ] }, @@ -446,10 +446,10 @@ func TestReader(t *testing.T) { exp := ABI{ Methods: map[string]Method{ "balance": Method{ - "balance", true, nil, nil, + "balance", true, false, nil, nil, }, "send": Method{ - "send", false, []Argument{ + "send", false, true, []Argument{ Argument{"amount", Uint256, false}, }, nil, }, @@ -549,7 +549,7 @@ func TestTestSlice(t *testing.T) { func TestMethodSignature(t *testing.T) { 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)" if m.Sig() != exp { t.Error("signature mismatch", exp, "!=", m.Sig()) @@ -561,7 +561,7 @@ func TestMethodSignature(t *testing.T) { } 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)" if m.Sig() != exp { t.Error("signature mismatch", exp, "!=", m.Sig()) diff --git a/accounts/abi/method.go b/accounts/abi/method.go index d56f3bc3dd..b78c86f0d2 100644 --- a/accounts/abi/method.go +++ b/accounts/abi/method.go @@ -31,10 +31,14 @@ import ( // 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 // 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. type Method struct { Name string Const bool + Payable bool Inputs []Argument Outputs []Argument } @@ -106,11 +110,14 @@ func (m Method) String() string { } outputs[i] += output.Type.String() } - constant := "" + constant, payable := "", "" if m.Const { 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) %sreturns(%v)", m.Name, strings.Join(inputs, ", "), constant, payable, strings.Join(outputs, ", ")) } func (m Method) Id() []byte { From 1582a2e4853374eb080354b8a2b471099e91b891 Mon Sep 17 00:00:00 2001 From: VoR0220 Date: Thu, 8 Dec 2016 15:18:49 -0600 Subject: [PATCH 2/2] accounts/abi: needed to add a field for sprintf Signed-off-by: VoR0220 --- accounts/abi/method.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/accounts/abi/method.go b/accounts/abi/method.go index b78c86f0d2..8e9c48aea6 100644 --- a/accounts/abi/method.go +++ b/accounts/abi/method.go @@ -117,7 +117,7 @@ func (m Method) String() string { if m.Payable { payable = "payable " } - return fmt.Sprintf("function %v(%v) %sreturns(%v)", m.Name, strings.Join(inputs, ", "), constant, payable, strings.Join(outputs, ", ")) + return fmt.Sprintf("function %v(%v)%s %sreturns(%v)", m.Name, strings.Join(inputs, ", "), constant, payable, strings.Join(outputs, ", ")) } func (m Method) Id() []byte {