diff --git a/native_contract.go b/native_contract.go index 570c356..a586e65 100644 --- a/native_contract.go +++ b/native_contract.go @@ -29,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" @@ -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..cf2e9c8 100644 --- a/native_contract_test.go +++ b/native_contract_test.go @@ -18,9 +18,13 @@ package ontology_go_sdk import ( + "encoding/json" "fmt" "testing" "time" + + "github.com/ontio/ontology/common" + "github.com/stretchr/testify/assert" ) func TestOntId(t *testing.T) { @@ -63,3 +67,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) +}