Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update sdk #141

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions native_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
31 changes: 31 additions & 0 deletions native_contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}