Skip to content

Commit 43a590e

Browse files
authored
Merge pull request #60 from moviepass/fix/missing-receiving-object-argument
Fix/missing receiving object argument
2 parents d831ccf + fce06fa commit 43a590e

4 files changed

Lines changed: 117 additions & 6 deletions

File tree

Sources/SuiKit/Types/Enums/Sui/Inputs/ObjectArg.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public enum ObjectArg: KeyProtocol {
3434

3535
/// Represents a shared object argument.
3636
case shared(SharedObjectArg)
37+
38+
case receiving(ReceivingObject)
3739

3840
/// A static function to create an `ObjectArg` instance from a JSON object.
3941
///
@@ -46,6 +48,8 @@ public enum ObjectArg: KeyProtocol {
4648
case "sharedObject":
4749
guard let shared = SharedObjectArg(input: input) else { return nil }
4850
return .shared(shared)
51+
case "receiving":
52+
return .receiving(ReceivingObject(input: input))
4953
default:
5054
return nil
5155
}
@@ -63,6 +67,9 @@ public enum ObjectArg: KeyProtocol {
6367
case .shared(let sharedArg):
6468
try Serializer.u8(serializer, UInt8(1))
6569
try Serializer._struct(serializer, value: sharedArg)
70+
case .receiving(let receiving):
71+
try Serializer.u8(serializer, UInt8(2))
72+
try Serializer._struct(serializer, value: receiving)
6673
}
6774
}
6875

@@ -83,6 +90,11 @@ public enum ObjectArg: KeyProtocol {
8390
return ObjectArg.shared(
8491
try Deserializer._struct(deserializer)
8592
)
93+
case 2:
94+
return ObjectArg.receiving(
95+
try Deserializer._struct(deserializer)
96+
)
97+
8698
default:
8799
throw SuiError.customError(message: "Unable to Deserialize")
88100
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// ReceivingObject.swift
3+
// SuiKit
4+
//
5+
// Copyright (c) 2024-2025 OpenDive
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in
15+
// all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
// THE SOFTWARE.
24+
//
25+
26+
import Foundation
27+
import SwiftyJSON
28+
29+
/// A structure representing a reference to an object that is either immutable or owned.
30+
/// Conforming to `KeyProtocol` makes this struct usable wherever object keys are needed.
31+
public struct ReceivingObject: KeyProtocol {
32+
/// A reference to a SuiObject.
33+
public var ref: SuiObjectRef
34+
35+
/// Initializes a new instance of `ReceivingObject` with the provided `SuiObjectRef`.
36+
///
37+
/// - Parameter ref: A reference to a `SuiObject`.
38+
public init(ref: SuiObjectRef) {
39+
self.ref = ref
40+
}
41+
42+
/// Initializes a new instance of `ReceivingObject` with the provided JSON object.
43+
///
44+
/// - Parameter input: A JSON object containing the data needed to initialize the `SuiObjectRef`.
45+
public init(input: JSON) {
46+
self.ref = SuiObjectRef(input: input)
47+
}
48+
49+
public func serialize(_ serializer: Serializer) throws {
50+
try Serializer._struct(serializer, value: self.ref)
51+
}
52+
53+
public static func deserialize(from deserializer: Deserializer) throws -> ReceivingObject {
54+
return ReceivingObject(
55+
ref: try Deserializer._struct(deserializer)
56+
)
57+
}
58+
}

Sources/SuiKit/Types/Structs/Builders/Transactions/Inputs/Inputs.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ public struct Inputs {
6060
)
6161
return .immOrOwned(immOrOwned)
6262
}
63+
64+
/// Creates an `ObjectArg` of type `.receiving` from the provided `SuiObjectRef`.
65+
///
66+
/// - Parameter suiObjectRef: The `SuiObjectRef` used to initialize the `ObjectArg`.
67+
/// - Throws: If unable to normalize the SUI address contained in `suiObjectRef`.
68+
/// - Returns: An `ObjectArg` object initialized with the `.receiving` variant.
69+
public static func receivingRef(suiObjectRef: SuiObjectRef) throws -> ObjectArg {
70+
let receiving = ReceivingObject(
71+
ref: SuiObjectRef(
72+
objectId: try normalizeSuiAddress(value: suiObjectRef.objectId),
73+
version: suiObjectRef.version,
74+
digest: suiObjectRef.digest
75+
)
76+
)
77+
return .receiving(receiving)
78+
}
6379

6480
/// Creates an `ObjectArg` of type `.shared` from the provided `SharedObjectRef`.
6581
///
@@ -100,6 +116,10 @@ public struct Inputs {
100116
if case .shared(let shared) = arg.object {
101117
return try normalizeSuiAddress(value: shared.objectId)
102118
}
119+
120+
if case .receiving(let receiving) = arg.object {
121+
return try normalizeSuiAddress(value: receiving.ref.objectId)
122+
}
103123

104124
throw SuiError.notImplemented
105125
}
@@ -115,6 +135,8 @@ public struct Inputs {
115135
return try normalizeSuiAddress(value: immOwned.ref.objectId)
116136
case .shared(let shared):
117137
return try normalizeSuiAddress(value: shared.objectId)
138+
case .receiving(let receiving):
139+
return try normalizeSuiAddress(value: receiving.ref.objectId)
118140
}
119141
}
120142

Sources/SuiKit/Types/Structs/Builders/Transactions/TransactionBlock/TransactionBlock.swift

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -753,15 +753,34 @@ public class TransactionBlock {
753753
// Update the input value in the objectsToResolve array based on the initial shared version or object reference
754754
guard let initialSharedVersion = object.getSharedObjectInitialVersion() else {
755755
guard let objRef = object.getObjectReference() else { continue }
756-
objectToResolve.input.value = .callArg(
757-
Input(
758-
type: .object(
759-
.immOrOwned(
760-
ImmOrOwned(ref: objRef)
756+
guard let objectData = object.data else { continue }
757+
758+
let isReceiving = try
759+
objectToResolve.normalizedType?.extractStructTag()?.address.hex() == Inputs.normalizeSuiAddress(value: "0x2") &&
760+
objectToResolve.normalizedType?.extractStructTag()?.module == "transfer" &&
761+
objectToResolve.normalizedType?.extractStructTag()?.name == "Receiving"
762+
763+
if(isReceiving) {
764+
objectToResolve.input.value = .callArg(
765+
Input(
766+
type: .object(
767+
.receiving(
768+
ReceivingObject(ref: objRef)
769+
)
761770
)
762771
)
763772
)
764-
)
773+
} else {
774+
objectToResolve.input.value = .callArg(
775+
Input(
776+
type: .object(
777+
.immOrOwned(
778+
ImmOrOwned(ref: objRef)
779+
)
780+
)
781+
)
782+
)
783+
}
765784
if resolvedObjects.count > Int(objectToResolve.input.index) {
766785
resolvedObjects[Int(objectToResolve.input.index)] = objectToResolve
767786
} else {

0 commit comments

Comments
 (0)