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

Automation price poc #420

Merged
merged 7 commits into from
Sep 26, 2023
Merged

Automation price poc #420

merged 7 commits into from
Sep 26, 2023

Conversation

v9n
Copy link
Member

@v9n v9n commented Sep 5, 2023

Automation pallet that trigger tasks based on price. Currently price is updated externally. More docs will come when this form into a runable state to explain the detail.

We organized the data structure into this tree:

  • [chain][exchange][asset1, asset2]: pricedata

Initialize asset

Screenshot 2023-09-21 at 6 44 27 PM

We can repeat this for many assets per that chain/exchange.

Example call 1

This example, we Initialize a pair for (DOT, USDT) on astar chain, artswap exchange. and also authorized "ALICE" wallet to update this asset price. In practice, "ALICE" should be updated to the wallet address of the oracle provider.

0xc8011461737461722061727468737761700c444f5410555344540604d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d

local polkadotjs decoded link

Example call 2

This example, we Initialize a pair for (KSM, USDC) on moonbeam chain, uniswap exchange. and also authorized "BOB" wallet to update this asset price

0xc801206d6f6f6e6265616d1c756e69737761700c4b534d10555344430604d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d

local polkadotjs decode link

Now we can inspect storage

Screenshot 2023-09-22 at 3 40 15 PM

Screenshot 2023-09-22 at 3 42 29 PM

Update price

Price can be update at the same time. It has these below argument

Screenshot 2023-09-21 at 6 51 12 PM

  • chains: list of chain
  • exchanges: list of exchange
  • assets1: list of asset1
  • assets2: list of asset2
  • prices: note the decimal to submit properly. only interger, not float
  • submittedAt: epoch at submission time.
  • rounds: an incremental value when submit price. We haven't used this parameter yet in our code base

Example we can update the price of 3 pairs

  • chains: [astar, astar, astar]
  • exchanges: [arthswap, arthswap, arthswap]
  • assets1: [ksm, tur, dot]
  • assets2: [tur, usdc, udsdt]
  • prices: [600, 6000, 3000]
  • submittedAt: [14243553252523,14243553252523,14243553252523,]
  • rounds: [200, 200, 200]

all the array obviously need to have the same length so we can construct the pair.

Example encoded call hash

Here we will update prices of 2 assets in a single extrinsic

0xc80208146173746172206d6f6f6e6265616d082061727468737761701c756e6973776170080c444f540c4b534d081055534454105553444308404b4c0000000000000000000000000080b14f0100000000000000000000000008d05c0abf8a010000000000000000000022170e65000000000000000000000000088403000000000000000000000000000057020000000000000000000000000000

polkadotjs decode link

now we can inspect the storage

Screenshot 2023-09-22 at 3 46 38 PM Screenshot 2023-09-22 at 3 47 08 PM

Task Schedule

Very similar to automation time.

Screenshot 2023-09-21 at 6 52 23 PM

  • chain: target chain
  • exchange: exchange name
  • asset1: first asset of the pair
  • asset2: second asset of the pair
  • expiredAt: if task has not executed after this, they will be removed
  • triggerFunction: accept only 2 values for now
    - gt
    - lt
  • triggerParam: an array of argument to pass to trigger function. At this moment, we only need to pass a signle element, which is the target price to be trigger

Screenshot 2023-09-21 at 6 55 40 PM

In this example task will be trigger when the TUR/USDC price is >= 60000000(note the decimal of the asset pair)

Example call

This is a task that is trigger when KSM/USDT is worth 5.5USDT

0xc8051461737461722061727468737761700c444f54105553445430f8f01b92010000000000000000000008677404c0d6540000000000000000000000000003010100411f03010100411f03010100411f97b33c19460f0d000000000000000000d0120000d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d01000a073048656c6c6f20776f726c6421ee59452a8949037b7efcf88a491000d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d

polkadotjs decode link

Chain Storage

These can be used to evaluate the storage

  • AssetRegistry: contains asset and pair metadata info, immediately created when initialize an asset
  • PriceRegistry: contains the lastest data only of asset price

For task insertion, it's stored in:

  • Tasks: hashmap of task id -> task data
  • SortedTasksIndex: hashmap of (chain, exchange, (asset1, asset2), trigger_function) -> BTreeMap(<price-target, [task_id])>
  • TaskQueue: Active task to be executed.
  • AccountTasks

@v9n v9n changed the title Automation price poc [wip]Automation price poc Sep 5, 2023
@v9n v9n marked this pull request as draft September 5, 2023 16:24
@v9n v9n force-pushed the automation-price-poc branch 3 times, most recently from 98fce28 to 5e56b96 Compare September 8, 2023 17:14
assets2: Vec<AssetName>,
prices: Vec<AssetPrice>,
submitted_at: Vec<u128>,
rounds: Vec<u128>,
Copy link
Member

Choose a reason for hiding this comment

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

Please add comments to explain the purpose of this function and its parameters.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi @imstar15

I had added the parameter document. On the PR link I also added encoded call data so one can click and decode

@v9n v9n force-pushed the automation-price-poc branch 5 times, most recently from cf31683 to df4e8d8 Compare September 18, 2023 01:32
@v9n v9n added the enhancement New feature or request label Sep 18, 2023
@v9n v9n self-assigned this Sep 18, 2023
@v9n v9n added the WIP work in progress, not ready for review label Sep 18, 2023
@v9n v9n force-pushed the automation-price-poc branch from 57a851a to e4986d4 Compare September 18, 2023 02:53
Copy link
Member

@chrisli30 chrisli30 left a comment

Choose a reason for hiding this comment

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

The existing code looks well organized so far.

@@ -260,8 +363,7 @@ pub mod pallet {
LiquidityRestrictions,
/// Too Many Assets Created
AssetLimitReached,
/// Direction Not Supported
DirectionNotSupported,
BadVersion,
Copy link
Member

@chrisli30 chrisli30 Sep 21, 2023

Choose a reason for hiding this comment

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

Other error code are self-explanatory but this BadVersion needs some comments.

Copy link
Member Author

Choose a reason for hiding this comment

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

comment is added

pallets/automation-price/src/lib.rs Show resolved Hide resolved
@v9n v9n force-pushed the automation-price-poc branch from d0cbc1c to 58c07c0 Compare September 22, 2023 02:16
@chrisli30
Copy link
Member

chrisli30 commented Sep 23, 2023

The trigger worked! I was able to see the task inserted into the automationPrice.accountTasks and to trigger it with a price movement. One feedback is that we should emit a automationPrice.AssetUpdated event during update (there is an automationPrice.AssetCreated for creation).

Encoded call on Turing - WRSTR, USDT, lt, 100
0xc8051c736869627579612061727468737761701457525354521055534454d52a0e65000000000000000000000000086c74046400000000000000000000000000000003010100411f03010100411f03010100411f2b857121f64f13000000000000000000d0120000d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d01000a073048656c6c6f20776f726c6421ee59452a8949077b123270018a491800d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d

Task is inserted into AccountTask
CleanShot 2023-09-22 at 16 54 00

Price fetch history from Turing’s PriceRegistry
CleanShot 2023-09-22 at 17 05 01

Events triggered by price movement from 200 to 80
CleanShot 2023-09-22 at 17 04 38

@chrisli30
Copy link
Member

The end result should be a ethereumChecked.transact event on Shibuya/Rocstar, but since I didn’t have an EVM smart contract set up locally I used system.remarkWithEvent.

Eventually, we should see three events in order.

  1. ethereumChecked.transact
  2. proxy.proxyExecuted
  3. xcmQueue.success

CleanShot 2023-09-22 at 17 17 25

An un-optimization pallet to enable an oracle price feed, price update,
task scheduling based on price and task triggering based on asset price
compartion with target price

The weight is hardcode, document is still lack off and data structure is
still being finalized.
@v9n v9n force-pushed the automation-price-poc branch from ef4c861 to aaa6260 Compare September 25, 2023 19:31
@v9n v9n changed the title [wip]Automation price poc Automation price poc Sep 25, 2023
@v9n
Copy link
Member Author

v9n commented Sep 25, 2023

Hi @chrisli30 @imstar15

Please give a final round of review. There are still many optimization we can do but we would like to merge this, have it run with the xcm-demo-price app and iterate.

Pending items I will need to address next is keep track in this issue: #434

@v9n v9n mentioned this pull request Sep 25, 2023
11 tasks
@v9n v9n marked this pull request as ready for review September 26, 2023 01:42
Copy link
Member

@chrisli30 chrisli30 left a comment

Choose a reason for hiding this comment

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

No major changes from my last review, so I believe that this is good enough to merge. I left one more minor comment though.

@@ -417,7 +416,7 @@ fn test_shift_tasks_movement_through_price_changes() {

let task_ids = get_task_ids_from_events();
let task_id1 = task_ids.get(task_ids.len().wrapping_sub(3)).unwrap();
let task_id2 = task_ids.get(task_ids.len().wrapping_sub(2)).unwrap();
let _task_id2 = task_ids.get(task_ids.len().wrapping_sub(2)).unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

Should the format of these task_id variable names be consistent?

@v9n v9n merged commit b6187fc into master Sep 26, 2023
@v9n v9n deleted the automation-price-poc branch September 26, 2023 20:15
@Lalsha40
Copy link

I need pr no

@imp1ssible23
Copy link

imp1ssible23 commented Mar 18, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request WIP work in progress, not ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants