Skip to content

Commit

Permalink
Merge pull request #189 from trifle-labs/handoff-to-peter
Browse files Browse the repository at this point in the history
Handoff to peter
  • Loading branch information
okwme authored Jul 15, 2024
2 parents 2be8ebb + e7d3512 commit 45fc35f
Show file tree
Hide file tree
Showing 24 changed files with 99 additions and 144 deletions.
23 changes: 11 additions & 12 deletions contracts/ExternalMetadata.sol
Original file line number Diff line number Diff line change
Expand Up @@ -240,25 +240,24 @@ contract ExternalMetadata is Ownable {
string(
abi.encodePacked(
"[",
'{"trait_type":"date","value":"',
'{"trait_type":"Day","value":"',
StringsExtended.toString(date),
'"}, {"trait_type":"day","value":"',
StringsExtended.toString(day),
'"}, {"trait_type":"month","value":"',
StringsExtended.toString(month),
'"}, {"trait_type":"year","value":"',
'"}, {"trait_type":"Month","value":"',
StringsExtended.toString(year),
'"}, {"trait_type":"1st-address","value":"',
'-',
Math.log10(month) + 1 == 1 ? "0" : "",
StringsExtended.toString(month),
'"}, {"trait_type":"1st Place","value":"',
StringsExtended.toHexString(fastestAddress),
'"}, {"trait_type":"1st-time","value":"',
'"}, {"trait_type":"1st Place Time","value":"',
StringsExtended.toString(fastestTime),
'"}, {"trait_type":"2nd-address","value":"',
'"}, {"trait_type":"2nd Place","value":"',
StringsExtended.toHexString(secondFastestAddress),
'"}, {"trait_type":"2nd-time","value":"',
'"}, {"trait_type":"2nd Place Time","value":"',
StringsExtended.toString(secondFastestTime),
'"}, {"trait_type":"3rd-address","value":"',
'"}, {"trait_type":"3rd Place","value":"',
StringsExtended.toHexString(thirdFastestAddress),
'"}, {"trait_type":"3rd-time","value":"',
'"}, {"trait_type":"3rd Place Time","value":"',
StringsExtended.toString(thirdFastestTime),
'"}]'
)
Expand Down
9 changes: 9 additions & 0 deletions scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,15 @@ const deployContracts = async (options) => {
]

returnObject.verificationData = verificationData
} else if (networkinfo['chainId'] == 12345) {
await deployer.sendTransaction({
to: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
value: ethers.utils.parseEther('1.0')
})
await deployer.sendTransaction({
to: '0xc795344b1b30E3CfEE1AFA1D5204B141940CF445',
value: ethers.utils.parseEther('1.0')
})
}

return returnObject
Expand Down
4 changes: 4 additions & 0 deletions server/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ PGPASSWORD=""
PGDATABASE=shovel
PGPORT=5432

MAINNET_RPC=
SEPOLIA_RPC=
GARNET_RPC=
BASE_SEPOLIA_RPC=
2 changes: 1 addition & 1 deletion server/contractData/12345-AnybodyProblem.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"address": "0x03025B79e75edE42CD6cfa73aBc7c8473d1583B5",
"address": "0x751DB7255687d9E5dC6DfCA48a9E1ED8A7d10516",
"chain": { "chainId": 12345, "name": "unknown" }
}
2 changes: 1 addition & 1 deletion server/contractData/12345-ExternalMetadata.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"address": "0x2643AA39900b53caeF3fE77faB588CF49C57Ac95",
"address": "0x8a15316f759e54449F36D7ca386e5268Db81FfC9",
"chain": { "chainId": 12345, "name": "unknown" }
}
2 changes: 1 addition & 1 deletion server/contractData/12345-Game_2_250Verifier.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"address": "0x53e196360c9AAB9c9AE9aD41BFaB76E766083a73",
"address": "0x7496EF7031b76AcD02c6f73A4421AFB4A42Fca4c",
"chain": { "chainId": 12345, "name": "unknown" }
}
2 changes: 1 addition & 1 deletion server/contractData/12345-Game_3_250Verifier.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"address": "0x6986BF30d9091F73352d7509FC5D796ef68CDd12",
"address": "0xE877CDACBB7827d4232Cde5f8de58371F144a0A4",
"chain": { "chainId": 12345, "name": "unknown" }
}
2 changes: 1 addition & 1 deletion server/contractData/12345-Game_4_250Verifier.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"address": "0x941dDe88e163c64DA03bbB3baEb17F4D6d6E1F02",
"address": "0xC8A395E3b82e515F88e0Ef548124c114f16cE9E3",
"chain": { "chainId": 12345, "name": "unknown" }
}
2 changes: 1 addition & 1 deletion server/contractData/12345-Game_5_125Verifier.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"address": "0x6E84AB6dCD0B5F4410767ea644778cCbF6Ed77fB",
"address": "0xDCe09254dD3592381b6A5b7a848B29890b656e01",
"chain": { "chainId": 12345, "name": "unknown" }
}
2 changes: 1 addition & 1 deletion server/contractData/12345-Game_6_125Verifier.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"address": "0x144Ab3451D4A03DE5b28e41F994060a7AfA27C9e",
"address": "0xd384A4A6F54F61d29b5e656DdadE31C84E2e4EfA",
"chain": { "chainId": 12345, "name": "unknown" }
}
2 changes: 1 addition & 1 deletion server/contractData/12345-Speedruns.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"address": "0x2524a59da2729fa225B099a802c94C7a8f322571",
"address": "0x55c7d3136cCdd9Adc7CFA576e6B20154CD51b716",
"chain": { "chainId": 12345, "name": "unknown" }
}
58 changes: 2 additions & 56 deletions server/contractData/ABI-12345-AnybodyProblem.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions server/contractData/ABI-12345-ExternalMetadata.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions server/contractData/ABI-12345-Speedruns.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"license": "MIT",
"type": "module",
"scripts": {
"shovel": "OUTPUT=1 bun run shovel-config.ts > config.json && ./shovel --config config.json",
"shovel": "OUTPUT=1 bun run shovel-config.ts > config.json && DATABASE_URL=postgresql://${USER}@localhost:5432/shovel ./shovel --config config.json",
"shovel:clean": "dropdb shovel && createdb shovel && rm config.json",
"dev": "bun run --hot src/index.ts",
"start": "./shovel --config config.json & bun run src/index.ts",
"smoke": "bun run tsc && bun test"
Expand Down
19 changes: 16 additions & 3 deletions server/shovel-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import { AnybodyProblem, Speedruns } from './contracts'
import { ethers } from 'ethers'
import { camelToSnakeCase } from './src/util'

export type Chain = 'mainnet' | 'sepolia' | 'garnet' | 'base_sepolia'
export type Chain =
| 'mainnet'
| 'sepolia'
| 'garnet'
| 'base_sepolia'
| 'localhost'
type KnownSource = Source & { name: Chain }

const mainnet: KnownSource = {
Expand All @@ -27,6 +32,14 @@ const sepolia: KnownSource = {
concurrency: 1
}

const localhost: KnownSource = {
name: 'localhost',
chain_id: 12345,
url: 'http://localhost:8545',
batch_size: 1000,
concurrency: 1
}

const garnet: KnownSource = {
name: 'garnet',
chain_id: 17069,
Expand Down Expand Up @@ -55,7 +68,7 @@ const STARTING_BLOCK = {
// mainnet: BigInt('2067803')
sepolia: BigInt('5716600'),
garnet: BigInt('2067803'),
base_sepolia: BigInt('11476234')
base_sepolia: BigInt('11912350')
}

// n.b. sources must match ABI in contracts to correctly sync
Expand Down Expand Up @@ -153,7 +166,7 @@ async function integrationFor(
if (process.env.OUTPUT) {
;(async function main() {
let integrations = await Promise.all([
integrationFor('Speedruns', 'Transfer', [
integrationFor('Speedruns', 'TransferSingle', [
['block_num DESC', 'tx_idx DESC', 'log_idx DESC']
]),
integrationFor('AnybodyProblem', 'RunCreated'),
Expand Down
2 changes: 1 addition & 1 deletion server/shovel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('shovel sanity test', () => {
// public | problems_body_removed | 0

expect(rows).toBeDefined()
expect(rowCount).toBeGreaterThanOrEqual(7)
expect(rowCount).toBeGreaterThanOrEqual(4)

// TODO: uncomment when there's data
// const tablesWithRows = rows.filter((row) => row.rows_n > 0)
Expand Down
9 changes: 8 additions & 1 deletion server/src/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ <h1>Anybody API /sse/base_sepolia</h1>
while (true) {
const { value, done } = await reader.read()
if (done) break
sseData.innerHTML += value + '<br><br><br>'
const prettyValue = JSON.stringify(
JSON.parse(
value.substring(value.indexOf('data:') + 5, value.indexOf('id:'))
),
null,
2
)
sseData.innerHTML += prettyValue + '<hr><hr><hr>'
window.scrollTo(0, document.body.scrollHeight)
}
</script>
Expand Down
2 changes: 1 addition & 1 deletion server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async function setupListener() {
db.query(`LISTEN "${source.name}-anybody_problem_level_solved"`)
db.query(`LISTEN "${source.name}-anybody_problem_run_created"`)
db.query(`LISTEN "${source.name}-anybody_problem_run_solved"`)
db.query(`LISTEN "${source.name}-speedruns_transfer"`)
db.query(`LISTEN "${source.name}-speedruns_transfer_single"`)
}

await Promise.all(
Expand Down
57 changes: 20 additions & 37 deletions server/src/leaderboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,28 @@ async function calculateDailyLeaderboard(day: number, chain: Chain) {
SELECT
run_id,
level,
MIN(time) AS fastest_time
FROM
MIN(time) AS fastest_time,
player
FROM
anybody_problem_level_solved
WHERE
day = ${day}
AND src_name = $1
GROUP BY
run_id, level
run_id, level, player
),
cumulative_times AS (
SELECT
run_id,
SUM(time) AS total_time
SUM(time) AS total_time,
player
FROM
anybody_problem_level_solved
WHERE
day = ${day}
AND src_name = $1
GROUP BY
run_id
run_id, player
HAVING
COUNT(level) = ${MAX_BODY_COUNT - 1}
),
Expand All @@ -79,23 +81,26 @@ async function calculateDailyLeaderboard(day: number, chain: Chain) {
run_id,
level,
fastest_time,
ROW_NUMBER() OVER (PARTITION BY level ORDER BY fastest_time) AS rank
ROW_NUMBER() OVER (PARTITION BY level ORDER BY fastest_time) AS rank,
player
FROM
fastest_times
),
ranked_cumulative_times AS (
SELECT
run_id,
total_time,
ROW_NUMBER() OVER (ORDER BY total_time) AS rank
ROW_NUMBER() OVER (ORDER BY total_time) AS rank,
player
FROM
cumulative_times
),
leaderboard AS (
SELECT
level,
run_id,
fastest_time AS time
fastest_time AS time,
player
FROM
ranked_fastest_times
WHERE
Expand All @@ -104,46 +109,24 @@ async function calculateDailyLeaderboard(day: number, chain: Chain) {
SELECT
NULL AS level,
run_id,
total_time AS time
total_time AS time,
player
FROM
ranked_cumulative_times
WHERE
rank <= ${DAILY_CATEGORY_LIMIT}
),
latest_transactions AS (
SELECT
token_id,
"to",
ROW_NUMBER() OVER (PARTITION BY token_id ORDER BY block_num DESC, tx_idx DESC, log_idx DESC) AS rn
FROM
speedruns_transfer
WHERE
token_id IN (SELECT run_id FROM leaderboard)
AND src_name = $1
),
current_players AS (
SELECT
token_id,
concat('0x', encode("to", 'hex')) as player
FROM
latest_transactions
WHERE
rn = 1
)
SELECT
leaderboard.level,
leaderboard.run_id,
leaderboard.time,
current_players.player
concat('0x', encode(player, 'hex')) as player
FROM
leaderboard
LEFT JOIN current_players ON leaderboard.run_id = current_players.token_id
ORDER BY
COALESCE(leaderboard.level, 0), leaderboard.time;
`

const result = await db.query(q, [chain])

function scores(rows: any[]): SpeedScore[] {
return rows.map((r: any) => ({
problemId: r.problem_id,
Expand Down Expand Up @@ -259,21 +242,21 @@ players_with_most_levels_solved AS (
)
SELECT
category,
player,
concat('0x', encode(player, 'hex')) as player,
metric
FROM
leaderboard
UNION ALL
SELECT
'Most Days Played' AS category,
player,
concat('0x', encode(player, 'hex')) as player,
days_played AS metric
FROM
players_with_most_days_played
UNION ALL
SELECT
'Most Solved' AS category,
player,
concat('0x', encode(player, 'hex')) as player,
solve_count AS metric
FROM
players_with_most_levels_solved;
Expand Down Expand Up @@ -313,7 +296,7 @@ FROM
export async function updateLeaderboard(chain: Chain) {
const start = Date.now()

const seasonStart = 1717632000
const seasonStart = 1719532800

// calculate daily leaderboards
const today = currentDayInUnixTime()
Expand Down
2 changes: 1 addition & 1 deletion src/anybody.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class Anybody extends EventEmitter {
startingBodies: 1,
windowWidth: 1000,
windowHeight: 1000,
pixelDensity: window.devicePixelRatio, //4, // Math.min(4, 4 * (window.devicePixelRatio ?? 1)),
pixelDensity: typeof window !== 'undefined' ? window.devicePixelRatio : 4, //4, // Math.min(4, 4 * (window.devicePixelRatio ?? 1)),
scalingFactor: 10n ** 3n,
minDistanceSquared: 200 * 200,
G: NORMAL_GRAVITY, // Gravitational constant
Expand Down
2 changes: 1 addition & 1 deletion src/visuals.js
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,7 @@ export const Visuals = {
graphic.rotate(-rotate + body.velocity.heading() + this.p.PI / 2)
this.drawImageAsset(BADDIE_SVG.core, coreWidth, coreColor, graphic)
if (!body.backgroundOnly) {
this.drawImageAsset(BADDIE_SVG.face, coreWidth, undefined, graphic)
this.drawImageAsset(BADDIE_SVG.face, coreWidth, coreColor, graphic)

// pupils always looking at missile, if no missile, look at mouse
const target =
Expand Down
Loading

0 comments on commit 45fc35f

Please sign in to comment.