Skip to content

Commit

Permalink
fix: optimize fleet creation
Browse files Browse the repository at this point in the history
  • Loading branch information
mindrunner committed Sep 9, 2024
1 parent a1d697c commit 3994c8b
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 30 deletions.
45 changes: 33 additions & 12 deletions src/main/basedbot/basedbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
getAssociatedTokenAddressSync,
TOKEN_PROGRAM_ID,
} from '@solana/spl-token'
import { PublicKey } from '@solana/web3.js'
import { getParsedTokenAccountsByOwner } from '@staratlas/data-source'
import { Fleet, Game } from '@staratlas/sage'
import BN from 'bn.js'
Expand All @@ -16,7 +17,7 @@ import { keyPair } from '../../service/wallet'
import { getFleetStrategy } from './fleet-strategies/get-fleet-strategy'
import { StrategyConfig } from './fleet-strategies/strategy-config'
import { createInfoStrategy } from './fsm/info'
import { createFleet } from './lib/sage/act/create-fleet'
import { createFleet, FleetShip } from './lib/sage/act/create-fleet'
import { depositCargo } from './lib/sage/act/deposit-cargo'
import { ensureShips } from './lib/sage/act/deposit-ship'
import { sageGame } from './lib/sage/state/game'
Expand Down Expand Up @@ -122,28 +123,48 @@ const ensureFleets = async (
logger.info('Creating fleets:', neededFleets)
}

const neededShips = new Map<string, number>()

neededFleets.forEach((fleetName) => {
const fleetStrategy = fleetStrategies.map.get(fleetName)!

fleetStrategy.fleet?.forEach((fleetShip) => {
const curr = neededShips.get(fleetShip.shipMint.toBase58()) ?? 0

neededShips.set(
fleetShip.shipMint.toBase58(),
curr + fleetShip.count,
)
})
})

const shipMints = Array.from(neededShips.keys())
.map((mint) => [
{
count: neededShips.get(mint) ?? 0,
shipMint: new PublicKey(mint),
} as FleetShip,
])
.flat()

await ensureShips(player, game, player.homeStarbase, shipMints)

