-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathContractCreateTransactionTests.swift
184 lines (147 loc) · 6.69 KB
/
ContractCreateTransactionTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// SPDX-License-Identifier: Apache-2.0
import HieroProtobufs
import SnapshotTesting
import XCTest
@testable import Hiero
internal class ContractCreateTransactionTests: XCTestCase {
private static let bytecodeFileId: FileId = 3003
private static let adminKey = Key.single(Resources.publicKey)
private static let gas: UInt64 = 0
private static let initialBalance = Hbar.fromTinybars(1000)
private static let maxAutomaticTokenAssociations: Int32 = 101
private static let autoRenewPeriod = Duration.hours(10)
private static let constructorParameters = Data([10, 11, 12, 13, 25])
private static let autoRenewAccountId: AccountId = 30
private static let stakedAccountId: AccountId = 3
private static let stakedNodeId: UInt64 = 4
private static func makeTransaction() throws -> ContractCreateTransaction {
try ContractCreateTransaction()
.nodeAccountIds(Resources.nodeAccountIds)
.transactionId(Resources.txId)
.sign(Resources.privateKey)
.bytecodeFileId(bytecodeFileId)
.adminKey(adminKey)
.gas(gas)
.initialBalance(initialBalance)
.maxAutomaticTokenAssociations(maxAutomaticTokenAssociations)
.autoRenewPeriod(autoRenewPeriod)
.constructorParameters(constructorParameters)
.autoRenewAccountId(autoRenewAccountId)
.stakedAccountId(stakedAccountId)
.maxTransactionFee(.fromTinybars(100_000))
.freeze()
}
private static func makeTransaction2() throws -> ContractCreateTransaction {
try ContractCreateTransaction()
.nodeAccountIds(Resources.nodeAccountIds)
.transactionId(Resources.txId)
.sign(Resources.privateKey)
.bytecodeFileId(bytecodeFileId)
.adminKey(adminKey)
.gas(gas)
.initialBalance(initialBalance)
.maxAutomaticTokenAssociations(maxAutomaticTokenAssociations)
.autoRenewPeriod(autoRenewPeriod)
.constructorParameters(constructorParameters)
.autoRenewAccountId(autoRenewAccountId)
.stakedNodeId(stakedNodeId)
.maxTransactionFee(.fromTinybars(100_000))
.freeze()
}
internal func testSerialize() throws {
let tx = try Self.makeTransaction().makeProtoBody()
assertSnapshot(matching: tx, as: .description)
}
internal func testToFromBytes() throws {
let tx = try Self.makeTransaction()
let tx2 = try Transaction.fromBytes(tx.toBytes())
XCTAssertEqual(try tx.makeProtoBody(), try tx2.makeProtoBody())
}
internal func testSerialize2() throws {
let tx = try Self.makeTransaction2().makeProtoBody()
assertSnapshot(matching: tx, as: .description)
}
internal func testToFromBytes2() throws {
let tx = try Self.makeTransaction2()
let tx2 = try Transaction.fromBytes(tx.toBytes())
XCTAssertEqual(try tx.makeProtoBody(), try tx2.makeProtoBody())
}
internal func testFromProtoBody() throws {
let protoData = Proto_ContractCreateTransactionBody.with { proto in
proto.fileID = Self.bytecodeFileId.toProtobuf()
proto.adminKey = Self.adminKey.toProtobuf()
proto.gas = Int64(Self.gas)
proto.initialBalance = Self.initialBalance.toTinybars()
proto.maxAutomaticTokenAssociations = Self.maxAutomaticTokenAssociations
proto.autoRenewPeriod = Self.autoRenewPeriod.toProtobuf()
proto.constructorParameters = Self.constructorParameters
proto.autoRenewAccountID = Self.autoRenewAccountId.toProtobuf()
proto.stakedAccountID = Self.stakedAccountId.toProtobuf()
}
let protoBody = Proto_TransactionBody.with { proto in
proto.contractCreateInstance = protoData
proto.transactionID = Resources.txId.toProtobuf()
}
let tx = try ContractCreateTransaction(protobuf: protoBody, protoData)
XCTAssertEqual(tx.bytecodeFileId, Self.bytecodeFileId)
XCTAssertEqual(tx.adminKey, Self.adminKey)
XCTAssertEqual(tx.gas, Self.gas)
XCTAssertEqual(tx.initialBalance, Self.initialBalance)
XCTAssertEqual(tx.maxAutomaticTokenAssociations, Self.maxAutomaticTokenAssociations)
XCTAssertEqual(tx.autoRenewPeriod, Self.autoRenewPeriod)
XCTAssertEqual(tx.constructorParameters, Self.constructorParameters)
XCTAssertEqual(tx.autoRenewAccountId, Self.autoRenewAccountId)
XCTAssertEqual(tx.stakedAccountId, Self.stakedAccountId)
XCTAssertEqual(tx.stakedNodeId, nil)
}
internal func testGetSetBytecodeFileId() {
let tx = ContractCreateTransaction()
tx.bytecodeFileId(Self.bytecodeFileId)
XCTAssertEqual(tx.bytecodeFileId, Self.bytecodeFileId)
}
internal func testGetSetAdminKey() {
let tx = ContractCreateTransaction()
tx.adminKey(Self.adminKey)
XCTAssertEqual(tx.adminKey, Self.adminKey)
}
internal func testGetSetGas() {
let tx = ContractCreateTransaction()
tx.gas(Self.gas)
XCTAssertEqual(tx.gas, Self.gas)
}
internal func testGetSetInitialBalance() {
let tx = ContractCreateTransaction()
tx.initialBalance(Self.initialBalance)
XCTAssertEqual(tx.initialBalance, Self.initialBalance)
}
internal func testGetSetMaxAutomaticTokenAssociations() {
let tx = ContractCreateTransaction()
tx.maxAutomaticTokenAssociations(Self.maxAutomaticTokenAssociations)
XCTAssertEqual(tx.maxAutomaticTokenAssociations, Self.maxAutomaticTokenAssociations)
}
internal func testGetSetAutoRenewPeriod() {
let tx = ContractCreateTransaction()
tx.autoRenewPeriod(Self.autoRenewPeriod)
XCTAssertEqual(tx.autoRenewPeriod, Self.autoRenewPeriod)
}
internal func testGetSetConstructorParameters() {
let tx = ContractCreateTransaction()
tx.constructorParameters(Self.constructorParameters)
XCTAssertEqual(tx.constructorParameters, Self.constructorParameters)
}
internal func testGetSetAutoRenewAccountId() {
let tx = ContractCreateTransaction()
tx.autoRenewAccountId(Self.autoRenewAccountId)
XCTAssertEqual(tx.autoRenewAccountId, Self.autoRenewAccountId)
}
internal func testGetSetStakedAccountId() {
let tx = ContractCreateTransaction()
tx.stakedAccountId(Self.stakedAccountId)
XCTAssertEqual(tx.stakedAccountId, Self.stakedAccountId)
}
internal func testGetSetStakedNodeId() {
let tx = ContractCreateTransaction()
tx.stakedNodeId(Self.stakedNodeId)
XCTAssertEqual(tx.stakedNodeId, Self.stakedNodeId)
}
}