Skip to content

Commit 0b99292

Browse files
authored
Merge pull request #36 from safient/feature/cli-35/provider-integration
Provider Support and refactor for DDay based claim
2 parents bd60640 + beb7fd6 commit 0b99292

File tree

6 files changed

+73
-23
lines changed

6 files changed

+73
-23
lines changed

package-lock.json

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@safient/contracts",
3-
"version": "0.1.14-alpha",
3+
"version": "0.1.15-alpha",
44
"description": "JavaScript SDK to manage and interact with the safe claims on Safient protocol.",
55
"keywords": [
66
"Web3",

src/contracts/Arbitrator.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { formatEther } from '@ethersproject/units';
55
import { Logger } from '@ethersproject/logger';
66
import networks from '../utils/networks.json';
77
import data from '../abis/AutoAppealableArbitrator.json';
8+
import { JsonRpcProvider } from '@ethersproject/providers';
89

910
/**
1011
* This class implements an interface to interact with the arbitrator contract

src/contracts/SafientMain.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import {
88
Safe,
99
Signer,
1010
} from '../types/Types';
11-
import { TransactionResponse } from '@ethersproject/providers';
11+
import { JsonRpcProvider, TransactionResponse } from '@ethersproject/providers';
1212
import { Contract } from '@ethersproject/contracts';
1313
import { BigNumber } from '@ethersproject/bignumber';
1414
import { formatEther, parseEther } from '@ethersproject/units';
1515
import { Logger } from '@ethersproject/logger';
1616
import { Bytes } from 'ethers';
1717
import networks from '../utils/networks.json';
18+
import {getNetworkUrl} from "../utils/networks"
1819
import data from '../abis/SafientMain.json';
1920

2021
/**
@@ -40,6 +41,9 @@ export class SafientMain {
4041
/** @ignore */
4142
private contract: Contract;
4243

44+
/**@ignore */
45+
private provider: JsonRpcProvider
46+
4347
/**
4448
* Arbitrator Constructor
4549
* @param signer Signer object
@@ -52,6 +56,9 @@ export class SafientMain {
5256

5357
const network = Object.values(networks).find((network) => chainId === network.chainId);
5458

59+
const networkUrl = getNetworkUrl(chainId)
60+
this.provider = new JsonRpcProvider(networkUrl)
61+
5562
network !== undefined && network.addresses.SafientMain !== ''
5663
? (this.safientMainAddress = network.addresses.SafientMain)
5764
: this.logger.throwError(`SafientMain contract not deployed on network with chain id: ${chainId}`);
@@ -80,6 +87,11 @@ export class SafientMain {
8087
value: string
8188
): Promise<TransactionResponse> => {
8289
try {
90+
if(claimType === ClaimType.DDayBased){
91+
const latestBlockNumber = await this.provider.getBlockNumber();
92+
const latestBlock = await this.provider.getBlock(latestBlockNumber);
93+
dDay = latestBlock.timestamp + dDay
94+
}
8395
this.tx = await this.contract.createSafe(
8496
beneficiaryAddress,
8597
safeId,
@@ -118,6 +130,13 @@ export class SafientMain {
118130
value: string
119131
): Promise<TransactionResponse> => {
120132
try {
133+
134+
if(claimType === ClaimType.DDayBased){
135+
const latestBlockNumber = await this.provider.getBlockNumber();
136+
const latestBlock = await this.provider.getBlock(latestBlockNumber);
137+
dDay = latestBlock.timestamp + dDay
138+
}
139+
121140
this.tx = await this.contract.syncSafe(
122141
creatorAddress,
123142
safeId,

src/utils/networks.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const dotenv = require('dotenv');
2+
dotenv.config()
3+
4+
5+
const networks = {
6+
localhost: {
7+
chainId: 31337,
8+
url: 'http://localhost:8545',
9+
},
10+
mainnet: {
11+
chainId: 1,
12+
url: `https://mainnet.infura.io/v3/${process.env.INFURA_API_KEY}`,
13+
},
14+
kovan: {
15+
chainId: 42,
16+
url: `https://kovan.infura.io/v3/${process.env.INFURA_API_KEY}`,
17+
18+
},
19+
polygontestnet: {
20+
chainId: 80001,
21+
url: 'https://matic-mumbai.chainstacklabs.com',
22+
},
23+
polygon : {
24+
chainId: 137,
25+
url: 'https://matic-mumbai.chainstacklabs.com',
26+
27+
},
28+
}
29+
30+
export const getNetworkUrl = (chainId) => {
31+
const network = Object.values(networks).find(network => chainId === network.chainId);
32+
return network.url
33+
}

test-sdk/claims.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,12 @@ describe('safientMain', async () => {
123123
const safientMain1 = new SafientMain(beneficiarySigner, chainId);
124124

125125
const beforeTotalNumberOfSafes = await safientMain1.getTotalNumberOfSafes();
126-
127126
// SUCCESS : create a safe(for claimType - SignalBased & signal - won't signal)
128127
await safientMain1.syncSafe(
129128
safeCreatorAddress, // 2nd account
130129
safeId[1],
131130
Types.ClaimType.SignalBased,
132-
6, // 6 seconds because opting SignalBased
131+
10, // 6 seconds because opting SignalBased
133132
0,
134133
'', // no metaevidence because SignalBased
135134
'' // no safe maintenence fee because SignalBased
@@ -140,7 +139,7 @@ describe('safientMain', async () => {
140139
const safe = await safientMain1.getSafeBySafeId(safeId[1]);
141140
expect(safe.createdBy).to.equal(safeCreatorAddress);
142141
expect(safe.beneficiary).to.equal(beneficiaryAddress);
143-
expect(Number(safe.signalingPeriod)).to.equal(6); // 6 seconds
142+
expect(Number(safe.signalingPeriod)).to.equal(10); // 6 seconds
144143
expect(Number(safe.endSignalTime)).to.equal(0);
145144
expect(Number(safe.latestSignalTime)).to.equal(0);
146145
expect(Number(safe.claimType)).to.equal(0); // SignalBased
@@ -151,7 +150,7 @@ describe('safientMain', async () => {
151150
beneficiaryAddress, // 2nd account
152151
safeId[2],
153152
Types.ClaimType.SignalBased,
154-
6,
153+
10,
155154
0,
156155
'',
157156
''
@@ -175,7 +174,7 @@ describe('safientMain', async () => {
175174
// SUCCESS : create a claim (ArbitrationBased) on safeId1
176175
const tx = await safientMain.createClaim(safeId[0], metaevidenceOrEvidenceURI);
177176
const txReceipt = await tx.wait();
178-
claimIdOfSafeId0 = txReceipt.events[2].args[2];
177+
claimIdOfSafeId0 = txReceipt.events[2].args[1];
179178

180179
expect(await safientMain.getTotalNumberOfClaims()).to.equal(beforeTotalNumberOfClaims + 1);
181180

@@ -192,7 +191,7 @@ describe('safientMain', async () => {
192191
// SUCCESS : create claim on safeId2
193192
tx = await safientMain.createClaim(safeId[1], '');
194193
txReceipt = await tx.wait();
195-
claimIdOfSafeId1 = txReceipt.events[0].args[2];
194+
claimIdOfSafeId1 = parseInt(txReceipt.events[0].args[1]._hex)
196195

197196
const safeWithSafeId1 = await safientMain.getSafeBySafeId(safeId[1]);
198197
expect(safeWithSafeId1.claimsCount).to.equal(1);
@@ -204,8 +203,7 @@ describe('safientMain', async () => {
204203
// SUCCESS : create claim on safeId3
205204
tx = await safientMain.createClaim(safeId[2], '');
206205
txReceipt = await tx.wait();
207-
claimIdOfSafeId2 = txReceipt.events[0].args[2];
208-
206+
claimIdOfSafeId2 = parseInt(txReceipt.events[0].args[1]._hex)
209207
const safeWithSafeId2 = await safientMain.getSafeBySafeId(safeId[2]);
210208
expect(safeWithSafeId2.claimsCount).to.equal(1);
211209

@@ -227,7 +225,7 @@ describe('safientMain', async () => {
227225
const mineNewBlock = new Promise((resolve, reject) => {
228226
setTimeout(() => {
229227
resolve(provider.send('evm_mine'));
230-
}, 7000);
228+
}, 11000);
231229
});
232230
const result = await mineNewBlock;
233231

@@ -328,17 +326,14 @@ describe('safientMain', async () => {
328326
const safientMainBeneficiary = new SafientMain(beneficiarySigner, chainId);
329327
const safientMainAccountX = new SafientMain(accountXSigner, chainId);
330328

331-
const latestBlockNumber = await provider.getBlockNumber();
332-
const latestBlock = await provider.getBlock(latestBlockNumber);
333-
const now = latestBlock.timestamp;
334329

335330
// SUCCESS : create another safe with safeId4 (for claimType - DDayBased) with DDay set to 6 seconds
336-
await safientMainCreator.createSafe(beneficiaryAddress, safeId[3], Types.ClaimType.DDayBased, 0, now + 6, '', '');
331+
await safientMainCreator.createSafe(beneficiaryAddress, safeId[3], Types.ClaimType.DDayBased, 0, 6, '', '');
337332

338333
// create a claim - before D-Day (claim should fail)
339334
const tx1 = await safientMainBeneficiary.createClaim(safeId[3], '');
340335
const txReceipt1 = await tx1.wait();
341-
const claimId1 = txReceipt1.events[0].args[2];
336+
const claimId1 = txReceipt1.events[0].args[1];
342337
const claimID1 = parseInt(claimId1._hex);
343338

344339
// check claim status (DDayBased)
@@ -356,7 +351,7 @@ describe('safientMain', async () => {
356351
// create a claim - before D-Day (claim should pass)
357352
const tx2 = await safientMainBeneficiary.createClaim(safeId[3], '');
358353
const txReceipt2 = await tx2.wait();
359-
const claimId2 = txReceipt2.events[0].args[2];
354+
const claimId2 = txReceipt2.events[0].args[1];
360355
const claimID2 = parseInt(claimId2._hex);
361356

362357
// check claim status (DDayBased)
@@ -381,7 +376,7 @@ describe('safientMain', async () => {
381376
// create a claim - before D-Day (6 seconds) (claim should fail)
382377
const tx1 = await safientMainBeneficiary.createClaim(safeId[4], '');
383378
const txReceipt1 = await tx1.wait();
384-
const claimId1 = txReceipt1.events[0].args[2];
379+
const claimId1 = txReceipt1.events[0].args[1];
385380
const claimID1 = parseInt(claimId1._hex);
386381

387382
// check claim status (DDayBased)
@@ -406,7 +401,7 @@ describe('safientMain', async () => {
406401
// create a claim - before D-Day (12 seconds) (claim should fail)
407402
const tx2 = await safientMainBeneficiary.createClaim(safeId[4], '');
408403
const txReceipt2 = await tx2.wait();
409-
const claimId2 = txReceipt2.events[0].args[2];
404+
const claimId2 = txReceipt2.events[0].args[1];
410405
const claimID2 = parseInt(claimId2._hex);
411406

412407
// check claim status (DDayBased)
@@ -424,7 +419,7 @@ describe('safientMain', async () => {
424419
// create a claim - after D-Day (10 + 2 = 12 seconds) (claim should pass)
425420
const tx3 = await safientMainBeneficiary.createClaim(safeId[4], '');
426421
const txReceipt3 = await tx3.wait();
427-
const claimId3 = txReceipt3.events[0].args[2];
422+
const claimId3 = txReceipt3.events[0].args[1];
428423
const claimID3 = parseInt(claimId3._hex);
429424

430425
// check claim status (DDayBased)

0 commit comments

Comments
 (0)