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

docs: Extracting winners from a closed auction guide #577

Merged
merged 30 commits into from
Jun 23, 2021
Merged
Changes from 10 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
838e6a8
docs: auctions
TarikGul Jun 4, 2021
f3e0dd0
docs: add more steps
TarikGul Jun 4, 2021
df26cba
comments
TarikGul Jun 7, 2021
c0e28f0
fix: rewrite guide
TarikGul Jun 7, 2021
f2e3153
fix: rewrite guide
TarikGul Jun 7, 2021
71a076a
fix: typos for rough draft
TarikGul Jun 7, 2021
7f6968f
Update AUCTIONS_WINNERS.md
TarikGul Jun 8, 2021
b856dd3
Update AUCTIONS_WINNERS.md
TarikGul Jun 8, 2021
aa8f85b
Update AUCTIONS_WINNERS.md
TarikGul Jun 8, 2021
b58491b
Update AUCTIONS_WINNERS.md
TarikGul Jun 8, 2021
e1e0766
fix:docs
TarikGul Jun 9, 2021
ad3d850
Update AUCTIONS_WINNERS.md
TarikGul Jun 9, 2021
e3e21c6
docs: refactor
TarikGul Jun 9, 2021
ce6a842
Merge branch 'tarik-auctions-guide' of github.com:paritytech/substrat…
TarikGul Jun 9, 2021
bc7eee2
docs: small fix
TarikGul Jun 9, 2021
2588ee0
docs: small fix
TarikGul Jun 9, 2021
176f9c3
Update AUCTIONS_WINNERS.md
TarikGul Jun 9, 2021
b5143fc
docs: remove relationships, take out indexing and replace
TarikGul Jun 9, 2021
876b49e
Update AUCTIONS_WINNERS.md
TarikGul Jun 9, 2021
7030b98
docs: fix indexing
TarikGul Jun 9, 2021
be4badf
docs: note for links
TarikGul Jun 9, 2021
61d0897
docs: fix ongoing auction
TarikGul Jun 9, 2021
4e1aeb9
docs: fix another paragraph
TarikGul Jun 9, 2021
0e4099c
docs: guides folder
TarikGul Jun 10, 2021
4f0f067
docs: fix
TarikGul Jun 11, 2021
6f3c731
docs: typo
TarikGul Jun 11, 2021
fd2aa19
Merge branch 'master' of github.com:paritytech/substrate-api-sidecar …
TarikGul Jun 15, 2021
750fbc3
docs: final touched
TarikGul Jun 15, 2021
fcc71d8
Update guides/AUCTIONS_WINNERS.md
TarikGul Jun 15, 2021
3a9eaed
Update guides/AUCTIONS_WINNERS.md
TarikGul Jun 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions AUCTIONS_WINNERS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<h1 style="text-align: center">WinnersData</h1>

## Summary

This is a guide to leverage substrate-api-sidecar's endpoints in order to extract the necessary data to know the winner's of an auction based on its `auctionIndex`.
TarikGul marked this conversation as resolved.
Show resolved Hide resolved


## Indexing

To find the winner of a completed auction we will need to know the block number the auction ended at. Since Sidecar is a stateless API and auction info is wiped once it is over we need the block number to make historic queries to a state when the relevant data was stored (keep reading for details).

TarikGul marked this conversation as resolved.
Show resolved Hide resolved
This can be done by leveraging the `/experimental/paras/auctions/current` endpoint.
TarikGul marked this conversation as resolved.
Show resolved Hide resolved

When there is an ongoing auction the return object will look like following below:

```
{
"at": {
"hash": "string",
TarikGul marked this conversation as resolved.
Show resolved Hide resolved
"height": "string"
},
"beginEnd": "string",
"finishEnd": "string",
"phase": "opening",
"auctionIndex": "string",
"leasePeriods": [
"string"
],
"winning": [
{
"bid": {
"accountId": "string",
"paraId": "string",
"amount": "string"
},
"leaseSet": [
"string"
]
}
]
}
```

Important keys here are `finishEnd` which is the last block at which the auction will take place, `auctionIndex` which will act as your point of reference for that specific auction, and `leasePeriods` which are the available lease period indexes that may be bid on for the specific `auctionIndex`.
TarikGul marked this conversation as resolved.
Show resolved Hide resolved

** Details of each key **

`finishEnd`: This is the last block at which the auction will take place. Indexing it will allow you to query the block at which the auction ended. From that block you can extract the lease winning related events. (To query the block: GET `/blocks/{finishEnd}`.)
TarikGul marked this conversation as resolved.
Show resolved Hide resolved

`auctionIndex`: The unique identifier for the auction.

`leasePeriods`: These are the available lease period indexes that may be bid on for the specific `auctionIndex`. You can use this to create two seperate relationships explained below.
TarikGul marked this conversation as resolved.
Show resolved Hide resolved


## Relationships

With the information you are now tracking there are two main relationships to note.

1. `auctionIndex` => `leasePeriods` (Lease Periods that are actively being auctioned off)

2. `paraId` => `leasePeriods` (Lease Periods that have been won by `paraId`).
TarikGul marked this conversation as resolved.
Show resolved Hide resolved


## Walkthrough

### Relay-chain Dev Setup for auctions (SKIP if you already have a development setup)!
-------------

The begining of this guide will start by briefly introducing setting up a simple parachain enviornment if you dont already know how or dont have one set for local developement.

Start by cloning [polkadot](https://github.com/paritytech/polkadot) and checking out the `rococo-v1` branch. NOTE: Before compiling make sure to adjust the [EndingPeriod](https://github.com/paritytech/polkadot/blob/rococo-v1/runtime/rococo/src/lib.rs#L745) to `100`, and [LeasePeriod](https://github.com/paritytech/polkadot/blob/rococo-v1/runtime/rococo/src/lib.rs#L761) to `100` so the auction time is fit for local development. You can then follow this [gist](https://gist.github.com/emostov/a58f887fce6af8a9b4aa2421114836c5) to get your alice and bob dev validator nodes up. Then you can call those extrinsic calls with the polkadot-js UI.
TarikGul marked this conversation as resolved.
Show resolved Hide resolved

### Using Sidecar to find the auction winners

`/experimental/paras/auctions/current` ENDPOINT

An auction will either be in two phases `starting` or `ending`. During this period when querying the endpoint you will receive a `finishEnd` which will denote the last block where the `AuctionClosed` event will take place as well as the `Leased` event. These events will be under the `on_initialize` key.
TarikGul marked this conversation as resolved.
Show resolved Hide resolved

It is important to index this block for the current `auctionIndex` because this will be your source of truth of where the winners of the auction are stored.
TarikGul marked this conversation as resolved.
Show resolved Hide resolved

The next important key is the `leasePeriods` that corresponds to the current `auctionIndex`. We will use these available `leasePeriods` to compare the winning results and see which `paraId`'s took which slots.
TarikGul marked this conversation as resolved.
Show resolved Hide resolved

By this point we have the below relationship.
TarikGul marked this conversation as resolved.
Show resolved Hide resolved

EX: (This is just an example, format the data from the endpoint however is necessary)
TarikGul marked this conversation as resolved.
Show resolved Hide resolved
```javascript
auctionIndex: {
leasePeriods: [
"String"
],
finishEnd: '200'
}
```

`/blocks/:blockId` ENDPOINT

Once the auction is over, its time to query the `blocks` endpoint at the block height given in the `finishEnd` field, and retrieve the events inside of `on_initialize`.

Example Response
```
{
authorId: ....,
extrinsics:....
...
on_initialize: {
events: [
{
"method": {
"pallet": "slots",
"method": "Leased"
},
"data": [
'1000', // ParaId
'5HpG9w8EBLe5XCrbczpwq5TSXvedjrBGCwqxK1iQ7qUsSWFc', // AccountId
'1', // LeasePeriod (begining of the lease period)
'4', // LeasePeriod (the count of the lease period)
'10000', // Balance (extra balance reserved)
'1000000000', // Balance (total balance)
]
},
{
"method": {
"pallet": "auctions",
"method": "AuctionClosed"
},
"data": [
...
]
}
]
}
}
```

Now that you have all the paraId's that won slots for that auction, you can compare it with the data relavant to the `auctionIndex`. Comparing the `leasePeriod`'s that are available during the active auction, to the `leasePeriod`'s that have been won and denoted in the `Leased` events (there may be multiple if there are multiple winners) will give you all the winners for that auction.
TarikGul marked this conversation as resolved.
Show resolved Hide resolved