Skip to content

Commit 6e645bb

Browse files
committed
add GetNodeV3BillingOptOutTimestamp to also retrieve the opt-out time
1 parent 562e1b6 commit 6e645bb

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

clients/tfchain-client-go/node.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -857,28 +857,48 @@ func (s *Substrate) GetAllowedTwinAdmins() ([]AccountID, error) {
857857
}
858858

859859
// IsNodeOptedOutOfV3Billing returns true if the node has opted out of v3 billing.
860+
// Deprecated: use GetNodeV3BillingOptOutTimestamp to also retrieve the opt-out time.
860861
func (s *Substrate) IsNodeOptedOutOfV3Billing(nodeID uint32) (bool, error) {
861-
cl, meta, err := s.GetClient()
862+
ts, err := s.GetNodeV3BillingOptOutTimestamp(nodeID)
862863
if err != nil {
863864
return false, err
864865
}
866+
return ts != nil, nil
867+
}
868+
869+
// GetNodeV3BillingOptOutTimestamp returns the Unix timestamp (seconds) at which the node
870+
// opted out of v3 billing, or nil if the node has not opted out.
871+
func (s *Substrate) GetNodeV3BillingOptOutTimestamp(nodeID uint32) (*uint64, error) {
872+
cl, meta, err := s.GetClient()
873+
if err != nil {
874+
return nil, err
875+
}
865876

866877
bytes, err := Encode(nodeID)
867878
if err != nil {
868-
return false, errors.Wrap(err, "substrate: encoding error building query arguments")
879+
return nil, errors.Wrap(err, "substrate: encoding error building query arguments")
869880
}
870881

871882
key, err := types.CreateStorageKey(meta, "TfgridModule", "NodeV3BillingOptOut", bytes)
872883
if err != nil {
873-
return false, errors.Wrap(err, "failed to create substrate query key")
884+
return nil, errors.Wrap(err, "failed to create substrate query key")
874885
}
875886

876887
raw, err := cl.RPC.State.GetStorageRawLatest(key)
877888
if err != nil {
878-
return false, errors.Wrap(err, "failed to lookup node v3 billing opt-out")
889+
return nil, errors.Wrap(err, "failed to lookup node v3 billing opt-out")
890+
}
891+
892+
if len(*raw) == 0 {
893+
return nil, nil
894+
}
895+
896+
var ts uint64
897+
if err := Decode(*raw, &ts); err != nil {
898+
return nil, errors.Wrap(err, "failed to decode node v3 billing opt-out timestamp")
879899
}
880900

881-
return len(*raw) > 0, nil
901+
return &ts, nil
882902
}
883903

884904
// SetNodeCertificate sets the node certificate type

clients/tfchain-client-js/lib/client.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const {
1919
const {
2020
createNode, updateNode, getNode,
2121
getNodeIDByPubkey, deleteNode, listNodes,
22-
optOutOfV3Billing, getAllowedTwinAdmins, isNodeOptedOutOfV3Billing
22+
optOutOfV3Billing, getAllowedTwinAdmins, isNodeOptedOutOfV3Billing, getNodeV3BillingOptOutTimestamp
2323
} = require('./node')
2424
const { signEntityTwinID, signEntityCreation } = require('./sign')
2525
const { getBalance, transfer } = require('./balance')
@@ -222,6 +222,10 @@ class Client {
222222
return isNodeOptedOutOfV3Billing(this, nodeID)
223223
}
224224

225+
async getNodeV3BillingOptOutTimestamp(nodeID) {
226+
return getNodeV3BillingOptOutTimestamp(this, nodeID)
227+
}
228+
225229
async setNodePower (nodeId, power, callback) {
226230
return setNodePower(this, nodeId, power, callback)
227231
}

clients/tfchain-client-js/lib/node.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,18 @@ async function getAllowedTwinAdmins (self) {
140140
return result.toJSON() || []
141141
}
142142

143-
// isNodeOptedOutOfV3Billing returns true if the node has opted out of v3 billing
143+
// isNodeOptedOutOfV3Billing returns true if the node has opted out of v3 billing.
144+
// Deprecated: use getNodeV3BillingOptOutTimestamp to also retrieve the opt-out time.
144145
async function isNodeOptedOutOfV3Billing (self, nodeID) {
146+
return (await getNodeV3BillingOptOutTimestamp(self, nodeID)) !== null
147+
}
148+
149+
// getNodeV3BillingOptOutTimestamp returns the Unix timestamp (seconds) at which the node
150+
// opted out of v3 billing, or null if the node has not opted out.
151+
async function getNodeV3BillingOptOutTimestamp (self, nodeID) {
145152
const result = await self.api.query.tfgridModule.nodeV3BillingOptOut(nodeID)
146-
return !result.isNone
153+
if (result.isNone) return null
154+
return result.unwrap().toNumber()
147155
}
148156

149157
async function validateNode (self, farmID) {
@@ -162,5 +170,6 @@ module.exports = {
162170
listNodes,
163171
optOutOfV3Billing,
164172
getAllowedTwinAdmins,
165-
isNodeOptedOutOfV3Billing
173+
isNodeOptedOutOfV3Billing,
174+
getNodeV3BillingOptOutTimestamp
166175
}

0 commit comments

Comments
 (0)