Skip to content

Commit

Permalink
#21 - Adds claim incentive flow (#22)
Browse files Browse the repository at this point in the history
* adds guardian claim incentive flow

* removes getContractInstance function in APIs
  • Loading branch information
devpavan04 authored Oct 15, 2021
1 parent 438e7f9 commit 8a98621
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 80 deletions.
35 changes: 26 additions & 9 deletions contracts/SafientMain.sol
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,31 @@ contract SafientMain is Safes, Claims, Guardians, IArbitrable {
string memory _safeId
) external returns (bool) {
Types.Safe memory safe = safes[_safeId];
return
_guardianProof(
_message,
_signature,
_guardianproof,
_secrets,
safe.createdBy,
safe.funds
);

uint256 safeFunds = safe.funds;

safe.funds = 0;
safes[_safeId] = safe;

bool result = _guardianProof(
_message,
_signature,
_guardianproof,
_secrets,
safe.createdBy,
safeFunds
);

require(result == true, "Invalid guardian proof");

return result;
}

/**
* @notice Claim the guardian rewards
* @param _funds Total funds need to be claimed
*/
function claimRewards(uint256 _funds) external returns (bool) {
return _claimRewards(_funds);
}
}
38 changes: 35 additions & 3 deletions contracts/components/Guardians.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ import "../libraries/Utils.sol";
* proofs and guardian incentivization
*/
contract Guardians {
/** @notice Maps guardian address to their earned rewards */
mapping(address => uint256) public guardianRewards;

modifier withdrawRewards(uint256 _funds) {
require(guardianRewards[msg.sender] != 0, "No rewards remaining");
require(
guardianRewards[msg.sender] >= _funds,
"Funds requested exceeds the total remaining funds"
);
_;
}

/**
* @notice Submit the guardian proof
* @param _message Message generated during the safe creation and also
Expand Down Expand Up @@ -68,9 +80,10 @@ contract Guardians {
keccak256(abi.encodePacked(_secrets[secretIndex]))
) {
safeFunds -= guardianValue;
_guardianproof[guardianIndex].guardianAddress.call{
value: guardianValue
}("");
address guardianAddress = _guardianproof[
guardianIndex
].guardianAddress;
guardianRewards[guardianAddress] += guardianValue;
}
}
}
Expand All @@ -80,4 +93,23 @@ contract Guardians {
}
}
}

/**
* @notice Claim the guardian rewards
* @param _funds Total funds need to be claimed
*/
function _claimRewards(uint256 _funds)
internal
withdrawRewards(_funds)
returns (bool)
{
guardianRewards[msg.sender] -= _funds;

address _to = msg.sender;

(bool sent, ) = _to.call{value: _funds}("");
require(sent, "Failed to send Ether");

return sent;
}
}
8 changes: 4 additions & 4 deletions contracts/components/Safes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ contract Safes {

emit CreateSafe(msg.sender, _beneficiary, metaEvidenceID);

return true;
return sent;
}

