From a8f369c09146d495c10973ede9d1c06b012d878a Mon Sep 17 00:00:00 2001 From: siovanus Date: Fri, 14 Aug 2020 10:09:14 +0800 Subject: [PATCH] add set peer percentage (#118) * add set peer percentage * fix commit cred bug * go fmt --- client/client.go | 2 +- cred.go | 2 +- go.sum | 2 +- native_contract.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 3 deletions(-) diff --git a/client/client.go b/client/client.go index 4812c6f..9def92c 100644 --- a/client/client.go +++ b/client/client.go @@ -223,7 +223,7 @@ func (this *ClientMgr) GetSmartContractEventByBlock(height uint32) ([]*sdkcom.Sm if err != nil { return nil, err } - if data == nil || string(data) == "" || string(data) == "\"\""{ + if data == nil || string(data) == "" || string(data) == "\"\"" { return nil, nil } return utils.GetSmartContactEvents(data) diff --git a/cred.go b/cred.go index df3c2f9..1126481 100644 --- a/cred.go +++ b/cred.go @@ -259,7 +259,7 @@ func (this *Credential) GetPublicKeyList(ontId string) (PublicKeyList, error) { func (this *Credential) CommitCredential(contractAddress common.Address, gasPrice, gasLimit uint64, credentialId, issuerId, holderId string, signer, payer *Account) (common.Uint256, error) { - index, _, err := this.GetPublicKeyId(holderId, hex.EncodeToString(keypair.SerializePublicKey(signer.GetPublicKey()))) + index, _, err := this.GetPublicKeyId(issuerId, hex.EncodeToString(keypair.SerializePublicKey(signer.GetPublicKey()))) if err != nil { return common.UINT256_EMPTY, fmt.Errorf("CommitCredential, this.GetPublicKeyId error: %s", err) } diff --git a/go.sum b/go.sum index 600e50c..c915513 100644 --- a/go.sum +++ b/go.sum @@ -121,4 +121,4 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= launchpad.net/gocheck v0.0.0-20140225173054-000000000087 h1:Izowp2XBH6Ya6rv+hqbceQyw/gSGoXfH/UPoTGduL54= -launchpad.net/gocheck v0.0.0-20140225173054-000000000087/go.mod h1:hj7XX3B/0A+80Vse0e+BUHsHMTEhd0O4cpUHr/e/BUM= +launchpad.net/gocheck v0.0.0-20140225173054-000000000087/go.mod h1:hj7XX3B/0A+80Vse0e+BUHsHMTEhd0O4cpUHr/e/BUM= \ No newline at end of file diff --git a/native_contract.go b/native_contract.go index fb6bef2..daa12e5 100644 --- a/native_contract.go +++ b/native_contract.go @@ -61,6 +61,7 @@ type NativeContract struct { OntId *OntId GlobalParams *GlobalParam Auth *Auth + Governance *Governance } func newNativeContract(ontSdk *OntologySdk) *NativeContract { @@ -70,6 +71,7 @@ func newNativeContract(ontSdk *OntologySdk) *NativeContract { native.OntId = &OntId{native: native, ontSdk: ontSdk} native.GlobalParams = &GlobalParam{native: native, ontSdk: ontSdk} native.Auth = &Auth{native: native, ontSdk: ontSdk} + native.Governance = &Governance{native: native, ontSdk: ontSdk} return native } @@ -2878,3 +2880,52 @@ func (this *Auth) VerifyToken(gasPrice, gasLimit uint64, payer, signer *Account, } return this.ontSdk.SendTransaction(tx) } + +type Governance struct { + ontSdk *OntologySdk + native *NativeContract +} + +type SetFeePercentageParam struct { + PeerPubkey string + Address common.Address + PeerCost uint32 + StakeCost uint32 +} + +func (this *Governance) SetFeePercentageTransaction(gasPrice, gasLimit uint64, peerPubkey string, address common.Address, + peerCost, stakeCost uint32) (*types.MutableTransaction, error) { + params := SetFeePercentageParam{ + PeerPubkey: peerPubkey, + Address: address, + PeerCost: peerCost, + StakeCost: stakeCost, + } + return this.native.NewNativeInvokeTransaction( + gasPrice, + gasLimit, + GOVERNANCE_CONTRACT_VERSION, + GOVERNANCE_CONTRACT_ADDRESS, + "setFeePercentage", + []interface{}{params}) +} + +func (this *Governance) SetFeePercentage(gasPrice, gasLimit uint64, payer, signer *Account, peerPubkey string, + peerCost, stakeCost uint32) (common.Uint256, error) { + tx, err := this.SetFeePercentageTransaction(gasPrice, gasLimit, peerPubkey, signer.Address, peerCost, stakeCost) + if err != nil { + return common.UINT256_EMPTY, err + } + if payer != nil { + this.ontSdk.SetPayer(tx, payer.Address) + err = this.ontSdk.SignToTransaction(tx, payer) + if err != nil { + return common.UINT256_EMPTY, err + } + } + err = this.ontSdk.SignToTransaction(tx, signer) + if err != nil { + return common.UINT256_EMPTY, err + } + return this.ontSdk.SendTransaction(tx) +}