From f0408529b922af571a7f89bf56f3f54f2a7b5bcc Mon Sep 17 00:00:00 2001 From: qiluge Date: Thu, 6 Apr 2023 11:28:47 +0800 Subject: [PATCH 1/2] add new method of gov --- native_contract.go | 60 +++++++++++++++++++++++++++++++++++++++++ native_contract_test.go | 30 +++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/native_contract.go b/native_contract.go index 570c356..365fdda 100644 --- a/native_contract.go +++ b/native_contract.go @@ -19,6 +19,7 @@ package ontology_go_sdk import ( "fmt" + "github.com/ontio/ontology/smartcontract/service/native/governance" "math/big" "github.com/ontio/ontology-crypto/keypair" @@ -3358,3 +3359,62 @@ func (this *Governance) SetFeePercentage(gasPrice, gasLimit uint64, payer, signe } return this.ontSdk.SendTransaction(tx) } + +func (this *Governance) GetCurrentView() (uint32, error) { + res, err := this.ontSdk.Native.PreExecInvokeNativeContract( + GOVERNANCE_CONTRACT_ADDRESS, GOVERNANCE_CONTRACT_VERSION, "getView", []interface{}{}) + if err != nil { + return 0, err + } + data, err := res.Result.ToByteArray() + if err != nil { + return 0, err + } + source := common.NewZeroCopySource(data) + view, eof := source.NextUint32() + if eof { + return 0, fmt.Errorf("eof") + } + return view, nil +} + +type GetAuthorizeInfoParam struct { + Addr common.Address + PubKey string +} + +func (this *Governance) GetAuthorizeInfo(user common.Address, pubkey string) (*governance.AuthorizeInfo, error) { + res, err := this.ontSdk.Native.PreExecInvokeNativeContract( + GOVERNANCE_CONTRACT_ADDRESS, GOVERNANCE_CONTRACT_VERSION, "getAuthorizeInfo", []interface{}{GetAuthorizeInfoParam{ + Addr: user, PubKey: pubkey, + }}) + if err != nil { + return nil, err + } + data, err := res.Result.ToByteArray() + if err != nil { + return nil, err + } + source := common.NewZeroCopySource(data) + info := &governance.AuthorizeInfo{} + err = info.Deserialization(source) + return info, err +} + +func (this *Governance) GetAddressFee(user common.Address) (uint64, error) { + res, err := this.ontSdk.Native.PreExecInvokeNativeContract( + GOVERNANCE_CONTRACT_ADDRESS, GOVERNANCE_CONTRACT_VERSION, "getAddressFee", []interface{}{user}) + if err != nil { + return 0, err + } + data, err := res.Result.ToByteArray() + if err != nil { + return 0, err + } + source := common.NewZeroCopySource(data) + amount, eof := source.NextUint64() + if eof { + return 0, fmt.Errorf("eof") + } + return amount, nil +} diff --git a/native_contract_test.go b/native_contract_test.go index c08b9a5..1367296 100644 --- a/native_contract_test.go +++ b/native_contract_test.go @@ -18,7 +18,10 @@ package ontology_go_sdk import ( + "encoding/json" "fmt" + "github.com/ontio/ontology/common" + "github.com/stretchr/testify/assert" "testing" "time" ) @@ -63,3 +66,30 @@ func TestOntId(t *testing.T) { fmt.Printf("TestOntId GetDocumentJson:%+v\n", string(document)) return } + +func TestGovReadData(t *testing.T) { + testNetUrl = "http://172.16.8.254:20336" + Init() + view, err := testOntSdk.Native.Governance.GetCurrentView() + assert.Nil(t, err) + t.Logf("current view: %d", view) + // consensus node, okex pool + user, err := common.AddressFromBase58("APBX1duPLaQ3ikmMCZixjmNi2B73ARq3w6") + assert.Nil(t, err) + authorizeInfo, err := testOntSdk.Native.Governance.GetAuthorizeInfo(user, "039cadf7145731b3c868bd3528da9172757f89b566fc0372cd51b41351c3b6f237") + assert.Nil(t, err) + data, _ := json.MarshalIndent(authorizeInfo, "", " ") + t.Log(string(data)) + fee, err := testOntSdk.Native.Governance.GetAddressFee(user) + assert.Nil(t, err) + t.Logf("fee: %d", fee) + // candidate node, BeRich + user, _ = common.AddressFromBase58("AHHSvf3Zn2zAhamkYRRYiXa4Ko5GNUrQjv") + authorizeInfo, err = testOntSdk.Native.Governance.GetAuthorizeInfo(user, "03446c4703bb907091eff15def2e1ead72772b70f187d0a0a237ae7d28c196f644") + assert.Nil(t, err) + data, _ = json.MarshalIndent(authorizeInfo, "", " ") + t.Log(string(data)) + fee, err = testOntSdk.Native.Governance.GetAddressFee(user) + assert.Nil(t, err) + t.Logf("fee: %d", fee) +} From a91c3f325b9f01ece2bbf019ed1b73b27f01c5e5 Mon Sep 17 00:00:00 2001 From: qiluge Date: Thu, 6 Apr 2023 11:31:42 +0800 Subject: [PATCH 2/2] reformat code --- native_contract.go | 2 +- native_contract_test.go | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/native_contract.go b/native_contract.go index 365fdda..a586e65 100644 --- a/native_contract.go +++ b/native_contract.go @@ -19,7 +19,6 @@ package ontology_go_sdk import ( "fmt" - "github.com/ontio/ontology/smartcontract/service/native/governance" "math/big" "github.com/ontio/ontology-crypto/keypair" @@ -30,6 +29,7 @@ import ( "github.com/ontio/ontology/core/types" cutils "github.com/ontio/ontology/core/utils" "github.com/ontio/ontology/smartcontract/service/native/global_params" + "github.com/ontio/ontology/smartcontract/service/native/governance" "github.com/ontio/ontology/smartcontract/service/native/ont" "github.com/ontio/ontology/smartcontract/service/native/ontid" nutils "github.com/ontio/ontology/smartcontract/service/native/utils" diff --git a/native_contract_test.go b/native_contract_test.go index 1367296..cf2e9c8 100644 --- a/native_contract_test.go +++ b/native_contract_test.go @@ -20,10 +20,11 @@ package ontology_go_sdk import ( "encoding/json" "fmt" - "github.com/ontio/ontology/common" - "github.com/stretchr/testify/assert" "testing" "time" + + "github.com/ontio/ontology/common" + "github.com/stretchr/testify/assert" ) func TestOntId(t *testing.T) {