-
Notifications
You must be signed in to change notification settings - Fork 647
/
pox_RevokeDelegateStxCommand.ts
96 lines (86 loc) · 3.19 KB
/
pox_RevokeDelegateStxCommand.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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`;
}
}