Skip to content

Commit

Permalink
chore: bulk-mine strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
mindrunner committed Sep 2, 2024
1 parent 99e9384 commit 1e2b827
Show file tree
Hide file tree
Showing 11 changed files with 593 additions and 17 deletions.
36 changes: 35 additions & 1 deletion src/main/basedbot/basedbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import {
getAssociatedTokenAddressSync,
TOKEN_PROGRAM_ID,
} from '@solana/spl-token'
import { PublicKey } from '@solana/web3.js'
import { getParsedTokenAccountsByOwner } from '@staratlas/data-source'
import { Fleet } from '@staratlas/sage'
import BN from 'bn.js'

import { Sentry } from '../../sentry'
Expand All @@ -15,6 +17,7 @@ import { keyPair } from '../../service/wallet'
import { getFleetStrategy } from './fleet-strategies/get-fleet-strategy'
import { createInfoStrategy } from './fsm/info'
import { Strategy } from './fsm/strategy'
import { createFleet } from './lib/sage/act/create-fleet'
import { depositCargo } from './lib/sage/act/deposit-cargo'
import { settleFleet } from './lib/sage/state/settle-fleet'
import { getPlayerContext, Player } from './lib/sage/state/user-account'
Expand All @@ -24,6 +27,7 @@ import {
getUserFleets,
} from './lib/sage/state/user-fleets'
import { getMapContext, WorldMap } from './lib/sage/state/world-map'
import { getName } from './lib/sage/util'
// eslint-disable-next-line import/max-dependencies

// eslint-disable-next-line require-await
Expand Down Expand Up @@ -106,6 +110,36 @@ const importR4 = async (player: Player): Promise<void> => {
)
}

const ensureFleets = async (
fleets: Array<Fleet>,
botConfig: BotConfig,
): Promise<void> => {
const existingFleets = fleets.map(getName)
const wantedFleets = Array.from(botConfig.fleetStrategies.keys())

const neededFleets = wantedFleets.filter((f) => !existingFleets.includes(f))

if (neededFleets.length > 0) {
logger.info('Creating fleets:', neededFleets)
}

const fleetMint = new PublicKey(
'9tGU2Mvtvvr2n7Fjmw3zbsdr5YrfGbLtPxR31bi5hTA4',
)

await Promise.all(
neededFleets.map((fleetName) =>
createFleet(
botConfig.player,
botConfig.player.homeStarbase,
fleetMint,
fleetName,
1,
),
),
)
}

