-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathargs.rs
More file actions
2923 lines (2668 loc) · 80.6 KB
/
Copy pathargs.rs
File metadata and controls
2923 lines (2668 loc) · 80.6 KB
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Structures encapsulating SDK arguments
use std::fmt::Display;
use std::path::PathBuf;
use std::str::FromStr;
use std::time::Duration as StdDuration;
use namada_core::address::Address;
use namada_core::chain::{BlockHeight, ChainId, Epoch};
use namada_core::collections::HashMap;
use namada_core::dec::Dec;
use namada_core::ethereum_events::EthAddress;
use namada_core::keccak::KeccakHash;
use namada_core::key::{common, SchemeType};
use namada_core::masp::{MaspEpoch, PaymentAddress};
use namada_core::time::DateTimeUtc;
use namada_core::{storage, token};
use namada_governance::cli::onchain::{
DefaultProposal, PgfFundingProposal, PgfStewardProposal,
};
use namada_ibc::IbcShieldingData;
use namada_token::masp::utils::RetryStrategy;
use namada_tx::data::GasLimit;
use namada_tx::Memo;
use serde::{Deserialize, Serialize};
use zeroize::Zeroizing;
use crate::eth_bridge::bridge_pool;
use crate::ibc::core::host::types::identifiers::{ChannelId, PortId};
use crate::signing::SigningTxData;
use crate::wallet::{DatedSpendingKey, DatedViewingKey};
use crate::{rpc, tx, Namada};
/// [`Duration`](StdDuration) wrapper that provides a
/// method to parse a value from a string.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
#[repr(transparent)]
pub struct Duration(pub StdDuration);
impl ::std::str::FromStr for Duration {
type Err = String;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
::duration_str::parse(s).map(Duration)
}
}
/// Abstraction of types being used in Namada
pub trait NamadaTypes: Clone + std::fmt::Debug {
/// Represents an address on the ledger
type Address: Clone + std::fmt::Debug;
/// Represents an address that defaults to a native token
type AddrOrNativeToken: Clone + std::fmt::Debug + From<Self::Address>;
/// Represents a key pair
type Keypair: Clone + std::fmt::Debug;
/// Represents the address of a Tendermint endpoint (used in context-less
/// CLI commands where chain config isn't available)
type TendermintAddress: Clone + std::fmt::Debug;
/// RPC address of a locally configured node
type ConfigRpcTendermintAddress: Clone
+ std::fmt::Debug
+ From<Self::TendermintAddress>;
/// Represents the address of an Ethereum endpoint
type EthereumAddress: Clone + std::fmt::Debug;
/// Represents a shielded viewing key
type ViewingKey: Clone + std::fmt::Debug;
/// Represents a shielded spending key
type SpendingKey: Clone + std::fmt::Debug;
/// Represents a shielded viewing key
type DatedViewingKey: Clone + std::fmt::Debug;
/// Represents a shielded spending key
type DatedSpendingKey: Clone + std::fmt::Debug;
/// Represents a shielded payment address
type PaymentAddress: Clone + std::fmt::Debug;
/// Represents the owner of a balance
type BalanceOwner: Clone + std::fmt::Debug;
/// Represents a public key
type PublicKey: Clone + std::fmt::Debug;
/// Represents the source of a Transfer
type TransferSource: Clone + std::fmt::Debug;
/// Represents the target of a Transfer
type TransferTarget: Clone + std::fmt::Debug;
/// Represents some data that is used in a transaction
type Data: Clone + std::fmt::Debug;
/// Bridge pool recommendations conversion rates table.
type BpConversionTable: Clone + std::fmt::Debug;
/// Address of a `namada-masp-indexer` live instance
type MaspIndexerAddress: Clone + std::fmt::Debug;
/// Represents a block height
type BlockHeight: Clone + std::fmt::Debug;
}
/// The concrete types being used in Namada SDK
#[derive(Clone, Debug)]
pub struct SdkTypes;
/// An entry in the Bridge pool recommendations conversion
/// rates table.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct BpConversionTableEntry {
/// An alias for the token, or the string representation
/// of its address if none is available.
pub alias: String,
/// Conversion rate from the given token to gwei.
pub conversion_rate: f64,
}
impl NamadaTypes for SdkTypes {
type AddrOrNativeToken = Address;
type Address = Address;
type BalanceOwner = namada_core::masp::BalanceOwner;
type BlockHeight = namada_core::chain::BlockHeight;
type BpConversionTable = HashMap<Address, BpConversionTableEntry>;
type ConfigRpcTendermintAddress = tendermint_rpc::Url;
type Data = Vec<u8>;
type DatedSpendingKey = DatedSpendingKey;
type DatedViewingKey = DatedViewingKey;
type EthereumAddress = ();
type Keypair = namada_core::key::common::SecretKey;
type MaspIndexerAddress = String;
type PaymentAddress = namada_core::masp::PaymentAddress;
type PublicKey = namada_core::key::common::PublicKey;
type SpendingKey = namada_core::masp::ExtendedSpendingKey;
type TendermintAddress = tendermint_rpc::Url;
type TransferSource = namada_core::masp::TransferSource;
type TransferTarget = namada_core::masp::TransferTarget;
type ViewingKey = namada_core::masp::ExtendedViewingKey;
}
/// Common query arguments
#[derive(Clone, Debug)]
pub struct Query<C: NamadaTypes = SdkTypes> {
/// The address of the ledger node as host:port
pub ledger_address: C::ConfigRpcTendermintAddress,
}
/// Common query arguments
#[derive(Clone, Debug)]
pub struct QueryWithoutCtx<C: NamadaTypes = SdkTypes> {
/// The address of the ledger node as host:port
pub ledger_address: C::TendermintAddress,
}
/// Transaction associated results arguments
#[derive(Clone, Debug)]
pub struct QueryResult<C: NamadaTypes = SdkTypes> {
/// Common query args
pub query: Query<C>,
/// Hash of transaction to lookup
pub tx_hash: String,
}
/// Custom transaction arguments
#[derive(Clone, Debug)]
pub struct TxCustom<C: NamadaTypes = SdkTypes> {
/// Common tx arguments
pub tx: Tx<C>,
/// Path to the tx WASM code file
pub code_path: Option<PathBuf>,
/// Path to the data file
pub data_path: Option<C::Data>,
/// Path to the serialized transaction
pub serialized_tx: Option<C::Data>,
/// The optional address that correspond to the signatures/signing-keys
pub owner: Option<C::Address>,
/// Generate an ephemeral signing key to be used only once to sign the
/// wrapper tx
pub disposable_signing_key: bool,
}
impl<C: NamadaTypes> TxBuilder<C> for TxCustom<C> {
fn tx<F>(self, func: F) -> Self
where
F: FnOnce(Tx<C>) -> Tx<C>,
{
TxCustom {
tx: func(self.tx),
..self
}
}
}
impl<C: NamadaTypes> TxCustom<C> {
/// Path to the tx WASM code file
pub fn code_path(self, code_path: PathBuf) -> Self {
Self {
code_path: Some(code_path),
..self
}
}
/// Path to the data file
pub fn data_path(self, data_path: C::Data) -> Self {
Self {
data_path: Some(data_path),
..self
}
}
/// Path to the serialized transaction
pub fn serialized_tx(self, serialized_tx: C::Data) -> Self {
Self {
serialized_tx: Some(serialized_tx),
..self
}
}
/// The address that correspond to the signatures/signing-keys
pub fn owner(self, owner: Option<C::Address>) -> Self {
Self { owner, ..self }
}
/// The flag to request an ephemeral signing key to be used only once to
/// sign the wrapper tx
pub fn disposable_signing_key(self, disposable_signing_key: bool) -> Self {
Self {
disposable_signing_key,
..self
}
}
}
impl TxCustom {
/// Build a transaction from this builder
pub async fn build(
&self,
context: &impl Namada,
) -> crate::error::Result<(namada_tx::Tx, Option<SigningTxData>)> {
tx::build_custom(context, self).await
}
}
/// An amount read in by the cli
#[derive(Copy, Clone, Debug)]
pub enum InputAmount {
/// An amount whose representation has been validated
/// against the allowed representation in storage
Validated(token::DenominatedAmount),
/// The parsed amount read in from the cli. It has
/// not yet been validated against the allowed
/// representation in storage.
Unvalidated(token::DenominatedAmount),
}
impl std::str::FromStr for InputAmount {
type Err = <token::DenominatedAmount as std::str::FromStr>::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> {
token::DenominatedAmount::from_str(s).map(InputAmount::Unvalidated)
}
}
impl From<token::DenominatedAmount> for InputAmount {
fn from(amt: token::DenominatedAmount) -> Self {
InputAmount::Unvalidated(amt)
}
}
/// Transparent transfer-specific arguments
#[derive(Clone, Debug)]
pub struct TxTransparentTransferData<C: NamadaTypes = SdkTypes> {
/// Transfer source address
pub source: C::Address,
/// Transfer target address
pub target: C::Address,
/// Transferred token address
pub token: C::Address,
/// Transferred token amount
pub amount: InputAmount,
}
/// Transparent transfer transaction arguments
#[derive(Clone, Debug)]
pub struct TxTransparentTransfer<C: NamadaTypes = SdkTypes> {
/// Common tx arguments
pub tx: Tx<C>,
/// The transfer specific data
pub data: Vec<TxTransparentTransferData<C>>,
/// Path to the TX WASM code file
pub tx_code_path: PathBuf,
}
impl<C: NamadaTypes> TxBuilder<C> for TxTransparentTransfer<C> {
fn tx<F>(self, func: F) -> Self
where
F: FnOnce(Tx<C>) -> Tx<C>,
{
TxTransparentTransfer {
tx: func(self.tx),
..self
}
}
}
impl<C: NamadaTypes> TxTransparentTransferData<C> {
/// Transfer source address
pub fn source(self, source: C::Address) -> Self {
Self { source, ..self }
}
/// Transfer target address
pub fn receiver(self, target: C::Address) -> Self {
Self { target, ..self }
}
/// Transferred token address
pub fn token(self, token: C::Address) -> Self {
Self { token, ..self }
}
/// Transferred token amount
pub fn amount(self, amount: InputAmount) -> Self {
Self { amount, ..self }
}
}
impl<C: NamadaTypes> TxTransparentTransfer<C> {
/// Path to the TX WASM code file
pub fn tx_code_path(self, tx_code_path: PathBuf) -> Self {
Self {
tx_code_path,
..self
}
}
}
impl TxTransparentTransfer {
/// Build a transaction from this builder
pub async fn build(
&mut self,
context: &impl Namada,
) -> crate::error::Result<(namada_tx::Tx, SigningTxData)> {
tx::build_transparent_transfer(context, self).await
}
}
/// Shielded transfer-specific arguments
#[derive(Clone, Debug)]
pub struct TxShieldedTransferData<C: NamadaTypes = SdkTypes> {
/// Transfer source spending key
pub source: C::SpendingKey,
/// Transfer target address
pub target: C::PaymentAddress,
/// Transferred token address
pub token: C::Address,
/// Transferred token amount
pub amount: InputAmount,
}
/// Shielded transfer transaction arguments
#[derive(Clone, Debug)]
pub struct TxShieldedTransfer<C: NamadaTypes = SdkTypes> {
/// Common tx arguments
pub tx: Tx<C>,
/// Transfer-specific data
pub data: Vec<TxShieldedTransferData<C>>,
/// Optional additional keys for gas payment
pub gas_spending_key: Option<C::SpendingKey>,
/// Generate an ephemeral signing key to be used only once to sign the
/// wrapper tx
pub disposable_signing_key: bool,
/// Path to the TX WASM code file
pub tx_code_path: PathBuf,
}
impl<C: NamadaTypes> TxBuilder<C> for TxShieldedTransfer<C> {
fn tx<F>(self, func: F) -> Self
where
F: FnOnce(Tx<C>) -> Tx<C>,
{
TxShieldedTransfer {
tx: func(self.tx),
..self
}
}
}
impl TxShieldedTransfer {
/// Build a transaction from this builder
pub async fn build(
&mut self,
context: &impl Namada,
) -> crate::error::Result<(namada_tx::Tx, SigningTxData)> {
tx::build_shielded_transfer(context, self).await
}
}
/// Shielding transfer-specific arguments
#[derive(Clone, Debug)]
pub struct TxShieldingTransferData<C: NamadaTypes = SdkTypes> {
/// Transfer source spending key
pub source: C::Address,
/// Transferred token address
pub token: C::Address,
/// Transferred token amount
pub amount: InputAmount,
}
/// Shielding transfer transaction arguments
#[derive(Clone, Debug)]
pub struct TxShieldingTransfer<C: NamadaTypes = SdkTypes> {
/// Common tx arguments
pub tx: Tx<C>,
/// Transfer target address
pub target: C::PaymentAddress,
/// Transfer-specific data
pub data: Vec<TxShieldingTransferData<C>>,
/// Path to the TX WASM code file
pub tx_code_path: PathBuf,
}
impl<C: NamadaTypes> TxBuilder<C> for TxShieldingTransfer<C> {
fn tx<F>(self, func: F) -> Self
where
F: FnOnce(Tx<C>) -> Tx<C>,
{
TxShieldingTransfer {
tx: func(self.tx),
..self
}
}
}
impl TxShieldingTransfer {
/// Build a transaction from this builder
pub async fn build(
&mut self,
context: &impl Namada,
) -> crate::error::Result<(namada_tx::Tx, SigningTxData, MaspEpoch)> {
tx::build_shielding_transfer(context, self).await
}
}
/// Unshielding transfer-specific arguments
#[derive(Clone, Debug)]
pub struct TxUnshieldingTransferData<C: NamadaTypes = SdkTypes> {
/// Transfer target address
pub target: C::Address,
/// Transferred token address
pub token: C::Address,
/// Transferred token amount
pub amount: InputAmount,
}
/// Unshielding transfer transaction arguments
#[derive(Clone, Debug)]
pub struct TxUnshieldingTransfer<C: NamadaTypes = SdkTypes> {
/// Common tx arguments
pub tx: Tx<C>,
/// Transfer source spending key
pub source: C::SpendingKey,
/// Transfer-specific data
pub data: Vec<TxUnshieldingTransferData<C>>,
/// Optional additional keys for gas payment
pub gas_spending_key: Option<C::SpendingKey>,
/// Generate an ephemeral signing key to be used only once to sign the
/// wrapper tx
pub disposable_signing_key: bool,
/// Path to the TX WASM code file
pub tx_code_path: PathBuf,
}
impl TxUnshieldingTransfer {
/// Build a transaction from this builder
pub async fn build(
&mut self,
context: &impl Namada,
) -> crate::error::Result<(namada_tx::Tx, SigningTxData)> {
tx::build_unshielding_transfer(context, self).await
}
}
/// IBC transfer transaction arguments
#[derive(Clone, Debug)]
pub struct TxIbcTransfer<C: NamadaTypes = SdkTypes> {
/// Common tx arguments
pub tx: Tx<C>,
/// Transfer source address
pub source: C::TransferSource,
/// Transfer target address
pub receiver: String,
/// Transferred token address
pub token: C::Address,
/// Transferred token amount
pub amount: InputAmount,
/// Port ID
pub port_id: PortId,
/// Channel ID
pub channel_id: ChannelId,
/// Timeout height of the destination chain
pub timeout_height: Option<u64>,
/// Timeout timestamp offset
pub timeout_sec_offset: Option<u64>,
/// Refund target address when the shielded transfer failure
pub refund_target: Option<C::TransferTarget>,
/// IBC shielding transfer data for the destination chain
pub ibc_shielding_data: Option<IbcShieldingData>,
/// Memo for IBC transfer packet
pub ibc_memo: Option<String>,
/// Optional additional keys for gas payment
pub gas_spending_key: Option<C::SpendingKey>,
/// Generate an ephemeral signing key to be used only once to sign the
/// wrapper tx
pub disposable_signing_key: bool,
/// Path to the TX WASM code file
pub tx_code_path: PathBuf,
}
impl<C: NamadaTypes> TxBuilder<C> for TxIbcTransfer<C> {
fn tx<F>(self, func: F) -> Self
where
F: FnOnce(Tx<C>) -> Tx<C>,
{
TxIbcTransfer {
tx: func(self.tx),
..self
}
}
}
impl<C: NamadaTypes> TxIbcTransfer<C> {
/// Transfer source address
pub fn source(self, source: C::TransferSource) -> Self {
Self { source, ..self }
}
/// Transfer target address
pub fn receiver(self, receiver: String) -> Self {
Self { receiver, ..self }
}
/// Transferred token address
pub fn token(self, token: C::Address) -> Self {
Self { token, ..self }
}
/// Transferred token amount
pub fn amount(self, amount: InputAmount) -> Self {
Self { amount, ..self }
}
/// Port ID
pub fn port_id(self, port_id: PortId) -> Self {
Self { port_id, ..self }
}
/// Channel ID
pub fn channel_id(self, channel_id: ChannelId) -> Self {
Self { channel_id, ..self }
}
/// Timeout height of the destination chain
pub fn timeout_height(self, timeout_height: u64) -> Self {
Self {
timeout_height: Some(timeout_height),
..self
}
}
/// Timeout timestamp offset
pub fn timeout_sec_offset(self, timeout_sec_offset: u64) -> Self {
Self {
timeout_sec_offset: Some(timeout_sec_offset),
..self
}
}
/// Refund target address
pub fn refund_target(self, refund_target: C::TransferTarget) -> Self {
Self {
refund_target: Some(refund_target),
..self
}
}
/// IBC shielding transfer data
pub fn ibc_shielding_data(self, shielding_data: IbcShieldingData) -> Self {
Self {
ibc_shielding_data: Some(shielding_data),
..self
}
}
/// Memo for IBC transfer packet
pub fn ibc_memo(self, ibc_memo: String) -> Self {
Self {
ibc_memo: Some(ibc_memo),
..self
}
}
/// Gas spending keys
pub fn gas_spending_keys(self, gas_spending_key: C::SpendingKey) -> Self {
Self {
gas_spending_key: Some(gas_spending_key),
..self
}
}
/// Path to the TX WASM code file
pub fn tx_code_path(self, tx_code_path: PathBuf) -> Self {
Self {
tx_code_path,
..self
}
}
}
impl TxIbcTransfer {
/// Build a transaction from this builder
pub async fn build(
&self,
context: &impl Namada,
) -> crate::error::Result<(namada_tx::Tx, SigningTxData, Option<MaspEpoch>)>
{
tx::build_ibc_transfer(context, self).await
}
}
/// Transaction to initialize create a new proposal
#[derive(Clone, Debug)]
pub struct InitProposal<C: NamadaTypes = SdkTypes> {
/// Common tx arguments
pub tx: Tx<C>,
/// The proposal data
pub proposal_data: C::Data,
/// Flag if proposal is of type Pgf stewards
pub is_pgf_stewards: bool,
/// Flag if proposal is of type Pgf funding
pub is_pgf_funding: bool,
/// Path to the tx WASM file
pub tx_code_path: PathBuf,
}
impl<C: NamadaTypes> TxBuilder<C> for InitProposal<C> {
fn tx<F>(self, func: F) -> Self
where
F: FnOnce(Tx<C>) -> Tx<C>,
{
InitProposal {
tx: func(self.tx),
..self
}
}
}
impl<C: NamadaTypes> InitProposal<C> {
/// The proposal data
pub fn proposal_data(self, proposal_data: C::Data) -> Self {
Self {
proposal_data,
..self
}
}
/// Flag if proposal is of type Pgf stewards
pub fn is_pgf_stewards(self, is_pgf_stewards: bool) -> Self {
Self {
is_pgf_stewards,
..self
}
}
/// Flag if proposal is of type Pgf funding
pub fn is_pgf_funding(self, is_pgf_funding: bool) -> Self {
Self {
is_pgf_funding,
..self
}
}
/// Path to the tx WASM file
pub fn tx_code_path(self, tx_code_path: PathBuf) -> Self {
Self {
tx_code_path,
..self
}
}
}
impl InitProposal {
/// Build a transaction from this builder
pub async fn build(
&self,
context: &impl Namada,
) -> crate::error::Result<(namada_tx::Tx, SigningTxData)> {
let current_epoch = rpc::query_epoch(context.client()).await?;
let governance_parameters =
rpc::query_governance_parameters(context.client()).await;
if self.is_pgf_funding {
let proposal = PgfFundingProposal::try_from(
self.proposal_data.as_ref(),
)
.map_err(|e| {
crate::error::TxSubmitError::FailedGovernaneProposalDeserialize(
e.to_string(),
)
})?
.validate(&governance_parameters, current_epoch, self.tx.force)
.map_err(|e| {
crate::error::TxSubmitError::InvalidProposal(e.to_string())
})?;
tx::build_pgf_funding_proposal(context, self, proposal).await
} else if self.is_pgf_stewards {
let proposal = PgfStewardProposal::try_from(
self.proposal_data.as_ref(),
)
.map_err(|e| {
crate::error::TxSubmitError::FailedGovernaneProposalDeserialize(
e.to_string(),
)
})?;
let nam_address = context.native_token();
let author_balance = rpc::get_token_balance(
context.client(),
&nam_address,
&proposal.proposal.author,
None,
)
.await?;
let proposal = proposal
.validate(
&governance_parameters,
current_epoch,
author_balance,
self.tx.force,
)
.map_err(|e| {
crate::error::TxSubmitError::InvalidProposal(e.to_string())
})?;
tx::build_pgf_stewards_proposal(context, self, proposal).await
} else {
let proposal = DefaultProposal::try_from(
self.proposal_data.as_ref(),
)
.map_err(|e| {
crate::error::TxSubmitError::FailedGovernaneProposalDeserialize(
e.to_string(),
)
})?;
let nam_address = context.native_token();
let author_balance = rpc::get_token_balance(
context.client(),
&nam_address,
&proposal.proposal.author,
None,
)
.await?;
let proposal = proposal
.validate(
&governance_parameters,
current_epoch,
author_balance,
self.tx.force,
)
.map_err(|e| {
crate::error::TxSubmitError::InvalidProposal(e.to_string())
})?;
tx::build_default_proposal(context, self, proposal).await
}
}
}
/// Transaction to vote on a proposal
#[derive(Clone, Debug)]
pub struct VoteProposal<C: NamadaTypes = SdkTypes> {
/// Common tx arguments
pub tx: Tx<C>,
/// Proposal id
pub proposal_id: u64,
/// The vote
pub vote: String,
/// The address of the voter
pub voter_address: C::Address,
/// Path to the TX WASM code file
pub tx_code_path: PathBuf,
}
impl<C: NamadaTypes> TxBuilder<C> for VoteProposal<C> {
fn tx<F>(self, func: F) -> Self
where
F: FnOnce(Tx<C>) -> Tx<C>,
{
VoteProposal {
tx: func(self.tx),
..self
}
}
}
impl<C: NamadaTypes> VoteProposal<C> {
/// Proposal id
pub fn proposal_id(self, proposal_id: u64) -> Self {
Self {
proposal_id,
..self
}
}
/// The vote
pub fn vote(self, vote: String) -> Self {
Self { vote, ..self }
}
/// The address of the voter
pub fn voter(self, voter_address: C::Address) -> Self {
Self {
voter_address,
..self
}
}
/// Path to the TX WASM code file
pub fn tx_code_path(self, tx_code_path: PathBuf) -> Self {
Self {
tx_code_path,
..self
}
}
}
impl VoteProposal {
/// Build a transaction from this builder
pub async fn build(
&self,
context: &impl Namada,
) -> crate::error::Result<(namada_tx::Tx, SigningTxData)> {
let current_epoch = rpc::query_epoch(context.client()).await?;
tx::build_vote_proposal(context, self, current_epoch).await
}
}
/// Transaction to initialize a new account
#[derive(Clone, Debug)]
pub struct TxInitAccount<C: NamadaTypes = SdkTypes> {
/// Common tx arguments
pub tx: Tx<C>,
/// Path to the VP WASM code file for the new account
pub vp_code_path: PathBuf,
/// Path to the TX WASM code file
pub tx_code_path: PathBuf,
/// Public key for the new account
pub public_keys: Vec<C::PublicKey>,
/// The account multisignature threshold
pub threshold: Option<u8>,
}
impl<C: NamadaTypes> TxBuilder<C> for TxInitAccount<C> {
fn tx<F>(self, func: F) -> Self
where
F: FnOnce(Tx<C>) -> Tx<C>,
{
TxInitAccount {
tx: func(self.tx),
..self
}
}
}
impl<C: NamadaTypes> TxInitAccount<C> {
/// A vector of public key to associate with the new account
pub fn public_keys(self, public_keys: Vec<C::PublicKey>) -> Self {
Self {
public_keys,
..self
}
}
/// A threshold to associate with the new account
pub fn threshold(self, threshold: u8) -> Self {
Self {
threshold: Some(threshold),
..self
}
}
/// Path to the VP WASM code file
pub fn vp_code_path(self, vp_code_path: PathBuf) -> Self {
Self {
vp_code_path,
..self
}
}
/// Path to the TX WASM code file
pub fn tx_code_path(self, tx_code_path: PathBuf) -> Self {
Self {
tx_code_path,
..self
}
}
}
impl TxInitAccount {
/// Build a transaction from this builder
pub async fn build(
&self,
context: &impl Namada,
) -> crate::error::Result<(namada_tx::Tx, SigningTxData)> {
tx::build_init_account(context, self).await
}
}
/// Transaction to initialize a new account
#[derive(Clone, Debug)]
pub struct TxBecomeValidator<C: NamadaTypes = SdkTypes> {
/// Common tx arguments
pub tx: Tx<C>,
/// Address of an account that will become a validator.
pub address: C::Address,
/// Signature scheme
pub scheme: SchemeType,
/// Consensus key
pub consensus_key: Option<C::PublicKey>,
/// Ethereum cold key
pub eth_cold_key: Option<C::PublicKey>,
/// Ethereum hot key
pub eth_hot_key: Option<C::PublicKey>,
/// Protocol key
pub protocol_key: Option<C::PublicKey>,
/// Commission rate
pub commission_rate: Dec,
/// Maximum commission rate change
pub max_commission_rate_change: Dec,
/// The validator email
pub email: String,
/// The validator description
pub description: Option<String>,
/// The validator website
pub website: Option<String>,
/// The validator's discord handle
pub discord_handle: Option<String>,
/// The validator's avatar
pub avatar: Option<String>,
/// The validator's name
pub name: Option<String>,
/// Path to the TX WASM code file
pub tx_code_path: PathBuf,
/// Don't encrypt the keypair
pub unsafe_dont_encrypt: bool,
}
impl<C: NamadaTypes> TxBuilder<C> for TxBecomeValidator<C> {
fn tx<F>(self, func: F) -> Self
where
F: FnOnce(Tx<C>) -> Tx<C>,
{
TxBecomeValidator {
tx: func(self.tx),
..self
}
}
}
impl<C: NamadaTypes> TxBecomeValidator<C> {
/// Set the address
pub fn address(self, address: C::Address) -> Self {
Self { address, ..self }
}
/// Set the commission rate
pub fn commission_rate(self, commission_rate: Dec) -> Self {
Self {
commission_rate,
..self
}
}
/// Set the max commission rate change
pub fn max_commission_rate_change(
self,
max_commission_rate_change: Dec,
) -> Self {
Self {
max_commission_rate_change,
..self
}
}
/// Set the email
pub fn email(self, email: String) -> Self {
Self { email, ..self }
}
/// Path to the TX WASM code file
pub fn tx_code_path(self, tx_code_path: PathBuf) -> Self {
Self {
tx_code_path,
..self
}
}
}
impl TxBecomeValidator {
/// Build the tx
pub async fn build(