forked from aptos-labs/aptos-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction_builder.rs
222 lines (190 loc) · 6.43 KB
/
transaction_builder.rs
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
// Copyright (c) Aptos
// SPDX-License-Identifier: Apache-2.0
use crate::{
move_types::account_address::AccountAddress,
types::{
chain_id::ChainId,
transaction::{authenticator::AuthenticationKey, RawTransaction, TransactionPayload},
},
};
use aptos_crypto::ed25519::Ed25519PublicKey;
use aptos_global_constants::{GAS_UNIT_PRICE, MAX_GAS_AMOUNT};
use aptos_types::transaction::{
authenticator::AuthenticationKeyPreimage, EntryFunction, ModuleBundle, Script,
};
pub use cached_packages::aptos_stdlib;
pub struct TransactionBuilder {
sender: Option<AccountAddress>,
sequence_number: Option<u64>,
payload: TransactionPayload,
max_gas_amount: u64,
gas_unit_price: u64,
expiration_timestamp_secs: u64,
chain_id: ChainId,
}
impl TransactionBuilder {
pub fn new(
payload: TransactionPayload,
expiration_timestamp_secs: u64,
chain_id: ChainId,
) -> Self {
Self {
payload,
chain_id,
expiration_timestamp_secs,
// TODO(Gas): double check this
max_gas_amount: MAX_GAS_AMOUNT,
gas_unit_price: std::cmp::max(GAS_UNIT_PRICE, 1),
sender: None,
sequence_number: None,
}
}
pub fn sender(mut self, sender: AccountAddress) -> Self {
self.sender = Some(sender);
self
}
pub fn sequence_number(mut self, sequence_number: u64) -> Self {
self.sequence_number = Some(sequence_number);
self
}
pub fn max_gas_amount(mut self, max_gas_amount: u64) -> Self {
self.max_gas_amount = max_gas_amount;
self
}
pub fn gas_unit_price(mut self, gas_unit_price: u64) -> Self {
self.gas_unit_price = gas_unit_price;
self
}
pub fn chain_id(mut self, chain_id: ChainId) -> Self {
self.chain_id = chain_id;
self
}
pub fn expiration_timestamp_secs(mut self, expiration_timestamp_secs: u64) -> Self {
self.expiration_timestamp_secs = expiration_timestamp_secs;
self
}
pub fn build(self) -> RawTransaction {
RawTransaction::new(
self.sender.expect("sender must have been set"),
self.sequence_number
.expect("sequence number must have been set"),
self.payload,
self.max_gas_amount,
self.gas_unit_price,
self.expiration_timestamp_secs,
self.chain_id,
)
}
}
#[derive(Clone, Debug)]
pub struct TransactionFactory {
max_gas_amount: u64,
gas_unit_price: u64,
transaction_expiration_time: u64,
chain_id: ChainId,
}
impl TransactionFactory {
pub fn new(chain_id: ChainId) -> Self {
Self {
// TODO(Gas): double check if this right
max_gas_amount: MAX_GAS_AMOUNT,
gas_unit_price: GAS_UNIT_PRICE,
transaction_expiration_time: 30,
chain_id,
}
}
pub fn with_max_gas_amount(mut self, max_gas_amount: u64) -> Self {
self.max_gas_amount = max_gas_amount;
self
}
pub fn with_gas_unit_price(mut self, gas_unit_price: u64) -> Self {
self.gas_unit_price = gas_unit_price;
self
}
pub fn with_transaction_expiration_time(mut self, transaction_expiration_time: u64) -> Self {
self.transaction_expiration_time = transaction_expiration_time;
self
}
pub fn with_chain_id(mut self, chain_id: ChainId) -> Self {
self.chain_id = chain_id;
self
}
pub fn payload(&self, payload: TransactionPayload) -> TransactionBuilder {
self.transaction_builder(payload)
}
pub fn module(&self, code: Vec<u8>) -> TransactionBuilder {
self.payload(TransactionPayload::ModuleBundle(ModuleBundle::singleton(
code,
)))
}
pub fn entry_function(&self, func: EntryFunction) -> TransactionBuilder {
self.payload(TransactionPayload::EntryFunction(func))
}
pub fn create_user_account(&self, public_key: &Ed25519PublicKey) -> TransactionBuilder {
let preimage = AuthenticationKeyPreimage::ed25519(public_key);
self.payload(aptos_stdlib::aptos_account_create_account(
AuthenticationKey::from_preimage(&preimage).derived_address(),
))
}
pub fn implicitly_create_user_account_and_transfer(
&self,
public_key: &Ed25519PublicKey,
amount: u64,
) -> TransactionBuilder {
let preimage = AuthenticationKeyPreimage::ed25519(public_key);
self.payload(aptos_stdlib::aptos_account_transfer(
AuthenticationKey::from_preimage(&preimage).derived_address(),
amount,
))
}
pub fn transfer(&self, to: AccountAddress, amount: u64) -> TransactionBuilder {
self.payload(aptos_stdlib::aptos_coin_transfer(to, amount))
}
pub fn account_transfer(&self, to: AccountAddress, amount: u64) -> TransactionBuilder {
self.payload(aptos_stdlib::aptos_account_transfer(to, amount))
}
pub fn mint(&self, to: AccountAddress, amount: u64) -> TransactionBuilder {
self.payload(aptos_stdlib::aptos_coin_mint(to, amount))
}
//
// Internal Helpers
//
pub fn script(&self, script: Script) -> TransactionBuilder {
self.payload(TransactionPayload::Script(script))
}
fn transaction_builder(&self, payload: TransactionPayload) -> TransactionBuilder {
TransactionBuilder {
sender: None,
sequence_number: None,
payload,
max_gas_amount: self.max_gas_amount,
gas_unit_price: self.gas_unit_price,
expiration_timestamp_secs: self.expiration_timestamp(),
chain_id: self.chain_id,
}
}
fn expiration_timestamp(&self) -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs()
+ self.transaction_expiration_time
}
}
pub struct DualAttestationMessage {
message: Box<[u8]>,
}
impl DualAttestationMessage {
pub fn new<M: Into<Vec<u8>>>(metadata: M, reciever: AccountAddress, amount: u64) -> Self {
let mut message = metadata.into();
bcs::serialize_into(&mut message, &reciever).unwrap();
bcs::serialize_into(&mut message, &amount).unwrap();
message.extend(b"@@$$APTOS_ATTEST$$@@");
Self {
message: message.into(),
}
}
pub fn message(&self) -> &[u8] {
&self.message
}
}