Leaderboard
Background
The trio of leaderboards offered through Sediment enables quick scaffolding of an initial on-chain feature. A leaderboard is a great feature for games looking to begin coming into Web3; but don’t want to go all-in on “Web3”.
This is because the leaderboard is designed to be set by either an authoritative server or another smart contract.
This guarantees that leaderboards cannot be manipulated maliciously by users directly.
There are currently three types of leaderboards:
-
Leaderboard - Uses address as the key for the ranking. Great for games that have user wallets whether non-custodial or otherwise.
-
BytesLeaderboard - Uses bytes32 as the key for the ranking. Great for games that have identifiers that they want to hash before putting on-chain.
-
String Leaderboard - Uses string as the key for the ranking. Great for anonymous user names that are pre-validated by a server.
Installation
Using Leaderboard
Setup Contract
Contract Interaction
API
Solidity API
Leaderboard
A contract to manage a leaderboard of scores associated with Ethereum addresses
This contract inherits from the Authority contract and uses role-based access control
User
Struct representing a user entry in the leaderboard
resetIndex
leaderboard
maxLength
paused
IncrementalReset
Reset
SubmitScore
SubmitScoreAndAdd
constructor
Constructor to initialize the contract
Parameters
Name | Type | Description |
---|---|---|
_maxLength | uint32 | The maximum length of the leaderboard |
submitScore
Submit a new high score for a user
Only callable by the SERVER_ROLE
Parameters
Name | Type | Description |
---|---|---|
user | address | The Ethereum address of the user |
highScore | uint64 | The new high score to be submitted |
length
Get the current length of the leaderboard
Return Values
Name | Type | Description |
---|---|---|
[0] | uint32 | The length of the leaderboard |
_addToLeaderboard
Internal function to add a new user to the leaderboard
Parameters
Name | Type | Description |
---|---|---|
user | address | The Ethereum address of the user |
highScore | uint64 | The new high score to be added |
index | uint32 | The index at which the new user should be inserted |
reset
Reset the entire leaderboard
Only callable by the MANAGER_ROLE Will revert if the leaderboard length is greater than 25,000
incrementalReset
Perform an incremental reset of the leaderboard
Only callable by the MANAGER_ROLE Removes up to 1,500 entries from the leaderboard
_sort
Internal function to sort the leaderboard array using the quicksort algorithm
Parameters
Name | Type | Description |
---|---|---|
arr | struct Leaderboard.User[] | The leaderboard array to be sorted |
left | int256 | The left index of the subarray to be sorted |
right | int256 | The right index of the subarray to be sorted |
Solidity API
BytesLeaderboard
A contract to manage a leaderboard of scores associated with user bytes32 values
This contract inherits from the Authority contract and uses role-based access control
User
Struct representing a user entry in the leaderboard
resetIndex
leaderboard
maxLength
paused
IncrementalReset
Reset
SubmitScore
SubmitScoreAndAdd
constructor
Constructor to initialize the contract
Parameters
Name | Type | Description |
---|---|---|
_maxLength | uint32 | The maximum length of the leaderboard |
submitScore
Submit a new high score for a user
Only callable by the SERVER_ROLE
Parameters
Name | Type | Description |
---|---|---|
user | bytes32 | The bytes32 identifier of the user |
highScore | uint64 | The new high score to be submitted |
length
Get the current length of the leaderboard
Return Values
Name | Type | Description |
---|---|---|
[0] | uint32 | The length of the leaderboard |
_addToLeaderboard
Internal function to add a new user to the leaderboard
Parameters
Name | Type | Description |
---|---|---|
user | bytes32 | The bytes32 identifier of the user |
highScore | uint64 | The new high score to be added |
index | uint32 | The index at which the new user should be inserted |
reset
Reset the entire leaderboard
Only callable by the MANAGER_ROLE Will revert if the leaderboard length is greater than 25,000
incrementalReset
Perform an incremental reset of the leaderboard
Only callable by the MANAGER_ROLE Removes up to 1,500 entries from the leaderboard
_sort
Internal function to sort the leaderboard array using the quicksort algorithm
Parameters
Name | Type | Description |
---|---|---|
arr | struct BytesLeaderboard.User[] | The leaderboard array to be sorted |
left | int256 | The left index of the subarray to be sorted |
right | int256 | The right index of the subarray to be sorted |
Solidity API
StringLeaderboard
A contract to manage a leaderboard of scores associated with string identifiers
This contract inherits from the Authority contract and uses role-based access control
User
Struct representing a user entry in the leaderboard
resetIndex
leaderboard
maxLength
paused
IncrementalReset
Reset
SubmitScore
SubmitScoreAndAdd
constructor
Constructor to initialize the contract
Parameters
Name | Type | Description |
---|---|---|
_maxLength | uint32 | The maximum length of the leaderboard |
submitScore
Submit a new high score for a user
Only callable by the SERVER_ROLE
Parameters
Name | Type | Description |
---|---|---|
user | string | The string identifier of the user |
highScore | uint64 | The new high score to be submitted |
length
Get the current length of the leaderboard
Return Values
Name | Type | Description |
---|---|---|
[0] | uint32 | The length of the leaderboard |
_addToLeaderboard
Internal function to add a new user to the leaderboard
Parameters
Name | Type | Description |
---|---|---|
user | string | The string identifier of the user |
highScore | uint64 | The new high score to be added |
index | uint32 | The index at which the new user should be inserted |
reset
Reset the entire leaderboard
Only callable by the MANAGER_ROLE Will revert if the leaderboard length is greater than 25,000
incrementalReset
Perform an incremental reset of the leaderboard
Only callable by the MANAGER_ROLE Removes up to 1,500 entries from the leaderboard
_sort
Internal function to sort the leaderboard array using the quicksort algorithm
Parameters
Name | Type | Description |
---|---|---|
arr | struct StringLeaderboard.User[] | The leaderboard array to be sorted |
left | int256 | The left index of the subarray to be sorted |
right | int256 | The right index of the subarray to be sorted |