@@ -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,
0 commit comments