Skip to content

Commit 3296b67

Browse files
authored
feat(api): prd network stake amount history (#40)
* LICENSE * docs(contributing): add contributing docs (#37) * docs(contributing): add contributing docs * docs(contributing): add contributing docs * feat(index): index history of total stake amount (#38) * feat(index): index history of total stake amount * feat(index): index history of total stake amount * feat(index): index history of total stake amount * feat(index): index history of total stake amount * feat(index): index history of total stake amount * feat(index): index history of total stake amount * feat(index): index history of total stake amount * feat(index): index history of total stake amount (#39)
1 parent a7d6936 commit 3296b67

File tree

11 files changed

+790
-4
lines changed

11 files changed

+790
-4
lines changed

CODE_OF_CONDUCT.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
We as members, contributors, and leaders pledge to make participation in our
6+
community a harassment-free experience for everyone, regardless of age, body
7+
size, visible or invisible disability, ethnicity, sex characteristics, gender
8+
identity and expression, level of experience, education, socio-economic status,
9+
nationality, personal appearance, race, religion, or sexual identity
10+
and orientation.
11+
12+
We pledge to act and interact in ways that contribute to an open, welcoming,
13+
diverse, inclusive, and healthy community.
14+
15+
## Our Standards
16+
17+
Examples of behavior that contributes to a positive environment for our
18+
community include:
19+
20+
- Demonstrating empathy and kindness toward other people
21+
- Being respectful of differing opinions, viewpoints, and experiences
22+
- Giving and gracefully accepting constructive feedback
23+
- Accepting responsibility and apologizing to those affected by our mistakes,
24+
and learning from the experience
25+
- Focusing on what is best not just for us as individuals, but for the
26+
overall community
27+
28+
Examples of unacceptable behavior include:
29+
30+
- The use of sexualized language or imagery, and sexual attention or
31+
advances of any kind
32+
- Trolling, insulting or derogatory comments, and personal or political attacks
33+
- Public or private harassment
34+
- Publishing others' private information, such as a physical or email
35+
address, without their explicit permission
36+
- Other conduct which could reasonably be considered inappropriate in a
37+
professional setting
38+
39+
## Enforcement Responsibilities
40+
41+
Community leaders are responsible for clarifying and enforcing our standards of
42+
acceptable behavior and will take appropriate and fair corrective action in
43+
response to any behavior that they deem inappropriate, threatening, offensive,
44+
or harmful.
45+
46+
Community leaders have the right and responsibility to remove, edit, or reject
47+
comments, commits, code, wiki edits, issues, and other contributions that are
48+
not aligned to this Code of Conduct, and will communicate reasons for moderation
49+
decisions when appropriate.
50+
51+
## Scope
52+
53+
This Code of Conduct applies within all community spaces, and also applies when
54+
an individual is officially representing the community in public spaces.
55+
Examples of representing our community include using an official e-mail address,
56+
posting via an official social media account, or acting as an appointed
57+
representative at an online or offline event.
58+
59+
## Enforcement
60+
61+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
62+
reported to the community leaders responsible for enforcement at
63+
contact@piplabs.xyz.
64+
All complaints will be reviewed and investigated promptly and fairly.
65+
66+
All community leaders are obligated to respect the privacy and security of the
67+
reporter of any incident.
68+
69+
## Enforcement Guidelines
70+
71+
Community leaders will follow these Community Impact Guidelines in determining
72+
the consequences for any action they deem in violation of this Code of Conduct:
73+
74+
### 1. Correction
75+
76+
**Community Impact**: Use of inappropriate language or other behavior deemed
77+
unprofessional or unwelcome in the community.
78+
79+
**Consequence**: A private, written warning from community leaders, providing
80+
clarity around the nature of the violation and an explanation of why the
81+
behavior was inappropriate. A public apology may be requested.
82+
83+
### 2. Warning
84+
85+
**Community Impact**: A violation through a single incident or series
86+
of actions.
87+
88+
**Consequence**: A warning with consequences for continued behavior. No
89+
interaction with the people involved, including unsolicited interaction with
90+
those enforcing the Code of Conduct, for a specified period of time. This
91+
includes avoiding interactions in community spaces as well as external channels
92+
like social media. Violating these terms may lead to a temporary or
93+
permanent ban.
94+
95+
### 3. Temporary Ban
96+
97+
**Community Impact**: A serious violation of community standards, including
98+
sustained inappropriate behavior.
99+
100+
**Consequence**: A temporary ban from any sort of interaction or public
101+
communication with the community for a specified period of time. No public or
102+
private interaction with the people involved, including unsolicited interaction
103+
with those enforcing the Code of Conduct, is allowed during this period.
104+
Violating these terms may lead to a permanent ban.
105+
106+
### 4. Permanent Ban
107+
108+
**Community Impact**: Demonstrating a pattern of violation of community
109+
standards, including sustained inappropriate behavior, harassment of an
110+
individual, or aggression toward or disparagement of classes of individuals.
111+
112+
**Consequence**: A permanent ban from any sort of public interaction within
113+
the community.
114+
115+
## Attribution
116+
117+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118+
version 2.0, available at
119+
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
120+
121+
Community Impact Guidelines were inspired by [Mozilla's code of conduct
122+
enforcement ladder](https://github.com/mozilla/diversity).
123+
124+
[homepage]: https://www.contributor-covenant.org
125+
126+
For answers to common questions about this code of conduct, see the FAQ at
127+
https://www.contributor-covenant.org/faq. Translations are available at
128+
https://www.contributor-covenant.org/translations.

CONTRIBUTING.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Contributing to Story
2+
Thank you for considering making contributions to Story and related repositories! Below you can find what's the best way that you can help.
3+
4+
## Coding Guideline
5+
6+
[Go coding guidelines](https://go.dev/wiki/CodeReviewComments)
7+
8+
[Good commit messages](https://chris.beams.io/posts/git-commit/)
9+
10+
## Bug Reports
11+
12+
* Ensure your issue [has not already been reported][1]. It may already be fixed!
13+
* Include the steps you carried out to produce the problem.
14+
* Include the behavior you observed along with the behavior you expected, and
15+
why you expected it.
16+
* Include any relevant stack traces or debugging output.
17+
18+
## Feature Requests
19+
20+
We welcome feedback with or without pull requests. If you have an idea for how
21+
to improve the project, great! All we ask is that you take the time to write a
22+
clear and concise explanation of what need you are trying to solve. If you have
23+
thoughts on _how_ it can be solved, include those too!
24+
25+
The best way to see a feature added, however, is to submit a pull request.
26+
27+
## Pull Requests
28+
29+
* Before creating your pull request, it's usually worth asking if the code
30+
you're planning on writing will actually be considered for merging. You can
31+
do this by [opening an issue][1] and asking. It may also help give the
32+
maintainers context for when the time comes to review your code.
33+
34+
* Ensure your [commit messages are well-written][2]. This can double as your
35+
pull request message, so it pays to take the time to write a clear message.
36+
37+
* Add tests for your feature. You should be able to look at other tests for
38+
examples. If you're unsure, don't hesitate to [open an issue][1] and ask!
39+
40+
* Submit your pull request!
41+
- Fork the repository on GitHub.
42+
- Make your changes on your fork repository.
43+
- Submit a PR.
44+
45+
* Each PR needs to link to a github issue. Please open a github issue first to describe the code changes you plan to make and the problem it solves. Then link the issue in your PR.
46+
47+
* PR title and body follows [conventional commit][3].
48+
- Title template: `type(app/pkg): concise description`
49+
- See [allowed types][4]
50+
- Description must be concise: lower case, no punctuation, no more than 50 characters.
51+
- Scope must be concise: only a one or two folders; e.g. 'client/cmd' or 'github' or '*'
52+
53+
## Find something to work on
54+
55+
We are always in need of help, be it fixing documentation, reporting bugs or writing some code.
56+
Look at places where you feel best coding practices aren't followed, code refactoring is needed or tests are missing.
57+
58+
If you have questions about the development process,
59+
feel free to [file an issue](https://github.com/piplabs/story-staking-api/issues/new).
60+
61+
## Code Review
62+
63+
To make it easier for your PR to receive reviews, consider the reviewers will need you to:
64+
65+
* follow [good coding guidelines](https://go.dev/wiki/CodeReviewComments).
66+
* write [good commit messages](https://chris.beams.io/posts/git-commit/).
67+
* break large changes into a logical series of smaller patches which individually make easily understandable changes, and in aggregate solve a broader issue.
68+
69+
[1]: https://github.com/piplabs/story-staking-api/issues
70+
[2]: https://chris.beams.io/posts/git-commit/#seven-rules
71+
[3]: https://www.conventionalcommits.org/en/v1.0.0
72+
[4]: https://github.com/conventional-changelog/commitlint/tree/master/%40commitlint/config-conventional#type-enum

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 PIP Labs
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

db/cl_block.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,13 @@ func GetLatestCLBlock(db *gorm.DB) (*CLBlock, error) {
3636

3737
return &clBlk, nil
3838
}
39+
40+
func GetCLBlocks(db *gorm.DB, heights []int64) ([]*CLBlock, error) {
41+
clBlocks := make([]*CLBlock, 0)
42+
43+
if err := db.Where("height IN (?)", heights).Find(&clBlocks).Error; err != nil {
44+
return nil, err
45+
}
46+
47+
return clBlocks, nil
48+
}

db/cl_total_stake.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package db
2+
3+
import (
4+
"gorm.io/gorm"
5+
"gorm.io/gorm/clause"
6+
)
7+
8+
type CLTotalStake struct {
9+
ID uint64 `gorm:"primarykey"`
10+
11+
UpdateAt int64 `gorm:"not null;column:update_at;index:idx_cl_total_stake_update_at,unique"`
12+
TotalStakeAmount int64 `gorm:"not null;column:total_stake_amount"`
13+
}
14+
15+
func (CLTotalStake) TableName() string {
16+
return "cl_total_stakes"
17+
}
18+
19+
func GetLatestCLTotalStake(db *gorm.DB) (*CLTotalStake, error) {
20+
var stake CLTotalStake
21+
22+
if err := db.Order("update_at DESC").First(&stake).Error; err != nil {
23+
return nil, err
24+
}
25+
26+
return &stake, nil
27+
}
28+
29+
func GetCLTotalStakes(db *gorm.DB) ([]*CLTotalStake, error) {
30+
var stakes []*CLTotalStake
31+
32+
if err := db.Order("update_at ASC").Find(&stakes).Error; err != nil {
33+
return nil, err
34+
}
35+
36+
return stakes, nil
37+
}
38+
39+
func GetLatestCLTotalStakeBefore(db *gorm.DB, timestamp int64) (*CLTotalStake, error) {
40+
var stake CLTotalStake
41+
42+
if err := db.Where("update_at <= ?", timestamp).Order("update_at DESC").First(&stake).Error; err != nil {
43+
return nil, err
44+
}
45+
46+
return &stake, nil
47+
}
48+
49+
func GetCLTotalStakesAfter(db *gorm.DB, timestamp int64) ([]*CLTotalStake, error) {
50+
var stakes []*CLTotalStake
51+
52+
if err := db.Where("update_at > ?", timestamp).Order("update_at ASC").Find(&stakes).Error; err != nil {
53+
return nil, err
54+
}
55+
56+
return stakes, nil
57+
}
58+
59+
func InsertCLTotalStake(db *gorm.DB, indexer string, stake *CLTotalStake) error {
60+
return db.Transaction(func(tx *gorm.DB) error {
61+
if err := tx.Clauses(clause.OnConflict{
62+
Columns: []clause.Column{{Name: "update_at"}},
63+
DoNothing: true,
64+
}).Create(stake).Error; err != nil {
65+
return err
66+
}
67+
68+
return nil
69+
})
70+
}
71+
72+
func BatchUpsertCLTotalStakes(db *gorm.DB, indexer string, stakes []*CLTotalStake, height int64) error {
73+
return db.Transaction(func(tx *gorm.DB) error {
74+
if err := tx.Clauses(clause.OnConflict{
75+
Columns: []clause.Column{{Name: "update_at"}},
76+
DoUpdates: clause.Assignments(map[string]interface{}{
77+
"total_stake_amount": gorm.Expr("excluded.total_stake_amount"),
78+
}),
79+
}).CreateInBatches(stakes, 100).Error; err != nil {
80+
return err
81+
}
82+
83+
return UpdateIndexPoint(tx, indexer, height)
84+
})
85+
}

db/index_point.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package db
22

33
import (
4+
"time"
5+
46
"gorm.io/gorm"
57
"gorm.io/gorm/clause"
68
)
@@ -31,6 +33,20 @@ func GetIndexPoint(db *gorm.DB, indexer string) (*IndexPoint, error) {
3133
return &indexPoint, nil
3234
}
3335

36+
func GetIndexPointTime(db *gorm.DB, indexer string) (time.Time, error) {
37+
var blockTime time.Time
38+
39+
if err := db.Table("index_points").
40+
Select("cl_blocks.time").
41+
Joins("join cl_blocks on cl_blocks.height = index_points.block_height").
42+
Where("index_points.indexer = ?", indexer).
43+
Scan(&blockTime).Error; err != nil {
44+
return time.Time{}, err
45+
}
46+
47+
return blockTime, nil
48+
}
49+
3450
func UpdateIndexPoint(db *gorm.DB, indexer string, blockHeight int64) error {
3551
return db.Model(&IndexPoint{}).Where("indexer = ?", indexer).Update("block_height", blockHeight).Error
3652
}

0 commit comments

Comments
 (0)