Skip to content

Commit 52e25e4

Browse files
committed
fix: better airdrop / key handling
1 parent ac39432 commit 52e25e4

30 files changed

+146
-51
lines changed

src/main/airdrop/airdrop.ts

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,53 @@
1-
import { PublicKey } from '@solana/web3.js'
1+
import { Keypair, PublicKey } from '@solana/web3.js'
22
import { readAllFromRPC } from '@staratlas/data-source'
33
import { PlayerProfile } from '@staratlas/player-profile'
44
import { CronJob } from 'cron'
5+
import * as fs from 'node:fs'
6+
import path from 'node:path'
57

68
import { config } from '../../config/index.js'
79
import { airdrop } from '../../lib/airdrop.js'
810
import { logger } from '../../logger.js'
911
import { connection } from '../../service/sol/index.js'
12+
import { loadKeypairFromFile } from '../../service/wallet'
1013
import { programs } from '../basedbot/lib/programs.js'
1114
import { sageGame } from '../basedbot/lib/sage/state/game.js'
12-
import { Faction } from '../basedbot/lib/util/galaxy-sectors-data.js'
13-
import { createAndInitializeCharacter } from '../basedbot/lib/util/profile.js'
15+
import { Faction } from '../basedbot/lib/util/galaxy-sectors-data'
16+
import { createAndInitializeCharacter } from '../basedbot/lib/util/profile'
1417

1518
let airdropCronJob: CronJob | undefined
1619

20+
const findKeyFiles = (dirPath: string): Map<string, Keypair> => {
21+
let keyMap = new Map<string, Keypair>()
22+
if (!fs.existsSync(dirPath)) {
23+
logger.warn(`${dirPath} does not exist`)
24+
return keyMap
25+
}
26+
27+
try {
28+
const entries = fs.readdirSync(dirPath, { withFileTypes: true })
29+
30+
for (const entry of entries) {
31+
const fullPath = path.join(dirPath, entry.name)
32+
33+
if (entry.isDirectory()) {
34+
keyMap = new Map([...keyMap, ...findKeyFiles(fullPath)])
35+
} else if (entry.isFile() && entry.name.endsWith('.json')) {
36+
try {
37+
const keyPair = loadKeypairFromFile(fullPath)
38+
keyMap.set(keyPair.publicKey.toBase58(), keyPair)
39+
} catch {
40+
logger.warn(`${entry.name} is not a Key`)
41+
}
42+
}
43+
}
44+
} catch (error) {
45+
console.error(`Error reading directory: ${dirPath}`, error)
46+
}
47+
48+
return keyMap
49+
}
50+
1751
export const create = async (): Promise<void> => {
1852
logger.info('Starting airdrop...')
1953
}
@@ -57,7 +91,25 @@ const airdropOrCreateProfile = async (user: PublicKey): Promise<void> => {
5791
)
5892
} else {
5993
logger.info(`Creating profile for ${user.toBase58()}`)
60-
await createAndInitializeCharacter(game, 'fleetbot', Faction.ONI)
94+
const keyDir = process.env.KEY_DIR || '/tmp/keys'
95+
logger.info(`Looking for keys in ${path.dirname(keyDir)}`)
96+
const keys = findKeyFiles(keyDir)
97+
98+
const keyPair = keys.get(user.toBase58())
99+
100+
if (keyPair) {
101+
const pubStr = keyPair.publicKey.toBase58()
102+
await createAndInitializeCharacter(
103+
game,
104+
`${pubStr.slice(0, 4)}...${pubStr.slice(-4)}`,
105+
Faction.ONI,
106+
keyPair,
107+
)
108+
} else {
109+
logger.warn(
110+
`No key found for ${user.toBase58()}, cannot create profile`,
111+
)
112+
}
61113
}
62114
}
63115

src/main/basedbot/basedbot.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,9 @@ const cleanupPods = async (player: Player, game: Game, starbase: Starbase) => {
225225

226226
logger.info(`Pod Cleanup Instructions: ${ixs.length}`)
227227

228-
await sendAndConfirmInstructions()(await ixReturnsToIxs(ixs, player.signer))
228+
await sendAndConfirmInstructions([keyPair])(
229+
await ixReturnsToIxs(ixs, player.signer),
230+
)
229231
}
230232

231233
const basedbot = async (botConfig: BotConfig) => {

src/main/basedbot/lib/programs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export type StarAtlasProgram<T extends Idl> = ProgramMethods<T>
2323
export const xpCategoryIds =
2424
config.sol.rpcEndpoint.includes('devnet') ||
2525
config.sol.rpcEndpoint.includes('validator') ||
26+
config.sol.rpcEndpoint.includes('universe') ||
2627
config.sol.rpcEndpoint.includes('localhost')
2728
? {
2829
dataRunningXpCategory:
@@ -50,6 +51,7 @@ export const xpCategoryIds =
5051
const programIds =
5152
config.sol.rpcEndpoint.includes('devnet') ||
5253
config.sol.rpcEndpoint.includes('validator') ||
54+
config.sol.rpcEndpoint.includes('universe') ||
5355
config.sol.rpcEndpoint.includes('localhost')
5456
? {
5557
sage: 'sAgezwJpDb1aHvzNr3o24cKjsETmFEKghBEyJ1askDi',

src/main/basedbot/lib/sage/act/create-fleet.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010

1111
import { logger } from '../../../../../logger.js'
1212
import { sendAndConfirmInstructions } from '../../../../../service/sol/send-and-confirm-tx.js'
13+
import { keyPair } from '../../../../../service/wallet'
1314
import { programs } from '../../programs.js'
1415
import { addShipToFleetIx } from '../ix/add-ship-to-fleet.js'
1516
import { createFleetIx } from '../ix/create-fleet.js'
@@ -136,7 +137,7 @@ export const createFleet = async (
136137
)
137138
}
138139

139-
await sendAndConfirmInstructions()(
140+
await sendAndConfirmInstructions([keyPair])(
140141
await ixReturnsToIxs(instructions, player.signer),
141142
)
142143
}

src/main/basedbot/lib/sage/act/deposit-cargo.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Game, Starbase } from '@staratlas/sage'
99
import BN from 'bn.js'
1010

1111
import { sendAndConfirmInstructions } from '../../../../../service/sol/send-and-confirm-tx.js'
12+
import { keyPair } from '../../../../../service/wallet'
1213
import { getTokenBalance } from '../../../basedbot.js'
1314
import { programs } from '../../programs.js'
1415
import { depositCargoIx } from '../ix/deposit-cargo.js'
@@ -76,7 +77,7 @@ export const depositCargo = async (
7677
),
7778
)
7879

