forked from darkforest-eth/darkforest-v0.6
-
Notifications
You must be signed in to change notification settings - Fork 3
Bulk Silver Withdraw #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
cha0sg0d
wants to merge
10
commits into
devcon
Choose a base branch
from
cha0s/bulk-silver-withdraw
base: devcon
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
00c14ea
test: bulk withdraw silver
4949cd4
contracts: add silver withdraw and bulk silver withdraw from asteroid
f9d3c38
clean bulk silver test
90eed09
test: confirm all silver is gone after bulk withdraw
88d7da5
contracts: fix tests
9f9437e
contracts: fix tests
ff1d18a
client: extract button works
af8fd5c
feat: working asteroid silver withdraw but bad implementation
8e399bf
temp: bulk withdraw in client
609702f
working demo of bulk withdraw button broken
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 | ||
|---|---|---|---|---|
|
|
@@ -70,6 +70,7 @@ import { | |||
| Transaction, | ||||
| TxIntent, | ||||
| UnconfirmedActivateArtifact, | ||||
| UnconfirmedBulkWithdrawSilver, | ||||
| UnconfirmedBuyHat, | ||||
| UnconfirmedCapturePlanet, | ||||
| UnconfirmedClaimReward, | ||||
|
|
@@ -2429,7 +2430,6 @@ class GameManager extends EventEmitter { | |||
|
|
||||
| public async withdrawSilver( | ||||
| locationId: LocationId, | ||||
| amount: number, | ||||
| bypassChecks = false | ||||
| ): Promise<Transaction<UnconfirmedWithdrawSilver>> { | ||||
| try { | ||||
|
|
@@ -2439,24 +2439,19 @@ class GameManager extends EventEmitter { | |||
| throw new Error('game has ended'); | ||||
| } | ||||
| const planet = this.entityStore.getPlanetWithId(locationId); | ||||
| console.log(`basic withdraw planet`, planet); | ||||
| if (!planet) { | ||||
| throw new Error('tried to withdraw silver from an unknown planet'); | ||||
| } | ||||
| if (planet.planetType !== PlanetType.TRADING_POST) { | ||||
| throw new Error('can only withdraw silver from spacetime rips'); | ||||
| if (planet.planetType !== PlanetType.SILVER_MINE) { | ||||
| throw new Error('can only withdraw silver from asteroid'); | ||||
| } | ||||
| if (planet.owner !== this.account) { | ||||
| throw new Error('can only withdraw silver from a planet you own'); | ||||
| } | ||||
| if (planet.transactions?.hasTransaction(isUnconfirmedWithdrawSilverTx)) { | ||||
| throw new Error('a withdraw silver action is already in progress for this planet'); | ||||
| } | ||||
| if (amount > planet.silver) { | ||||
| throw new Error('not enough silver to withdraw!'); | ||||
| } | ||||
| if (amount === 0) { | ||||
| throw new Error('must withdraw more than 0 silver!'); | ||||
| } | ||||
| if (planet.destroyed) { | ||||
| throw new Error("can't withdraw silver from a destroyed planet"); | ||||
| } | ||||
|
|
@@ -2467,9 +2462,8 @@ class GameManager extends EventEmitter { | |||
| const txIntent: UnconfirmedWithdrawSilver = { | ||||
| methodName: 'withdrawSilver', | ||||
| contract: this.contractsAPI.contract, | ||||
| args: Promise.resolve([locationIdToDecStr(locationId), amount * CONTRACT_PRECISION]), | ||||
| args: Promise.resolve([locationIdToDecStr(locationId)]), | ||||
| locationId, | ||||
| amount, | ||||
| }; | ||||
|
|
||||
| // Always await the submitTransaction so we can catch rejections | ||||
|
|
@@ -2482,6 +2476,70 @@ class GameManager extends EventEmitter { | |||
| } | ||||
| } | ||||
|
|
||||
| public async bulkWithdrawSilver( | ||||
| locationIds: LocationId[], | ||||
| bypassChecks = false | ||||
| ): Promise<Transaction<UnconfirmedBulkWithdrawSilver>> { | ||||
| const planets: Planet[] = []; | ||||
| // Clean Ids | ||||
| for (const locationId of locationIds) { | ||||
| if (!bypassChecks) { | ||||
| const planet = this.entityStore.getPlanetWithId(locationId); | ||||
| if (!planet) { | ||||
| continue; | ||||
| } | ||||
| if (planet.planetType !== PlanetType.SILVER_MINE) { | ||||
| continue; | ||||
| } | ||||
| if (planet.owner !== this.account) { | ||||
| continue; | ||||
| } | ||||
| if (planet.destroyed) { | ||||
| continue; | ||||
| } | ||||
| planets.push(planet); | ||||
| } | ||||
| } | ||||
| try { | ||||
| if (!bypassChecks) { | ||||
| if (!this.account) throw new Error('no account'); | ||||
| if (this.checkGameHasEnded()) { | ||||
| throw new Error('game has ended'); | ||||
| } | ||||
| } | ||||
|
|
||||
| if (locationIds.length === 0) throw new Error('no asteriods found to withdraw'); | ||||
|
|
||||
| const ids = planets.map((p) => `0x${p.locationId}`); | ||||
| const txIntent: UnconfirmedBulkWithdrawSilver = { | ||||
| methodName: 'bulkWithdrawSilver', | ||||
| contract: this.contractsAPI.contract, | ||||
| args: Promise.resolve([[...ids]]), | ||||
| locationIds: planets.map((p) => p.locationId), | ||||
| }; | ||||
|
|
||||
| console.log(`clean args`, Promise.resolve([[...ids]])); | ||||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
|
|
||||
| // Always await the submitTransaction so we can catch rejections | ||||
| const tx = await this.contractsAPI.submitTransaction(txIntent, { gasLimit: 15000000 }); | ||||
|
|
||||
| return tx; | ||||
| } catch (e) { | ||||
| this.getNotificationsManager().txInitError('withdrawSilver', e.message); | ||||
| throw e; | ||||
| } | ||||
| } | ||||
|
|
||||
| public async withdrawFrom20Largest() { | ||||
| const locationIds = this.getMyPlanets() | ||||
| .filter((p) => p.planetType === PlanetType.SILVER_MINE) | ||||
| .sort((a, b) => b.planetLevel - a.planetLevel) | ||||
| .slice(0, 20) | ||||
| .map((p) => p.locationId); | ||||
| console.log(`withdrawing from`, locationIds); | ||||
| return this.bulkWithdrawSilver(locationIds); | ||||
| } | ||||
|
|
||||
| /** | ||||
| * We have two locations which planet state can live: on the server, and on the blockchain. We use | ||||
| * the blockchain for the 'physics' of the universe, and the webserver for optional 'add-on' | ||||
|
|
||||
This file contains hidden or 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 hidden or 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 hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -6,8 +6,6 @@ import { CapturePlanetButton } from '../Components/CapturePlanetButton'; | |||||
| import { VerticalSplit } from '../Components/CoreUI'; | ||||||
| import { MineArtifactButton } from '../Components/MineArtifactButton'; | ||||||
| import { | ||||||
| OpenBroadcastPaneButton, | ||||||
| OpenHatPaneButton, | ||||||
| OpenManagePlanetInventoryButton, | ||||||
| OpenPlanetInfoButton, | ||||||
| OpenUpgradeDetailsPaneButton, | ||||||
|
|
@@ -53,15 +51,10 @@ function PlanetContextPaneContent({ | |||||
| } | ||||||
|
|
||||||
| let upgradeRow = null; | ||||||
| if (!p?.destroyed && owned) { | ||||||
| if (!p?.destroyed && owned && p?.planetType == PlanetType.PLANET) { | ||||||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| upgradeRow = <OpenUpgradeDetailsPaneButton modal={modal} planetId={p?.locationId} />; | ||||||
| } | ||||||
|
|
||||||
| let hatRow = null; | ||||||
| if (!p?.destroyed && owned) { | ||||||
| hatRow = <OpenHatPaneButton modal={modal} planetId={p?.locationId} />; | ||||||
| } | ||||||
|
|
||||||
| let withdrawRow = null; | ||||||
| if (!p?.destroyed && owned && p?.planetType === PlanetType.TRADING_POST) { | ||||||
| withdrawRow = <WithdrawSilver wrapper={planet} />; | ||||||
|
|
@@ -86,12 +79,10 @@ function PlanetContextPaneContent({ | |||||
| <VerticalSplit> | ||||||
| <> | ||||||
| {upgradeRow} | ||||||
| <OpenBroadcastPaneButton modal={modal} planetId={p?.locationId} /> | ||||||
| <OpenPlanetInfoButton modal={modal} planetId={p?.locationId} /> | ||||||
| </> | ||||||
| <> | ||||||
| <OpenManagePlanetInventoryButton modal={modal} planetId={p?.locationId} /> | ||||||
| {hatRow} | ||||||
| </> | ||||||
| </VerticalSplit> | ||||||
| {withdrawRow} | ||||||
|
|
||||||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.