@dorafactory/maci-sdk 0.1.2 → 0.1.3-pre.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,8 @@ import { OfflineSigner } from '@cosmjs/proto-signing';
2
2
  import { ContractParams } from '../../types';
3
3
  import {
4
4
  createAMaciClientBy,
5
+ createApiMaciClientBy,
6
+ createApiSaasClientBy,
5
7
  createContractClientByWallet,
6
8
  createMaciClientBy,
7
9
  createOracleMaciClientBy,
@@ -27,9 +29,9 @@ export class Contract {
27
29
  public rpcEndpoint: string;
28
30
  public registryAddress: string;
29
31
  public saasAddress: string;
32
+ public apiSaasAddress: string;
30
33
  public maciCodeId: number;
31
34
  public oracleCodeId: number;
32
- public saasOracleCodeId: number;
33
35
  public feegrantOperator: string;
34
36
  public whitelistBackendPubkey: string;
35
37
 
@@ -38,9 +40,9 @@ export class Contract {
38
40
  rpcEndpoint,
39
41
  registryAddress,
40
42
  saasAddress,
43
+ apiSaasAddress,
41
44
  maciCodeId,
42
45
  oracleCodeId,
43
- saasOracleCodeId,
44
46
  feegrantOperator,
45
47
  whitelistBackendPubkey,
46
48
  }: ContractParams) {
@@ -48,9 +50,9 @@ export class Contract {
48
50
  this.rpcEndpoint = rpcEndpoint;
49
51
  this.registryAddress = registryAddress;
50
52
  this.saasAddress = saasAddress;
53
+ this.apiSaasAddress = apiSaasAddress;
51
54
  this.maciCodeId = maciCodeId;
52
55
  this.oracleCodeId = oracleCodeId;
53
- this.saasOracleCodeId = saasOracleCodeId;
54
56
  this.feegrantOperator = feegrantOperator;
55
57
  this.whitelistBackendPubkey = whitelistBackendPubkey;
56
58
  }
@@ -1005,6 +1007,20 @@ export class Contract {
1005
1007
  });
1006
1008
  }
1007
1009
 
1010
+ async apiMaciClient({
1011
+ signer,
1012
+ contractAddress,
1013
+ }: {
1014
+ signer: OfflineSigner;
1015
+ contractAddress: string;
1016
+ }) {
1017
+ return createApiMaciClientBy({
1018
+ rpcEndpoint: this.rpcEndpoint,
1019
+ wallet: signer,
1020
+ contractAddress,
1021
+ });
1022
+ }
1023
+
1008
1024
  async saasClient({
1009
1025
  signer,
1010
1026
  contractAddress,
@@ -1019,7 +1035,465 @@ export class Contract {
1019
1035
  });
1020
1036
  }
1021
1037
 
1038
+ async apiSaasClient({
1039
+ signer,
1040
+ contractAddress,
1041
+ }: {
1042
+ signer: OfflineSigner;
1043
+ contractAddress: string;
1044
+ }) {
1045
+ return createApiSaasClientBy({
1046
+ rpcEndpoint: this.rpcEndpoint,
1047
+ wallet: signer,
1048
+ contractAddress,
1049
+ });
1050
+ }
1051
+
1022
1052
  async contractClient({ signer }: { signer: OfflineSigner }) {
1023
1053
  return createContractClientByWallet(this.rpcEndpoint, signer);
1024
1054
  }
