Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rework fee payment #1687

Merged
merged 41 commits into from
Dec 28, 2021
Merged

rework fee payment #1687

merged 41 commits into from
Dec 28, 2021

Conversation

zqhxuyuan
Copy link
Contributor

@zqhxuyuan zqhxuyuan commented Dec 8, 2021

closes: #1233

  • init charge fee pool from treasury
    • each foreign asset has one sub account of Pallet
    • on_runtime_upgrade, each sub account get PoolSize of native asset and ED of foreign token from treasury account
  • how to trigger swap if needed
    • trigger by block number
    • trigger by balance: each asset has native asset balance threshold,default to PoolSize div 5.
  • change ensure_can_charge_fee to use fix rate
    • use an switch to change to chage fee from pool
    • directly replace the swap_from_dex
  • add WeightTrader depend on the rate in the storage.
    • move to other package(i.e. runtime?), so that no depend on xcm related module.
    • more integration test when dynamic rate failed, then use fixed rate instead.
  • testcase

design work flow:
image

review point:

  • the initial_bootstrap_balance value is suitable? and also the trigger_threshold=initial_bootstrap_balance/5?
  • current each sub account has same initial_bootstrap_balance, do they need different setting?

discussion:

  • do we need ED limitation for the treasury fee pool account? if trigger swap from dex, the min_target_amount of DEX::swap_with_exact_supply then should be ED + account.
  • what if swap from dex failed, should we get native asset from treasury account like the initial bootstrap phase? current, we only have one-shot initial phase. after that if treasury account's native asset not enough, it swap out native asset with its foreign asset(which gets from user account), then the treasury account's native asset goes up.

@zqhxuyuan zqhxuyuan changed the title fee payment first commit [WIP] rework fee payment [WIP] Dec 8, 2021
@zqhxuyuan zqhxuyuan changed the title rework fee payment [WIP] rework fee payment Dec 14, 2021
@zqhxuyuan zqhxuyuan marked this pull request as ready for review December 14, 2021 06:08
Copy link
Member

@xlc xlc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can change to type Currency: Currency<Self::AccountId, Balance = Balance> to avoid u128 and conversion

modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
@zqhxuyuan
Copy link
Contributor Author

Great! I think we could also replace all PalletBalanceOf<T> to Balance.

modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
runtime/karura/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
runtime/common/src/precompile/mock.rs Outdated Show resolved Hide resolved
runtime/mandala/src/lib.rs Outdated Show resolved Hide resolved
runtime/acala/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
Copy link
Member

@xlc xlc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thing to keep in mind is that people can always send money to system accounts. So need to make sure nothing goes bad if that happens.

modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
primitives/src/lib.rs Outdated Show resolved Hide resolved
runtime/acala/src/lib.rs Outdated Show resolved Hide resolved
…hmarks --features=with-mandala-runtime -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=module_transaction_payment --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --template=./templates/runtime-weight-template.hbs --output=./runtime/mandala/src/weights/
@zqhxuyuan
Copy link
Contributor Author

/bench runtime acala module_transaction_payment

@ghost
Copy link

ghost commented Dec 22, 2021

Finished benchmark for branch: fee_management_rework

Benchmark: Benchmark Runtime Acala Module

cargo run --release --color=never --bin=acala --features=runtime-benchmarks --features=with-acala-runtime -- benchmark --chain=acala-latest --steps=50 --repeat=20 --pallet=module_transaction_payment --extrinsic="*" --execution=wasm --wasm-execution=compiled --heap-pages=4096 --template=./templates/runtime-weight-template.hbs --output=./runtime/acala/src/weights/

Results

Pallet: "module_transaction_payment", Extrinsic: "set_alternative_fee_swap_path", Lowest values: [], Highest values: [], Steps: 50, Repeat: 20
Raw Storage Info

Storage: TransactionPayment AlternativeFeeSwapPath (r:0 w:1)

Median Slopes Analysis

-- Extrinsic Time --