await Promise.all(
neededFleets.map(async (fleetName) => {
neededFleets.map((fleetName) => {
const fleetStrategy = fleetStrategies.map.get(fleetName)!

if (!fleetStrategy.fleet) {
logger.info('Cannot ensure fleet without config.')

return Promise.resolve()
}
await ensureShips(

return createFleet(
player,
game,
player.homeStarbase,
fleetStrategy.fleet,
).then(() =>
createFleet(
player,
game,
player.homeStarbase,
fleetStrategy.fleet!,
fleetName,
),
fleetStrategy.fleet!,
fleetName,
)
}),
)
Expand Down
4 changes: 2 additions & 2 deletions src/main/basedbot/fleet-strategies/get-fleet-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export const getFleetStrategy = (
// return atlasnetFcStrategy(5)(map, player, game)
return disbandAllStrategy(map, player, game)
case '34ghznSJCYEMrS1aC55UYZZUuxfuurA9441aKnigmYyz':
return atlasnetFcStrategy(1)(map, player, game)
// return disbandAllStrategy(map, player, game)
// return atlasnetFcStrategy(5)(map, player, game)
return disbandAllStrategy(map, player, game)
default:
throw new Error('Unknown strategy')
}
Expand Down
73 changes: 61 additions & 12 deletions src/main/basedbot/lib/sage/act/create-fleet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { PublicKey } from '@solana/web3.js'
import { InstructionReturn, ixReturnsToIxs } from '@staratlas/data-source'
import {
Game,
Ship,
Starbase,
StarbasePlayer,
WrappedShipEscrow,
} from '@staratlas/sage'

import { logger } from '../../../../../logger'
import { sendAndConfirmInstructions } from '../../../../../service/sol/send-and-confirm-tx'
import { programs } from '../../programs'
import { addShipToFleetIx } from '../ix/add-ship-to-fleet'
Expand Down Expand Up @@ -36,6 +38,11 @@ const getShipEscrowIndex = (
return index
}

type ShipMintMap = {
mint: PublicKey
ship: Ship
}

export const createFleet = async (
player: Player,
game: Game,
Expand All @@ -45,32 +52,74 @@ export const createFleet = async (
): Promise<void> => {
const instructions: InstructionReturn[] = []

const [head, ...tail] = fleetShips
const shipMints = (
await Promise.all(
fleetShips.map(async (fleetShip) => {
return {
mint: fleetShip.shipMint,
ship: await getShipByMint(
fleetShip.shipMint,
game,
programs,
),
} as ShipMintMap
}),
)
).reduce(
(acc, curr) => acc.set(curr.mint.toBase58(), curr.ship),
new Map<string, Ship>(),
)

const [starbasePlayer, [cargoStatsDefinition]] = await Promise.all([
getStarbasePlayer(player, starbase, programs),
getCargoStatsDefinition(),
])

const [starbasePlayer, headShip, [cargoStatsDefinition]] =
await Promise.all([
getStarbasePlayer(player, starbase, programs),
getShipByMint(head.shipMint, game, programs),
getCargoStatsDefinition(),
])
const [head, ...tail] = fleetShips.sort(
(a, b) =>
getShipEscrowIndex(
starbasePlayer,
shipMints.get(a.shipMint.toBase58())!.key,
) -
getShipEscrowIndex(
starbasePlayer,
shipMints.get(b.shipMint.toBase58())!.key,
),
)

const shipKey = shipMints.get(head.shipMint.toBase58())?.key

if (!shipKey) throw new Error('No ship found')

const escrowIndex = getShipEscrowIndex(starbasePlayer, shipKey)

logger.info(`Escrow index ${escrowIndex} for ${head.shipMint.toBase58()}`)

const createFleetReturn = createFleetIx(
player,
game,
starbase,
starbasePlayer,
programs,
headShip.key,
shipKey,
cargoStatsDefinition.key,
head.count,
name,
getShipEscrowIndex(starbasePlayer, headShip.key),
escrowIndex,
)

instructions.push(createFleetReturn.instructions)

for (const fleetShip of tail) {
const ship = await getShipByMint(fleetShip.shipMint, game, programs)
const shipKey2 = shipMints.get(fleetShip.shipMint.toBase58())?.key

if (!shipKey2) throw new Error('No ship found')

const escrowIndex2 = getShipEscrowIndex(starbasePlayer, shipKey2)

logger.info(
`Escrow index ${escrowIndex2} for ${fleetShip.shipMint.toBase58()}`,
)

instructions.push(
addShipToFleetIx(
Expand All @@ -80,9 +129,9 @@ export const createFleet = async (
starbasePlayer,
programs,
createFleetReturn.fleetKey[0],
ship.key,
shipKey2,
fleetShip.count,
getShipEscrowIndex(starbasePlayer, ship.key),
escrowIndex2,
),
)
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/basedbot/lib/sage/act/load-cargo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ export const loadCargo = async (
cargoTokenAccount.delegatedAmount.toString(),
)

if (!cargoTokenAccount) {
throw new Error('Cargo not found at origin Starbase')
}

Check failure on line 77 in src/main/basedbot/lib/sage/act/load-cargo.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`

Check failure on line 77 in src/main/basedbot/lib/sage/act/load-cargo.ts

View workflow job for this annotation

GitHub Actions / lint

More than 1 blank line not allowed

Check failure on line 77 in src/main/basedbot/lib/sage/act/load-cargo.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`

Check failure on line 77 in src/main/basedbot/lib/sage/act/load-cargo.ts

View workflow job for this annotation

GitHub Actions / lint

More than 1 blank line not allowed
if (fuelAmountAtOrigin.lt(new BN(amount))) {
throw new Error('Not enough cargo available at origin Starbase')
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/basedbot/lib/sage/ix/add-ship-to-fleet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ export const addShipToFleetIx = (
shipAmount,
shipEscrowIndex,
keyIndex: 0,
fleetShipInfoIndex: 0,
fleetShipInfoIndex: null,
},
)

0 comments on commit 3994c8b

Please sign in to comment.