1055
+
1056
+ async createApiSaasMaciRound({
1057
+ signer,
1058
+ operatorPubkey,
1059
+ startVoting,
1060
+ endVoting,
1061
+ title,
1062
+ description,
1063
+ link,
1064
+ maxVoter,
1065
+ voteOptionMap,
1066
+ whitelistBackendPubkey,
1067
+ gasStation = false,
1068
+ fee = 1.8,
1069
+ }: CreateSaasOracleMaciRoundParams & { signer: OfflineSigner }) {
1070
+ const startTime = (startVoting.getTime() * 1_000_000).toString();
1071
+ const endTime = (endVoting.getTime() * 1_000_000).toString();
1072
+
1073
+ const client = await createApiSaasClientBy({
1074
+ rpcEndpoint: this.rpcEndpoint,
1075
+ wallet: signer,
1076
+ contractAddress: this.saasAddress,
1077
+ });
1078
+ const [operatorPubkeyX, operatorPubkeyY] = unpackPubKey(
1079
+ BigInt(operatorPubkey)
1080
+ );
1081
+
1082
+ const roundParams = {
1083
+ certificationSystem: '0',
1084
+ circuitType: '0',
1085
+ coordinator: {
1086
+ x: operatorPubkeyX.toString(),
1087
+ y: operatorPubkeyY.toString(),
1088
+ },
1089
+ maxVoters: maxVoter,
1090
+ roundInfo: {
1091
+ title,
1092
+ description: description || '',
1093
+ link: link || '',
1094
+ },
1095
+ startTime,
1096
+ endTime,
1097
+ voteOptionMap,
1098
+ whitelistBackendPubkey:
1099
+ whitelistBackendPubkey || this.whitelistBackendPubkey,
1100
+ };
1101
+
1102
+ let createResponse;
1103
+
1104
+ if (gasStation && typeof fee !== 'object') {
1105
+ // When gasStation is true and fee is not StdFee, we need to simulate first then add granter
1106
+ const [{ address }] = await signer.getAccounts();
1107
+ const contractClient = await this.contractClient({ signer });
1108
+ const msg = {
1109
+ create_oracle_maci_round: {
1110
+ certification_system: '0',
1111
+ circuit_type: '0',
1112
+ coordinator: roundParams.coordinator,
1113
+ max_voters: roundParams.maxVoters.toString(),
1114
+ round_info: roundParams.roundInfo,
1115
+ start_time: roundParams.startTime,
1116
+ end_time: roundParams.endTime,
1117
+ vote_option_map: roundParams.voteOptionMap,
1118
+ whitelist_backend_pubkey: roundParams.whitelistBackendPubkey,
1119
+ },
1120
+ };
1121
+ const gasEstimation = await contractClient.simulate(
1122
+ address,
1123
+ [
1124
+ {
1125
+ typeUrl: '/cosmwasm.wasm.v1.MsgExecuteContract',
1126
+ value: {
1127
+ sender: address,
1128
+ contract: this.saasAddress,
1129
+ msg: new TextEncoder().encode(JSON.stringify(msg)),
1130
+ },
1131
+ },
1132
+ ],
1133
+ ''
1134
+ );
1135
+ const multiplier = typeof fee === 'number' ? fee : 1.8;
1136
+ const gasPrice = GasPrice.fromString('10000000000peaka');
1137
+ const calculatedFee = calculateFee(
1138
+ Math.round(gasEstimation * multiplier),
1139
+ gasPrice
1140
+ );
1141
+ const grantFee: StdFee = {
1142
+ amount: calculatedFee.amount,
1143
+ gas: calculatedFee.gas,
1144
+ granter: this.saasAddress,
1145
+ };
1146
+ createResponse = await client.createApiMaciRound(roundParams, grantFee);
1147
+ } else if (gasStation && typeof fee === 'object') {
1148
+ // When gasStation is true and fee is StdFee, add granter
1149
+ const grantFee: StdFee = {
1150
+ ...fee,
1151
+ granter: this.saasAddress,
1152
+ };
1153
+ createResponse = await client.createApiMaciRound(roundParams, grantFee);
1154
+ } else {
1155
+ createResponse = await client.createApiMaciRound(roundParams, fee);
1156
+ }
1157
+
1158
+ let contractAddress = '';
1159
+ createResponse.events.map((event) => {
1160
+ if (event.type === 'wasm') {
1161
+ let actionEvent = event.attributes.find(
1162
+ (attr) => attr.key === 'action'
1163
+ )!;
1164
+ if (actionEvent.value === 'created_api_maci_round') {
1165
+ contractAddress = event.attributes
1166
+ .find((attr) => attr.key === 'round_addr')!
1167
+ .value.toString();
1168
+ }
1169
+ }
1170
+ });
1171
+ return {
1172
+ ...createResponse,
1173
+ contractAddress,
1174
+ };
1175
+ }
1176
+
1177
+ async setApiSaasMaciRoundInfo({
1178
+ signer,
1179
+ contractAddress,
1180
+ title,
1181
+ description,
1182
+ link,
1183
+ gasStation = false,
1184
+ fee = 1.8,
1185
+ }: {
1186
+ signer: OfflineSigner;
1187
+ contractAddress: string;
1188
+ title: string;
1189
+ description: string;
1190
+ link: string;
1191
+ gasStation?: boolean;
1192
+ fee?: StdFee | 'auto' | number;
1193
+ }) {
1194
+ const client = await createApiSaasClientBy({
1195
+ rpcEndpoint: this.rpcEndpoint,
1196
+ wallet: signer,
1197
+ contractAddress: this.saasAddress,
1198
+ });
1199
+
1200
+ const roundInfo = {
1201
+ title,
1202
+ description,
1203
+ link,
1204
+ };
1205
+
1206
+ if (gasStation && typeof fee !== 'object') {
1207
+ // When gasStation is true and fee is not StdFee, we need to simulate first then add granter
1208
+ const [{ address }] = await signer.getAccounts();
1209
+ const contractClient = await this.contractClient({ signer });
1210
+ const msg = {
1211
+ set_round_info: {
1212
+ contract_addr: contractAddress,
1213
+ round_info: roundInfo,
1214
+ },
1215
+ };
1216
+ const gasEstimation = await contractClient.simulate(
1217
+ address,
1218
+ [
1219
+ {
1220
+ typeUrl: '/cosmwasm.wasm.v1.MsgExecuteContract',
1221
+ value: {
1222
+ sender: address,
1223
+ contract: this.saasAddress,
1224
+ msg: new TextEncoder().encode(JSON.stringify(msg)),
1225
+ },
1226
+ },
1227
+ ],
1228
+ ''
1229
+ );
1230
+ const multiplier = typeof fee === 'number' ? fee : 1.8;
1231
+ const gasPrice = GasPrice.fromString('10000000000peaka');
1232
+ const calculatedFee = calculateFee(
1233
+ Math.round(gasEstimation * multiplier),
1234
+ gasPrice
1235
+ );
1236
+ const grantFee: StdFee = {
1237
+ amount: calculatedFee.amount,
1238
+ gas: calculatedFee.gas,
1239
+ granter: this.saasAddress,
1240
+ };
1241
+ return client.setRoundInfo(
1242
+ {
1243
+ contractAddr: contractAddress,
1244
+ roundInfo,
1245
+ },
1246
+ grantFee
1247
+ );
1248
+ } else if (gasStation && typeof fee === 'object') {
1249
+ // When gasStation is true and fee is StdFee, add granter
1250
+ const grantFee: StdFee = {
1251
+ ...fee,
1252
+ granter: this.saasAddress,
1253
+ };
1254
+ return client.setRoundInfo(
1255
+ {
1256
+ contractAddr: contractAddress,
1257
+ roundInfo,
1258
+ },
1259
+ grantFee
1260
+ );
1261
+ }
1262
+
1263
+ return client.setRoundInfo(
1264
+ {
1265
+ contractAddr: contractAddress,
1266
+ roundInfo,
1267
+ },
1268
+ fee
1269
+ );
1270
+ }
1271
+
1272
+ async setApiSaasMaciRoundVoteOptions({
1273
+ signer,
1274
+ contractAddress,
1275
+ voteOptionMap,
1276
+ gasStation = false,
1277
+ fee = 1.8,
1278
+ }: {
1279
+ signer: OfflineSigner;
1280
+ contractAddress: string;
1281
+ voteOptionMap: string[];
1282
+ gasStation?: boolean;
1283
+ fee?: StdFee | 'auto' | number;
1284
+ }) {
1285
+ const client = await createApiSaasClientBy({
1286
+ rpcEndpoint: this.rpcEndpoint,
1287
+ wallet: signer,
1288
+ contractAddress: this.saasAddress,
1289
+ });
1290
+
1291
+ if (gasStation && typeof fee !== 'object') {
1292
+ // When gasStation is true and fee is not StdFee, we need to simulate first then add granter
1293
+ const [{ address }] = await signer.getAccounts();
1294
+ const contractClient = await this.contractClient({ signer });
1295
+ const msg = {
1296
+ set_vote_options_map: {
1297
+ contract_addr: contractAddress,
1298
+ vote_option_map: voteOptionMap,
1299
+ },
1300
+ };
1301
+ const gasEstimation = await contractClient.simulate(
1302
+ address,
1303
+ [
1304
+ {
1305
+ typeUrl: '/cosmwasm.wasm.v1.MsgExecuteContract',
1306
+ value: {
1307
+ sender: address,
1308
+ contract: this.saasAddress,
1309
+ msg: new TextEncoder().encode(JSON.stringify(msg)),
1310
+ },
1311
+ },
1312
+ ],
1313
+ ''
1314
+ );
1315
+ const multiplier = typeof fee === 'number' ? fee : 1.8;
1316
+ const gasPrice = GasPrice.fromString('10000000000peaka');
1317
+ const calculatedFee = calculateFee(
1318
+ Math.round(gasEstimation * multiplier),
1319
+ gasPrice
1320
+ );
1321
+ const grantFee: StdFee = {
1322
+ amount: calculatedFee.amount,
1323
+ gas: calculatedFee.gas,
1324
+ granter: this.saasAddress,
1325
+ };
1326
+ return client.setVoteOptionsMap(
1327
+ {
1328
+ contractAddr: contractAddress,
1329
+ voteOptionMap,
1330
+ },
1331
+ grantFee
1332
+ );
1333
+ } else if (gasStation && typeof fee === 'object') {
1334
+ // When gasStation is true and fee is StdFee, add granter
1335
+ const grantFee: StdFee = {
1336
+ ...fee,
1337
+ granter: this.saasAddress,
1338
+ };
1339
+ return client.setVoteOptionsMap(
1340
+ {
1341
+ contractAddr: contractAddress,
1342
+ voteOptionMap,
1343
+ },
1344
+ grantFee
1345
+ );
1346
+ }
1347
+
1348
+ return client.setVoteOptionsMap(
1349
+ {
1350
+ contractAddr: contractAddress,
1351
+ voteOptionMap,
1352
+ },
1353
+ fee
1354
+ );
1355
+ }
1356
+
1357
+ async addApiSaasOperator({
1358
+ signer,
1359
+ operator,
1360
+ gasStation = false,
1361
+ fee = 1.8,
1362
+ }: {
1363
+ signer: OfflineSigner;
1364
+ operator: string;
1365
+ gasStation?: boolean;
1366
+ fee?: StdFee | 'auto' | number;
1367
+ }) {
1368
+ const client = await createApiSaasClientBy({
1369
+ rpcEndpoint: this.rpcEndpoint,
1370
+ wallet: signer,
1371
+ contractAddress: this.saasAddress,
1372
+ });
1373
+
1374
+ if (gasStation && typeof fee !== 'object') {
1375
+ // When gasStation is true and fee is not StdFee, we need to simulate first then add granter
1376
+ const [{ address }] = await signer.getAccounts();
1377
+ const contractClient = await this.contractClient({ signer });
1378
+ const msg = {
1379
+ add_operator: {
1380
+ operator,
1381
+ },
1382
+ };
1383
+ const gasEstimation = await contractClient.simulate(
1384
+ address,
1385
+ [
1386
+ {
1387
+ typeUrl: '/cosmwasm.wasm.v1.MsgExecuteContract',
1388
+ value: {
1389
+ sender: address,
1390
+ contract: this.saasAddress,
1391
+ msg: new TextEncoder().encode(JSON.stringify(msg)),
1392
+ },
1393
+ },
1394
+ ],
1395
+ ''
1396
+ );
1397
+ const multiplier = typeof fee === 'number' ? fee : 1.8;
1398
+ const gasPrice = GasPrice.fromString('10000000000peaka');
1399
+ const calculatedFee = calculateFee(
1400
+ Math.round(gasEstimation * multiplier),
1401
+ gasPrice
1402
+ );
1403
+ const grantFee: StdFee = {
1404
+ amount: calculatedFee.amount,
1405
+ gas: calculatedFee.gas,
1406
+ granter: this.saasAddress,
1407
+ };
1408
+ return client.addOperator({ operator }, grantFee);
1409
+ } else if (gasStation && typeof fee === 'object') {
1410
+ // When gasStation is true and fee is StdFee, add granter
1411
+ const grantFee: StdFee = {
1412
+ ...fee,
1413
+ granter: this.saasAddress,
1414
+ };
1415
+ return client.addOperator({ operator }, grantFee);
1416
+ }
1417
+
1418
+ return client.addOperator({ operator }, fee);
1419
+ }
1420
+
1421
+ async removeApiSaasOperator({
1422
+ signer,
1423
+ operator,
1424
+ gasStation = false,
1425
+ fee = 1.8,
1426
+ }: {
1427
+ signer: OfflineSigner;
1428
+ operator: string;
1429
+ gasStation?: boolean;
1430
+ fee?: StdFee | 'auto' | number;
1431
+ }) {
1432
+ const client = await createApiSaasClientBy({
1433
+ rpcEndpoint: this.rpcEndpoint,
1434
+ wallet: signer,
1435
+ contractAddress: this.saasAddress,
1436
+ });
1437
+
1438
+ if (gasStation && typeof fee !== 'object') {
1439
+ // When gasStation is true and fee is not StdFee, we need to simulate first then add granter
1440
+ const [{ address }] = await signer.getAccounts();
1441
+ const contractClient = await this.contractClient({ signer });
1442
+ const msg = {
1443
+ remove_operator: {
1444
+ operator,
1445
+ },
1446
+ };
1447
+ const gasEstimation = await contractClient.simulate(
1448
+ address,
1449
+ [
1450
+ {
1451
+ typeUrl: '/cosmwasm.wasm.v1.MsgExecuteContract',
1452
+ value: {
1453
+ sender: address,
1454
+ contract: this.saasAddress,
1455
+ msg: new TextEncoder().encode(JSON.stringify(msg)),
1456
+ },
1457
+ },
1458
+ ],
1459
+ ''
1460
+ );
1461
+ const multiplier = typeof fee === 'number' ? fee : 1.8;
1462
+ const gasPrice = GasPrice.fromString('10000000000peaka');
1463
+ const calculatedFee = calculateFee(
1464
+ Math.round(gasEstimation * multiplier),
1465
+ gasPrice
1466
+ );
1467
+ const grantFee: StdFee = {
1468
+ amount: calculatedFee.amount,
1469
+ gas: calculatedFee.gas,
1470
+ granter: this.saasAddress,
1471
+ };
1472
+ return client.removeOperator({ operator }, grantFee);
1473
+ } else if (gasStation && typeof fee === 'object') {
1474
+ // When gasStation is true and fee is StdFee, add granter
1475
+ const grantFee: StdFee = {
1476
+ ...fee,
1477
+ granter: this.saasAddress,
1478
+ };
1479
+ return client.removeOperator({ operator }, grantFee);
1480
+ }
1481
+
1482
+ return client.removeOperator({ operator }, fee);
1483
+ }
1484
+
1485
+ async isApiSaasOperator({
1486
+ signer,
1487
+ operator,
1488
+ }: {
1489
+ signer: OfflineSigner;
1490
+ operator: string;
1491
+ }) {
1492
+ const client = await createApiSaasClientBy({
1493
+ rpcEndpoint: this.rpcEndpoint,
1494
+ wallet: signer,
1495
+ contractAddress: this.saasAddress,
1496
+ });
1497
+ return client.isOperator({ address: operator });
1498
+ }
1025
1499
  }