/**
Expand Down Expand Up @@ -193,7 +193,7 @@ contract Safes {

emit CreateSafe(_creator, msg.sender, metaEvidenceID);

return true;
return sent;
}

/**
Expand All @@ -212,7 +212,7 @@ contract Safes {
(bool sent, ) = address(this).call{value: msg.value}("");
require(sent, "Failed to send Ether");

return true;
return sent;
}

/**
Expand All @@ -236,7 +236,7 @@ contract Safes {
(bool sent, ) = _to.call{value: funds}("");
require(sent, "Failed to send Ether");

return true;
return sent;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@safient/contracts",
"version": "0.1.8",
"version": "0.1.9",
"description": "JavaScript SDK to manage and interact with the safe claims on Safient protocol.",
"keywords": [
"Web3",
Expand Down
2 changes: 1 addition & 1 deletion src/abis/SafientMain.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"abi":[{"inputs":[{"internalType":"contract IArbitrator","name":"_arbitrator","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"createdBy","type":"address"},{"indexed":true,"internalType":"string","name":"safeId","type":"string"},{"indexed":true,"internalType":"uint256","name":"id","type":"uint256"}],"name":"CreateClaim","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"createdBy","type":"address"},{"indexed":true,"internalType":"address","name":"beneficiary","type":"address"},{"indexed":true,"internalType":"uint256","name":"metaEvidenceId","type":"uint256"}],"name":"CreateSafe","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"contract IArbitrator","name":"_arbitrator","type":"address"},{"indexed":true,"internalType":"uint256","name":"_disputeID","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_metaEvidenceID","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_evidenceGroupID","type":"uint256"}],"name":"Dispute","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"contract IArbitrator","name":"_arbitrator","type":"address"},{"indexed":true,"internalType":"uint256","name":"_evidenceGroupID","type":"uint256"},{"indexed":true,"internalType":"address","name":"_party","type":"address"},{"indexed":false,"internalType":"string","name":"_evidence","type":"string"}],"name":"Evidence","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"_metaEvidenceID","type":"uint256"},{"indexed":false,"internalType":"string","name":"_evidence","type":"string"}],"name":"MetaEvidence","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"contract IArbitrator","name":"_arbitrator","type":"address"},{"indexed":true,"internalType":"uint256","name":"_disputeID","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_ruling","type":"uint256"}],"name":"Ruling","type":"event"},{"inputs":[],"name":"arbitrator","outputs":[{"internalType":"contract IArbitrator","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"claims","outputs":[{"internalType":"uint256","name":"id","type":"uint256"},{"internalType":"address","name":"claimedBy","type":"address"},{"internalType":"enum Types.ClaimType","name":"claimType","type":"uint8"},{"internalType":"uint256","name":"metaEvidenceId","type":"uint256"},{"internalType":"uint256","name":"evidenceGroupId","type":"uint256"},{"internalType":"enum Types.ClaimStatus","name":"status","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"claimsCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"_safeId","type":"string"},{"internalType":"string","name":"_evidence","type":"string"}],"name":"createClaim","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"_beneficiary","type":"address"},{"internalType":"string","name":"_safeId","type":"string"},{"internalType":"enum Types.ClaimType","name":"_claimType","type":"uint8"},{"internalType":"uint256","name":"_signalingPeriod","type":"uint256"},{"internalType":"string","name":"_metaEvidence","type":"string"}],"name":"createSafe","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"string","name":"_safeId","type":"string"}],"name":"depositFunds","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"evidenceGroupID","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"_safeId","type":"string"},{"internalType":"uint256","name":"_disputeID","type":"uint256"}],"name":"getClaimStatus","outputs":[{"internalType":"enum Types.ClaimStatus","name":"status","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"_message","type":"string"},{"internalType":"bytes","name":"_signature","type":"bytes"},{"components":[{"internalType":"bytes32","name":"secretHash","type":"bytes32"},{"internalType":"address","name":"guardianAddress","type":"address"}],"internalType":"struct Types.RecoveryProof[]","name":"_guardianproof","type":"tuple[]"},{"internalType":"string[]","name":"_secrets","type":"string[]"},{"internalType":"string","name":"_safeId","type":"string"}],"name":"guardianProof","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"metaEvidenceID","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_disputeID","type":"uint256"},{"internalType":"uint256","name":"_ruling","type":"uint256"}],"name":"rule","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"rulingOptions","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"","type":"string"}],"name":"safes","outputs":[{"internalType":"string","name":"id","type":"string"},{"internalType":"address","name":"createdBy","type":"address"},{"internalType":"address","name":"currentOwner","type":"address"},{"internalType":"address","name":"beneficiary","type":"address"},{"internalType":"uint256","name":"signalingPeriod","type":"uint256"},{"internalType":"uint256","name":"endSignalTime","type":"uint256"},{"internalType":"uint256","name":"latestSignalTime","type":"uint256"},{"internalType":"enum Types.ClaimType","name":"claimType","type":"uint8"},{"internalType":"uint256","name":"metaEvidenceId","type":"uint256"},{"internalType":"uint256","name":"claimsCount","type":"uint256"},{"internalType":"uint256","name":"funds","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"safesCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"_safeId","type":"string"}],"name":"sendSignal","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_disputeID","type":"uint256"},{"internalType":"string","name":"_evidence","type":"string"}],"name":"submitEvidence","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_creator","type":"address"},{"internalType":"string","name":"_safeId","type":"string"},{"internalType":"enum Types.ClaimType","name":"_claimType","type":"uint8"},{"internalType":"uint256","name":"_signalingPeriod","type":"uint256"},{"internalType":"string","name":"_metaEvidence","type":"string"}],"name":"syncSafe","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"string","name":"_safeId","type":"string"}],"name":"withdrawFunds","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]}
{"abi":[{"inputs":[{"internalType":"contract IArbitrator","name":"_arbitrator","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"createdBy","type":"address"},{"indexed":true,"internalType":"string","name":"safeId","type":"string"},{"indexed":true,"internalType":"uint256","name":"id","type":"uint256"}],"name":"CreateClaim","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"createdBy","type":"address"},{"indexed":true,"internalType":"address","name":"beneficiary","type":"address"},{"indexed":true,"internalType":"uint256","name":"metaEvidenceId","type":"uint256"}],"name":"CreateSafe","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"contract IArbitrator","name":"_arbitrator","type":"address"},{"indexed":true,"internalType":"uint256","name":"_disputeID","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_metaEvidenceID","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_evidenceGroupID","type":"uint256"}],"name":"Dispute","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"contract IArbitrator","name":"_arbitrator","type":"address"},{"indexed":true,"internalType":"uint256","name":"_evidenceGroupID","type":"uint256"},{"indexed":true,"internalType":"address","name":"_party","type":"address"},{"indexed":false,"internalType":"string","name":"_evidence","type":"string"}],"name":"Evidence","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"_metaEvidenceID","type":"uint256"},{"indexed":false,"internalType":"string","name":"_evidence","type":"string"}],"name":"MetaEvidence","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"contract IArbitrator","name":"_arbitrator","type":"address"},{"indexed":true,"internalType":"uint256","name":"_disputeID","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_ruling","type":"uint256"}],"name":"Ruling","type":"event"},{"inputs":[],"name":"arbitrator","outputs":[{"internalType":"contract IArbitrator","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_funds","type":"uint256"}],"name":"claimRewards","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"claims","outputs":[{"internalType":"uint256","name":"id","type":"uint256"},{"internalType":"address","name":"claimedBy","type":"address"},{"internalType":"enum Types.ClaimType","name":"claimType","type":"uint8"},{"internalType":"uint256","name":"metaEvidenceId","type":"uint256"},{"internalType":"uint256","name":"evidenceGroupId","type":"uint256"},{"internalType":"enum Types.ClaimStatus","name":"status","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"claimsCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"_safeId","type":"string"},{"internalType":"string","name":"_evidence","type":"string"}],"name":"createClaim","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"_beneficiary","type":"address"},{"internalType":"string","name":"_safeId","type":"string"},{"internalType":"enum Types.ClaimType","name":"_claimType","type":"uint8"},{"internalType":"uint256","name":"_signalingPeriod","type":"uint256"},{"internalType":"string","name":"_metaEvidence","type":"string"}],"name":"createSafe","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"string","name":"_safeId","type":"string"}],"name":"depositFunds","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"evidenceGroupID","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"_safeId","type":"string"},{"internalType":"uint256","name":"_disputeID","type":"uint256"}],"name":"getClaimStatus","outputs":[{"internalType":"enum Types.ClaimStatus","name":"status","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"_message","type":"string"},{"internalType":"bytes","name":"_signature","type":"bytes"},{"components":[{"internalType":"bytes32","name":"secretHash","type":"bytes32"},{"internalType":"address","name":"guardianAddress","type":"address"}],"internalType":"struct Types.RecoveryProof[]","name":"_guardianproof","type":"tuple[]"},{"internalType":"string[]","name":"_secrets","type":"string[]"},{"internalType":"string","name":"_safeId","type":"string"}],"name":"guardianProof","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"guardianRewards","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"metaEvidenceID","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_disputeID","type":"uint256"},{"internalType":"uint256","name":"_ruling","type":"uint256"}],"name":"rule","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"rulingOptions","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"","type":"string"}],"name":"safes","outputs":[{"internalType":"string","name":"id","type":"string"},{"internalType":"address","name":"createdBy","type":"address"},{"internalType":"address","name":"currentOwner","type":"address"},{"internalType":"address","name":"beneficiary","type":"address"},{"internalType":"uint256","name":"signalingPeriod","type":"uint256"},{"internalType":"uint256","name":"endSignalTime","type":"uint256"},{"internalType":"uint256","name":"latestSignalTime","type":"uint256"},{"internalType":"enum Types.ClaimType","name":"claimType","type":"uint8"},{"internalType":"uint256","name":"metaEvidenceId","type":"uint256"},{"internalType":"uint256","name":"claimsCount","type":"uint256"},{"internalType":"uint256","name":"funds","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"safesCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"_safeId","type":"string"}],"name":"sendSignal","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_disputeID","type":"uint256"},{"internalType":"string","name":"_evidence","type":"string"}],"name":"submitEvidence","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_creator","type":"address"},{"internalType":"string","name":"_safeId","type":"string"},{"internalType":"enum Types.ClaimType","name":"_claimType","type":"uint8"},{"internalType":"uint256","name":"_signalingPeriod","type":"uint256"},{"internalType":"string","name":"_metaEvidence","type":"string"}],"name":"syncSafe","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"string","name":"_safeId","type":"string"}],"name":"withdrawFunds","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]}
Loading

0 comments on commit 8a98621

Please sign in to comment.