Skip to content

Commit

Permalink
feat(TCK): Implement JSON-RPC endpoint for AccountUpdateTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
rwalworth authored Nov 20, 2024
1 parent 53ccd28 commit cfb2cce
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Sources/Hedera/Timestamp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ extension Timestamp: ProtobufCodable {

internal func toProtobuf() -> Protobuf {
.with { proto in
proto.seconds = Int64(seconds)
proto.nanos = Int32(subSecondNanos)
proto.seconds = Int64(truncatingIfNeeded: seconds)
proto.nanos = Int32(truncatingIfNeeded: subSecondNanos)
}
}
}
24 changes: 20 additions & 4 deletions Sources/HederaTCK/JSONHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,30 @@ internal func getJson<T>(_ json: JSONObject, _ paramName: String, _ functionName
}
return val as! T
}
if T.self == Int.self || T.self == Int32.self || T.self == UInt32.self || T.self == Int64.self
|| T.self == UInt64.self
{
if T.self == Int32.self {
guard let val = json.intValue else {
throw JSONError.invalidParams("\(functionName): \(paramName) MUST be an integer.")
throw JSONError.invalidParams("\(functionName): \(paramName) MUST be an int32.")
}
return Int32(truncatingIfNeeded: val) as! T
}
if T.self == UInt32.self {
guard let val = json.intValue else {
throw JSONError.invalidParams("\(functionName): \(paramName) MUST be a uint32.")
}
return UInt32(truncatingIfNeeded: val) as! T
}
if T.self == Int64.self {
guard let val = json.intValue else {
throw JSONError.invalidParams("\(functionName): \(paramName) MUST be an int64.")
}
return val as! T
}
if T.self == UInt64.self {
guard let val = json.intValue else {
throw JSONError.invalidParams("\(functionName): \(paramName) MUST be a uint64.")
}
return UInt64(truncatingIfNeeded: val) as! T
}
if T.self == Double.self || T.self == Float.self {
guard let val = json.doubleValue else {
throw JSONError.invalidParams("\(functionName): \(paramName) MUST be a double.")
Expand Down
75 changes: 74 additions & 1 deletion Sources/HederaTCK/SDKClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ internal class SDKClient {
)
}

let threshold: Int? = try getOptionalJsonParameter("threshold", parameters, "generateKey")
let threshold: Int64? = try getOptionalJsonParameter("threshold", parameters, "generateKey")
if threshold != nil, type != .thresholdKeyType {
throw JSONError.invalidParams(
"generateKey: threshold MUST NOT be provided for types other than thresholdKey.")
Expand Down Expand Up @@ -337,4 +337,77 @@ internal class SDKClient {
])

}

internal func updateAccount(_ parameters: [String: JSONObject]?) async throws -> JSONObject {
var accountUpdateTransaction = AccountUpdateTransaction()

if let params = parameters {
if let accountId: String = try getOptionalJsonParameter("accountId", params, #function) {
accountUpdateTransaction.accountId = try AccountId.fromString(accountId)
}

if let key: String = try getOptionalJsonParameter("key", params, #function) {
accountUpdateTransaction.key = try getHederaKey(key)
}

if let autoRenewPeriod: Int64 = try getOptionalJsonParameter(
"autoRenewPeriod", params, #function)
{
accountUpdateTransaction.autoRenewPeriod = Duration(
seconds: UInt64(truncatingIfNeeded: autoRenewPeriod))
}

if let expirationTime: Int64 = try getOptionalJsonParameter("expirationTime", params, #function) {
accountUpdateTransaction.expirationTime = Timestamp(
seconds: UInt64(truncatingIfNeeded: expirationTime), subSecondNanos: 0)
}

if let receiverSignatureRequired: Bool = try getOptionalJsonParameter(
"receiverSignatureRequired", params, #function)
{
accountUpdateTransaction.receiverSignatureRequired = receiverSignatureRequired
}

if let memo: String = try getOptionalJsonParameter("memo", params, #function) {
accountUpdateTransaction.accountMemo = memo
}

if let maxAutoTokenAssociations: Int32 = try getOptionalJsonParameter(
"maxAutoTokenAssociations", params, #function)
{
accountUpdateTransaction.maxAutomaticTokenAssociations = maxAutoTokenAssociations
}

if let stakedAccountId: String = try getOptionalJsonParameter(
"stakedAccountId", params, #function)
{
accountUpdateTransaction.stakedAccountId = try AccountId.fromString(stakedAccountId)
}

if let stakedNodeId: Int64 = try getOptionalJsonParameter(
"stakedNodeId", params, #function)
{
accountUpdateTransaction.stakedNodeId = UInt64(truncatingIfNeeded: stakedNodeId)
}

if let declineStakingReward: Bool = try getOptionalJsonParameter(
"declineStakingReward", params, #function)
{
accountUpdateTransaction.declineStakingReward = declineStakingReward
}

if let commonTransactionParams: [String: JSONObject] = try getOptionalJsonParameter(
"commonTransactionParams", params, #function)
{
try fillOutCommonTransactionParameters(
&accountUpdateTransaction, params: commonTransactionParams, client: self.client, function: #function
)
}
}

let txReceipt = try await accountUpdateTransaction.execute(client).getReceipt(client)
return JSONObject.dictionary([
"status": JSONObject.string(txReceipt.status.description)
])
}
}
8 changes: 8 additions & 0 deletions Sources/HederaTCK/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ private struct TCKServer {
id: request.id,
result: try sdkClient.setup(getRequiredJsonParameter("params", request.toDict(), request.method)))
///
/// updateAccount
///
case "updateAccount":
return JSONResponse(
id: request.id,
result: try await sdkClient.updateAccount(
getOptionalJsonParameter("params", request.toDict(), request.method)))
///
/// Method Not Found
///
default:
Expand Down

0 comments on commit cfb2cce

Please sign in to comment.