79-
await sendAndConfirmInstructions()(
80+
await sendAndConfirmInstructions([keyPair])(
8081
await ixReturnsToIxs(instructions, player.signer),
8182
)
8283
}

src/main/basedbot/lib/sage/act/deposit-ship.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import BN from 'bn.js'
1515

1616
import { connection } from '../../../../../service/sol/index.js'
1717
import { sendAndConfirmInstructions } from '../../../../../service/sol/send-and-confirm-tx.js'
18+
import { keyPair } from '../../../../../service/wallet'
1819
import { programs } from '../../programs.js'
1920
import { addShipEscrowIx } from '../ix/add-ship-escrow.js'
2021
import { getShipByMint, getStarbasePlayer } from '../state/starbase-player.js'
@@ -89,7 +90,7 @@ export const depositShip = async (
8990
),
9091
)
9192

92-
await sendAndConfirmInstructions()(
93+
await sendAndConfirmInstructions([keyPair])(
9394
await ixReturnsToIxs(instructions, player.signer),
9495
)
9596
}

src/main/basedbot/lib/sage/act/disband-fleet.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Game, Starbase, WrappedShipEscrow } from '@staratlas/sage'
44
import dayjs from '../../../../../dayjs.js'
55
import { logger } from '../../../../../logger.js'
66
import { sendAndConfirmInstructions } from '../../../../../service/sol/send-and-confirm-tx.js'
7+
import { keyPair } from '../../../../../service/wallet'
78
import { programs } from '../../programs.js'
89
import { closeDisbandedFleetIx } from '../ix/close-disbanded-fleet.js'
910
import { disbandFleetIx } from '../ix/disband-fleet.js'
@@ -85,5 +86,7 @@ export const disbandFleet = async (
8586
`Added ${ixs.length} ixs for disbanding fleet ${getName(fleet)}`,
8687
)
8788

88-
await sendAndConfirmInstructions()(await ixReturnsToIxs(ixs, player.signer))
89+
await sendAndConfirmInstructions([keyPair])(
90+
await ixReturnsToIxs(ixs, player.signer),
91+
)
8992
}

src/main/basedbot/lib/sage/act/dock.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ixReturnsToIxs } from '@staratlas/data-source'
22
import { Game } from '@staratlas/sage'
33

44
import { sendAndConfirmInstructions } from '../../../../../service/sol/send-and-confirm-tx.js'
5+
import { keyPair } from '../../../../../service/wallet'
56
import { programs } from '../../programs.js'
67
import { Coordinates } from '../../util/coordinates.js'
78
import { dockIx } from '../ix/dock.js'
@@ -34,5 +35,5 @@ export const dock = async (
3435

3536
const instructions = await ixReturnsToIxs(ix, player.signer)
3637

37-
await sendAndConfirmInstructions()(instructions)
38+
await sendAndConfirmInstructions([keyPair])(instructions)
3839
}

src/main/basedbot/lib/sage/act/end-mine.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Game } from '@staratlas/sage'
66

77
import { logger } from '../../../../../logger.js'
88
import { sendAndConfirmInstructions } from '../../../../../service/sol/send-and-confirm-tx.js'
9+
import { keyPair } from '../../../../../service/wallet'
910
import { programs } from '../../programs.js'
1011
import { miningHandlerIx } from '../ix/fleet-state-handler.js'
1112
import { stopMiningIx } from '../ix/stop-mining.js'
@@ -80,7 +81,7 @@ export const endMine = async (
8081
),
8182
],
8283
player.signer,
83-
).then(sendAndConfirmInstructions())
84+
).then(sendAndConfirmInstructions([keyPair]))
8485

8586
await ixReturnsToIxs(
8687
[
@@ -95,5 +96,5 @@ export const endMine = async (
9596
),
9697
],
9798
player.signer,
98-
).then(sendAndConfirmInstructions())
99+
).then(sendAndConfirmInstructions([keyPair]))
99100
}

src/main/basedbot/lib/sage/act/end-move.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Game } from '@staratlas/sage'
66

77
import { logger } from '../../../../../logger.js'
88
import { sendAndConfirmInstructions } from '../../../../../service/sol/send-and-confirm-tx.js'
9+
import { keyPair } from '../../../../../service/wallet'
910
import { programs } from '../../programs.js'
1011
import { movementSubwarpHandlerIx } from '../ix/movement-subwarp-handler.js'
1112
import { stopWarpIx } from '../ix/stop-warp.js'
@@ -39,5 +40,5 @@ export const endMove = async (
3940
player.signer,
4041
)
4142

42-
await sendAndConfirmInstructions()(instructions)
43+
await sendAndConfirmInstructions([keyPair])(instructions)
4344
}

0 commit comments

Comments
 (0)