@@ -1142,7 +1142,8 @@ export class MACI {
11421142 address,
11431143 contractAddress,
11441144 payload,
1145- gasStation,
1145+ gasStation = false ,
1146+ granter,
11461147 fee = 1.8 ,
11471148 } : {
11481149 signer : OfflineSigner ;
@@ -1153,6 +1154,7 @@ export class MACI {
11531154 encPubkeys : PubKey ;
11541155 } ;
11551156 gasStation ?: boolean ;
1157+ granter ?: string ;
11561158 fee ?: StdFee | 'auto' | number ;
11571159 } ) {
11581160 try {
@@ -1211,7 +1213,7 @@ export class MACI {
12111213 const grantFee : StdFee = {
12121214 amount : calculatedFee . amount ,
12131215 gas : calculatedFee . gas ,
1214- granter : contractAddress ,
1216+ granter : granter || contractAddress ,
12151217 } ;
12161218 return client . execute (
12171219 address ,
@@ -1223,7 +1225,7 @@ export class MACI {
12231225 // When gasStation is true and fee is StdFee, add granter
12241226 const grantFee : StdFee = {
12251227 ...fee ,
1226- granter : contractAddress ,
1228+ granter : granter || contractAddress ,
12271229 } ;
12281230 return client . execute (
12291231 address ,
@@ -1326,6 +1328,8 @@ export class MACI {
13261328 proof,
13271329 nullifier,
13281330 newPubkey,
1331+ gasStation = false ,
1332+ granter,
13291333 fee = 'auto' ,
13301334 } : {
13311335 signer : OfflineSigner ;
@@ -1334,13 +1338,91 @@ export class MACI {
13341338 proof : Groth16ProofType ;
13351339 nullifier : bigint ;
13361340 newPubkey : PubKey ;
1341+ gasStation ?: boolean ;
1342+ granter ?: string ;
13371343 fee ?: number | StdFee | 'auto' ;
13381344 } ) {
13391345 const client = await this . contract . amaciClient ( {
13401346 signer,
13411347 contractAddress,
13421348 } ) ;
13431349
1350+ if ( gasStation === true && typeof fee !== 'object' ) {
1351+ // When gasStation is true and fee is not StdFee, we need to simulate first then add granter
1352+ const [ { address } ] = await signer . getAccounts ( ) ;
1353+ const contractClient = await this . contract . contractClient ( { signer } ) ;
1354+
1355+ const msg = {
1356+ add_new_key : {
1357+ d,
1358+ groth16_proof : proof ,
1359+ nullifier : nullifier . toString ( ) ,
1360+ pubkey : {
1361+ x : newPubkey [ 0 ] . toString ( ) ,
1362+ y : newPubkey [ 1 ] . toString ( ) ,
1363+ } ,
1364+ } ,
1365+ } ;
1366+
1367+ const gasEstimation = await contractClient . simulate (
1368+ address ,
1369+ [
1370+ {
1371+ typeUrl : '/cosmwasm.wasm.v1.MsgExecuteContract' ,
1372+ value : {
1373+ sender : address ,
1374+ contract : contractAddress ,
1375+ msg : new TextEncoder ( ) . encode ( JSON . stringify ( msg ) ) ,
1376+ } ,
1377+ } ,
1378+ ] ,
1379+ ''
1380+ ) ;
1381+ const multiplier = typeof fee === 'number' ? fee : 1.8 ;
1382+ const gasPrice = GasPrice . fromString ( '10000000000peaka' ) ;
1383+ const calculatedFee = calculateFee (
1384+ Math . round ( gasEstimation * multiplier ) ,
1385+ gasPrice
1386+ ) ;
1387+ const grantFee : StdFee = {
1388+ amount : calculatedFee . amount ,
1389+ gas : calculatedFee . gas ,
1390+ granter : granter || contractAddress ,
1391+ } ;
1392+
1393+ return await client . addNewKey (
1394+ {
1395+ d,
1396+ groth16Proof : proof ,
1397+ nullifier : nullifier . toString ( ) ,
1398+ pubkey : {
1399+ x : newPubkey [ 0 ] . toString ( ) ,
1400+ y : newPubkey [ 1 ] . toString ( ) ,
1401+ } ,
1402+ } ,
1403+ grantFee
1404+ ) ;
1405+ } else if ( gasStation === true && typeof fee === 'object' ) {
1406+ // When gasStation is true and fee is StdFee, add granter
1407+ const grantFee : StdFee = {
1408+ ...fee ,
1409+ granter : granter || contractAddress ,
1410+ } ;
1411+
1412+ return await client . addNewKey (
1413+ {
1414+ d,
1415+ groth16Proof : proof ,
1416+ nullifier : nullifier . toString ( ) ,
1417+ pubkey : {
1418+ x : newPubkey [ 0 ] . toString ( ) ,
1419+ y : newPubkey [ 1 ] . toString ( ) ,
1420+ } ,
1421+ } ,
1422+ grantFee
1423+ ) ;
1424+ }
1425+
13441426 return await client . addNewKey (
13451427 {
13461428 d,
0 commit comments