Model:
Time ~= 5.148
µs

Reads = 0
Writes = 1

Min Squares Analysis

-- Extrinsic Time --

Model:
Time ~= 5.148
µs

Reads = 0
Writes = 1

Pallet: "module_transaction_payment", Extrinsic: "set_swap_balance_threshold", Lowest values: [], Highest values: [], Steps: 50, Repeat: 20
Raw Storage Info

Storage: TransactionPayment PoolSize (r:1 w:0)
Storage: TransactionPayment SwapBalanceThreshold (r:0 w:1)

Median Slopes Analysis

-- Extrinsic Time --

Model:
Time ~= 27.87
µs

Reads = 1
Writes = 1

Min Squares Analysis

-- Extrinsic Time --

Model:
Time ~= 27.87
µs

Reads = 1
Writes = 1

Pallet: "module_transaction_payment", Extrinsic: "enable_initial_pool", Lowest values: [], Highest values: [], Steps: 50, Repeat: 20
Raw Storage Info

Storage: TransactionPayment PoolSize (r:1 w:1)
Storage: TransactionPayment AlternativeFeeSwapPath (r:1 w:0)
Storage: Dex TradingPairStatuses (r:1 w:0)
Storage: Dex LiquidityPool (r:1 w:0)
Storage: Tokens Accounts (r:2 w:2)
Storage: System Account (r:2 w:2)
Storage: TransactionPayment TokenFixedRate (r:0 w:1)
Storage: TransactionPayment SwapBalanceThreshold (r:0 w:1)

Median Slopes Analysis

-- Extrinsic Time --

Model:
Time ~= 163.1
µs

Reads = 8
Writes = 7

Min Squares Analysis

-- Extrinsic Time --

Model:
Time ~= 163.1
µs

Reads = 8
Writes = 7

Pallet: "module_transaction_payment", Extrinsic: "on_finalize", Lowest values: [], Highest values: [], Steps: 50, Repeat: 20
Raw Storage Info

Storage: TransactionPayment NextFeeMultiplier (r:1 w:1)
Storage: System BlockWeight (r:1 w:0)

Median Slopes Analysis

-- Extrinsic Time --

Model:
Time ~= 13.58
µs

Reads = 2
Writes = 1

Min Squares Analysis

-- Extrinsic Time --

Model:
Time ~= 13.58
µs

Reads = 2
Writes = 1

Acala Benchmarking Bot and others added 2 commits December 22, 2021 16:14
…hmarks --features=with-acala-runtime -- benchmark --chain=acala-latest --steps=50 --repeat=20 --pallet=module_transaction_payment --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --template=./templates/runtime-weight-template.hbs --output=./runtime/acala/src/weights/
Copy link
Member

@xlc xlc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the name treasury account is over used and confusing. we should only use name treasury account to the real treasury, and call it fee pool account for the pallet sub account.

modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
runtime/karura/src/lib.rs Outdated Show resolved Hide resolved
modules/transaction-payment/src/lib.rs Outdated Show resolved Hide resolved
@zqhxuyuan
Copy link
Contributor Author

/bench runtime karura module_transaction_payment

@ghost
Copy link

ghost commented Dec 27, 2021

Finished benchmark for branch: fee_management_rework

Benchmark: Benchmark Runtime Karura Module

cargo run --release --color=never --bin=acala --features=runtime-benchmarks --features=with-karura-runtime -- benchmark --chain=karura-dev --steps=50 --repeat=20 --pallet=module_transaction_payment --extrinsic="*" --execution=wasm --wasm-execution=compiled --heap-pages=4096 --template=./templates/runtime-weight-template.hbs --output=./runtime/karura/src/weights/

Results

Pallet: "module_transaction_payment", Extrinsic: "set_alternative_fee_swap_path", Lowest values: [], Highest values: [], Steps: 50, Repeat: 20
Raw Storage Info

Storage: Balances Reserves (r:1 w:1)
Storage: TransactionPayment AlternativeFeeSwapPath (r:0 w:1)

