Skip to content

Commit 13f2d48

Browse files
authored
Charge update fees per VAA submitted (#340)
* Charge update fees per VAA submitted * Rename UpdateFee struct to BaseUpdateFee * Update comment
1 parent dec5183 commit 13f2d48

File tree

4 files changed

+55
-32
lines changed

4 files changed

+55
-32
lines changed

aptos/contracts/sources/governance/governance.move

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ module pyth::governance {
320320
fun test_execute_governance_instruction_set_update_fee() {
321321
let initial_update_fee = 325;
322322
setup_test(100, 50, x"f06413c0148c78916554f134dcd17a7c8029a3a2bda475a4a1182305c53078bf", initial_update_fee);
323-
assert!(state::get_update_fee() == initial_update_fee, 1);
323+
assert!(state::get_base_update_fee() == initial_update_fee, 1);
324324

325325
// A VAA with:
326326
// - Emitter chain ID 50
@@ -339,7 +339,7 @@ module pyth::governance {
339339
assert!(state::get_last_executed_governance_sequence() == 1, 1);
340340

341341
let expected = 17000;
342-
assert!(state::get_update_fee() == expected, 1);
342+
assert!(state::get_base_update_fee() == expected, 1);
343343
}
344344

345345
#[test]

aptos/contracts/sources/governance/set_update_fee.move

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module pyth::set_update_fee {
1616
public(friend) fun execute(payload: vector<u8>) {
1717
let SetUpdateFee { mantissa, exponent } = from_byte_vec(payload);
1818
let fee = apply_exponent(mantissa, exponent);
19-
state::set_update_fee(fee);
19+
state::set_base_update_fee(fee);
2020
}
2121

2222
fun from_byte_vec(bytes: vector<u8>): SetUpdateFee {

aptos/contracts/sources/pyth.move

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,11 @@ module pyth::pyth {
126126
/// you need to call an entry function.
127127
///
128128
/// This function will charge an update fee, transferring some AptosCoin's
129-
/// from the given funder account to the Pyth contract. The amount of coins transferred can be
130-
/// queried with get_update_fee(). The signer must have sufficient account balance to
131-
/// pay this fee, otherwise the transaction will abort.
129+
/// from the given funder account to the Pyth contract. The amount of coins that will be transferred
130+
/// to perform this update can be queried with get_update_fee(&vaas). The signer must have sufficient
131+
/// account balance to pay this fee, otherwise the transaction will abort.
132132
public entry fun update_price_feeds_with_funder(account: &signer, vaas: vector<vector<u8>>) {
133-
let coins = coin::withdraw<AptosCoin>(account, get_update_fee());
133+
let coins = coin::withdraw<AptosCoin>(account, get_update_fee(&vaas));
134134
update_price_feeds(vaas, coins);
135135
}
136136

@@ -141,17 +141,17 @@ module pyth::pyth {
141141
/// should be used to fetch these VAAs from the Price Service. More information about this
142142
/// process can be found at https://docs.pyth.network/consume-data.
143143
///
144-
/// The given fee must contain a sufficient number of coins to pay the update fee.
145-
/// The update fee amount can be queried by calling get_update_fee().
144+
/// The given fee must contain a sufficient number of coins to pay the update fee for the given vaas.
145+
/// The update fee amount can be queried by calling get_update_fee(&vaas).
146146
public fun update_price_feeds(vaas: vector<vector<u8>>, fee: Coin<AptosCoin>) {
147+
// Charge the message update fee
148+
assert!(get_update_fee(&vaas) <= coin::value(&fee), error::insufficient_fee());
149+
coin::deposit(@pyth, fee);
150+
147151
// Update the price feed from each VAA
148152
while (!vector::is_empty(&vaas)) {
149153
update_price_feed_from_single_vaa(vector::pop_back(&mut vaas));
150154
};
151-
152-
// Charge the message update fee
153-
assert!(get_update_fee() <= coin::value(&fee), error::insufficient_fee());
154-
coin::deposit(@pyth, fee);
155155
}
156156

157157
fun update_price_feed_from_single_vaa(vaa: vector<u8>) {
@@ -198,7 +198,7 @@ module pyth::pyth {
198198
vaas: vector<vector<u8>>,
199199
price_identifiers: vector<vector<u8>>,
200200
publish_times: vector<u64>) {
201-
let coins = coin::withdraw<AptosCoin>(account, get_update_fee());
201+
let coins = coin::withdraw<AptosCoin>(account, get_update_fee(&vaas));
202202
update_price_feeds_if_fresh(vaas, price_identifiers, publish_times, coins);
203203
}
204204

@@ -361,9 +361,9 @@ module pyth::pyth {
361361
price_info::get_price_feed(&state::get_latest_price_info(price_identifier)))
362362
}
363363

364-
/// Get the number of AptosCoin's required to perform one batch update
365-
public fun get_update_fee(): u64 {
366-
state::get_update_fee()
364+
/// Get the number of AptosCoin's required to perform the given price updates.
365+
public fun get_update_fee(update_data: &vector<vector<u8>>): u64 {
366+
state::get_base_update_fee() * vector::length(update_data)
367367
}
368368

369369
// -----------------------------------------------------------------------------
@@ -450,6 +450,29 @@ module pyth::pyth {
450450
/// - payload corresponding to the batch price attestation of the prices returned by get_mock_price_infos()
451451
const TEST_VAAS: vector<vector<u8>> = vector[x"0100000000010036eb563b80a24f4253bee6150eb8924e4bdf6e4fa1dfc759a6664d2e865b4b134651a7b021b7f1ce3bd078070b688b6f2e37ce2de0d9b48e6a78684561e49d5201527e4f9b00000001001171f8dcb863d176e2c420ad6610cf687359612b6fb392e0642b0ca6b1f186aa3b0000000000000001005032574800030000000102000400951436e0be37536be96f0896366089506a59763d036728332d3e3038047851aea7c6c75c89f14810ec1c54c03ab8f1864a4c4032791f05747f560faec380a695d1000000000000049a0000000000000008fffffffb00000000000005dc0000000000000003000000000100000001000000006329c0eb000000006329c0e9000000006329c0e400000000000006150000000000000007215258d81468614f6b7e194c5d145609394f67b041e93e6695dcc616faadd0603b9551a68d01d954d6387aff4df1529027ffb2fee413082e509feb29cc4904fe000000000000041a0000000000000003fffffffb00000000000005cb0000000000000003010000000100000001000000006329c0eb000000006329c0e9000000006329c0e4000000000000048600000000000000078ac9cf3ab299af710d735163726fdae0db8465280502eb9f801f74b3c1bd190333832fad6e36eb05a8972fe5f219b27b5b2bb2230a79ce79beb4c5c5e7ecc76d00000000000003f20000000000000002fffffffb00000000000005e70000000000000003010000000100000001000000006329c0eb000000006329c0e9000000006329c0e40000000000000685000000000000000861db714e9ff987b6fedf00d01f9fea6db7c30632d6fc83b7bc9459d7192bc44a21a28b4c6619968bd8c20e95b0aaed7df2187fd310275347e0376a2cd7427db800000000000006cb0000000000000001fffffffb00000000000005e40000000000000003010000000100000001000000006329c0eb000000006329c0e9000000006329c0e400000000000007970000000000000001"];
452452

453+
#[test(aptos_framework = @aptos_framework)]
454+
fun test_get_update_fee(aptos_framework: &signer) {
455+
let single_update_fee = 50;
456+
let (burn_capability, mint_capability, coins) = setup_test(aptos_framework, 500, 23, x"5d1f252d5de865279b00c84bce362774c2804294ed53299bc4a0389a5defef92", vector[], 50, 0);
457+
458+
// Pass in a single VAA
459+
assert!(get_update_fee(&vector[
460+
x"fb1543888001083cf2e6ef3afdcf827e89b11efd87c563638df6e1995ada9f93",
461+
]) == single_update_fee, 1);
462+
463+
// Pass in multiple VAAs
464+
assert!(get_update_fee(&vector[
465+
x"4ee17a1a4524118de513fddcf82b77454e51be5d6fc9e29fc72dd6c204c0e4fa",
466+
x"c72fdf81cfc939d4286c93fbaaae2eec7bae28a5926fa68646b43a279846ccc1",
467+
x"d9a8123a793529c31200339820a3210059ecace6c044f81ecad62936e47ca049",
468+
x"84e4f21b3e65cef47fda25d15b4eddda1edf720a1d062ccbf441d6396465fbe6",
469+
x"9e73f9041476a93701a0b9c7501422cc2aa55d16100bec628cf53e0281b6f72f"
470+
]) == 250, 1);
471+
472+
coin::destroy_zero(coins);
473+
cleanup_test(burn_capability, mint_capability);
474+
}
475+
453476
#[test(aptos_framework = @aptos_framework)]
454477
#[expected_failure(abort_code = 6)]
455478
fun test_update_price_feeds_corrupt_vaa(aptos_framework: &signer) {
@@ -534,7 +557,7 @@ module pyth::pyth {
534557
coin::register<AptosCoin>(&funder);
535558
coin::deposit(funder_addr, coins);
536559

537-
assert!(get_update_fee() == update_fee, 1);
560+
assert!(get_update_fee(&TEST_VAAS) == update_fee, 1);
538561
assert!(coin::balance<AptosCoin>(signer::address_of(&funder)) == initial_balance, 1);
539562
assert!(coin::balance<AptosCoin>(@pyth) == 0, 1);
540563

@@ -545,10 +568,10 @@ module pyth::pyth {
545568
check_price_feeds_cached(&get_mock_price_infos());
546569

547570
// Check that the funder's balance has decreased by the update_fee amount
548-
assert!(coin::balance<AptosCoin>(signer::address_of(&funder)) == initial_balance - get_update_fee(), 1);
571+
assert!(coin::balance<AptosCoin>(signer::address_of(&funder)) == initial_balance - get_update_fee(&TEST_VAAS), 1);
549572

550573
// Check that the amount has been transferred to the Pyth contract
551-
assert!(coin::balance<AptosCoin>(@pyth) == get_update_fee(), 1);
574+
assert!(coin::balance<AptosCoin>(@pyth) == get_update_fee(&TEST_VAAS), 1);
552575

553576
cleanup_test(burn_capability, mint_capability);
554577
}
@@ -566,7 +589,7 @@ module pyth::pyth {
566589
coin::register<AptosCoin>(&funder);
567590
coin::deposit(funder_addr, coins);
568591

569-
assert!(get_update_fee() == update_fee, 1);
592+
assert!(get_update_fee(&TEST_VAAS) == update_fee, 1);
570593
assert!(coin::balance<AptosCoin>(signer::address_of(&funder)) == initial_balance, 1);
571594
assert!(coin::balance<AptosCoin>(@pyth) == 0, 1);
572595

@@ -820,7 +843,7 @@ module pyth::pyth {
820843
coin::register<AptosCoin>(&funder);
821844
coin::deposit(funder_addr, coins);
822845

823-
assert!(get_update_fee() == update_fee, 1);
846+
assert!(get_update_fee(&TEST_VAAS) == update_fee, 1);
824847
assert!(coin::balance<AptosCoin>(signer::address_of(&funder)) == initial_balance, 1);
825848
assert!(coin::balance<AptosCoin>(@pyth) == 0, 1);
826849

@@ -841,10 +864,10 @@ module pyth::pyth {
841864
check_price_feeds_cached(&get_mock_price_infos());
842865

843866
// Check that the funder's balance has decreased by the update_fee amount
844-
assert!(coin::balance<AptosCoin>(signer::address_of(&funder)) == initial_balance - get_update_fee(), 1);
867+
assert!(coin::balance<AptosCoin>(signer::address_of(&funder)) == initial_balance - get_update_fee(&TEST_VAAS), 1);
845868

846869
// Check that the amount has been transferred to the Pyth contract
847-
assert!(coin::balance<AptosCoin>(@pyth) == get_update_fee(), 1);
870+
assert!(coin::balance<AptosCoin>(@pyth) == get_update_fee(&TEST_VAAS), 1);
848871

849872
cleanup_test(burn_capability, mint_capability);
850873
}
@@ -887,7 +910,7 @@ module pyth::pyth {
887910
coin::register<AptosCoin>(&funder);
888911
coin::deposit(funder_addr, coins);
889912

890-
assert!(get_update_fee() == update_fee, 1);
913+
assert!(get_update_fee(&TEST_VAAS) == update_fee, 1);
891914
assert!(coin::balance<AptosCoin>(signer::address_of(&funder)) == initial_balance, 1);
892915
assert!(coin::balance<AptosCoin>(@pyth) == 0, 1);
893916

aptos/contracts/sources/state.move

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ module pyth::state {
2727
threshold_secs: u64,
2828
}
2929

30-
/// The fee charged per batch update
31-
struct UpdateFee has key {
30+
/// The update fee charged per VAA
31+
struct BaseUpdateFee has key {
3232
fee: u64,
3333
}
3434

@@ -72,7 +72,7 @@ module pyth::state {
7272
move_to(pyth, StalePriceThreshold{
7373
threshold_secs: stale_price_threshold,
7474
});
75-
move_to(pyth, UpdateFee{
75+
move_to(pyth, BaseUpdateFee{
7676
fee: update_fee,
7777
});
7878
let sources = set::new<DataSource>();
@@ -101,8 +101,8 @@ module pyth::state {
101101
borrow_global<StalePriceThreshold>(@pyth).threshold_secs
102102
}
103103

104-
public fun get_update_fee(): u64 acquires UpdateFee {
105-
borrow_global<UpdateFee>(@pyth).fee
104+
public fun get_base_update_fee(): u64 acquires BaseUpdateFee {
105+
borrow_global<BaseUpdateFee>(@pyth).fee
106106
}
107107

108108
public fun is_valid_data_source(data_source: DataSource): bool acquires DataSources {
@@ -173,8 +173,8 @@ module pyth::state {
173173
valid_governance_data_source.source = source;
174174
}
175175

176-
public(friend) fun set_update_fee(fee: u64) acquires UpdateFee {
177-
let update_fee = borrow_global_mut<UpdateFee>(@pyth);
176+
public(friend) fun set_base_update_fee(fee: u64) acquires BaseUpdateFee {
177+
let update_fee = borrow_global_mut<BaseUpdateFee>(@pyth);
178178
update_fee.fee = fee
179179
}
180180

0 commit comments

Comments
 (0)