-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6c86241
commit e675811
Showing
6 changed files
with
223 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { | ||
getAssociatedTokenAddressSync, | ||
TOKEN_PROGRAM_ID, | ||
} from '@solana/spl-token' | ||
import { PublicKey } from '@solana/web3.js' | ||
import { | ||
createAssociatedTokenAccountIdempotent, | ||
getParsedTokenAccountsByOwner, | ||
InstructionReturn, | ||
ixReturnsToIxs, | ||
} from '@staratlas/data-source' | ||
import { Starbase } from '@staratlas/sage' | ||
import BN from 'bn.js' | ||
|
||
import { connection } from '../../../../../service/sol' | ||
import { sendAndConfirmInstructions } from '../../../../../service/sol/send-and-confirm-tx' | ||
import { programs } from '../../programs' | ||
import { depositCargoIx } from '../ix/import-cargo' | ||
import { getCargoType } from '../state/cargo-types' | ||
import { | ||
getCargoPodsForStarbasePlayer, | ||
getStarbasePlayer, | ||
} from '../state/starbase-player' | ||
import { Player } from '../state/user-account' | ||
|
||
export const depositCargo = async ( | ||
player: Player, | ||
starbase: Starbase, | ||
mint: PublicKey, | ||
amount: BN, | ||
// eslint-disable-next-line max-params | ||
): Promise<void> => { | ||
const instructions: InstructionReturn[] = [] | ||
|
||
const starbasePlayer = await getStarbasePlayer(player, starbase, programs) | ||
|
||
const sourceTokenAccount = getAssociatedTokenAddressSync( | ||
mint, | ||
player.signer.publicKey(), | ||
) | ||
|
||
const cargoPodTo = await getCargoPodsForStarbasePlayer( | ||
starbasePlayer, | ||
programs, | ||
) | ||
const destinationTokenAccount = getAssociatedTokenAddressSync( | ||
mint, | ||
cargoPodTo.key, | ||
true, | ||
) | ||
|
||
instructions.push( | ||
createAssociatedTokenAccountIdempotent(mint, cargoPodTo.key, true) | ||
.instructions, | ||
) | ||
|
||
const cargoType = getCargoType(player.cargoTypes, player.game, mint) | ||
|
||
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( | ||
depositCargoIx( | ||
player, | ||
starbase, | ||
starbasePlayer, | ||
cargoPodTo.key, | ||
sourceTokenAccount, | ||
destinationTokenAccount, | ||
cargoType.key, | ||
programs, | ||
amount, | ||
), | ||
) | ||
|
||
await sendAndConfirmInstructions( | ||
await ixReturnsToIxs(instructions, player.signer), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { Starbase, StarbasePlayer } from '@staratlas/sage' | ||
import BN from 'bn.js' | ||
|
||
import { StarAtlasPrograms } from '../../programs' | ||
import { Player } from '../state/user-account' | ||
|
||
export const depositCargoIx = ( | ||
player: Player, | ||
starbase: Starbase, | ||
starbasePlayer: StarbasePlayer, | ||
cargoPodTo: PublicKey, | ||
tokenFrom: PublicKey, | ||
tokenTo: PublicKey, | ||
cargoType: PublicKey, | ||
programs: StarAtlasPrograms, | ||
amount: BN, | ||
// eslint-disable-next-line max-params | ||
): InstructionReturn => | ||
StarbasePlayer.depositCargoToGame( | ||
programs.sage, | ||
programs.cargo, | ||
starbasePlayer.key, | ||
player.signer, | ||
player.profile.key, | ||
player.profileFaction.key, | ||
starbase.key, | ||
cargoPodTo, | ||
cargoType, | ||
player.game.data.cargo.statsDefinition, | ||
tokenFrom, | ||
tokenTo, | ||
player.game.key, | ||
player.game.data.gameState, | ||
{ | ||
amount, | ||
keyIndex: 0, | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters