diff --git a/package-lock.json b/package-lock.json index 0d3ccfb..56b82a0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@safient/contracts", - "version": "0.1.13-alpha", + "version": "0.1.14-alpha", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@safient/contracts", - "version": "0.1.13-alpha", + "version": "0.1.14-alpha", "dependencies": { "@ethersproject/address": "^5.2.0", "@ethersproject/bignumber": "^5.2.0", @@ -18019,7 +18019,9 @@ "version": "2.0.0", "resolved": "https://registry.npmjs.org/@typechain/ethers-v5/-/ethers-v5-2.0.0.tgz", "integrity": "sha512-0xdCkyGOzdqh4h5JSf+zoWx85IusEjDcPIwNEHP8mrWSnCae4rvrqB+/gtpdNfX7zjlFlZiMeePn2r63EI3Lrw==", - "requires": {} + "requires": { + "ethers": "^5.0.2" + } }, "@types/abstract-leveldown": { "version": "5.0.2", diff --git a/package.json b/package.json index 7c95d89..31c78b1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@safient/contracts", - "version": "0.1.14-alpha", + "version": "0.1.15-alpha", "description": "JavaScript SDK to manage and interact with the safe claims on Safient protocol.", "keywords": [ "Web3", diff --git a/src/contracts/Arbitrator.ts b/src/contracts/Arbitrator.ts index a628b34..1ead398 100644 --- a/src/contracts/Arbitrator.ts +++ b/src/contracts/Arbitrator.ts @@ -5,6 +5,7 @@ import { formatEther } from '@ethersproject/units'; import { Logger } from '@ethersproject/logger'; import networks from '../utils/networks.json'; import data from '../abis/AutoAppealableArbitrator.json'; +import { JsonRpcProvider } from '@ethersproject/providers'; /** * This class implements an interface to interact with the arbitrator contract diff --git a/src/contracts/SafientMain.ts b/src/contracts/SafientMain.ts index de84964..0c5f262 100644 --- a/src/contracts/SafientMain.ts +++ b/src/contracts/SafientMain.ts @@ -8,13 +8,14 @@ import { Safe, Signer, } from '../types/Types'; -import { TransactionResponse } from '@ethersproject/providers'; +import { JsonRpcProvider, TransactionResponse } from '@ethersproject/providers'; import { Contract } from '@ethersproject/contracts'; import { BigNumber } from '@ethersproject/bignumber'; import { formatEther, parseEther } from '@ethersproject/units'; import { Logger } from '@ethersproject/logger'; import { Bytes } from 'ethers'; import networks from '../utils/networks.json'; +import {getNetworkUrl} from "../utils/networks" import data from '../abis/SafientMain.json'; /** @@ -40,6 +41,9 @@ export class SafientMain { /** @ignore */ private contract: Contract; + /**@ignore */ + private provider: JsonRpcProvider + /** * Arbitrator Constructor * @param signer Signer object @@ -52,6 +56,9 @@ export class SafientMain { const network = Object.values(networks).find((network) => chainId === network.chainId); + const networkUrl = getNetworkUrl(chainId) + this.provider = new JsonRpcProvider(networkUrl) + network !== undefined && network.addresses.SafientMain !== '' ? (this.safientMainAddress = network.addresses.SafientMain) : this.logger.throwError(`SafientMain contract not deployed on network with chain id: ${chainId}`); @@ -80,6 +87,11 @@ export class SafientMain { value: string ): Promise => { try { + if(claimType === ClaimType.DDayBased){ + const latestBlockNumber = await this.provider.getBlockNumber(); + const latestBlock = await this.provider.getBlock(latestBlockNumber); + dDay = latestBlock.timestamp + dDay + } this.tx = await this.contract.createSafe( beneficiaryAddress, safeId, @@ -118,6 +130,13 @@ export class SafientMain { value: string ): Promise => { try { + + if(claimType === ClaimType.DDayBased){ + const latestBlockNumber = await this.provider.getBlockNumber(); + const latestBlock = await this.provider.getBlock(latestBlockNumber); + dDay = latestBlock.timestamp + dDay + } + this.tx = await this.contract.syncSafe( creatorAddress, safeId, diff --git a/src/utils/networks.js b/src/utils/networks.js new file mode 100644 index 0000000..7c7743b --- /dev/null +++ b/src/utils/networks.js @@ -0,0 +1,33 @@ +const dotenv = require('dotenv'); +dotenv.config() + + +const networks = { + localhost: { + chainId: 31337, + url: 'http://localhost:8545', + }, + mainnet: { + chainId: 1, + url: `https://mainnet.infura.io/v3/${process.env.INFURA_API_KEY}`, + }, + kovan: { + chainId: 42, + url: `https://kovan.infura.io/v3/${process.env.INFURA_API_KEY}`, + + }, + polygontestnet: { + chainId: 80001, + url: 'https://matic-mumbai.chainstacklabs.com', + }, + polygon : { + chainId: 137, + url: 'https://matic-mumbai.chainstacklabs.com', + + }, +} + +export const getNetworkUrl = (chainId) => { + const network = Object.values(networks).find(network => chainId === network.chainId); + return network.url +} \ No newline at end of file diff --git a/test-sdk/claims.js b/test-sdk/claims.js index 4289774..b460563 100644 --- a/test-sdk/claims.js +++ b/test-sdk/claims.js @@ -123,13 +123,12 @@ describe('safientMain', async () => { const safientMain1 = new SafientMain(beneficiarySigner, chainId); const beforeTotalNumberOfSafes = await safientMain1.getTotalNumberOfSafes(); - // SUCCESS : create a safe(for claimType - SignalBased & signal - won't signal) await safientMain1.syncSafe( safeCreatorAddress, // 2nd account safeId[1], Types.ClaimType.SignalBased, - 6, // 6 seconds because opting SignalBased + 10, // 6 seconds because opting SignalBased 0, '', // no metaevidence because SignalBased '' // no safe maintenence fee because SignalBased @@ -140,7 +139,7 @@ describe('safientMain', async () => { const safe = await safientMain1.getSafeBySafeId(safeId[1]); expect(safe.createdBy).to.equal(safeCreatorAddress); expect(safe.beneficiary).to.equal(beneficiaryAddress); - expect(Number(safe.signalingPeriod)).to.equal(6); // 6 seconds + expect(Number(safe.signalingPeriod)).to.equal(10); // 6 seconds expect(Number(safe.endSignalTime)).to.equal(0); expect(Number(safe.latestSignalTime)).to.equal(0); expect(Number(safe.claimType)).to.equal(0); // SignalBased @@ -151,7 +150,7 @@ describe('safientMain', async () => { beneficiaryAddress, // 2nd account safeId[2], Types.ClaimType.SignalBased, - 6, + 10, 0, '', '' @@ -175,7 +174,7 @@ describe('safientMain', async () => { // SUCCESS : create a claim (ArbitrationBased) on safeId1 const tx = await safientMain.createClaim(safeId[0], metaevidenceOrEvidenceURI); const txReceipt = await tx.wait(); - claimIdOfSafeId0 = txReceipt.events[2].args[2]; + claimIdOfSafeId0 = txReceipt.events[2].args[1]; expect(await safientMain.getTotalNumberOfClaims()).to.equal(beforeTotalNumberOfClaims + 1); @@ -192,7 +191,7 @@ describe('safientMain', async () => { // SUCCESS : create claim on safeId2 tx = await safientMain.createClaim(safeId[1], ''); txReceipt = await tx.wait(); - claimIdOfSafeId1 = txReceipt.events[0].args[2]; + claimIdOfSafeId1 = parseInt(txReceipt.events[0].args[1]._hex) const safeWithSafeId1 = await safientMain.getSafeBySafeId(safeId[1]); expect(safeWithSafeId1.claimsCount).to.equal(1); @@ -204,8 +203,7 @@ describe('safientMain', async () => { // SUCCESS : create claim on safeId3 tx = await safientMain.createClaim(safeId[2], ''); txReceipt = await tx.wait(); - claimIdOfSafeId2 = txReceipt.events[0].args[2]; - + claimIdOfSafeId2 = parseInt(txReceipt.events[0].args[1]._hex) const safeWithSafeId2 = await safientMain.getSafeBySafeId(safeId[2]); expect(safeWithSafeId2.claimsCount).to.equal(1); @@ -227,7 +225,7 @@ describe('safientMain', async () => { const mineNewBlock = new Promise((resolve, reject) => { setTimeout(() => { resolve(provider.send('evm_mine')); - }, 7000); + }, 11000); }); const result = await mineNewBlock; @@ -328,17 +326,14 @@ describe('safientMain', async () => { const safientMainBeneficiary = new SafientMain(beneficiarySigner, chainId); const safientMainAccountX = new SafientMain(accountXSigner, chainId); - const latestBlockNumber = await provider.getBlockNumber(); - const latestBlock = await provider.getBlock(latestBlockNumber); - const now = latestBlock.timestamp; // SUCCESS : create another safe with safeId4 (for claimType - DDayBased) with DDay set to 6 seconds - await safientMainCreator.createSafe(beneficiaryAddress, safeId[3], Types.ClaimType.DDayBased, 0, now + 6, '', ''); + await safientMainCreator.createSafe(beneficiaryAddress, safeId[3], Types.ClaimType.DDayBased, 0, 6, '', ''); // create a claim - before D-Day (claim should fail) const tx1 = await safientMainBeneficiary.createClaim(safeId[3], ''); const txReceipt1 = await tx1.wait(); - const claimId1 = txReceipt1.events[0].args[2]; + const claimId1 = txReceipt1.events[0].args[1]; const claimID1 = parseInt(claimId1._hex); // check claim status (DDayBased) @@ -356,7 +351,7 @@ describe('safientMain', async () => { // create a claim - before D-Day (claim should pass) const tx2 = await safientMainBeneficiary.createClaim(safeId[3], ''); const txReceipt2 = await tx2.wait(); - const claimId2 = txReceipt2.events[0].args[2]; + const claimId2 = txReceipt2.events[0].args[1]; const claimID2 = parseInt(claimId2._hex); // check claim status (DDayBased) @@ -381,7 +376,7 @@ describe('safientMain', async () => { // create a claim - before D-Day (6 seconds) (claim should fail) const tx1 = await safientMainBeneficiary.createClaim(safeId[4], ''); const txReceipt1 = await tx1.wait(); - const claimId1 = txReceipt1.events[0].args[2]; + const claimId1 = txReceipt1.events[0].args[1]; const claimID1 = parseInt(claimId1._hex); // check claim status (DDayBased) @@ -406,7 +401,7 @@ describe('safientMain', async () => { // create a claim - before D-Day (12 seconds) (claim should fail) const tx2 = await safientMainBeneficiary.createClaim(safeId[4], ''); const txReceipt2 = await tx2.wait(); - const claimId2 = txReceipt2.events[0].args[2]; + const claimId2 = txReceipt2.events[0].args[1]; const claimID2 = parseInt(claimId2._hex); // check claim status (DDayBased) @@ -424,7 +419,7 @@ describe('safientMain', async () => { // create a claim - after D-Day (10 + 2 = 12 seconds) (claim should pass) const tx3 = await safientMainBeneficiary.createClaim(safeId[4], ''); const txReceipt3 = await tx3.wait(); - const claimId3 = txReceipt3.events[0].args[2]; + const claimId3 = txReceipt3.events[0].args[1]; const claimID3 = parseInt(claimId3._hex); // check claim status (DDayBased)