utility-workspaces 0.12.4

Library for automating workflows and testing Utility smart contracts.
Documentation
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use std::fmt;
use std::path::Path;

use unc_primitives::views::AccountView;

use crate::error::ErrorKind;
use crate::rpc::query::{
    Query, ViewAccessKey, ViewAccessKeyList, ViewAccount, ViewCode, ViewFunction, ViewState,
};
use crate::types::{AccountId, InMemorySigner, PublicKey, SecretKey, UncToken};
use crate::{BlockHeight, CryptoHash, Network, Worker};

use crate::operations::{CallTransaction, CreateAccountTransaction, Transaction};
use crate::result::{Execution, ExecutionFinalResult, Result};

/// `Account` is directly associated to an account in the network provided by the
/// [`Worker`] that creates it. This type offers methods to interact with any
/// network, such as creating transactions and calling into contract functions.
#[derive(Clone)]
pub struct Account {
    signer: InMemorySigner,
    worker: Worker<dyn Network>,
}

impl fmt::Debug for Account {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Account")
            .field("id", &self.signer.account_id)
            .finish()
    }
}

impl Account {
    /// Create a new account with the given path to the credentials JSON file
    pub fn from_file(
        path: impl AsRef<Path>,
        worker: &Worker<impl Network + 'static>,
    ) -> Result<Self> {
        let signer = InMemorySigner::from_file(path.as_ref())?;
        Ok(Self::new(signer, worker.clone().coerce()))
    }

    /// Create an [`Account`] object from an [`AccountId`] and [`SecretKey`].
    pub fn from_secret_key(
        id: AccountId,
        sk: SecretKey,
        worker: &Worker<impl Network + 'static>,
    ) -> Self {
        Self {
            signer: InMemorySigner::from_secret_key(id, sk),
            worker: worker.clone().coerce(),
        }
    }

    pub(crate) fn new(signer: InMemorySigner, worker: Worker<dyn Network>) -> Self {
        Self { signer, worker }
    }

    /// Grab the current account identifier
    pub fn id(&self) -> &AccountId {
        &self.signer.account_id
    }

    /// Grab the signer of the account. This signer is used to sign all transactions
    /// sent to the network.
    pub fn signer(&self) -> &InMemorySigner {
        &self.signer
    }

    /// Call a contract on the network specified within `worker`, and return
    /// a [`CallTransaction`] object that we will make use to populate the
    /// rest of the call details. Note that the current [`Account`]'s secret
    /// key is used as the signer of the transaction.
    pub fn call(&self, contract_id: &AccountId, function: &str) -> CallTransaction {
        CallTransaction::new(
            self.worker.clone(),
            contract_id.to_owned(),
            self.signer.clone(),
            function,
        )
    }

