Skip to content

Commit 0de4cd6

Browse files
authored
Merge pull request #152 from MeshJS/bug/drep-id
fix(wallet): enhance DRep script handling and error checks
2 parents 9e463c6 + 0f54000 commit 0de4cd6

File tree

6 files changed

+88
-17
lines changed

6 files changed

+88
-17
lines changed

prisma/schema.prisma

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,4 @@ model BalanceSnapshot {
107107
assetBalances Json
108108
isArchived Boolean
109109
snapshotDate DateTime @default(now())
110-
}
110+
}

src/components/pages/wallet/governance/ballot/ballot.tsx

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,32 @@ export default function BallotCard({
118118
setLoading(true);
119119
try {
120120
if (!multisigWallet) throw new Error("Multisig Wallet could not be built.");
121-
const dRepId = appWallet.dRepId;
121+
const dRepId = multisigWallet?.getKeysByRole(3) ? multisigWallet?.getDRepId() : appWallet?.dRepId;
122+
if (!dRepId) {
123+
setAlert("DRep not found");
124+
toast({
125+
title: "DRep not found",
126+
description: `Please register as a DRep and retry.`,
127+
duration: 10000,
128+
variant: "destructive",
129+
});
130+
return;
131+
}
132+
const scriptCbor = multisigWallet?.getKeysByRole(3) ? multisigWallet?.getScript().scriptCbor : appWallet.scriptCbor;
133+
const drepCbor = multisigWallet?.getKeysByRole(3) ? multisigWallet?.getDRepScript() : appWallet.scriptCbor;
134+
if (!scriptCbor) {
135+
setAlert("Script not found");
136+
return;
137+
}
138+
if (!drepCbor) {
139+
setAlert("DRep script not found");
140+
return;
141+
}
142+
const changeAddress = multisigWallet?.getKeysByRole(3) ? multisigWallet?.getScript().address : appWallet.address;
143+
if (!changeAddress) {
144+
setAlert("Change address not found");
145+
return;
146+
}
122147
const txBuilder = getTxBuilder(network);
123148

124149
// Ensure minimum ADA for fee and voting
@@ -134,7 +159,7 @@ export default function BallotCard({
134159
utxo.output.amount,
135160
utxo.output.address,
136161
)
137-
.txInScript(appWallet.scriptCbor);
162+
.txInScript(scriptCbor);
138163
}
139164

140165
// Submit a vote for each proposal in the ballot
@@ -162,9 +187,8 @@ export default function BallotCard({
162187
}
163188

164189
txBuilder
165-
.voteScript(appWallet.scriptCbor)
166-
.selectUtxosFrom(utxos)
167-
.changeAddress(appWallet.address);
190+
.voteScript(drepCbor)
191+
.changeAddress(changeAddress);
168192

169193
await newTransaction({
170194
txBuilder,

src/components/pages/wallet/governance/drep/registerDrep.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ export default function RegisterDRep() {
8888
if (!dRepId) {
8989
throw new Error("DRep not found");
9090
}
91+
const scriptCbor = multisigWallet?.getKeysByRole(3) ? multisigWallet?.getScript().scriptCbor : appWallet.scriptCbor;
92+
const drepCbor = multisigWallet?.getKeysByRole(3) ? multisigWallet?.getDRepScript() : appWallet.scriptCbor;
93+
if (!scriptCbor) {
94+
throw new Error("Script not found");
95+
}
96+
if (!drepCbor) {
97+
throw new Error("DRep script not found");
98+
}
9199
try {
92100
const { anchorUrl, anchorHash } = await createAnchor();
93101

@@ -106,15 +114,15 @@ export default function RegisterDRep() {
106114
utxo.output.amount,
107115
utxo.output.address,
108116
)
109-
.txInScript(multisigWallet.getScript().scriptCbor!);
117+
.txInScript(scriptCbor);
110118
}
111119

112120
txBuilder
113121
.drepRegistrationCertificate(dRepId, {
114122
anchorUrl: anchorUrl,
115123
anchorDataHash: anchorHash,
116124
})
117-
.certificateScript(multisigWallet.getDRepScript()!)
125+
.certificateScript(drepCbor)
118126
.changeAddress(multisigWallet.getScript().address);
119127

120128

src/components/pages/wallet/governance/drep/retire.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ export default function Retire({ appWallet }: { appWallet: Wallet }) {
3737

3838
const txBuilder = getTxBuilder(network);
3939
const dRepId = multisigWallet?.getKeysByRole(3) ? multisigWallet?.getDRepId() : appWallet?.dRepId;
40+
const scriptCbor = multisigWallet?.getKeysByRole(3) ? multisigWallet?.getScript().scriptCbor : appWallet.scriptCbor;
41+
const drepCbor = multisigWallet?.getKeysByRole(3) ? multisigWallet?.getDRepScript() : appWallet.scriptCbor;
42+
const changeAddress = multisigWallet?.getKeysByRole(3) ? multisigWallet?.getScript().address : appWallet.address;
43+
if (!changeAddress) {
44+
throw new Error("Change address not found");
45+
}
46+
if (!scriptCbor) {
47+
throw new Error("Script not found");
48+
}
49+
if (!drepCbor) {
50+
throw new Error("DRep script not found");
51+
}
4052
if (!dRepId) {
4153
throw new Error("DRep not found");
4254
}
@@ -50,10 +62,10 @@ export default function Retire({ appWallet }: { appWallet: Wallet }) {
5062
}
5163

5264
txBuilder
53-
.txInScript(multisigWallet.getScript().scriptCbor!)
54-
.changeAddress(multisigWallet.getScript().address)
65+
.txInScript(scriptCbor)
66+
.changeAddress(changeAddress)
5567
.drepDeregistrationCertificate(dRepId, "500000000")
56-
.certificateScript(multisigWallet.getDRepScript()!);
68+
.certificateScript(drepCbor);
5769

5870
await newTransaction({
5971
txBuilder,

src/components/pages/wallet/governance/drep/updateDrep.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@ export default function UpdateDRep() {
8888
if (!dRepId) {
8989
throw new Error("DRep not found");
9090
}
91+
const scriptCbor = multisigWallet?.getKeysByRole(3) ? multisigWallet?.getScript().scriptCbor : appWallet.scriptCbor;
92+
const drepCbor = multisigWallet?.getKeysByRole(3) ? multisigWallet?.getDRepScript() : appWallet.scriptCbor;
93+
if (!scriptCbor) {
94+
throw new Error("Script not found");
95+
}
96+
if (!drepCbor) {
97+
throw new Error("DRep script not found");
98+
}
99+
const changeAddress = multisigWallet?.getKeysByRole(3) ? multisigWallet?.getScript().address : appWallet.address;
100+
if (!changeAddress) {
101+
throw new Error("Change address not found");
102+
}
91103
try {
92104
const { anchorUrl, anchorHash } = await createAnchor();
93105

@@ -106,16 +118,16 @@ export default function UpdateDRep() {
106118
utxo.output.amount,
107119
utxo.output.address,
108120
)
109-
.txInScript(multisigWallet.getScript().scriptCbor!);
121+
.txInScript(scriptCbor);
110122
}
111123

112124
txBuilder
113125
.drepUpdateCertificate(dRepId, {
114126
anchorUrl: anchorUrl,
115127
anchorDataHash: anchorHash,
116128
})
117-
.certificateScript(multisigWallet.getDRepScript()!)
118-
.changeAddress(multisigWallet.getScript().address);
129+
.certificateScript(drepCbor)
130+
.changeAddress(changeAddress);
119131

120132
await newTransaction({
121133
txBuilder,

src/components/pages/wallet/governance/proposal/voteButtton.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,21 @@ export default function VoteButton({
103103
});
104104
return;
105105
}
106+
const scriptCbor = multisigWallet?.getKeysByRole(3) ? multisigWallet?.getScript().scriptCbor : appWallet.scriptCbor;
107+
const drepCbor = multisigWallet?.getKeysByRole(3) ? multisigWallet?.getDRepScript() : appWallet.scriptCbor;
108+
if (!scriptCbor) {
109+
setAlert("Script not found");
110+
return;
111+
}
112+
if (!drepCbor) {
113+
setAlert("DRep script not found");
114+
return;
115+
}
116+
const changeAddress = multisigWallet?.getKeysByRole(3) ? multisigWallet?.getScript().address : appWallet.address;
117+
if (!changeAddress) {
118+
setAlert("Change address not found");
119+
return;
120+
}
106121
const txBuilder = getTxBuilder(network);
107122

108123
const assetMap = new Map<Unit, Quantity>();
@@ -117,7 +132,7 @@ export default function VoteButton({
117132
utxo.output.amount,
118133
utxo.output.address,
119134
)
120-
.txInScript(appWallet.scriptCbor);
135+
.txInScript(scriptCbor);
121136
}
122137
txBuilder
123138
.vote(
@@ -133,8 +148,8 @@ export default function VoteButton({
133148
voteKind: voteKind,
134149
},
135150
)
136-
.voteScript(appWallet.scriptCbor)
137-
.changeAddress(appWallet.address);
151+
.voteScript(drepCbor)
152+
.changeAddress(changeAddress);
138153

139154
await newTransaction({
140155
txBuilder,

0 commit comments

Comments
 (0)