Skip to content

Commit

Permalink
feat: Updating TransactionId for scheduled transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
andrix10 committed Mar 3, 2021
1 parent eac0b8c commit 6488224
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions transaction_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
type TransactionID struct {
AccountID AccountID
ValidStart time.Time
Nonce []byte
scheduled bool
}

// NewTransactionID constructs a new Transaction id struct with the provided AccountID and the valid start time set
Expand All @@ -21,13 +23,17 @@ func NewTransactionID(accountID AccountID) TransactionID {
allowance := -(time.Duration(rand.Intn(5*int(time.Second))) + (8 * time.Second))
validStart := time.Now().UTC().Add(allowance)

return TransactionID{accountID, validStart}
return TransactionID{accountID, validStart, nil, false}
}

// NewTransactionIDWithValidStart constructs a new Transaction id struct with the provided AccountID and the valid start
// time set to a provided time.
func NewTransactionIDWithValidStart(accountID AccountID, validStart time.Time) TransactionID {
return TransactionID{accountID, validStart}
return TransactionID{accountID, validStart, nil, false}
}

func NewTransactionIDWithNonce(byte []byte) TransactionID {
return TransactionID{AccountID{}, time.Time{}, byte, false}
}

// GetReceipt queries the network for a receipt corresponding to the TransactionID's transaction. If the status of the
Expand Down Expand Up @@ -66,19 +72,29 @@ func (id TransactionID) GetRecord(client *Client) (TransactionRecord, error) {
// String returns a string representation of the TransactionID in `AccountID@ValidStartSeconds.ValidStartNanos` format
func (id TransactionID) String() string {
pb := timeToProto(id.ValidStart)

return fmt.Sprintf("%v@%v.%v", id.AccountID, pb.Seconds, pb.Nanos)
}

func (id TransactionID) toProto() *proto.TransactionID {
return &proto.TransactionID{
TransactionValidStart: timeToProto(id.ValidStart),
AccountID: id.AccountID.toProto(),
Nonce: id.Nonce,
Scheduled: id.scheduled,
}
}

func transactionIDFromProto(pb *proto.TransactionID) TransactionID {
validStart := timeFromProto(pb.TransactionValidStart)
accountID := accountIDFromProto(pb.AccountID)
var validStart time.Time
if pb.TransactionValidStart != nil {
validStart = timeFromProto(pb.TransactionValidStart)
}

var accountID AccountID
if pb.AccountID != nil {
accountID = accountIDFromProto(pb.AccountID)
}

return TransactionID{accountID, validStart}
return TransactionID{accountID, validStart, pb.Nonce, pb.Scheduled}
}

0 comments on commit 6488224

Please sign in to comment.