const basedbot = async (botConfig: BotConfig) => {
logger.info(
'-------------------------------------------------------------------------------------',
Expand All @@ -116,7 +150,7 @@ const basedbot = async (botConfig: BotConfig) => {
fleets.map((f) => getFleetInfo(f, player, map)),
)

await importR4(player)
await Promise.all([importR4(player), ensureFleets(fleets, botConfig)])

await Promise.all(
fleetInfos.map((fleetInfo) => settleFleet(fleetInfo, player)),
Expand Down
31 changes: 31 additions & 0 deletions src/main/basedbot/fleet-strategies/atlasnet-fc-strategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { mine } from '../fsm/configs/mine'
import { createMiningStrategy } from '../fsm/mine'
import { Strategy } from '../fsm/strategy'
import { Player } from '../lib/sage/state/user-account'
import { WorldMap } from '../lib/sage/state/world-map'
import { galaxySectorsData } from '../lib/util/galaxy-sectors-data'

export const atlasnetFcStrategy =
(count: number) =>
(map: WorldMap, player: Player): Map<string, Strategy> => {
const ans: Map<string, Strategy> = new Map<string, Strategy>()
const sectors = galaxySectorsData()
.filter((sector) => sector.closestFaction === player.faction)
.sort((a, b) => a.name.localeCompare(b.name))

for (let i = 0; i < count; i++) {
ans.set(
`basedbot-mud-${i}`,
createMiningStrategy(
mine(
map,
player.homeCoordinates,
sectors[i % sectors.length].coordinates,
),
player,
),
)
}

return ans
}
3 changes: 3 additions & 0 deletions src/main/basedbot/fleet-strategies/get-fleet-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Strategy } from '../fsm/strategy'
import { Player } from '../lib/sage/state/user-account'
import { WorldMap } from '../lib/sage/state/world-map'

import { atlasnetFcStrategy } from './atlasnet-fc-strategy'
import { atlasnetLuStrategy } from './atlasnet-lu-strategy'
import { mainnetLuStrategy } from './mainnet-lu-strategy'

Expand All @@ -14,6 +15,8 @@ export const getFleetStrategy = (
return atlasnetLuStrategy(map, player)
case 'AePY3wEoUFcFuXeUU9X26YK6tNKQMZovBgvY54LK2B8N':
return mainnetLuStrategy(map, player)
case 'CgHvzwGbwWv3CwLTvEgeqSKeD8EwMdTfiiCG3dFrKVVC':
return atlasnetFcStrategy(100)(map, player)
default:
throw new Error('Unknown strategy')
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/basedbot/fsm/configs/mine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { mineableByCoordinates, WorldMap } from '../../lib/sage/state/world-map'
import { Coordinates } from '../../lib/util/coordinates'

import { mineConfig, MineConfig } from './mine-config'

export const mine = (
map: WorldMap,
homeBase: Coordinates,
targetBase: Coordinates,
): MineConfig =>
mineConfig({
homeBase,
targetBase,
resource: mineableByCoordinates(map, targetBase).values().next().value,
})
45 changes: 45 additions & 0 deletions src/main/basedbot/lib/sage/act/create-fleet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { PublicKey } from '@solana/web3.js'
import { InstructionReturn, ixReturnsToIxs } from '@staratlas/data-source'
import { Starbase } from '@staratlas/sage'

import { sendAndConfirmInstructions } from '../../../../../service/sol/send-and-confirm-tx'
import { programs } from '../../programs'
import { createFleetIx } from '../ix/create-fleet'
import { getCargoStatsDefinition } from '../state/cargo-stats-definition'
import { getStarbasePlayer } from '../state/starbase-player'
import { Player } from '../state/user-account'

export const createFleet = async (
player: Player,
starbase: Starbase,
_mint: PublicKey,
name: string,
amount: number,
// eslint-disable-next-line max-params
): Promise<void> => {
const instructions: InstructionReturn[] = []

const starbasePlayer = await getStarbasePlayer(player, starbase, programs)
// TODO: Ask Sammy about this
const [shipEscrow] = starbasePlayer.wrappedShipEscrows

const [cargoStatsDefinition] = await getCargoStatsDefinition()

instructions.push(
createFleetIx(
player,
starbase,
starbasePlayer,
programs,
shipEscrow.ship,
0,
cargoStatsDefinition.key,
amount,
name,
).instructions,
)

await sendAndConfirmInstructions(
await ixReturnsToIxs(instructions, player.signer),
)
}
74 changes: 74 additions & 0 deletions src/main/basedbot/lib/sage/act/deposit-ship.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { PublicKey } from '@solana/web3.js'
import { Starbase } from '@staratlas/sage'
import BN from 'bn.js'

import { Player } from '../state/user-account'

export const depositShip = async (
_player: Player,
_starbase: Starbase,
_mint: PublicKey,
_amount: BN,
// eslint-disable-next-line max-params,require-await
): Promise<void> => {
throw new Error('Not implemented')
// const instructions: InstructionReturn[] = []
//
// const starbasePlayer = await getStarbasePlayer(player, starbase, programs)
//
// const sourceTokenAccount = getAssociatedTokenAddressSync(
// mint,
// player.signer.publicKey(),
// )
//
//
// const destinationTokenAccount = getAssociatedTokenAddressSync(
// mint,
// cargoPodTo.key,
// true,
// )
//
// instructions.push(
// createAssociatedTokenAccountIdempotent(mint, cargoPodTo.key, true)
// .instructions,
// )
//
// const allTokenAccounts = await getParsedTokenAccountsByOwner(
// connection,
// player.signer.publicKey(),
// TOKEN_PROGRAM_ID,
// )
//
// const [cargoTokenAccount] = allTokenAccounts.filter((it) =>
// it.address.equals(sourceTokenAccount),
// )
//
// if (!cargoTokenAccount) {
// throw new Error('Cargo token account not found')
// }
// const amountAtOrigin = new BN(cargoTokenAccount.amount.toString())
//
// if (!cargoTokenAccount) {
// throw new Error('Cargo not found at origin')
// }
// if (amountAtOrigin.lt(new BN(amount))) {
// throw new Error('Not enough cargo available at origin')
// }
//
// instructions.push(
// addShipIx(
// player,
// starbase,
// starbasePlayer,
// programs,
// sourceTokenAccount,
// ship,
// destinationTokenAccount,
// amount,
// ),
// )
//
// await sendAndConfirmInstructions(
// await ixReturnsToIxs(instructions, player.signer),
// )
}
37 changes: 37 additions & 0 deletions src/main/basedbot/lib/sage/ix/add-ship.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { PublicKey } from '@solana/web3.js'
import { InstructionReturn } from '@staratlas/data-source'
import { SagePlayerProfile, Starbase, StarbasePlayer } from '@staratlas/sage'
import BN from 'bn.js'

import { StarAtlasPrograms } from '../../programs'
import { Player } from '../state/user-account'

export const addShipIx = (
player: Player,
starbase: Starbase,
starbasePlayer: StarbasePlayer,
programs: StarAtlasPrograms,
originTokenAccount: PublicKey,
ship: PublicKey,
shipEscrowTokenAccount: PublicKey,
shipAmount: BN,
// eslint-disable-next-line max-params
): InstructionReturn =>
SagePlayerProfile.addShipEscrow(
programs.sage,
player.profile.key,
player.profileFaction.key,
player.profile.key,
player.signer,
originTokenAccount,
ship,
shipEscrowTokenAccount,
starbasePlayer.key,
starbase.key,
player.game.key,
player.game.data.gameState,
{
shipAmount,
index: 0,
},
)
46 changes: 46 additions & 0 deletions src/main/basedbot/lib/sage/ix/create-fleet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { PublicKey } from '@solana/web3.js'
import { InstructionReturn, stringToByteArray } from '@staratlas/data-source'
import { Fleet, Starbase, StarbasePlayer } from '@staratlas/sage'

import { StarAtlasPrograms } from '../../programs'
import { Player } from '../state/user-account'

type CreateFleetReturn = {
fleetKey: [PublicKey, number]
cargoHoldKey: [PublicKey, number]
fuelTankKey: [PublicKey, number]
ammoBankKey: [PublicKey, number]
instructions: InstructionReturn
}

export const createFleetIx = (
player: Player,
starbase: Starbase,
starbasePlayer: StarbasePlayer,
programs: StarAtlasPrograms,
ship: PublicKey,
shipEscrowIndex: number,
cargoStatsDefinition: PublicKey,
shipAmount: number,
name: string,
// eslint-disable-next-line max-params
): CreateFleetReturn =>
Fleet.createFleet(
programs.sage,
programs.cargo,
player.signer,
player.profile.key,
player.profileFaction.key,
ship,
starbasePlayer.key,
starbase.key,
player.game.key,
player.game.data.gameState,
cargoStatsDefinition,
{
shipAmount,
fleetLabel: stringToByteArray(name, 32),
shipEscrowIndex,
keyIndex: 0,
},
)
19 changes: 19 additions & 0 deletions src/main/basedbot/lib/sage/state/cargo-stats-definition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { CargoStatsDefinition } from '@staratlas/cargo'
import { readAllFromRPC } from '@staratlas/data-source'

import { connection } from '../../../../../service/sol'
import { programs } from '../../programs'

export const getCargoStatsDefinition = async (): Promise<
Array<CargoStatsDefinition>
> => {
const cargoTypesAccountData = await readAllFromRPC(
connection,
programs.cargo,
CargoStatsDefinition,
)

return cargoTypesAccountData
.filter((f) => f.type === 'ok' && 'data' in f)
.map((f) => (f as any).data)
}
Loading

0 comments on commit 1e2b827

Please sign in to comment.