@safient/contracts
Version:
JavaScript SDK to manage and interact with the safe claims on Safient protocol.
536 lines • 26 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SafientMain = void 0;
var Types_1 = require("../types/Types");
var providers_1 = require("@ethersproject/providers");
var contracts_1 = require("@ethersproject/contracts");
var units_1 = require("@ethersproject/units");
var logger_1 = require("@ethersproject/logger");
var networks_json_1 = __importDefault(require("../utils/networks.json"));
var networks_1 = require("../utils/networks");
var SafientMain_json_1 = __importDefault(require("../abis/SafientMain.json"));
/**
* This class implements an interface for the safient protocol's contract interaction allowing
* users to create and interact with the safes and the claims
*/
var SafientMain = /** @class */ (function () {
/**
* Arbitrator Constructor
* @param signer Signer object
* @param chainId Provider chainId
*/
function SafientMain(signer, chainId) {
var _this = this;
/**
* This function creates a new safe by the safe creator
* @param beneficiaryAddress Address of the beneficiary who can claim to inherit this safe
* @param safeId Id of the safe
* @param claimType Type of claim the inheritor has go through
* @param claimPeriod The value use to vaiditate based on claimType after which the beneficiary can directly claim the safe
* @param metaevidenceURI IPFS URI pointing to the metaevidence related to general agreement, arbitration details, actors involved etc
* @param value Safe maintanence fee in Gwei, minimum arbitration fee required
* @returns A transaction response
*/
this.createSafe = function (beneficiaryAddress, safeId, claimType, claimPeriod, metaevidenceURI, value) { return __awaiter(_this, void 0, void 0, function () {
var latestBlockNumber, latestBlock, _a, e_1;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 5, , 6]);
if (!(claimType === Types_1.ClaimType.DDayBased || claimType === Types_1.ClaimType.Expirion)) return [3 /*break*/, 3];
return [4 /*yield*/, this.provider.getBlockNumber()];
case 1:
latestBlockNumber = _b.sent();
return [4 /*yield*/, this.provider.getBlock(latestBlockNumber)];
case 2:
latestBlock = _b.sent();
claimPeriod = latestBlock.timestamp + claimPeriod;
_b.label = 3;
case 3:
_a = this;
return [4 /*yield*/, this.contract.createSafe(beneficiaryAddress, safeId, claimType, claimPeriod, metaevidenceURI, {
value: value,
})];
case 4:
_a.tx = _b.sent();
return [2 /*return*/, this.tx];
case 5:
e_1 = _b.sent();
this.logger.throwError(e_1.message);
return [3 /*break*/, 6];
case 6: return [2 /*return*/];
}
});
}); };
/**
* This function creates a new safe by the safe beneficiary
* @param creatorAddress Address of the creator who created the safe offchain
* @param safeId Id of the safe
* @param claimType Type of claim the inheritor has go through
* @param claimPeriod The value use to vaiditate based on claimType after which the beneficiary can directly claim the safe
* @param metaevidenceURI IPFS URI pointing to the metaevidence related to general agreement, arbitration details, actors involved etc
* @param value Safe maintanence fee in Gwei, minimum arbitration fee required
* @returns A transaction response
*/
this.syncSafe = function (creatorAddress, safeId, claimType, claimPeriod, metaevidenceURI, value) { return __awaiter(_this, void 0, void 0, function () {
var latestBlockNumber, latestBlock, _a, e_2;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 5, , 6]);
if (!(claimType === Types_1.ClaimType.DDayBased || claimType === Types_1.ClaimType.Expirion)) return [3 /*break*/, 3];
return [4 /*yield*/, this.provider.getBlockNumber()];
case 1:
latestBlockNumber = _b.sent();
return [4 /*yield*/, this.provider.getBlock(latestBlockNumber)];
case 2:
latestBlock = _b.sent();
claimPeriod = latestBlock.timestamp + claimPeriod;
_b.label = 3;
case 3:
_a = this;
return [4 /*yield*/, this.contract.syncSafe(creatorAddress, safeId, claimType, claimPeriod, metaevidenceURI, {
value: value,
})];
case 4:
_a.tx = _b.sent();
return [2 /*return*/, this.tx];
case 5:
e_2 = _b.sent();
this.logger.throwError(e_2.message);
return [3 /*break*/, 6];
case 6: return [2 /*return*/];
}
});
}); };
/**
* This function creates a claim on a safe
* @param safeId Id of the safe
* @param evidenceURI IPFS URI pointing to the evidence submitted by the claim creator
* @returns A transaction response
*/
this.createClaim = function (safeId, evidenceURI) { return __awaiter(_this, void 0, void 0, function () {
var _a, e_3;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 3]);
_a = this;
return [4 /*yield*/, this.contract.createClaim(safeId, evidenceURI)];
case 1:
_a.tx = _b.sent();
return [2 /*return*/, this.tx];
case 2:
e_3 = _b.sent();
this.logger.throwError(e_3.message);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); };
/**
* This function deposits funds into a safe
* @param safeId Id of the safe
* @param value Funds in Gwei
* @returns A transaction response
*/
this.depositFunds = function (safeId, value) { return __awaiter(_this, void 0, void 0, function () {
var _a, e_4;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 3]);
_a = this;
return [4 /*yield*/, this.contract.depositFunds(safeId, { value: value })];
case 1:
_a.tx = _b.sent();
return [2 /*return*/, this.tx];
case 2:
e_4 = _b.sent();
this.logger.throwError(e_4.message);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); };
/**
* This function withdraws funds from a safe, only safe's current owner can execute this
* @param safeId Id of the safe
* @returns A transaction response
*/
this.withdrawFunds = function (safeId) { return __awaiter(_this, void 0, void 0, function () {
var _a, e_5;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 3]);
_a = this;
return [4 /*yield*/, this.contract.withdrawFunds(safeId)];
case 1:
_a.tx = _b.sent();
return [2 /*return*/, this.tx];
case 2:
e_5 = _b.sent();
this.logger.throwError(e_5.message);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); };
/**
* This function submits the evidence supporting a claim, only claim creator can execute this
* @param disputeId Id of the dispute representing the claim
* @param evidenceURI IPFS URI pointing to the evidence submitted by the claim creator
* @returns A transaction response
*/
this.submitEvidence = function (disputeId, evidenceURI) { return __awaiter(_this, void 0, void 0, function () {
var _a, e_6;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 3]);
_a = this;
return [4 /*yield*/, this.contract.submitEvidence(disputeId, evidenceURI)];
case 1:
_a.tx = _b.sent();
return [2 /*return*/, this.tx];
case 2:
e_6 = _b.sent();
this.logger.throwError(e_6.message);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); };
/**
* This function signals a safe in response to the claim made on that safe
* @param safeId Id of the safe
* @returns A transaction response
*/
this.sendSignal = function (safeId) { return __awaiter(_this, void 0, void 0, function () {
var _a, e_7;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 3]);
_a = this;
return [4 /*yield*/, this.contract.sendSignal(safeId)];
case 1:
_a.tx = _b.sent();
return [2 /*return*/, this.tx];
case 2:
e_7 = _b.sent();
this.logger.throwError(e_7.message);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); };
/**
* This function returns the status (0 - Active, 1 - Passed, 2 - Failed or 3 - Refused To Arbitrate) of a claim by it's id
* @param safeId Id of the safe
* @param claimId Id of the claim
* @returns The status of the claim
*/
this.getClaimStatus = function (safeId, claimId) { return __awaiter(_this, void 0, void 0, function () {
var claimStatus, e_8;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this.contract.getClaimStatus(safeId, claimId)];
case 1:
claimStatus = _a.sent();
return [2 /*return*/, claimStatus];
case 2:
e_8 = _a.sent();
this.logger.throwError(e_8.message);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); };
/**
* This function returns a safe by it's id
* @param safeId Id of the safe
* @returns The Safe object containing safe data
*/
this.getSafeBySafeId = function (safeId) { return __awaiter(_this, void 0, void 0, function () {
var safe, e_9;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this.contract.safes(safeId)];
case 1:
safe = _a.sent();
return [2 /*return*/, safe];
case 2:
e_9 = _a.sent();
this.logger.throwError(e_9.message);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); };
/**
* This function returns a claim by it's id
* @param claimId Id of the claim
* @returns The Claim object containing claim data
*/
this.getClaimByClaimId = function (claimId) { return __awaiter(_this, void 0, void 0, function () {
var claim, e_10;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this.contract.claims(claimId)];
case 1:
claim = _a.sent();
return [2 /*return*/, claim];
case 2:
e_10 = _a.sent();
this.logger.throwError(e_10.message);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); };
/**
* This function returns the total number of safes created on the SafientMain contract
* @returns The total number of safes created on the SafientMain contract
*/
this.getTotalNumberOfSafes = function () { return __awaiter(_this, void 0, void 0, function () {
var totalSafes, e_11;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this.contract.safesCount()];
case 1:
totalSafes = _a.sent();
return [2 /*return*/, Number(totalSafes)];
case 2:
e_11 = _a.sent();
this.logger.throwError(e_11.message);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); };
/**
* This function returns the total number of claims created on the SafientMain contract
* @returns The total number of claims created on the SafientMain contract
*/
this.getTotalNumberOfClaims = function () { return __awaiter(_this, void 0, void 0, function () {
var totalClaims, e_12;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this.contract.claimsCount()];
case 1:
totalClaims = _a.sent();
return [2 /*return*/, Number(totalClaims)];
case 2:
e_12 = _a.sent();
this.logger.throwError(e_12.message);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); };
/**
* This function returns the balance of the SafientMain contract
* @returns The balance of SafientMain contract in ETH
*/
this.getContractBalance = function () { return __awaiter(_this, void 0, void 0, function () {
var mainContractBalance, e_13;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this.contract.getBalance()];
case 1:
mainContractBalance = _a.sent();
return [2 /*return*/, Number((0, units_1.formatEther)(mainContractBalance))];
case 2:
e_13 = _a.sent();
this.logger.throwError(e_13.message);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); };
/**
* This function submits the guardian proof
* @param message Message generated during safe creation, also signed by the safe creator
* @param signature Signature of the message signed by the creator
* @param guardianProof Object containing guardian address and his secret
* @param secrets Array of all the secrets of all the guardians, for cross verification
* @param safeId Id of the safe
* @returns If the guardian proof was true or false
*/
this.guardianProof = function (message, signature, guardianProof, secrets, safeId) { return __awaiter(_this, void 0, void 0, function () {
var result, e_14;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this.contract.guardianProof(message, signature, guardianProof, secrets, safeId)];
case 1:
result = _a.sent();
return [2 /*return*/, result];
case 2:
e_14 = _a.sent();
this.logger.throwError(e_14.message);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); };
/**
* This function allows the guardians to claim their rewards
* @param funds Total funds need to be claimed in ETH
* @returns A transaction response
*/
this.claimRewards = function (funds) { return __awaiter(_this, void 0, void 0, function () {
var _a, e_15;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 3]);
_a = this;
return [4 /*yield*/, this.contract.claimRewards((0, units_1.parseEther)(String(funds)))];
case 1:
_a.tx = _b.sent();
return [2 /*return*/, this.tx];
case 2:
e_15 = _b.sent();
this.logger.throwError(e_15.message);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); };
/**
* This function returns the total guardian reward balance of a guardian
* @param address The address of the guardian
* @returns The total guardian reward balance in ETH
*/
this.getGuardianRewards = function (address) { return __awaiter(_this, void 0, void 0, function () {
var guardianReward, e_16;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this.contract.guardianRewards(address)];
case 1:
guardianReward = _a.sent();
return [2 /*return*/, Number((0, units_1.formatEther)(guardianReward))];
case 2:
e_16 = _a.sent();
this.logger.throwError(e_16.message);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); };
/**
* This function updates the D-Day of a safe
* @param safeId Id of the safe
* @param dDay The timestamp in unix epoch milliseconds after which the beneficiary can directly claim the safe
* @returns A transaction response
*/
this.updateDDay = function (safeId, dDay) { return __awaiter(_this, void 0, void 0, function () {
var _a, e_17;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 3]);
_a = this;
return [4 /*yield*/, this.contract.updateDDay(safeId, dDay)];
case 1:
_a.tx = _b.sent();
return [2 /*return*/, this.tx];
case 2:
e_17 = _b.sent();
this.logger.throwError(e_17.message);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); };
this.updateEDay = function (safeId, eDay) { return __awaiter(_this, void 0, void 0, function () {
var _a, e_18;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 3]);
_a = this;
return [4 /*yield*/, this.contract.updateEDay(safeId, eDay)];
case 1:
_a.tx = _b.sent();
return [2 /*return*/, this.tx];
case 2:
e_18 = _b.sent();
this.logger.throwError(e_18.message);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); };
this.logger = logger_1.Logger.globalLogger();
this.signer = signer;
this.safientMainABI = SafientMain_json_1.default.abi;
var network = Object.values(networks_json_1.default).find(function (network) { return chainId === network.chainId; });
var networkUrl = (0, networks_1.getNetworkUrl)(chainId);
this.provider = new providers_1.JsonRpcProvider(networkUrl);
network !== undefined && network.addresses.SafientMain !== ''
? (this.safientMainAddress = network.addresses.SafientMain)
: this.logger.throwError("SafientMain contract not deployed on network with chain id: ".concat(chainId));
this.contract = new contracts_1.Contract(this.safientMainAddress, this.safientMainABI, this.signer);
}
return SafientMain;
}());
exports.SafientMain = SafientMain;
//# sourceMappingURL=SafientMain.js.map