@aephia/atlas-kit 0.8.0-next.12 → 0.8.0-next.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/claim-stakes/actions/index.d.ts.map +1 -1
- package/dist/claim-stakes/actions/index.js +2 -5
- package/dist/claim-stakes/actions/index.js.map +1 -1
- package/dist/crafting/actions/habs.d.ts +186 -0
- package/dist/crafting/actions/habs.d.ts.map +1 -0
- package/dist/crafting/actions/habs.js +443 -0
- package/dist/crafting/actions/habs.js.map +1 -0
- package/dist/crafting/actions/index.d.ts +1 -0
- package/dist/crafting/actions/index.d.ts.map +1 -1
- package/dist/crafting/actions/index.js +1 -0
- package/dist/crafting/actions/index.js.map +1 -1
- package/dist/internal/c4/crafting-hab-storage.d.ts +25 -0
- package/dist/internal/c4/crafting-hab-storage.d.ts.map +1 -0
- package/dist/internal/c4/crafting-hab-storage.js +118 -0
- package/dist/internal/c4/crafting-hab-storage.js.map +1 -0
- package/dist/internal/c4/discriminators.d.ts +1 -0
- package/dist/internal/c4/discriminators.d.ts.map +1 -1
- package/dist/internal/c4/discriminators.js +3 -1
- package/dist/internal/c4/discriminators.js.map +1 -1
- package/dist/internal/c4/runtime-bindings.d.ts +1 -1
- package/dist/internal/c4/runtime-bindings.d.ts.map +1 -1
- package/dist/internal/c4/runtime-bindings.js +1 -1
- package/dist/internal/c4/runtime-bindings.js.map +1 -1
- package/dist/internal/planning/authorization-adapter.d.ts +39 -0
- package/dist/internal/planning/authorization-adapter.d.ts.map +1 -1
- package/dist/internal/planning/authorization-adapter.js +57 -1
- package/dist/internal/planning/authorization-adapter.js.map +1 -1
- package/dist/internal/planning/crafting-hab-storage-actions.d.ts +10 -0
- package/dist/internal/planning/crafting-hab-storage-actions.d.ts.map +1 -0
- package/dist/internal/planning/crafting-hab-storage-actions.js +31 -0
- package/dist/internal/planning/crafting-hab-storage-actions.js.map +1 -0
- package/dist/internal/planning/placement-fee.d.ts +6 -0
- package/dist/internal/planning/placement-fee.d.ts.map +1 -0
- package/dist/internal/planning/placement-fee.js +9 -0
- package/dist/internal/planning/placement-fee.js.map +1 -0
- package/dist/internal/planning/profile-storage-funding-guard.d.ts +1 -1
- package/dist/internal/planning/profile-storage-funding-guard.d.ts.map +1 -1
- package/dist/internal/planning/profile-storage-funding-guard.js +2 -1
- package/dist/internal/planning/profile-storage-funding-guard.js.map +1 -1
- package/dist/internal/planning/storage-action-readers.d.ts.map +1 -1
- package/dist/internal/planning/storage-action-readers.js +3 -0
- package/dist/internal/planning/storage-action-readers.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { type Address } from '@solana/kit';
|
|
2
|
+
import type { SageContext } from '../../client/contracts.js';
|
|
3
|
+
import { type WorldStarbaseData } from '../../internal/c4/world.js';
|
|
4
|
+
import { type PlanAuthorization } from '../../internal/planning/authorization-adapter.js';
|
|
5
|
+
import type { Plan } from '../../planning/index.js';
|
|
6
|
+
import type { CraftingHabSnapshot } from '../index.js';
|
|
7
|
+
declare const TIERS: readonly ["one", "two", "three", "four", "five"];
|
|
8
|
+
/** A Starbase Player of the authorization Profile, as `starbases` reads it.
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* declare const player: CraftingHabStarbasePlayer;
|
|
12
|
+
* console.log(player.system.address);
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export interface CraftingHabStarbasePlayer {
|
|
16
|
+
readonly address: Address;
|
|
17
|
+
readonly playerProfile: {
|
|
18
|
+
readonly address: Address;
|
|
19
|
+
};
|
|
20
|
+
readonly system: {
|
|
21
|
+
readonly address: Address;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/** Inputs for placing a Crafting Hab.
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* declare const authorization: PlanAuthorization;
|
|
28
|
+
* declare const craftingHab: Address; // a fresh keypair's address
|
|
29
|
+
* const options: PlanPlaceCraftingHabOptions = {
|
|
30
|
+
* authorization,
|
|
31
|
+
* craftingHab,
|
|
32
|
+
* definitionId: 0,
|
|
33
|
+
* initialRentAtlasRaw: 100_000_000n,
|
|
34
|
+
* };
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export interface PlanPlaceCraftingHabOptions {
|
|
38
|
+
readonly authorization: PlanAuthorization;
|
|
39
|
+
/** The address of a new keypair the caller generates; it signs the
|
|
40
|
+
* placement and becomes the Hab's account. */
|
|
41
|
+
readonly craftingHab: Address;
|
|
42
|
+
readonly definitionId: number;
|
|
43
|
+
/** Raw ATLAS moved from the Character into the Hab's rent at placement. */
|
|
44
|
+
readonly initialRentAtlasRaw?: bigint;
|
|
45
|
+
}
|
|
46
|
+
/** Inputs for topping up a Crafting Hab's rent.
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* declare const authorization: PlanAuthorization;
|
|
50
|
+
* declare const starbasePlayer: CraftingHabStarbasePlayer;
|
|
51
|
+
* const options: PlanTopUpCraftingHabRentOptions = {
|
|
52
|
+
* authorization,
|
|
53
|
+
* starbasePlayer,
|
|
54
|
+
* amountAtlasRaw: 100_000_000n,
|
|
55
|
+
* };
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export interface PlanTopUpCraftingHabRentOptions {
|
|
59
|
+
readonly authorization: PlanAuthorization;
|
|
60
|
+
/** The authorization Profile's Starbase Player in the Hab's System. */
|
|
61
|
+
readonly starbasePlayer: CraftingHabStarbasePlayer;
|
|
62
|
+
readonly amountAtlasRaw: bigint;
|
|
63
|
+
}
|
|
64
|
+
/** Inputs for deconstructing a Crafting Hab.
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* declare const authorization: PlanAuthorization;
|
|
68
|
+
* declare const starbasePlayer: CraftingHabStarbasePlayer;
|
|
69
|
+
* const options: PlanDeconstructCraftingHabOptions = {
|
|
70
|
+
* authorization,
|
|
71
|
+
* starbasePlayer,
|
|
72
|
+
* };
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export interface PlanDeconstructCraftingHabOptions {
|
|
76
|
+
readonly authorization: PlanAuthorization;
|
|
77
|
+
/** The authorization Profile's Starbase Player in the Hab's System, which
|
|
78
|
+
* receives the Hab item. */
|
|
79
|
+
readonly starbasePlayer: CraftingHabStarbasePlayer;
|
|
80
|
+
/** Raw ATLAS to top up the Hab's rent with first, in the same transaction,
|
|
81
|
+
* when it owes rent; whatever is left after rent returns to the Character. */
|
|
82
|
+
readonly topUpAtlasRaw?: bigint;
|
|
83
|
+
}
|
|
84
|
+
/** What placing a Crafting Hab at a Starbase costs.
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* declare const quote: CraftingHabPlacementQuote;
|
|
88
|
+
* console.log(quote.placementFeeAtlasRaw, quote.rentAtlasRawPerSecond);
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export interface CraftingHabPlacementQuote {
|
|
92
|
+
readonly placementFeeAtlasRaw: bigint;
|
|
93
|
+
/** Rent per second in raw ATLAS (8 decimals); fractional, accrued by the
|
|
94
|
+
* game. Divide by 100,000,000 for ATLAS. */
|
|
95
|
+
readonly rentAtlasRawPerSecond: number;
|
|
96
|
+
/** Plots the Starbase's levels up to its own offer for the Hab's tier. */
|
|
97
|
+
readonly plots: number;
|
|
98
|
+
}
|
|
99
|
+
/** What placing a Crafting Hab of a definition costs at a Starbase: the
|
|
100
|
+
* placement fee, the rent rate, and the plots for its tier. Pure; the game
|
|
101
|
+
* also requires a free plot, the Hab item and the Character's research.
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* declare const starbase: WorldStarbaseData;
|
|
105
|
+
* declare const definition: { tier: 'one'; placementFeeMultiplier: { raw: bigint }; rentMultiplier: { value: number } };
|
|
106
|
+
* const quote = deriveCraftingHabPlacementQuote(starbase, definition);
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
export declare function deriveCraftingHabPlacementQuote(starbase: Pick<WorldStarbaseData, 'level' | 'craftingHabPlots' | 'baseCraftingHabPlacementFee' | 'baseCraftingHabRentRate'>, definition: {
|
|
110
|
+
readonly tier: (typeof TIERS)[number];
|
|
111
|
+
readonly placementFeeMultiplier: {
|
|
112
|
+
readonly raw: bigint;
|
|
113
|
+
};
|
|
114
|
+
readonly rentMultiplier: {
|
|
115
|
+
readonly value: number;
|
|
116
|
+
};
|
|
117
|
+
}): CraftingHabPlacementQuote;
|
|
118
|
+
/** Plans placing a Crafting Hab on a Starbase plot.
|
|
119
|
+
*
|
|
120
|
+
* The game takes one Hab item of the definition from the Starbase Player,
|
|
121
|
+
* charges the placement fee and any initial rent from the Character's ATLAS,
|
|
122
|
+
* and puts the Hab on the first level, up to the Starbase's own, with a free
|
|
123
|
+
* plot for its tier. The Starbase must belong to the Profile's faction, and
|
|
124
|
+
* the Character's research caps how many Habs it holds and their tier. The
|
|
125
|
+
* Profile funds the new Hab's storage and the Star System's growth.
|
|
126
|
+
*
|
|
127
|
+
* The caller generates the Hab's keypair; its address signs the placement
|
|
128
|
+
* with the authorization key. Placement races and research beyond the
|
|
129
|
+
* Character's cap remain chain-authoritative.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts
|
|
133
|
+
* declare const ctx: SageContext;
|
|
134
|
+
* declare const starbasePlayer: CraftingHabStarbasePlayer;
|
|
135
|
+
* declare const authorization: PlanAuthorization;
|
|
136
|
+
* declare const craftingHab: Address;
|
|
137
|
+
* const plan = await planPlaceCraftingHab(ctx, starbasePlayer, {
|
|
138
|
+
* authorization,
|
|
139
|
+
* craftingHab,
|
|
140
|
+
* definitionId: 0,
|
|
141
|
+
* });
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
export declare function planPlaceCraftingHab(ctx: SageContext, starbasePlayer: CraftingHabStarbasePlayer, input: PlanPlaceCraftingHabOptions): Promise<Plan>;
|
|
145
|
+
/** Plans moving raw ATLAS from the Character into a Crafting Hab's rent.
|
|
146
|
+
*
|
|
147
|
+
* The game refuses a top-up once the Hab has been delinquent past the Game's
|
|
148
|
+
* eviction grace period. Rent accrual remains chain-authoritative.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```ts
|
|
152
|
+
* declare const ctx: SageContext;
|
|
153
|
+
* declare const hab: CraftingHabSnapshot;
|
|
154
|
+
* declare const starbasePlayer: CraftingHabStarbasePlayer;
|
|
155
|
+
* declare const authorization: PlanAuthorization;
|
|
156
|
+
* const plan = await planTopUpCraftingHabRent(ctx, hab, {
|
|
157
|
+
* authorization,
|
|
158
|
+
* starbasePlayer,
|
|
159
|
+
* amountAtlasRaw: 100_000_000n,
|
|
160
|
+
* });
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
export declare function planTopUpCraftingHabRent(ctx: SageContext, hab: CraftingHabSnapshot, input: PlanTopUpCraftingHabRentOptions): Promise<Plan>;
|
|
164
|
+
/** Plans deconstructing a Crafting Hab: the game closes it, returns its Hab
|
|
165
|
+
* item to the Starbase Player and its remaining rent to the Character, and
|
|
166
|
+
* frees its plot, moving a newer Hab down into it when one is waiting.
|
|
167
|
+
*
|
|
168
|
+
* The Hab must be Active, with no buildings and no running Crafting
|
|
169
|
+
* Processes, and its rent current. The closed account's storage returns to
|
|
170
|
+
* the Profile. Rent settlement at deconstruction remains chain-authoritative.
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* ```ts
|
|
174
|
+
* declare const ctx: SageContext;
|
|
175
|
+
* declare const hab: CraftingHabSnapshot;
|
|
176
|
+
* declare const starbasePlayer: CraftingHabStarbasePlayer;
|
|
177
|
+
* declare const authorization: PlanAuthorization;
|
|
178
|
+
* const plan = await planDeconstructCraftingHab(ctx, hab, {
|
|
179
|
+
* authorization,
|
|
180
|
+
* starbasePlayer,
|
|
181
|
+
* });
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
export declare function planDeconstructCraftingHab(ctx: SageContext, hab: CraftingHabSnapshot, input: PlanDeconstructCraftingHabOptions): Promise<Plan>;
|
|
185
|
+
export {};
|
|
186
|
+
//# sourceMappingURL=habs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"habs.d.ts","sourceRoot":"","sources":["../../../src/crafting/actions/habs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,KAAK,OAAO,EAAE,MAAM,aAAa,CAAC;AAEtD,OAAO,KAAK,EAAc,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAkBzE,OAAO,EAEL,KAAK,iBAAiB,EACvB,MAAM,4BAA4B,CAAC;AAMpC,OAAO,EAKL,KAAK,iBAAiB,EACvB,MAAM,kDAAkD,CAAC;AAS1D,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,yBAAyB,CAAC;AAOpD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAYvD,QAAA,MAAM,KAAK,kDAAmD,CAAC;AAE/D;;;;;;GAMG;AACH,MAAM,WAAW,yBAAyB;IACxC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,aAAa,EAAE;QAAE,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;KAAE,CAAC;IACtD,QAAQ,CAAC,MAAM,EAAE;QAAE,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;KAAE,CAAC;CAChD;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,WAAW,2BAA2B;IAC1C,QAAQ,CAAC,aAAa,EAAE,iBAAiB,CAAC;IAC1C;kDAC8C;IAC9C,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAC;IAC9B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,2EAA2E;IAC3E,QAAQ,CAAC,mBAAmB,CAAC,EAAE,MAAM,CAAC;CACvC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,WAAW,+BAA+B;IAC9C,QAAQ,CAAC,aAAa,EAAE,iBAAiB,CAAC;IAC1C,uEAAuE;IACvE,QAAQ,CAAC,cAAc,EAAE,yBAAyB,CAAC;IACnD,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;CACjC;AAED;;;;;;;;;;GAUG;AACH,MAAM,WAAW,iCAAiC;IAChD,QAAQ,CAAC,aAAa,EAAE,iBAAiB,CAAC;IAC1C;gCAC4B;IAC5B,QAAQ,CAAC,cAAc,EAAE,yBAAyB,CAAC;IACnD;kFAC8E;IAC9E,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;CACjC;AAED;;;;;;GAMG;AACH,MAAM,WAAW,yBAAyB;IACxC,QAAQ,CAAC,oBAAoB,EAAE,MAAM,CAAC;IACtC;gDAC4C;IAC5C,QAAQ,CAAC,qBAAqB,EAAE,MAAM,CAAC;IACvC,0EAA0E;IAC1E,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB;AAsED;;;;;;;;;GASG;AACH,wBAAgB,+BAA+B,CAC7C,QAAQ,EAAE,IAAI,CACZ,iBAAiB,EACf,OAAO,GACP,kBAAkB,GAClB,6BAA6B,GAC7B,yBAAyB,CAC5B,EACD,UAAU,EAAE;IACV,QAAQ,CAAC,IAAI,EAAE,CAAC,OAAO,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC;IACtC,QAAQ,CAAC,sBAAsB,EAAE;QAAE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;KAAE,CAAC;IAC1D,QAAQ,CAAC,cAAc,EAAE;QAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;CACrD,GACA,yBAAyB,CAe3B;AAoFD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAsB,oBAAoB,CACxC,GAAG,EAAE,WAAW,EAChB,cAAc,EAAE,yBAAyB,EACzC,KAAK,EAAE,2BAA2B,GACjC,OAAO,CAAC,IAAI,CAAC,CA2Of;AA6BD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,wBAAwB,CAC5C,GAAG,EAAE,WAAW,EAChB,GAAG,EAAE,mBAAmB,EACxB,KAAK,EAAE,+BAA+B,GACrC,OAAO,CAAC,IAAI,CAAC,CAqDf;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,0BAA0B,CAC9C,GAAG,EAAE,WAAW,EAChB,GAAG,EAAE,mBAAmB,EACxB,KAAK,EAAE,iCAAiC,GACvC,OAAO,CAAC,IAAI,CAAC,CA4Hf"}
|
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
import { isAddress } from '@solana/kit';
|
|
2
|
+
import { InvalidActionInputError, SageSdkError } from '../../client/errors.js';
|
|
3
|
+
import { readContextRawAccounts } from '../../client/reconciliation.js';
|
|
4
|
+
import { characterAccountDefinition, findCharacterPda, } from '../../internal/c4/character.js';
|
|
5
|
+
import { craftingHabAccountDefinition } from '../../internal/c4/crafting.js';
|
|
6
|
+
import { habPlotAvailable } from '../../internal/c4/crafting-hab-storage.js';
|
|
7
|
+
import { deriveCurrencyCache } from '../../internal/c4/currency-cache.js';
|
|
8
|
+
import { factionValue } from '../../internal/c4/faction.js';
|
|
9
|
+
import { deriveRegionBuildingPolicyAddress } from '../../internal/c4/pda.js';
|
|
10
|
+
import { deriveProfileFactionAddress, profileFactionIdentityAccountDefinition, } from '../../internal/c4/profile-faction.js';
|
|
11
|
+
import { readRegionBuildingPolicy } from '../../internal/c4/region-building-policy.js';
|
|
12
|
+
import { starbasePlayerAccountDefinition } from '../../internal/c4/starbase.js';
|
|
13
|
+
import { starSystemAccountDefinition, } from '../../internal/c4/world.js';
|
|
14
|
+
import { requireGameAddress } from '../../internal/game-context.js';
|
|
15
|
+
import { accountAbsentPrecondition, accountStatePrecondition, } from '../../internal/planning/account-state.js';
|
|
16
|
+
import { adaptCraftingHabRentTopUpInstruction, adaptDeconstructCraftingHabInstruction, adaptPlaceCraftingHabInstruction, readPlanAuthorization, } from '../../internal/planning/authorization-adapter.js';
|
|
17
|
+
import { readCraftingHabStorageAction } from '../../internal/planning/crafting-hab-storage-actions.js';
|
|
18
|
+
import { structurePlacementFee } from '../../internal/planning/placement-fee.js';
|
|
19
|
+
import { RENT_ADDRESS } from '../../internal/planning/rent.js';
|
|
20
|
+
import { CLOCK_ADDRESS, readClock, } from '../../internal/planning/faction-enrollment.js';
|
|
21
|
+
import { planStorageFunding } from '../../internal/planning/storage-actions.js';
|
|
22
|
+
import { buildPlannerPlan } from '../../planning/plan-construction.js';
|
|
23
|
+
import { resolveCraftingHabDefinition } from '../../registry/index.js';
|
|
24
|
+
import { decodeRegistryGame, gameCraftingHabsRegistrySection, } from '../../internal/c4/game.js';
|
|
25
|
+
const U64_MAX = 18446744073709551615n;
|
|
26
|
+
const LEVELS = [
|
|
27
|
+
'level-0',
|
|
28
|
+
'level-1',
|
|
29
|
+
'level-2',
|
|
30
|
+
'level-3',
|
|
31
|
+
'level-4',
|
|
32
|
+
'level-5',
|
|
33
|
+
'css',
|
|
34
|
+
];
|
|
35
|
+
const TIERS = ['one', 'two', 'three', 'four', 'five'];
|
|
36
|
+
function fail(message, address) {
|
|
37
|
+
throw new SageSdkError('ACTION_PRECONDITION_FAILED', message, { address });
|
|
38
|
+
}
|
|
39
|
+
function invalid(field, value, accepted, remedy) {
|
|
40
|
+
throw new InvalidActionInputError(field, value, accepted, remedy);
|
|
41
|
+
}
|
|
42
|
+
function readOptions(value, required, optional = []) {
|
|
43
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value))
|
|
44
|
+
return invalid('options', value, 'a plain object', 'Provide the documented options object before retrying.');
|
|
45
|
+
const options = value;
|
|
46
|
+
const missing = required.find((key) => !Object.hasOwn(options, key));
|
|
47
|
+
const unexpected = Object.keys(options).find((key) => !required.includes(key) && !optional.includes(key));
|
|
48
|
+
if (missing !== undefined || unexpected !== undefined)
|
|
49
|
+
invalid('options', missing === undefined ? unexpected : `missing ${missing}`, `the fields ${[...required, ...optional].join(', ')}`, 'Remove unknown fields and provide every required option before retrying.');
|
|
50
|
+
return options;
|
|
51
|
+
}
|
|
52
|
+
function present(raw, address, message) {
|
|
53
|
+
if (raw === undefined || raw.executable)
|
|
54
|
+
return fail(message, address);
|
|
55
|
+
return raw;
|
|
56
|
+
}
|
|
57
|
+
function atlasAmount(value, field, positive) {
|
|
58
|
+
if (typeof value !== 'bigint' ||
|
|
59
|
+
value < (positive ? 1n : 0n) ||
|
|
60
|
+
value > U64_MAX)
|
|
61
|
+
return invalid(field, value, positive ? 'a positive u64 bigint' : 'a u64 bigint', 'Provide the raw ATLAS amount (8 decimals) as a bigint, then retry.');
|
|
62
|
+
return value;
|
|
63
|
+
}
|
|
64
|
+
function levelIndex(starbase) {
|
|
65
|
+
return LEVELS.indexOf(starbase.level);
|
|
66
|
+
}
|
|
67
|
+
/** What placing a Crafting Hab of a definition costs at a Starbase: the
|
|
68
|
+
* placement fee, the rent rate, and the plots for its tier. Pure; the game
|
|
69
|
+
* also requires a free plot, the Hab item and the Character's research.
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* declare const starbase: WorldStarbaseData;
|
|
73
|
+
* declare const definition: { tier: 'one'; placementFeeMultiplier: { raw: bigint }; rentMultiplier: { value: number } };
|
|
74
|
+
* const quote = deriveCraftingHabPlacementQuote(starbase, definition);
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export function deriveCraftingHabPlacementQuote(starbase, definition) {
|
|
78
|
+
const level = LEVELS.indexOf(starbase.level);
|
|
79
|
+
const tier = TIERS.indexOf(definition.tier);
|
|
80
|
+
const fee = starbase.baseCraftingHabPlacementFee[level][tier];
|
|
81
|
+
const rent = starbase.baseCraftingHabRentRate[level][tier];
|
|
82
|
+
return {
|
|
83
|
+
placementFeeAtlasRaw: structurePlacementFee(fee.raw, definition.placementFeeMultiplier.raw),
|
|
84
|
+
rentAtlasRawPerSecond: rent.value * definition.rentMultiplier.value,
|
|
85
|
+
plots: starbase.craftingHabPlots
|
|
86
|
+
.slice(0, level + 1)
|
|
87
|
+
.reduce((sum, row) => sum + row[tier], 0),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/** The authorization Profile's current Starbase Player and its Star System,
|
|
91
|
+
* read fresh. */
|
|
92
|
+
async function readPlayer(ctx, snapshot, authorization, game) {
|
|
93
|
+
if (snapshot.playerProfile.address !== authorization.profile)
|
|
94
|
+
fail(`Starbase Player ${snapshot.address} belongs to Profile ${snapshot.playerProfile.address}, not authorization Profile ${authorization.profile}. Use the authorization Profile's Starbase Player and retry.`, snapshot.address);
|
|
95
|
+
const character = await findCharacterPda(ctx.programs, authorization.profile, game);
|
|
96
|
+
const system = snapshot.system.address;
|
|
97
|
+
const [playerCandidate, systemCandidate, characterCandidate] = await readContextRawAccounts(ctx, [snapshot.address, system, character]);
|
|
98
|
+
const playerRaw = present(playerCandidate, snapshot.address, `Starbase Player ${snapshot.address} is missing. Register at the Starbase and retry.`);
|
|
99
|
+
const player = starbasePlayerAccountDefinition.decode(playerRaw, ctx.programs);
|
|
100
|
+
const systemRaw = present(systemCandidate, system, `Star System ${system} is missing. Refresh it and retry.`);
|
|
101
|
+
const systemData = starSystemAccountDefinition.decode(systemRaw, ctx.programs);
|
|
102
|
+
const characterRaw = present(characterCandidate, character, `Profile ${authorization.profile} has no Character ${character}. Register the Character and retry.`);
|
|
103
|
+
const characterData = characterAccountDefinition.decode(characterRaw, ctx.programs);
|
|
104
|
+
if (player.playerProfile !== authorization.profile ||
|
|
105
|
+
player.system !== system ||
|
|
106
|
+
player.character !== character ||
|
|
107
|
+
systemData.game !== game)
|
|
108
|
+
fail(`Starbase Player ${snapshot.address} no longer matches Profile ${authorization.profile}, its Character or Star System ${system} in the configured Game. Refresh it and retry.`, snapshot.address);
|
|
109
|
+
const starbase = systemData.starbase;
|
|
110
|
+
if (starbase === undefined)
|
|
111
|
+
return fail(`Star System ${system} has no Starbase. Choose a System with a Starbase and retry.`, system);
|
|
112
|
+
if (player.sequenceId !== systemData.sequenceId)
|
|
113
|
+
fail(`Starbase Player ${snapshot.address} belongs to an earlier Starbase at Star System ${system}. Register at the current Starbase and retry.`, snapshot.address);
|
|
114
|
+
return {
|
|
115
|
+
character,
|
|
116
|
+
characterRaw,
|
|
117
|
+
characterData,
|
|
118
|
+
playerRaw,
|
|
119
|
+
player,
|
|
120
|
+
system,
|
|
121
|
+
systemRaw,
|
|
122
|
+
systemData,
|
|
123
|
+
starbase,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/** Plans placing a Crafting Hab on a Starbase plot.
|
|
127
|
+
*
|
|
128
|
+
* The game takes one Hab item of the definition from the Starbase Player,
|
|
129
|
+
* charges the placement fee and any initial rent from the Character's ATLAS,
|
|
130
|
+
* and puts the Hab on the first level, up to the Starbase's own, with a free
|
|
131
|
+
* plot for its tier. The Starbase must belong to the Profile's faction, and
|
|
132
|
+
* the Character's research caps how many Habs it holds and their tier. The
|
|
133
|
+
* Profile funds the new Hab's storage and the Star System's growth.
|
|
134
|
+
*
|
|
135
|
+
* The caller generates the Hab's keypair; its address signs the placement
|
|
136
|
+
* with the authorization key. Placement races and research beyond the
|
|
137
|
+
* Character's cap remain chain-authoritative.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```ts
|
|
141
|
+
* declare const ctx: SageContext;
|
|
142
|
+
* declare const starbasePlayer: CraftingHabStarbasePlayer;
|
|
143
|
+
* declare const authorization: PlanAuthorization;
|
|
144
|
+
* declare const craftingHab: Address;
|
|
145
|
+
* const plan = await planPlaceCraftingHab(ctx, starbasePlayer, {
|
|
146
|
+
* authorization,
|
|
147
|
+
* craftingHab,
|
|
148
|
+
* definitionId: 0,
|
|
149
|
+
* });
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
export async function planPlaceCraftingHab(ctx, starbasePlayer, input) {
|
|
153
|
+
const options = readOptions(input, ['authorization', 'craftingHab', 'definitionId'], ['initialRentAtlasRaw']);
|
|
154
|
+
const authorization = readPlanAuthorization(options.authorization);
|
|
155
|
+
if (typeof options.craftingHab !== 'string' ||
|
|
156
|
+
!isAddress(options.craftingHab))
|
|
157
|
+
invalid('craftingHab', options.craftingHab, "a new keypair's address", 'Generate a keypair for the Hab, pass its address and sign with it, then retry.');
|
|
158
|
+
const craftingHab = options.craftingHab;
|
|
159
|
+
if (!Number.isSafeInteger(options.definitionId))
|
|
160
|
+
invalid('definitionId', options.definitionId, 'a Crafting Hab definition id', 'Choose a definition from listCraftingHabDefinitions, then retry.');
|
|
161
|
+
const initialRent = options.initialRentAtlasRaw === undefined
|
|
162
|
+
? 0n
|
|
163
|
+
: atlasAmount(options.initialRentAtlasRaw, 'initialRentAtlasRaw', false);
|
|
164
|
+
const game = requireGameAddress(ctx);
|
|
165
|
+
// The fee and the Hab item come from the Game's definition; read it fresh so
|
|
166
|
+
// the Plan can guard the exact Game it priced against.
|
|
167
|
+
const [gameCandidate] = await readContextRawAccounts(ctx, [game]);
|
|
168
|
+
const gameRaw = present(gameCandidate, game, `The configured Game ${game} is missing. Verify the cluster and Game, then retry.`);
|
|
169
|
+
const definition = gameCraftingHabsRegistrySection(gameRaw, decodeRegistryGame(ctx.programs, gameRaw)).definitions.find(({ id }) => id === options.definitionId);
|
|
170
|
+
if (definition === undefined)
|
|
171
|
+
return invalid('definitionId', options.definitionId, 'a Crafting Hab definition id of the configured Game', 'Choose a definition from listCraftingHabDefinitions, then retry.');
|
|
172
|
+
const fresh = await readPlayer(ctx, starbasePlayer, authorization, game);
|
|
173
|
+
const { starbase, system, characterData } = fresh;
|
|
174
|
+
const [profileFaction, [currencyCache], regionId] = await Promise.all([
|
|
175
|
+
deriveProfileFactionAddress(ctx.programs, authorization.profile),
|
|
176
|
+
deriveCurrencyCache(ctx.programs, game),
|
|
177
|
+
Promise.resolve(fresh.systemData.regionId),
|
|
178
|
+
]);
|
|
179
|
+
const policyAddress = regionId === undefined
|
|
180
|
+
? ctx.programs.sage
|
|
181
|
+
: (await deriveRegionBuildingPolicyAddress(ctx.programs, game, regionId))[0];
|
|
182
|
+
const [factionCandidate, existing, profileCandidate, rent, ...policyCandidate] = await readContextRawAccounts(ctx, [
|
|
183
|
+
profileFaction,
|
|
184
|
+
craftingHab,
|
|
185
|
+
authorization.profile,
|
|
186
|
+
RENT_ADDRESS,
|
|
187
|
+
...(regionId === undefined ? [] : [policyAddress]),
|
|
188
|
+
]);
|
|
189
|
+
if (existing !== undefined)
|
|
190
|
+
fail(`Account ${craftingHab} already exists, so it cannot become a new Crafting Hab. Generate a fresh keypair and retry.`, craftingHab);
|
|
191
|
+
const faction = profileFactionIdentityAccountDefinition.decode(present(factionCandidate, profileFaction, `Profile ${authorization.profile} has no faction. Join a faction and retry.`), ctx.programs);
|
|
192
|
+
if (faction.profile !== authorization.profile ||
|
|
193
|
+
faction.factionId !== factionValue(starbase.owner))
|
|
194
|
+
fail(`The Starbase at Star System ${system} belongs to ${starbase.owner}, not the Profile's faction; Crafting Habs can only be placed at the Profile's own faction's Starbases.`, system);
|
|
195
|
+
const level = levelIndex(starbase);
|
|
196
|
+
const tier = TIERS.indexOf(definition.tier);
|
|
197
|
+
const provided = new Set([
|
|
198
|
+
...starbase.locationTags,
|
|
199
|
+
...starbase.buildingTags,
|
|
200
|
+
]);
|
|
201
|
+
const missingTags = definition.requiredTagIds.filter((tag) => !provided.has(tag));
|
|
202
|
+
if (missingTags.length > 0)
|
|
203
|
+
fail(`${definition.name} requires Starbase tags ${missingTags.join(', ')}, which the Starbase at Star System ${system} does not provide. Choose another Starbase or Hab, then retry.`, system);
|
|
204
|
+
const modifiers = characterData.modifiers.values;
|
|
205
|
+
if (regionId !== undefined) {
|
|
206
|
+
const policy = readRegionBuildingPolicy(ctx.programs, policyCandidate[0], policyAddress, { game, regionId });
|
|
207
|
+
const missing = policy.requiredResearchTags.filter((tag) => !modifiers.researchTags.includes(tag));
|
|
208
|
+
if (missing.length > 0)
|
|
209
|
+
fail(`Character ${fresh.character} lacks research tags ${missing.join(', ')}, which Region ${String(regionId)} requires to build in Star System ${system}. Unlock that research, or choose a System in another Region.`, policyAddress);
|
|
210
|
+
}
|
|
211
|
+
if (modifiers.maxResidentialPlotTier !== undefined &&
|
|
212
|
+
tier + 1 > modifiers.maxResidentialPlotTier)
|
|
213
|
+
fail(`Character ${fresh.character} may place Crafting Habs up to tier ${String(modifiers.maxResidentialPlotTier)}, below ${definition.name}'s tier ${definition.tier}. Research a higher residential tier or pick a lower tier.`, fresh.character);
|
|
214
|
+
if (characterData.counts.craftingHabs >= modifiers.maxResidentialPlots)
|
|
215
|
+
fail(`Character ${fresh.character} already holds ${String(characterData.counts.craftingHabs)} of its ${String(modifiers.maxResidentialPlots)} residential plots. Research more plots or deconstruct a Crafting Hab.`, fresh.character);
|
|
216
|
+
if (!habPlotAvailable(fresh.systemRaw, level, tier))
|
|
217
|
+
fail(`The Starbase at Star System ${system} has no free tier-${definition.tier} Crafting Hab plot on any level up to its own. Wait for a plot to free up or choose another Starbase.`, system);
|
|
218
|
+
const held = fresh.player.cargo.amounts.find((a) => a.cargoId === definition.cargoId)
|
|
219
|
+
?.amount ?? 0n;
|
|
220
|
+
if (held < 1n)
|
|
221
|
+
fail(`Starbase Player ${starbasePlayer.address} holds no ${definition.name} item (cargo ${String(definition.cargoId)}), which placement consumes. Bring one to the Starbase and retry.`, starbasePlayer.address);
|
|
222
|
+
const quote = deriveCraftingHabPlacementQuote(starbase, definition);
|
|
223
|
+
if (characterData.atlasRaw < quote.placementFeeAtlasRaw + initialRent)
|
|
224
|
+
fail(`Character ${fresh.character} has ${String(characterData.atlasRaw)} raw ATLAS, below the ${String(quote.placementFeeAtlasRaw)} raw placement fee plus ${String(initialRent)} raw initial rent. Add ATLAS or lower the rent, then retry.`, fresh.character);
|
|
225
|
+
const instruction = adaptPlaceCraftingHabInstruction(ctx.programs, {
|
|
226
|
+
authorization,
|
|
227
|
+
game,
|
|
228
|
+
profileFaction,
|
|
229
|
+
craftingHab,
|
|
230
|
+
system,
|
|
231
|
+
starbasePlayer: starbasePlayer.address,
|
|
232
|
+
character: fresh.character,
|
|
233
|
+
currencyCache,
|
|
234
|
+
regionBuildingPolicy: policyAddress,
|
|
235
|
+
definitionId: definition.id,
|
|
236
|
+
initialRentRaw: options.initialRentAtlasRaw === undefined ? undefined : initialRent,
|
|
237
|
+
});
|
|
238
|
+
const accounts = new Map([
|
|
239
|
+
[system, fresh.systemRaw],
|
|
240
|
+
[authorization.profile, profileCandidate],
|
|
241
|
+
[RENT_ADDRESS, rent],
|
|
242
|
+
]);
|
|
243
|
+
const storage = planStorageFunding(ctx.programs, {
|
|
244
|
+
action: readCraftingHabStorageAction(ctx.programs, instruction, accounts),
|
|
245
|
+
accounts,
|
|
246
|
+
label: 'Crafting Hab placement',
|
|
247
|
+
});
|
|
248
|
+
const description = `Place ${definition.name} ${craftingHab} at the Starbase of Star System ${system}: the game takes one Hab item from Starbase Player ${starbasePlayer.address}, a ${String(quote.placementFeeAtlasRaw)} raw ATLAS placement fee and ${String(initialRent)} raw initial rent from Character ${fresh.character}. Rent accrues at ${String(quote.rentAtlasRawPerSecond)} raw ATLAS per second. ${storage.disclosure} Placement races remain chain-authoritative.`;
|
|
249
|
+
return buildPlannerPlan(ctx.programs, {
|
|
250
|
+
kind: 'crafting.place-crafting-hab',
|
|
251
|
+
preconditions: [
|
|
252
|
+
accountAbsentPrecondition(craftingHab, `Account ${craftingHab} still does not exist.`),
|
|
253
|
+
...(await Promise.all([
|
|
254
|
+
accountStatePrecondition(gameRaw, `The configured Game keeps ${definition.name}'s item, tier and fee multiplier.`),
|
|
255
|
+
accountStatePrecondition(fresh.systemRaw, `Star System ${system} keeps its Starbase level, placement fee, faction and Hab plots.`),
|
|
256
|
+
accountStatePrecondition(fresh.playerRaw, `Starbase Player ${starbasePlayer.address} keeps its Hab item.`),
|
|
257
|
+
accountStatePrecondition(fresh.characterRaw, `Character ${fresh.character} keeps its ATLAS, research and Hab count.`),
|
|
258
|
+
...(regionId === undefined
|
|
259
|
+
? []
|
|
260
|
+
: [
|
|
261
|
+
policyCandidate[0] === undefined
|
|
262
|
+
? Promise.resolve(accountAbsentPrecondition(policyAddress, `Region ${String(regionId)} still has no building policy.`))
|
|
263
|
+
: accountStatePrecondition(policyCandidate[0], `Region ${String(regionId)} keeps its building policy.`),
|
|
264
|
+
]),
|
|
265
|
+
])),
|
|
266
|
+
storage.guard,
|
|
267
|
+
],
|
|
268
|
+
steps: [
|
|
269
|
+
{
|
|
270
|
+
describes: description,
|
|
271
|
+
instruction,
|
|
272
|
+
signers: [authorization.authority, craftingHab],
|
|
273
|
+
},
|
|
274
|
+
],
|
|
275
|
+
summary: description,
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
/** Reads a Crafting Hab of the authorization Profile, fresh. */
|
|
279
|
+
async function readHab(ctx, hab, authorization, game) {
|
|
280
|
+
if (hab.ownerProfile.address !== authorization.profile)
|
|
281
|
+
fail(`Crafting Hab ${hab.address} belongs to Profile ${hab.ownerProfile.address}; only that Profile can manage it.`, hab.address);
|
|
282
|
+
const [candidate] = await readContextRawAccounts(ctx, [hab.address]);
|
|
283
|
+
const raw = present(candidate, hab.address, `Crafting Hab ${hab.address} is missing; it may have been deconstructed. Refresh it and retry.`);
|
|
284
|
+
const data = craftingHabAccountDefinition.decode(raw, ctx.programs);
|
|
285
|
+
if (data.ownerProfile !== authorization.profile || data.game !== game)
|
|
286
|
+
fail(`Crafting Hab ${hab.address} no longer belongs to Profile ${authorization.profile} in the configured Game. Refresh it and retry.`, hab.address);
|
|
287
|
+
return { raw, data };
|
|
288
|
+
}
|
|
289
|
+
/** Plans moving raw ATLAS from the Character into a Crafting Hab's rent.
|
|
290
|
+
*
|
|
291
|
+
* The game refuses a top-up once the Hab has been delinquent past the Game's
|
|
292
|
+
* eviction grace period. Rent accrual remains chain-authoritative.
|
|
293
|
+
*
|
|
294
|
+
* @example
|
|
295
|
+
* ```ts
|
|
296
|
+
* declare const ctx: SageContext;
|
|
297
|
+
* declare const hab: CraftingHabSnapshot;
|
|
298
|
+
* declare const starbasePlayer: CraftingHabStarbasePlayer;
|
|
299
|
+
* declare const authorization: PlanAuthorization;
|
|
300
|
+
* const plan = await planTopUpCraftingHabRent(ctx, hab, {
|
|
301
|
+
* authorization,
|
|
302
|
+
* starbasePlayer,
|
|
303
|
+
* amountAtlasRaw: 100_000_000n,
|
|
304
|
+
* });
|
|
305
|
+
* ```
|
|
306
|
+
*/
|
|
307
|
+
export async function planTopUpCraftingHabRent(ctx, hab, input) {
|
|
308
|
+
const options = readOptions(input, [
|
|
309
|
+
'authorization',
|
|
310
|
+
'starbasePlayer',
|
|
311
|
+
'amountAtlasRaw',
|
|
312
|
+
]);
|
|
313
|
+
const authorization = readPlanAuthorization(options.authorization);
|
|
314
|
+
const amount = atlasAmount(options.amountAtlasRaw, 'amountAtlasRaw', true);
|
|
315
|
+
const game = requireGameAddress(ctx);
|
|
316
|
+
const current = await readHab(ctx, hab, authorization, game);
|
|
317
|
+
const fresh = await readPlayer(ctx, options.starbasePlayer, authorization, game);
|
|
318
|
+
if (current.data.system !== fresh.system)
|
|
319
|
+
fail(`Crafting Hab ${hab.address} is in Star System ${current.data.system}, not ${fresh.system}. Use the Starbase Player in the Hab's System and retry.`, hab.address);
|
|
320
|
+
if (fresh.characterData.atlasRaw < amount)
|
|
321
|
+
fail(`Character ${fresh.character} has ${String(fresh.characterData.atlasRaw)} raw ATLAS, less than the ${String(amount)} to top up. Add ATLAS or top up less, then retry.`, fresh.character);
|
|
322
|
+
const instruction = adaptCraftingHabRentTopUpInstruction(ctx.programs, {
|
|
323
|
+
authorization,
|
|
324
|
+
character: fresh.character,
|
|
325
|
+
craftingHab: hab.address,
|
|
326
|
+
system: fresh.system,
|
|
327
|
+
starbasePlayer: fresh.playerRaw.address,
|
|
328
|
+
game,
|
|
329
|
+
amountRaw: amount,
|
|
330
|
+
});
|
|
331
|
+
const description = `Top up Crafting Hab ${hab.address}'s rent with ${String(amount)} raw ATLAS from Character ${fresh.character} (rent balance now ${String(current.data.rentBalanceRaw)} raw). The game refuses a top-up once the Hab has been delinquent past the eviction grace period; rent accrual remains chain-authoritative.`;
|
|
332
|
+
return buildPlannerPlan(ctx.programs, {
|
|
333
|
+
kind: 'crafting.top-up-crafting-hab-rent',
|
|
334
|
+
preconditions: [
|
|
335
|
+
await accountStatePrecondition(fresh.characterRaw, `Character ${fresh.character} keeps its ATLAS.`),
|
|
336
|
+
],
|
|
337
|
+
steps: [
|
|
338
|
+
{
|
|
339
|
+
describes: description,
|
|
340
|
+
instruction,
|
|
341
|
+
signers: [authorization.authority],
|
|
342
|
+
},
|
|
343
|
+
],
|
|
344
|
+
summary: description,
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
/** Plans deconstructing a Crafting Hab: the game closes it, returns its Hab
|
|
348
|
+
* item to the Starbase Player and its remaining rent to the Character, and
|
|
349
|
+
* frees its plot, moving a newer Hab down into it when one is waiting.
|
|
350
|
+
*
|
|
351
|
+
* The Hab must be Active, with no buildings and no running Crafting
|
|
352
|
+
* Processes, and its rent current. The closed account's storage returns to
|
|
353
|
+
* the Profile. Rent settlement at deconstruction remains chain-authoritative.
|
|
354
|
+
*
|
|
355
|
+
* @example
|
|
356
|
+
* ```ts
|
|
357
|
+
* declare const ctx: SageContext;
|
|
358
|
+
* declare const hab: CraftingHabSnapshot;
|
|
359
|
+
* declare const starbasePlayer: CraftingHabStarbasePlayer;
|
|
360
|
+
* declare const authorization: PlanAuthorization;
|
|
361
|
+
* const plan = await planDeconstructCraftingHab(ctx, hab, {
|
|
362
|
+
* authorization,
|
|
363
|
+
* starbasePlayer,
|
|
364
|
+
* });
|
|
365
|
+
* ```
|
|
366
|
+
*/
|
|
367
|
+
export async function planDeconstructCraftingHab(ctx, hab, input) {
|
|
368
|
+
const options = readOptions(input, ['authorization', 'starbasePlayer'], ['topUpAtlasRaw']);
|
|
369
|
+
const authorization = readPlanAuthorization(options.authorization);
|
|
370
|
+
const topUp = options.topUpAtlasRaw === undefined
|
|
371
|
+
? 0n
|
|
372
|
+
: atlasAmount(options.topUpAtlasRaw, 'topUpAtlasRaw', true);
|
|
373
|
+
const game = requireGameAddress(ctx);
|
|
374
|
+
const current = await readHab(ctx, hab, authorization, game);
|
|
375
|
+
const fresh = await readPlayer(ctx, options.starbasePlayer, authorization, game);
|
|
376
|
+
const data = current.data;
|
|
377
|
+
if (data.system !== fresh.system)
|
|
378
|
+
fail(`Crafting Hab ${hab.address} is in Star System ${data.system}, not ${fresh.system}. Use the Starbase Player in the Hab's System and retry.`, hab.address);
|
|
379
|
+
if (data.state.kind !== 'active')
|
|
380
|
+
fail(`Crafting Hab ${hab.address} is ${data.state.kind}; the game deconstructs only an Active Hab. ${data.state.kind === 'design' ? 'Finalize or cancel its building changes first.' : 'Pay its rent to reactivate it first.'}`, hab.address);
|
|
381
|
+
if (data.buildings.length > 0)
|
|
382
|
+
fail(`Crafting Hab ${hab.address} still has ${String(data.buildings.length)} building types; remove every building before deconstructing.`, hab.address);
|
|
383
|
+
if (data.jobsRunning > 0)
|
|
384
|
+
fail(`Crafting Hab ${hab.address} runs ${String(data.jobsRunning)} Crafting Processes; complete or cancel them before deconstructing.`, hab.address);
|
|
385
|
+
const group = fresh.starbase.craftingHabs.find((entry) => entry.chronoAddresses.includes(hab.address));
|
|
386
|
+
if (group === undefined)
|
|
387
|
+
return fail(`Crafting Hab ${hab.address} is not on any plot of the Starbase at Star System ${fresh.system}. Refresh the System and retry.`, fresh.system);
|
|
388
|
+
// The game settles rent first and refuses a Hab whose balance cannot cover
|
|
389
|
+
// it. Rent accrues fractionally, so this refuses only a clear shortfall.
|
|
390
|
+
const definition = await resolveCraftingHabDefinition(ctx, data.definitionId);
|
|
391
|
+
const [clock] = await readContextRawAccounts(ctx, [CLOCK_ADDRESS]);
|
|
392
|
+
const now = readClock(present(clock, CLOCK_ADDRESS, 'The Clock sysvar is missing. Refresh chain time and retry.'));
|
|
393
|
+
const rate = deriveCraftingHabPlacementQuote(fresh.starbase, definition).rentAtlasRawPerSecond;
|
|
394
|
+
const elapsed = now > data.lastRentAtUnixSeconds ? now - data.lastRentAtUnixSeconds : 0n;
|
|
395
|
+
const due = BigInt(Math.floor(Number(elapsed) * rate));
|
|
396
|
+
if (data.rentBalanceRaw + topUp < due)
|
|
397
|
+
fail(`Crafting Hab ${hab.address} owes about ${String(due)} raw ATLAS of rent but holds ${String(data.rentBalanceRaw + topUp)}${topUp > 0n ? ' with the top-up' : ''}, so the game refuses to deconstruct it. Pass topUpAtlasRaw of at least ${String(due - data.rentBalanceRaw)}, with some margin for rent accruing until the transaction lands, then retry.`, hab.address);
|
|
398
|
+
if (fresh.characterData.atlasRaw < topUp)
|
|
399
|
+
fail(`Character ${fresh.character} has ${String(fresh.characterData.atlasRaw)} raw ATLAS, less than the ${String(topUp)} to top up. Add ATLAS or top up less, then retry.`, fresh.character);
|
|
400
|
+
const [currencyCache] = await deriveCurrencyCache(ctx.programs, game);
|
|
401
|
+
const instruction = adaptDeconstructCraftingHabInstruction(ctx.programs, {
|
|
402
|
+
authorization,
|
|
403
|
+
character: fresh.character,
|
|
404
|
+
game,
|
|
405
|
+
craftingHab: hab.address,
|
|
406
|
+
system: fresh.system,
|
|
407
|
+
starbasePlayer: fresh.playerRaw.address,
|
|
408
|
+
currencyCache,
|
|
409
|
+
plotLevel: group.key,
|
|
410
|
+
plotIndex: group.chronoAddresses.indexOf(hab.address),
|
|
411
|
+
});
|
|
412
|
+
const description = `Deconstruct Crafting Hab ${hab.address}: return its Hab item to Starbase Player ${fresh.playerRaw.address} and its ${String(data.rentBalanceRaw + topUp)} raw rent balance, less rent due, to Character ${fresh.character}, and free its plot at level ${String(group.key)}. The closed account's storage returns to the Profile. The game refuses while rent is delinquent; rent settlement remains chain-authoritative.`;
|
|
413
|
+
const steps = [
|
|
414
|
+
...(topUp === 0n
|
|
415
|
+
? []
|
|
416
|
+
: [
|
|
417
|
+
{
|
|
418
|
+
describes: `Top up Crafting Hab ${hab.address}'s rent with ${String(topUp)} raw ATLAS from Character ${fresh.character}, so its rent is current when it is deconstructed.`,
|
|
419
|
+
instruction: adaptCraftingHabRentTopUpInstruction(ctx.programs, {
|
|
420
|
+
authorization,
|
|
421
|
+
character: fresh.character,
|
|
422
|
+
craftingHab: hab.address,
|
|
423
|
+
system: fresh.system,
|
|
424
|
+
starbasePlayer: fresh.playerRaw.address,
|
|
425
|
+
game,
|
|
426
|
+
amountRaw: topUp,
|
|
427
|
+
}),
|
|
428
|
+
signers: [authorization.authority],
|
|
429
|
+
},
|
|
430
|
+
]),
|
|
431
|
+
{ describes: description, instruction, signers: [authorization.authority] },
|
|
432
|
+
];
|
|
433
|
+
return buildPlannerPlan(ctx.programs, {
|
|
434
|
+
kind: 'crafting.deconstruct-crafting-hab',
|
|
435
|
+
preconditions: await Promise.all([
|
|
436
|
+
accountStatePrecondition(current.raw, `Crafting Hab ${hab.address} stays Active and empty.`),
|
|
437
|
+
accountStatePrecondition(fresh.systemRaw, `Star System ${fresh.system} keeps the Hab at plot level ${String(group.key)}, index ${String(group.chronoAddresses.indexOf(hab.address))}.`),
|
|
438
|
+
]),
|
|
439
|
+
steps,
|
|
440
|
+
summary: description,
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
//# sourceMappingURL=habs.js.map
|