-
Notifications
You must be signed in to change notification settings - Fork 131
/
ContractInfo.js
284 lines (261 loc) · 9.43 KB
/
ContractInfo.js
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*-
*
* Hedera JavaScript SDK
*
* Copyright (C) 2020 - 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import ContractId from "./ContractId.js";
import AccountId from "../account/AccountId.js";
import StakingInfo from "../StakingInfo.js";
import Timestamp from "../Timestamp.js";
import Duration from "../Duration.js";
import Hbar from "../Hbar.js";
import Long from "long";
import * as HashgraphProto from "@hashgraph/proto";
import TokenRelationshipMap from "../account/TokenRelationshipMap.js";
import Key from "../Key.js";
import LedgerId from "../LedgerId.js";
const { proto } = HashgraphProto;
/**
* @typedef {import("../StakingInfo.js").StakingInfoJson} StakingInfoJson
*/
/**
* Response when the client sends the node CryptoGetInfoQuery.
*/
export default class ContractInfo {
/**
* @private
* @param {object} props
* @param {ContractId} props.contractId
* @param {AccountId} props.accountId
* @param {string} props.contractAccountId
* @param {?Key} props.adminKey
* @param {Timestamp} props.expirationTime
* @param {Duration} props.autoRenewPeriod
* @param {?AccountId} props.autoRenewAccountId
* @param {Long} props.storage
* @param {string} props.contractMemo
* @param {Hbar} props.balance
* @param {boolean} props.isDeleted
* @param {TokenRelationshipMap} props.tokenRelationships
* @param {LedgerId|null} props.ledgerId
* @param {?StakingInfo} props.stakingInfo
*/
constructor(props) {
/**
* ID of the contract instance, in the format used in transactions.
*
* @readonly
*/
this.contractId = props.contractId;
/**
* ID of the cryptocurrency account owned by the contract instance,
* in the format used in transactions.
*
* @readonly
*/
this.accountId = props.accountId;
/**
* ID of both the contract instance and the cryptocurrency account owned by the contract
* instance, in the format used by Solidity.
*
* @readonly
*/
this.contractAccountId = props.contractAccountId;
/**
* The state of the instance and its fields can be modified arbitrarily if this key signs a
* transaction to modify it. If this is null, then such modifications are not possible,
* and there is no administrator that can override the normal operation of this smart
* contract instance. Note that if it is created with no admin keys, then there is no
* administrator to authorize changing the admin keys, so there can never be any admin keys
* for that instance.
*
* @readonly
*/
this.adminKey = props.adminKey != null ? props.adminKey : null;
/**
* The current time at which this contract instance (and its account) is set to expire.
*
* @readonly
*/
this.expirationTime = props.expirationTime;
/**
* The expiration time will extend every this many seconds. If there are insufficient funds,
* then it extends as long as possible. If the account is empty when it expires,
* then it is deleted.
*
* @readonly
*/
this.autoRenewPeriod = props.autoRenewPeriod;
/**
* ID of the an account to charge for auto-renewal of this contract. If not set, or set
* to an account with zero hbar balance, the contract's own hbar balance will be used
* to cover auto-renewal fees.
*
* @readonly
*/
this.autoRenewAccountId = props.autoRenewAccountId;
/**
* Number of bytes of storage being used by this instance (which affects the cost to
* extend the expiration time).
*
* @readonly
*/
this.storage = props.storage;
/**
* The memo associated with the contract (max 100 bytes).
*
* @readonly
*/
this.contractMemo = props.contractMemo;
/**
* The current balance of the contract.
*
* @readonly
*/
this.balance = props.balance;
/**
* Whether the contract has been deleted
*
* @readonly
*/
this.isDeleted = props.isDeleted;
/**
* The tokens associated to the contract
*
* @readonly
*/
this.tokenRelationships = props.tokenRelationships;
/**
* The ledger ID the response was returned from; please see <a href="https://github.com/hashgraph/hedera-improvement-proposal/blob/master/HIP/hip-198.md">HIP-198</a> for the network-specific IDs.
*/
this.ledgerId = props.ledgerId;
/**
* Staking metadata for this account.
*/
this.stakingInfo = props.stakingInfo;
Object.freeze(this);
}
/**
* @internal
* @param {HashgraphProto.proto.ContractGetInfoResponse.IContractInfo} info
* @returns {ContractInfo}
*/
static _fromProtobuf(info) {
const autoRenewPeriod = /** @type {Long | number} */ (
/** @type {HashgraphProto.proto.IDuration} */ (info.autoRenewPeriod)
.seconds
);
return new ContractInfo({
contractId: ContractId._fromProtobuf(
/** @type {HashgraphProto.proto.IContractID} */ (
info.contractID
)
),
accountId: AccountId._fromProtobuf(
/** @type {HashgraphProto.proto.IAccountID} */ (info.accountID)
),
contractAccountId:
info.contractAccountID != null ? info.contractAccountID : "",
adminKey:
info.adminKey != null
? Key._fromProtobufKey(info.adminKey)
: null,
expirationTime: Timestamp._fromProtobuf(
/** @type {HashgraphProto.proto.ITimestamp} */ (
info.expirationTime
)
),
autoRenewPeriod: new Duration(autoRenewPeriod),
autoRenewAccountId:
info.autoRenewAccountId != null
? AccountId._fromProtobuf(info.autoRenewAccountId)
: null,
storage:
info.storage != null
? info.storage instanceof Long
? info.storage
: Long.fromValue(info.storage)
: Long.ZERO,
contractMemo: info.memo != null ? info.memo : "",
balance: Hbar.fromTinybars(info.balance != null ? info.balance : 0),
isDeleted: /** @type {boolean} */ (info.deleted),
tokenRelationships: TokenRelationshipMap._fromProtobuf(
info.tokenRelationships != null ? info.tokenRelationships : []
),
ledgerId:
info.ledgerId != null
? LedgerId.fromBytes(info.ledgerId)
: null,
stakingInfo:
info.stakingInfo != null
? StakingInfo._fromProtobuf(info.stakingInfo)
: null,
});
}
/**
* @internal
* @returns {HashgraphProto.proto.ContractGetInfoResponse.IContractInfo}
*/
_toProtobuf() {
return {
contractID: this.contractId._toProtobuf(),
accountID: this.accountId._toProtobuf(),
contractAccountID: this.contractAccountId,
adminKey:
this.adminKey != null ? this.adminKey._toProtobufKey() : null,
expirationTime: this.expirationTime._toProtobuf(),
autoRenewPeriod:
this.autoRenewPeriod != null
? this.autoRenewPeriod._toProtobuf()
: null,
autoRenewAccountId:
this.autoRenewAccountId != null
? this.autoRenewAccountId._toProtobuf()
: null,
storage: this.storage,
memo: this.contractMemo,
balance: this.balance.toTinybars(),
deleted: this.isDeleted,
tokenRelationships:
this.tokenRelationships != null
? this.tokenRelationships._toProtobuf()
: null,
ledgerId: this.ledgerId != null ? this.ledgerId.toBytes() : null,
stakingInfo:
this.stakingInfo != null
? this.stakingInfo._toProtobuf()
: null,
};
}
/**
* @param {Uint8Array} bytes
* @returns {ContractInfo}
*/
static fromBytes(bytes) {
return ContractInfo._fromProtobuf(
proto.ContractGetInfoResponse.ContractInfo.decode(bytes)
);
}
/**
* @returns {Uint8Array}
*/
toBytes() {
return proto.ContractGetInfoResponse.ContractInfo.encode(
this._toProtobuf()
).finish();
}
}