Median Slopes Analysis

-- Extrinsic Time --

Model:
Time ~= 40.47
µs

Reads = 1
Writes = 2

Min Squares Analysis

-- Extrinsic Time --

Model:
Time ~= 40.47
µs

Reads = 1
Writes = 2

Pallet: "module_transaction_payment", Extrinsic: "set_swap_balance_threshold", Lowest values: [], Highest values: [], Steps: 50, Repeat: 20
Raw Storage Info

Storage: TransactionPayment PoolSize (r:1 w:0)
Storage: TransactionPayment SwapBalanceThreshold (r:0 w:1)

Median Slopes Analysis

-- Extrinsic Time --

Model:
Time ~= 24.7
µs

Reads = 1
Writes = 1

Min Squares Analysis

-- Extrinsic Time --

Model:
Time ~= 24.7
µs

Reads = 1
Writes = 1

Pallet: "module_transaction_payment", Extrinsic: "enable_charge_fee_pool", Lowest values: [], Highest values: [], Steps: 50, Repeat: 20
Raw Storage Info

Storage: TransactionPayment PoolSize (r:1 w:1)
Storage: TransactionPayment AlternativeFeeSwapPath (r:1 w:0)
Storage: Dex TradingPairStatuses (r:1 w:0)
Storage: Dex LiquidityPool (r:1 w:0)
Storage: Tokens Accounts (r:2 w:2)
Storage: System Account (r:2 w:2)
Storage: TransactionPayment TokenExchangeRate (r:0 w:1)
Storage: TransactionPayment SwapBalanceThreshold (r:0 w:1)

Median Slopes Analysis

-- Extrinsic Time --

Model:
Time ~= 147.2
µs

Reads = 8
Writes = 7

Min Squares Analysis

-- Extrinsic Time --

Model:
Time ~= 147.2
µs

Reads = 8
Writes = 7

Pallet: "module_transaction_payment", Extrinsic: "on_finalize", Lowest values: [], Highest values: [], Steps: 50, Repeat: 20
Raw Storage Info

Storage: TransactionPayment NextFeeMultiplier (r:1 w:1)
Storage: System BlockWeight (r:1 w:0)

Median Slopes Analysis

-- Extrinsic Time --

Model:
Time ~= 12.24
µs

Reads = 2
Writes = 1

Min Squares Analysis

-- Extrinsic Time --

Model:
Time ~= 12.24
µs

Reads = 2
Writes = 1

…hmarks --features=with-karura-runtime -- benchmark --chain=karura-dev --steps=50 --repeat=20 --pallet=module_transaction_payment --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --template=./templates/runtime-weight-template.hbs --output=./runtime/karura/src/weights/
@zqhxuyuan
Copy link
Contributor Author

/bench runtime mandala module_transaction_payment

@ghost
Copy link

ghost commented Dec 27, 2021

Finished benchmark for branch: fee_management_rework

Benchmark: Benchmark Runtime Mandala Module

cargo run --release --color=never --bin=acala --features=runtime-benchmarks --features=with-mandala-runtime -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=module_transaction_payment --extrinsic="*" --execution=wasm --wasm-execution=compiled --heap-pages=4096 --template=./templates/runtime-weight-template.hbs --output=./runtime/mandala/src/weights/

Results

Pallet: "module_transaction_payment", Extrinsic: "set_alternative_fee_swap_path", Lowest values: [], Highest values: [], Steps: 50, Repeat: 20
Raw Storage Info

Storage: Balances Reserves (r:1 w:1)
Storage: TransactionPayment AlternativeFeeSwapPath (r:0 w:1)

Median Slopes Analysis

-- Extrinsic Time --

Model:
Time ~= 42.86
µs

Reads = 1
Writes = 2

Min Squares Analysis

-- Extrinsic Time --

Model:
Time ~= 42.86
µs

Reads = 1
Writes = 2

