Skip to content

Commit 916b048

Browse files
committed
feat(sdk): add preaddnewkey func and update rawvote to remove pubkey params
1 parent 14d7457 commit 916b048

File tree

2 files changed

+150
-27
lines changed

2 files changed

+150
-27
lines changed

packages/sdk/src/libs/maci/maci.ts

Lines changed: 116 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,6 @@ export class MACI {
747747
signer,
748748
address,
749749
contractAddress,
750-
pubKey,
751750
payload,
752751
gasStation = false,
753752
granter,
@@ -756,7 +755,6 @@ export class MACI {
756755
signer: OfflineSigner;
757756
address?: string;
758757
contractAddress: string;
759-
pubKey: PubKey;
760758
payload: {
761759
msg: bigint[];
762760
encPubkeys: PubKey;
@@ -765,29 +763,7 @@ export class MACI {
765763
granter?: string;
766764
fee?: StdFee | 'auto' | number;
767765
}) {
768-
const stateIdx = await this.getStateIdxByPubKey({
769-
contractAddress,
770-
pubKey,
771-
});
772-
773-
if (stateIdx === -1) {
774-
throw new Error(
775-
'State index is not set, Please signup or addNewKey first'
776-
);
777-
}
778-
779766
try {
780-
const round = await this.indexer.getRoundWithFields(contractAddress, [
781-
'maciType',
782-
'voiceCreditAmount',
783-
]);
784-
785-
if (isErrorResponse(round)) {
786-
throw new Error(
787-
`Failed to get round info: ${round.error.type} ${round.error.message}`
788-
);
789-
}
790-
791767
if (!address) {
792768
address = (await signer.getAccounts())[0].address;
793769
}
@@ -1437,6 +1413,122 @@ export class MACI {
14371413
);
14381414
}
14391415

1416+
async rawPreAddNewKey({
1417+
signer,
1418+
contractAddress,
1419+
d,
1420+
proof,
1421+
nullifier,
1422+
newPubkey,
1423+
gasStation = false,
1424+
granter,
1425+
fee = 'auto',
1426+
}: {
1427+
signer: OfflineSigner;
1428+
contractAddress: string;
1429+
d: string[];
1430+
proof: Groth16ProofType;
1431+
nullifier: bigint;
1432+
newPubkey: PubKey;
1433+
gasStation?: boolean;
1434+
granter?: string;
1435+
fee?: number | StdFee | 'auto';
1436+
}) {
1437+
const client = await this.contract.amaciClient({
1438+
signer,
1439+
contractAddress,
1440+
});
1441+
1442+
if (gasStation === true && typeof fee !== 'object') {
1443+
// When gasStation is true and fee is not StdFee, we need to simulate first then add granter
1444+
const [{ address }] = await signer.getAccounts();
1445+
const contractClient = await this.contract.contractClient({ signer });
1446+
1447+
const msg = {
1448+
pre_add_new_key: {
1449+
d,
1450+
groth16_proof: proof,
1451+
nullifier: nullifier.toString(),
1452+
pubkey: {
1453+
x: newPubkey[0].toString(),
1454+
y: newPubkey[1].toString(),
1455+
},
1456+
},
1457+
};
1458+
1459+
const gasEstimation = await contractClient.simulate(
1460+
address,
1461+
[
1462+
{
1463+
typeUrl: '/cosmwasm.wasm.v1.MsgExecuteContract',
1464+
value: {
1465+
sender: address,
1466+
contract: contractAddress,
1467+
msg: new TextEncoder().encode(JSON.stringify(msg)),
1468+
},
1469+
},
1470+
],
1471+
''
1472+
);
1473+
const multiplier = typeof fee === 'number' ? fee : 1.8;
1474+
const gasPrice = GasPrice.fromString('10000000000peaka');
1475+
const calculatedFee = calculateFee(
1476+
Math.round(gasEstimation * multiplier),
1477+
gasPrice
1478+
);
1479+
const grantFee: StdFee = {
1480+
amount: calculatedFee.amount,
1481+
gas: calculatedFee.gas,
1482+
granter: granter || contractAddress,
1483+
};
1484+
1485+
return await client.preAddNewKey(
1486+
{
1487+
d,
1488+
groth16Proof: proof,
1489+
nullifier: nullifier.toString(),
1490+
pubkey: {
1491+
x: newPubkey[0].toString(),
1492+
y: newPubkey[1].toString(),
1493+
},
1494+
},
1495+
grantFee
1496+
);
1497+
} else if (gasStation === true && typeof fee === 'object') {
1498+
// When gasStation is true and fee is StdFee, add granter
1499+
const grantFee: StdFee = {
1500+
...fee,
1501+
granter: granter || contractAddress,
1502+
};
1503+
1504+
return await client.preAddNewKey(
1505+
{
1506+
d,
1507+
groth16Proof: proof,
1508+
nullifier: nullifier.toString(),
1509+
pubkey: {
1510+
x: newPubkey[0].toString(),
1511+
y: newPubkey[1].toString(),
1512+
},
1513+
},
1514+
grantFee
1515+
);
1516+
}
1517+
1518+
return await client.preAddNewKey(
1519+
{
1520+
d,
1521+
groth16Proof: proof,
1522+
nullifier: nullifier.toString(),
1523+
pubkey: {
1524+
x: newPubkey[0].toString(),
1525+
y: newPubkey[1].toString(),
1526+
},
1527+
},
1528+
fee
1529+
);
1530+
}
1531+
14401532
async claimAMaciRound({
14411533
signer,
14421534
contractAddress,

packages/sdk/src/maci.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,6 @@ export class MaciClient {
734734
signer,
735735
address,
736736
contractAddress,
737-
pubKey,
738737
payload,
739738
gasStation = false,
740739
granter,
@@ -743,7 +742,6 @@ export class MaciClient {
743742
signer?: OfflineSigner;
744743
address?: string;
745744
contractAddress: string;
746-
pubKey: PubKey;
747745
payload: {
748746
msg: bigint[];
749747
encPubkeys: PubKey;
@@ -756,7 +754,6 @@ export class MaciClient {
756754
signer: this.getSigner(signer),
757755
address,
758756
contractAddress,
759-
pubKey,
760757
payload,
761758
gasStation,
762759
granter,
@@ -836,4 +833,38 @@ export class MaciClient {
836833
fee,
837834
});
838835
}
836+
837+
async rawPreAddNewKey({
838+
signer,
839+
contractAddress,
840+
d,
841+
proof,
842+
nullifier,
843+
newPubkey,
844+
gasStation = false,
845+
granter,
846+
fee,
847+
}: {
848+
signer?: OfflineSigner;
849+
contractAddress: string;
850+
d: string[];
851+
proof: Groth16ProofType;
852+
nullifier: bigint;
853+
newPubkey: PubKey;
854+
gasStation?: boolean;
855+
granter?: string;
856+
fee?: StdFee | 'auto' | number;
857+
}) {
858+
return await this.maci.rawPreAddNewKey({
859+
signer: this.getSigner(signer),
860+
contractAddress,
861+
d,
862+
proof,
863+
nullifier,
864+
newPubkey,
865+
gasStation,
866+
granter,
867+
fee,
868+
});
869+
}
839870
}

0 commit comments

Comments
 (0)