-
Notifications
You must be signed in to change notification settings - Fork 679
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(pox-4): Add RevokeDelegateStxCommand
- Loading branch information
1 parent
d6047eb
commit 1166268
Showing
5 changed files
with
118 additions
and
0 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
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
96 changes: 96 additions & 0 deletions
96
contrib/core-contract-tests/tests/pox-4/pox_RevokeDelegateStxCommand.ts
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,96 @@ | ||
import { PoxCommand, Real, Stub, Wallet } from "./pox_CommandModel.ts"; | ||
import { poxAddressToTuple } from "@stacks/stacking"; | ||
import { expect } from "vitest"; | ||
import { Cl, someCV, tupleCV } from "@stacks/transactions"; | ||
|
||
/** | ||
* The `RevokeDelegateStxCommand` revokes the delegation for stacking within PoX-4. | ||
* | ||
* Constraints for running this command include: | ||
* - The `Stacker` has to currently be delegating. | ||
*/ | ||
export class RevokeDelegateStxCommand implements PoxCommand { | ||
readonly wallet: Wallet; | ||
|
||
/** | ||
* Constructs a RevokeDelegateStxCommand to revoke delegate uSTX for stacking. | ||
* | ||
* @param wallet - Represents the Stacker's wallet. | ||
*/ | ||
constructor(wallet: Wallet) { | ||
this.wallet = wallet; | ||
} | ||
|
||
check(model: Readonly<Stub>): boolean { | ||
// Constraints for running this command include: | ||
// - The Stacker has to currently be delegating. | ||
return ( | ||
model.stackingMinimum > 0 && | ||
model.wallets.get(this.wallet.stxAddress)!.hasDelegated === true | ||
); | ||
} | ||
|
||
run(model: Stub, real: Real): void { | ||
// Get the Operator's wallet | ||
const operatorWallet = model.wallets.get(this.wallet.delegatedTo)!; | ||
|
||
// Act | ||
const revokeDelegateStx = real.network.callPublicFn( | ||
"ST000000000000000000002AMW42H.pox-4", | ||
"revoke-delegate-stx", | ||
[], | ||
this.wallet.stxAddress, | ||
); | ||
|
||
// Assert | ||
expect(revokeDelegateStx.result).toBeOk( | ||
someCV( | ||
tupleCV({ | ||
"amount-ustx": Cl.uint(this.wallet.delegatedMaxAmount), | ||
"delegated-to": Cl.principal( | ||
operatorWallet.stxAddress || "", | ||
), | ||
"pox-addr": Cl.some( | ||
poxAddressToTuple(operatorWallet.btcAddress || ""), | ||
), | ||
"until-burn-ht": Cl.some(Cl.uint(this.wallet.delegatedUntilBurnHt)), | ||
}), | ||
), | ||
); | ||
|
||
// Get the Stacker's wallet from the model and update the two wallets involved with the new state. | ||
const wallet = model.wallets.get(this.wallet.stxAddress)!; | ||
// Update model so that we know this wallet is not delegating anymore. | ||
// This is important in order to prevent the test from revoking the delegation | ||
// multiple times with the same address. | ||
wallet.hasDelegated = false; | ||
wallet.delegatedTo = ""; | ||
wallet.delegatedUntilBurnHt = 0; | ||
wallet.delegatedMaxAmount = 0; | ||
|
||
// Remove the Stacker from the Pool Operator's pool members list | ||
let walletIndexInDelegatorsList = operatorWallet.hasPoolMembers.indexOf( | ||
wallet.stxAddress, | ||
); | ||
expect(walletIndexInDelegatorsList).toBeGreaterThan(-1); | ||
operatorWallet.hasPoolMembers.splice(walletIndexInDelegatorsList, 1); | ||
|
||
// Log to console for debugging purposes. This is not necessary for the | ||
// test to pass but it is useful for debugging and eyeballing the test. | ||
console.info( | ||
`✓ ${ | ||
this.wallet.label.padStart( | ||
8, | ||
" ", | ||
) | ||
} ${"revoke-delegate-stx".padStart(34, " ")}`, | ||
); | ||
} | ||
|
||
toString() { | ||
// fast-check will call toString() in case of errors, e.g. property failed. | ||
// It will then make a minimal counterexample, a process called 'shrinking' | ||
// https://github.com/dubzzz/fast-check/issues/2864#issuecomment-1098002642 | ||
return `${this.wallet.stxAddress} revoke-delegate-stx`; | ||
} | ||
} |