From 94ae07e5540c147dc3c2912294750031707c20e2 Mon Sep 17 00:00:00 2001 From: lukas Date: Fri, 6 Sep 2024 12:11:14 +0200 Subject: [PATCH] feat: disband --- .eslintrc.js | 2 +- .../basedbot/lib/sage/act/disband-fleet.ts | 61 ++++++++++++++++--- .../lib/sage/ix/close-disbanded-fleet.ts | 22 +++++++ .../sage/ix/{disband.ts => disband-fleet.ts} | 0 .../lib/sage/ix/disbanded-fleet-to-escrow.ts | 40 ++++++++++++ .../lib/sage/state/get-fleet-ships.ts | 28 +++++++++ src/service/sol/send-and-confirm-tx.ts | 1 + 7 files changed, 144 insertions(+), 10 deletions(-) create mode 100644 src/main/basedbot/lib/sage/ix/close-disbanded-fleet.ts rename src/main/basedbot/lib/sage/ix/{disband.ts => disband-fleet.ts} (100%) create mode 100644 src/main/basedbot/lib/sage/ix/disbanded-fleet-to-escrow.ts create mode 100644 src/main/basedbot/lib/sage/state/get-fleet-ships.ts diff --git a/.eslintrc.js b/.eslintrc.js index 1da2d948..b2a64a01 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -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'], diff --git a/src/main/basedbot/lib/sage/act/disband-fleet.ts b/src/main/basedbot/lib/sage/act/disband-fleet.ts index 48bf8ab2..1f6358ca 100644 --- a/src/main/basedbot/lib/sage/act/disband-fleet.ts +++ b/src/main/basedbot/lib/sage/act/disband-fleet.ts @@ -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' @@ -13,16 +16,56 @@ export const disbandFleet = async ( starbase: Starbase, fleet: Fleet, ): Promise => { - 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)) } diff --git a/src/main/basedbot/lib/sage/ix/close-disbanded-fleet.ts b/src/main/basedbot/lib/sage/ix/close-disbanded-fleet.ts new file mode 100644 index 00000000..520a3778 --- /dev/null +++ b/src/main/basedbot/lib/sage/ix/close-disbanded-fleet.ts @@ -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 }, + ) diff --git a/src/main/basedbot/lib/sage/ix/disband.ts b/src/main/basedbot/lib/sage/ix/disband-fleet.ts similarity index 100% rename from src/main/basedbot/lib/sage/ix/disband.ts rename to src/main/basedbot/lib/sage/ix/disband-fleet.ts diff --git a/src/main/basedbot/lib/sage/ix/disbanded-fleet-to-escrow.ts b/src/main/basedbot/lib/sage/ix/disbanded-fleet-to-escrow.ts new file mode 100644 index 00000000..69c36cdd --- /dev/null +++ b/src/main/basedbot/lib/sage/ix/disbanded-fleet-to-escrow.ts @@ -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, + }, + ) diff --git a/src/main/basedbot/lib/sage/state/get-fleet-ships.ts b/src/main/basedbot/lib/sage/state/get-fleet-ships.ts new file mode 100644 index 00000000..3ab865cb --- /dev/null +++ b/src/main/basedbot/lib/sage/state/get-fleet-ships.ts @@ -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> => { + 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) +} diff --git a/src/service/sol/send-and-confirm-tx.ts b/src/service/sol/send-and-confirm-tx.ts index 9b76b245..51f0b94f 100644 --- a/src/service/sol/send-and-confirm-tx.ts +++ b/src/service/sol/send-and-confirm-tx.ts @@ -85,6 +85,7 @@ export const sendAndConfirmTx = async ( return txId } catch (e) { + logger.warn(`Confirmation failed: ${(e as Error).message}}`) await sleep(500) } }