    /// View call to a specified contract function. Returns a result which can
    /// be deserialized into borsh or JSON.
    pub fn view(&self, contract_id: &AccountId, function: &str) -> Query<'_, ViewFunction> {
        self.worker.view(contract_id, function)
    }

    /// Transfer UNC to an account specified by `receiver_id` with the amount
    /// specified by `amount`. Returns the execution details of this transaction
    pub async fn transfer_unc(
        &self,
        receiver_id: &AccountId,
        amount: UncToken,
    ) -> Result<ExecutionFinalResult> {
        self.worker
            .transfer_unc(self.signer(), receiver_id, amount)
            .await
    }

    /// Deletes the current account, and returns the execution details of this
    /// transaction. The beneficiary will receive the funds of the account deleted
    pub async fn delete_account(self, beneficiary_id: &AccountId) -> Result<ExecutionFinalResult> {
        self.worker
            .delete_account(self.id(), &self.signer, beneficiary_id)
            .await
    }

    /// Views the current account's details such as balance and storage usage.
    pub fn view_account(&self) -> Query<'_, ViewAccount> {
        self.worker.view_account(self.id())
    }

    /// Views the current accounts's access key, given the [`PublicKey`] associated to it.
    pub fn view_access_key(&self, pk: &PublicKey) -> Query<'_, ViewAccessKey> {
        Query::new(
            self.worker.client(),
            ViewAccessKey {
                account_id: self.id().clone(),
                public_key: pk.clone(),
            },
        )
    }

    /// Views all the [`AccessKey`]s of the current account. This will return a list of
    /// [`AccessKey`]s along with each associated [`PublicKey`].
    ///
    /// [`AccessKey`]: crate::types::AccessKey
    pub fn view_access_keys(&self) -> Query<'_, ViewAccessKeyList> {
        Query::new(
            self.worker.client(),
            ViewAccessKeyList {
                account_id: self.id().clone(),
            },
        )
    }

    /// Create a new sub account. Returns a [`CreateAccountTransaction`] object
    /// that we can make use of to fill out the rest of the details. The subaccount
    /// id will be in the form of: "{new_account_id}.{parent_account_id}"
    pub fn create_subaccount<'a, 'b>(
        &'a self,
        new_account_id: &'b str,
    ) -> CreateAccountTransaction<'a, 'b> {
        CreateAccountTransaction::new(
            &self.worker,
            self.signer.clone(),
            self.id().clone(),
            new_account_id,
        )
    }

    /// Deploy contract code or WASM bytes to the account, and return us a new
    /// [`Contract`] object that we can use to interact with the contract.
    pub async fn deploy(&self, wasm: &[u8]) -> Result<Execution<Contract>> {
        let outcome = self
            .worker
            .client()
            .deploy(&self.signer, self.id(), wasm.into())
            .await?;

        Ok(Execution {
            result: Contract::new(self.signer().clone(), self.worker.clone()),
            details: ExecutionFinalResult::from_view(outcome),
        })
    }

    /// Start a batch transaction, using the current account as the signer and
    /// making calls into the contract provided by `contract_id`. Returns a
    /// [`Transaction`] object that we can use to add Actions to the batched
    /// transaction. Call `transact` to send the batched transaction to the
    /// network.
    pub fn batch(&self, contract_id: &AccountId) -> Transaction {
        Transaction::new(
            self.worker.clone(),
            self.signer().clone(),
            contract_id.clone(),
        )
    }

    /// Store the credentials of this account locally in the directory provided.
    pub async fn store_credentials(&self, save_dir: impl AsRef<Path> + Send) -> Result<()> {
        let savepath = save_dir.as_ref();
        std::fs::create_dir_all(&save_dir).map_err(|e| ErrorKind::Io.custom(e))?;
        let savepath = savepath.join(format!("{}.json", self.id()));
        crate::rpc::tool::write_cred_to_file(&savepath, self.id(), &self.secret_key().0)
    }

    /// Get the keys of this account. The public key can be retrieved from the secret key.
    pub fn secret_key(&self) -> &SecretKey {
        &self.signer.secret_key
    }

    /// Sets the [`SecretKey`] of this account. Future transactions will be signed
    /// using this newly provided key.
    pub fn set_secret_key(&mut self, sk: SecretKey) {
        self.signer.secret_key = sk;
    }
}

/// `Contract` is directly associated to a contract in the network provided by the
/// [`Worker`] that creates it. This type offers methods to interact with any
/// network, such as creating transactions and calling into contract functions.
#[derive(Clone)]
pub struct Contract {
    pub(crate) account: Account,
}

impl fmt::Debug for Contract {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Contract")
            .field("id", self.account.id())
            .finish()
    }
}

impl Contract {
    /// Create a [`Contract`] object from an [`AccountId`] and [`SecretKey`].
    pub fn from_secret_key(
        id: AccountId,
        sk: SecretKey,
        worker: &Worker<impl Network + 'static>,
    ) -> Self {
        Self::account(Account::from_secret_key(id, sk, worker))
    }

