Skip to content

Commit 746f15e

Browse files
committed
feat(sdk): update deactivate/addnewkey gas params
1 parent b9f4484 commit 746f15e

File tree

2 files changed

+96
-5
lines changed

2 files changed

+96
-5
lines changed

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

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

packages/sdk/src/maci.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,7 @@ export class MaciClient {
778778
contractAddress,
779779
payload,
780780
gasStation = false,
781+
granter,
781782
fee,
782783
}: {
783784
signer?: OfflineSigner;
@@ -788,14 +789,16 @@ export class MaciClient {
788789
encPubkeys: PubKey;
789790
};
790791
gasStation?: boolean;
791-
fee?: StdFee;
792+
granter?: string;
793+
fee?: StdFee | 'auto' | number;
792794
}) {
793795
return await this.maci.rawDeactivate({
794796
signer: this.getSigner(signer),
795797
address,
796798
contractAddress,
797799
payload,
798800
gasStation,
801+
granter,
799802
fee,
800803
});
801804
}
@@ -807,6 +810,8 @@ export class MaciClient {
807810
proof,
808811
nullifier,
809812
newPubkey,
813+
gasStation = false,
814+
granter,
810815
fee,
811816
}: {
812817
signer?: OfflineSigner;
@@ -815,7 +820,9 @@ export class MaciClient {
815820
proof: Groth16ProofType;
816821
nullifier: bigint;
817822
newPubkey: PubKey;
818-
fee?: number | StdFee | 'auto';
823+
gasStation?: boolean;
824+
granter?: string;
825+
fee?: StdFee | 'auto' | number;
819826
}) {
820827
return await this.maci.rawAddNewKey({
821828
signer: this.getSigner(signer),
@@ -824,6 +831,8 @@ export class MaciClient {
824831
proof,
825832
nullifier,
826833
newPubkey,
834+
gasStation,
835+
granter,
827836
fee,
828837
});
829838
}

0 commit comments

Comments
 (0)