From 770b1657b15c9ce746f1d853af863f2d0e265115 Mon Sep 17 00:00:00 2001 From: miancan Date: Fri, 31 May 2024 10:13:12 +0800 Subject: [PATCH 1/5] fix: update ObjectChange owner type --- models/read_transaction.go | 2 +- sui/read_transaction_api.go | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/models/read_transaction.go b/models/read_transaction.go index a756fec..934eed5 100644 --- a/models/read_transaction.go +++ b/models/read_transaction.go @@ -189,7 +189,7 @@ type SuiTransactionBlockResponse struct { type ObjectChange struct { Type string `json:"type"` Sender string `json:"sender"` - Owner ObjectOwner `json:"owner"` + Owner interface{} `json:"owner"` ObjectType string `json:"objectType"` ObjectId string `json:"objectId"` PackageId string `json:"packageId"` diff --git a/sui/read_transaction_api.go b/sui/read_transaction_api.go index 0205405..861823e 100644 --- a/sui/read_transaction_api.go +++ b/sui/read_transaction_api.go @@ -55,10 +55,26 @@ func (s *suiReadTransactionFromSuiImpl) SuiGetTransactionBlock(ctx context.Conte if gjson.ParseBytes(respBytes).Get("error").Exists() { return rsp, errors.New(gjson.ParseBytes(respBytes).Get("error").String()) } - err = json.Unmarshal([]byte(gjson.ParseBytes(respBytes).Get("result").Raw), &rsp) + raws := gjson.ParseBytes(respBytes).Get("result") + err = json.Unmarshal([]byte(raws.Raw), &rsp) if err != nil { return rsp, err } + objectChanges := raws.Get("objectChanges").Array() + if len(objectChanges) != len(rsp.ObjectChanges) { + return rsp, nil + } + for index, item := range objectChanges { + owner := item.Get("owner") + if owner.IsObject() { + var ownerObject models.ObjectOwner + err = json.Unmarshal([]byte(owner.Raw), &ownerObject) + if err != nil { + return rsp, err + } + rsp.ObjectChanges[index].Owner = ownerObject + } + } return rsp, nil } From 60944260c1cdba0fa646f604218cb29ea6a4f74b Mon Sep 17 00:00:00 2001 From: miancan Date: Fri, 31 May 2024 11:16:15 +0800 Subject: [PATCH 2/5] Revert "fix: update ObjectChange owner type" This reverts commit 770b1657b15c9ce746f1d853af863f2d0e265115. --- models/read_transaction.go | 2 +- sui/read_transaction_api.go | 18 +----------------- 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/models/read_transaction.go b/models/read_transaction.go index aceac33..eb3fcb4 100644 --- a/models/read_transaction.go +++ b/models/read_transaction.go @@ -189,7 +189,7 @@ type SuiTransactionBlockResponse struct { type ObjectChange struct { Type string `json:"type"` Sender string `json:"sender"` - Owner interface{} `json:"owner"` + Owner ObjectOwner `json:"owner"` ObjectType string `json:"objectType"` ObjectId string `json:"objectId"` PackageId string `json:"packageId"` diff --git a/sui/read_transaction_api.go b/sui/read_transaction_api.go index 861823e..0205405 100644 --- a/sui/read_transaction_api.go +++ b/sui/read_transaction_api.go @@ -55,26 +55,10 @@ func (s *suiReadTransactionFromSuiImpl) SuiGetTransactionBlock(ctx context.Conte if gjson.ParseBytes(respBytes).Get("error").Exists() { return rsp, errors.New(gjson.ParseBytes(respBytes).Get("error").String()) } - raws := gjson.ParseBytes(respBytes).Get("result") - err = json.Unmarshal([]byte(raws.Raw), &rsp) + err = json.Unmarshal([]byte(gjson.ParseBytes(respBytes).Get("result").Raw), &rsp) if err != nil { return rsp, err } - objectChanges := raws.Get("objectChanges").Array() - if len(objectChanges) != len(rsp.ObjectChanges) { - return rsp, nil - } - for index, item := range objectChanges { - owner := item.Get("owner") - if owner.IsObject() { - var ownerObject models.ObjectOwner - err = json.Unmarshal([]byte(owner.Raw), &ownerObject) - if err != nil { - return rsp, err - } - rsp.ObjectChanges[index].Owner = ownerObject - } - } return rsp, nil } From 72c09006525c2deff628cdd3407c8c9b28e3f118 Mon Sep 17 00:00:00 2001 From: miancan Date: Fri, 31 May 2024 11:16:48 +0800 Subject: [PATCH 3/5] fix: update ObjectChange owner type --- models/read_transaction.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/read_transaction.go b/models/read_transaction.go index eb3fcb4..aceac33 100644 --- a/models/read_transaction.go +++ b/models/read_transaction.go @@ -189,7 +189,7 @@ type SuiTransactionBlockResponse struct { type ObjectChange struct { Type string `json:"type"` Sender string `json:"sender"` - Owner ObjectOwner `json:"owner"` + Owner interface{} `json:"owner"` ObjectType string `json:"objectType"` ObjectId string `json:"objectId"` PackageId string `json:"packageId"` From 50509636eaa98d55dbbe122e0b8b5b5324754903 Mon Sep 17 00:00:00 2001 From: miancan Date: Fri, 31 May 2024 11:22:25 +0800 Subject: [PATCH 4/5] fix: update ObjectChange owner type --- models/read_transaction.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/read_transaction.go b/models/read_transaction.go index aceac33..cbe7331 100644 --- a/models/read_transaction.go +++ b/models/read_transaction.go @@ -189,7 +189,7 @@ type SuiTransactionBlockResponse struct { type ObjectChange struct { Type string `json:"type"` Sender string `json:"sender"` - Owner interface{} `json:"owner"` + Owner interface{} `json:"owner"` //sometime string,sometime object ObjectType string `json:"objectType"` ObjectId string `json:"objectId"` PackageId string `json:"packageId"` From d51dbdb85d2ba18498165ec09a3c2063dd8f7019 Mon Sep 17 00:00:00 2001 From: miancan Date: Fri, 31 May 2024 15:23:13 +0800 Subject: [PATCH 5/5] fix: rewrite ObjectOwner UnmarshalJSON --- models/read_object.go | 23 ++++++++++++++++++++++- models/read_transaction.go | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/models/read_object.go b/models/read_object.go index ac9cea3..9feea0f 100644 --- a/models/read_object.go +++ b/models/read_object.go @@ -89,7 +89,28 @@ type SuiObjectResponseError struct { Digest string `json:"digest"` } -type ObjectOwner struct { +func (o *ObjectOwner) UnmarshalJSON(data []byte) error { + res := gjson.ParseBytes(data) + if !res.IsObject() { + o.ObjectType = res.String() + return nil + } + o.AddressOwner = res.Get("AddressOwner").String() + o.ObjectOwner = res.Get("ObjectOwner").String() + shard := res.Get("Shared").String() + if shard != "" { + var objectShare ObjectShare + err := json.Unmarshal([]byte(shard), &objectShare) + if err != nil { + return err + } + o.Shared = objectShare + } + return nil +} + +type ObjectOwner struct { //This struct has its own UnmarshalJSON method !!! + ObjectType string `json:"ObjectType"` //if Immutable return Immutable // the owner's Sui address AddressOwner string `json:"AddressOwner"` ObjectOwner string `json:"ObjectOwner"` diff --git a/models/read_transaction.go b/models/read_transaction.go index cbe7331..eb3fcb4 100644 --- a/models/read_transaction.go +++ b/models/read_transaction.go @@ -189,7 +189,7 @@ type SuiTransactionBlockResponse struct { type ObjectChange struct { Type string `json:"type"` Sender string `json:"sender"` - Owner interface{} `json:"owner"` //sometime string,sometime object + Owner ObjectOwner `json:"owner"` ObjectType string `json:"objectType"` ObjectId string `json:"objectId"` PackageId string `json:"packageId"`