    pub(crate) fn new(signer: InMemorySigner, worker: Worker<dyn Network>) -> Self {
        Self {
            account: Account::new(signer, worker),
        }
    }

    pub(crate) fn account(account: Account) -> Self {
        Self { account }
    }

    /// Grab the current contract's account identifier
    pub fn id(&self) -> &AccountId {
        self.account.id()
    }

    /// Treat this [`Contract`] object as an [`Account`] type. This does nothing
    /// on chain/network, and is merely allowing `Account::*` functions to be
    /// used from this `Contract`.
    pub fn as_account(&self) -> &Account {
        &self.account
    }

    /// Treat this [`Contract`] object as an [`Account`] type. This does nothing
    /// on chain/network, and is merely allowing `Account::*` functions to be
    /// used from this `Contract`.
    pub fn as_account_mut(&mut self) -> &mut Account {
        &mut self.account
    }

    /// Grab the signer of the account. This signer is used to sign all transactions
    /// sent to the network.
    pub fn signer(&self) -> &InMemorySigner {
        self.account.signer()
    }

    /// Call the current contract's function using the contract's own account
    /// secret key to do the signing. Returns a [`CallTransaction`] object that
    /// we will make use to populate the rest of the call details.
    ///
    /// If we want to make use of the contract's secret key as a signer to call
    /// into another contract, use `contract.as_account().call` instead.
    pub fn call(&self, function: &str) -> CallTransaction {
        self.account.call(self.id(), function)
    }

    /// Call a view function into the current contract. Returns a result which can
    /// be deserialized into borsh or JSON.
    pub fn view(&self, function: &str) -> Query<'_, ViewFunction> {
        self.account.view(self.id(), function)
    }

    /// View the WASM code bytes of this contract.
    pub fn view_code(&self) -> Query<'_, ViewCode> {
        self.account.worker.view_code(self.id())
    }

    /// View a contract's state map of key value pairs.
    pub fn view_state(&self) -> Query<'_, ViewState> {
        self.account.worker.view_state(self.id())
    }

    /// Views the current contract's details such as balance and storage usage.
    pub fn view_account(&self) -> Query<'_, ViewAccount> {
        self.account.worker.view_account(self.id())
    }

    /// Views the current contract's access key, given the [`PublicKey`] associated to it.
    pub fn view_access_key(&self, pk: &PublicKey) -> Query<'_, ViewAccessKey> {
        self.account.view_access_key(pk)
    }

    /// Views all the [`AccessKey`]s of the current contract. This will return a list of
    /// [`AccessKey`]s along with each associated [`PublicKey`].
    ///
    /// [`AccessKey`]: crate::types::AccessKey
    pub fn view_access_keys(&self) -> Query<'_, ViewAccessKeyList> {
        self.account.view_access_keys()
    }

    /// Deletes the current contract, and returns the execution details of this
    /// transaction. The beneficiary will receive the funds of the account deleted
    pub async fn delete_contract(self, beneficiary_id: &AccountId) -> Result<ExecutionFinalResult> {
        self.account.delete_account(beneficiary_id).await
    }

    /// Start a batch transaction, using the current contract's secret key as the
    /// signer, making calls into itself. Returns a [`Transaction`] object that
    /// we can use to add Actions to the batched transaction. Call `transact`
    /// to send the batched transaction to the network.
    pub fn batch(&self) -> Transaction {
        self.account.batch(self.id())
    }
}

/// Details of an Account or Contract. This is an non-exhaustive list of items
/// that the account stores in the blockchain state.
///
/// This struct is the same as [`AccountDetails`] with the exception that it provides
/// optional fields that guard against 'null' overwrites when making a patch.
#[derive(Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct AccountDetailsPatch {
    pub balance: Option<UncToken>,
    pub pledging: Option<UncToken>,
    pub power: Option<u128>,
    pub code_hash: Option<CryptoHash>,
    pub storage_usage: Option<u64>,
    pub(crate) storage_paid_at: Option<BlockHeight>,
}