Pallet: "module_transaction_payment", Extrinsic: "set_swap_balance_threshold", Lowest values: [], Highest values: [], Steps: 50, Repeat: 20
Raw Storage Info

Storage: TransactionPayment PoolSize (r:1 w:0)
Storage: TransactionPayment SwapBalanceThreshold (r:0 w:1)

Median Slopes Analysis

-- Extrinsic Time --

Model:
Time ~= 24.28
µs

Reads = 1
Writes = 1

Min Squares Analysis

-- Extrinsic Time --

Model:
Time ~= 24.28
µs

Reads = 1
Writes = 1

Pallet: "module_transaction_payment", Extrinsic: "enable_charge_fee_pool", Lowest values: [], Highest values: [], Steps: 50, Repeat: 20
Raw Storage Info

Storage: TransactionPayment PoolSize (r:1 w:1)
Storage: TransactionPayment AlternativeFeeSwapPath (r:1 w:0)
Storage: Dex TradingPairStatuses (r:1 w:0)
Storage: Dex LiquidityPool (r:1 w:0)
Storage: Tokens Accounts (r:2 w:2)
Storage: System Account (r:2 w:2)
Storage: TransactionPayment TokenExchangeRate (r:0 w:1)
Storage: TransactionPayment SwapBalanceThreshold (r:0 w:1)

Median Slopes Analysis

-- Extrinsic Time --

Model:
Time ~= 152.3
µs

Reads = 8
Writes = 7

Min Squares Analysis

-- Extrinsic Time --

Model:
Time ~= 152.3
µs

Reads = 8
Writes = 7

Pallet: "module_transaction_payment", Extrinsic: "on_finalize", Lowest values: [], Highest values: [], Steps: 50, Repeat: 20
Raw Storage Info

Storage: TransactionPayment NextFeeMultiplier (r:1 w:1)
Storage: System BlockWeight (r:1 w:0)

Median Slopes Analysis

-- Extrinsic Time --

Model:
Time ~= 12.8
µs

Reads = 2
Writes = 1

Min Squares Analysis

-- Extrinsic Time --

Model:
Time ~= 12.8
µs

Reads = 2
Writes = 1

…hmarks --features=with-mandala-runtime -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=module_transaction_payment --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --template=./templates/runtime-weight-template.hbs --output=./runtime/mandala/src/weights/
@zqhxuyuan
Copy link
Contributor Author

/bench runtime acala module_transaction_payment

@ghost
Copy link

ghost commented Dec 27, 2021

Finished benchmark for branch: fee_management_rework

Benchmark: Benchmark Runtime Acala Module

cargo run --release --color=never --bin=acala --features=runtime-benchmarks --features=with-acala-runtime -- benchmark --chain=acala-latest --steps=50 --repeat=20 --pallet=module_transaction_payment --extrinsic="*" --execution=wasm --wasm-execution=compiled --heap-pages=4096 --template=./templates/runtime-weight-template.hbs --output=./runtime/acala/src/weights/

Results

Pallet: "module_transaction_payment", Extrinsic: "set_alternative_fee_swap_path", Lowest values: [], Highest values: [], Steps: 50, Repeat: 20
Raw Storage Info

Storage: Balances Reserves (r:1 w:1)
Storage: TransactionPayment AlternativeFeeSwapPath (r:0 w:1)

Median Slopes Analysis

-- Extrinsic Time --

Model:
Time ~= 43.91
µs

Reads = 1
Writes = 2

Min Squares Analysis

-- Extrinsic Time --

Model:
Time ~= 43.91
µs

Reads = 1
Writes = 2

Pallet: "module_transaction_payment", Extrinsic: "set_swap_balance_threshold", Lowest values: [], Highest values: [], Steps: 50, Repeat: 20
Raw Storage Info

Storage: TransactionPayment PoolSize (r:1 w:0)
Storage: TransactionPayment SwapBalanceThreshold (r:0 w:1)

Median Slopes Analysis

-- Extrinsic Time --

Model:
Time ~= 26.39
µs

Reads = 1
Writes = 1

