Skip to content

Commit

Permalink
feat: disband
Browse files Browse the repository at this point in the history
  • Loading branch information
mindrunner committed Sep 8, 2024
1 parent 10593cf commit 94ae07e
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ module.exports = {
],
'max-lines': 'off',
'max-lines-per-function': 'off',
'max-params': ['error', { max: 10 }],
'max-params': ['error', { max: 15 }],
'max-statements': 'off',
'multiline-comment-style': 'off',
'multiline-ternary': ['error', 'always-multiline'],
Expand Down
61 changes: 52 additions & 9 deletions src/main/basedbot/lib/sage/act/disband-fleet.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { InstructionReturn, ixReturnsToIxs } from '@staratlas/data-source'
import { Fleet, Game, Starbase } from '@staratlas/sage'
import { Fleet, Game, Starbase, WrappedShipEscrow } from '@staratlas/sage'

import { sendAndConfirmInstructions } from '../../../../../service/sol/send-and-confirm-tx'
import { programs } from '../../programs'
import { disbandFleetIx } from '../ix/disband'
import { closeDisbandedFleetIx } from '../ix/close-disbanded-fleet'
import { disbandFleetIx } from '../ix/disband-fleet'
import { disbandedFleetToEscrowIx } from '../ix/disbanded-fleet-to-escrow'
import { getFleetShips } from '../state/get-fleet-ships'
import { getStarbasePlayer } from '../state/starbase-player'
import { Player } from '../state/user-account'

Expand All @@ -13,16 +16,56 @@ export const disbandFleet = async (
starbase: Starbase,
fleet: Fleet,
): Promise<void> => {
const instructions: InstructionReturn[] = []

const ixs: InstructionReturn[] = []
const starbasePlayer = await getStarbasePlayer(player, starbase, programs)

instructions.push(
disbandFleetIx(player, game, starbase, starbasePlayer, programs, fleet)
.instructions,
const { disbandedFleetKey, instructions } = disbandFleetIx(
player,
game,
starbase,
starbasePlayer,
programs,
fleet,
)

await sendAndConfirmInstructions(
await ixReturnsToIxs(instructions, player.signer),
ixs.push(instructions)

const [fleetShips] = await getFleetShips(fleet)
let i = 0

for (const fleetShipInfo of fleetShips.fleetShips) {
const pred = (v: WrappedShipEscrow) => v.ship.equals(fleetShipInfo.ship)
// const shipEscrow = starbasePlayer.wrappedShipEscrows.find(pred)
const shipEscrowIndex =
starbasePlayer.wrappedShipEscrows.findIndex(pred)

ixs.push(
disbandedFleetToEscrowIx(
player,
game,
starbase,
starbasePlayer,
programs,
shipEscrowIndex,
disbandedFleetKey[0],
fleet.data.fleetShips,
fleetShipInfo.ship,
i,
fleetShipInfo.amount,
),
)
console.log('Pushed disbanded fleet to escrow instruction')
i += 1
}

ixs.push(
closeDisbandedFleetIx(
player,
programs,
disbandedFleetKey[0],
fleet.data.fleetShips,
),
)

await sendAndConfirmInstructions(await ixReturnsToIxs(ixs, player.signer))
}
22 changes: 22 additions & 0 deletions src/main/basedbot/lib/sage/ix/close-disbanded-fleet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { PublicKey } from '@solana/web3.js'
import { InstructionReturn } from '@staratlas/data-source'
import { DisbandedFleet } from '@staratlas/sage'

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

export const closeDisbandedFleetIx = (
player: Player,
programs: StarAtlasPrograms,
disbandedFleetKey: PublicKey,
fleetShipsKey: PublicKey,
): InstructionReturn =>
DisbandedFleet.closeDisbandedFleet(
programs.sage,
player.signer,
player.profile.key,
'funder',
disbandedFleetKey,
fleetShipsKey,
{ keyIndex: 0 },
)
File renamed without changes.
40 changes: 40 additions & 0 deletions src/main/basedbot/lib/sage/ix/disbanded-fleet-to-escrow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { PublicKey } from '@solana/web3.js'
import { InstructionReturn } from '@staratlas/data-source'
import { DisbandedFleet, Game, Starbase, StarbasePlayer } from '@staratlas/sage'
import BN from 'bn.js'

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

export const disbandedFleetToEscrowIx = (
player: Player,
game: Game,
starbase: Starbase,
starbasePlayer: StarbasePlayer,
programs: StarAtlasPrograms,
shipEscrowIndex: number,
disbandedFleet: PublicKey,
fleetShips: PublicKey,
shipKey: PublicKey,
shipIndex: number,
shipAmount: BN,
): InstructionReturn =>
DisbandedFleet.disbandedFleetToEscrow(
programs.sage,
player.signer,
player.profile.key,
player.profileFaction.key,
disbandedFleet,
fleetShips,
shipKey,
starbasePlayer.key,
starbase.key,
game.key,
game.data.gameState,
{
fleetShipInfoIndex: shipIndex,
keyIndex: 0,
shipAmount: shipAmount.toNumber(),
shipEscrowIndex,
},
)
28 changes: 28 additions & 0 deletions src/main/basedbot/lib/sage/state/get-fleet-ships.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { readAllFromRPC } from '@staratlas/data-source'
import { Fleet, FleetShips } from '@staratlas/sage'

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

export const getFleetShips = async (
fleet: Fleet,
): Promise<Array<FleetShips>> => {
const resources = await readAllFromRPC(
connection,
programs.sage,
FleetShips,
'processed',
[
{
memcmp: {
offset: 8 + 1,
bytes: fleet.key.toBase58(),
},
},
],
)

return resources
.filter((p) => p.type === 'ok' && 'data' in p)
.map((p) => (p as any).data)
}
1 change: 1 addition & 0 deletions src/service/sol/send-and-confirm-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const sendAndConfirmTx = async (

return txId
} catch (e) {
logger.warn(`Confirmation failed: ${(e as Error).message}}`)
await sleep(500)
}
}
Expand Down

0 comments on commit 94ae07e

Please sign in to comment.