impl AccountDetailsPatch {
    pub fn reduce(&mut self, acc: Self) {
        if let Some(balance) = acc.balance {
            self.balance = Some(balance);
        }
        if let Some(pledging) = acc.pledging {
            self.pledging = Some(pledging);
        }
        if let Some(power) = acc.power {
            self.power = Some(power);
        }
        if let Some(code_hash) = acc.code_hash {
            self.code_hash = Some(code_hash);
        }
        if let Some(storage) = acc.storage_usage {
            self.storage_usage = Some(storage);
        }
        if let Some(storage_paid_at) = acc.storage_paid_at {
            self.storage_paid_at = Some(storage_paid_at);
        }
    }

    pub fn balance(mut self, balance: UncToken) -> Self {
        self.balance = Some(balance);
        self
    }

    pub fn pledging(mut self, pledging: UncToken) -> Self {
        self.pledging = Some(pledging);
        self
    }

    pub fn power(mut self, power: u128) -> Self {
        self.power = Some(power);
        self
    }

    pub fn code_hash(mut self, code_hash: CryptoHash) -> Self {
        self.code_hash = Some(code_hash);
        self
    }

    pub fn storage_usage(mut self, storage_usage: u64) -> Self {
        self.storage_usage = Some(storage_usage);
        self
    }
}

impl From<AccountDetails> for AccountDetailsPatch {
    fn from(account: AccountDetails) -> Self {
        Self {
            balance: Some(account.balance),
            pledging: Some(account.pledging),
            power: Some(account.power),
            code_hash: Some(account.code_hash),
            storage_usage: Some(account.storage_usage),
            storage_paid_at: Some(account.storage_paid_at),
        }
    }
}

/// Details of an Account or Contract. This is an non-exhaustive list of items
/// that the account stores in the blockchain state.
#[derive(Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct AccountDetails {
    pub balance: UncToken,
    pub pledging: UncToken,
    pub power: u128,
    pub code_hash: CryptoHash,
    pub storage_usage: u64,
    // Deprecated value. Mainly used to be able to convert back into an AccountView
    pub(crate) storage_paid_at: BlockHeight,
}

impl AccountDetails {
    pub fn new() -> Self {
        Self {
            balance: UncToken::from_unc(0),
            pledging: UncToken::from_unc(0),
            power: 0,
            code_hash: CryptoHash::default(),
            storage_usage: 0,
            storage_paid_at: 0,
        }
    }

    pub(crate) fn into_unc_account(self) -> unc_primitives::account::Account {
        unc_primitives::account::Account::new(
            self.balance.as_attounc(),
            self.pledging.as_attounc(),
            self.power,
            unc_primitives::hash::CryptoHash(self.code_hash.0),
            self.storage_usage,
        )
    }
}

impl Default for AccountDetails {
    fn default() -> Self {
        Self::new()
    }
}

impl From<AccountView> for AccountDetails {
    fn from(account: AccountView) -> Self {
        Self {
            balance: UncToken::from_attounc(account.amount),
            pledging: UncToken::from_attounc(account.pledging),
            power: account.power,
            code_hash: CryptoHash(account.code_hash.0),
            storage_usage: account.storage_usage,
            storage_paid_at: account.storage_paid_at,
        }
    }
}

impl From<AccountDetailsPatch> for AccountDetails {
    fn from(value: AccountDetailsPatch) -> Self {
        Self {
            balance: value.balance.unwrap_or_default(),
            pledging: value.pledging.unwrap_or_default(),
            power: value.power.unwrap_or_default(),
            code_hash: value.code_hash.unwrap_or_default(),
            storage_usage: value.storage_usage.unwrap_or_default(),
            storage_paid_at: value.storage_paid_at.unwrap_or_default(),
        }
    }
}