Min Squares Analysis

-- Extrinsic Time --

Model:
Time ~= 26.39
µs

Reads = 1
Writes = 1

Pallet: "module_transaction_payment", Extrinsic: "enable_charge_fee_pool", Lowest values: [], Highest values: [], Steps: 50, Repeat: 20
Raw Storage Info

Storage: TransactionPayment PoolSize (r:1 w:1)
Storage: TransactionPayment AlternativeFeeSwapPath (r:1 w:0)
Storage: Dex TradingPairStatuses (r:1 w:0)
Storage: Dex LiquidityPool (r:1 w:0)
Storage: Tokens Accounts (r:2 w:2)
Storage: System Account (r:2 w:2)
Storage: TransactionPayment TokenExchangeRate (r:0 w:1)
Storage: TransactionPayment SwapBalanceThreshold (r:0 w:1)

Median Slopes Analysis

-- Extrinsic Time --

Model:
Time ~= 155.4
µs

Reads = 8
Writes = 7

Min Squares Analysis

-- Extrinsic Time --

Model:
Time ~= 155.4
µs

Reads = 8
Writes = 7

Pallet: "module_transaction_payment", Extrinsic: "on_finalize", Lowest values: [], Highest values: [], Steps: 50, Repeat: 20
Raw Storage Info

Storage: TransactionPayment NextFeeMultiplier (r:1 w:1)
Storage: System BlockWeight (r:1 w:0)

Median Slopes Analysis

-- Extrinsic Time --

Model:
Time ~= 13.47
µs

Reads = 2
Writes = 1

Min Squares Analysis

-- Extrinsic Time --

Model:
Time ~= 13.47
µs

Reads = 2
Writes = 1

…hmarks --features=with-acala-runtime -- benchmark --chain=acala-latest --steps=50 --repeat=20 --pallet=module_transaction_payment --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --template=./templates/runtime-weight-template.hbs --output=./runtime/acala/src/weights/
Copy link
Member

@wangjj9219 wangjj9219 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zqhxuyuan zqhxuyuan merged commit fb19db6 into master Dec 28, 2021
@zqhxuyuan zqhxuyuan deleted the fee_management_rework branch December 28, 2021 00:41
@wangjj9219
Copy link
Member

@StrawberryFlavor need test it on testnet

@zqhxuyuan
Copy link
Contributor Author

A few note about test, ping me if you have question:

  • there'll be an transfer from treasury account to PalletId sub account to initialize charge fee pool
  • when user have enough foreign tokens, but don't have enough native asset, exchange fee directly from charge fee pool
  • when the charge fee pool is lt some threshold(which is gt ED and some reserve), trigger swap from dex
  • each foreign token has an sub-account. that's each token has an charge fee pool
  • after swap from dex, the charge fee pool get charge of native asset, and left only ED of foreign asset.
  • then user can continue exchange fee from charge fee pool again.

syan095 pushed a commit that referenced this pull request Jan 7, 2022
* origin/master: (102 commits)
  Fix collect_fee (#1754)
  Update HEADER-GPL3
  Update extrinsic-ordering-check-from-bin.yml (#1752)
  Update HEADER-GPL3
  bump version (#1751)
  Remove homa-lite from karura runtime (#1744)
  off-by-one (#1747)
  Revert "simulate exchange rate (#1742)" (#1746)
  simulate exchange rate (#1742)
  bump version (#1743)
  refactor homa (#1648)
  Update stable asset (#1741)
  add more info to events (#1740)
  Fix mandala swap path error (#1736)
  update stable asset (#1738)
  remove unnecessary code (#1735)
  fix currency id testing (#1733)
  rework fee payment (#1687)
  Add Deposit for Setting Alternative Fee Swap Path (#1730)
  Add register_erc20_asset and update_erc20_asset (#1731)
  ...

# Conflicts:
#	.github/workflows/coverage.yml.disabled
#	Cargo.lock
#	Cargo.toml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

XCM execution fee management
6 participants