Skip to content

Commit

Permalink
feat(contracts): adds updated getScore and setMaxScoreAge functions
Browse files Browse the repository at this point in the history
  • Loading branch information
aminah-io committed Nov 28, 2023
1 parent 2837a5e commit c599895
Show file tree
Hide file tree
Showing 3 changed files with 4,884 additions and 4,868 deletions.
69 changes: 68 additions & 1 deletion contracts/GitcoinPassportDecoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ contract GitcoinPassportDecoder is
// Score attestation schema UID
bytes32 public scoreSchemaUID;

// Maximum score age
uint256 public maxScoreAge;

/// A provider with the same name already exists
/// @param provider the name of the duplicate provider
error ProviderAlreadyExists(string provider);
Expand Down Expand Up @@ -143,6 +146,14 @@ contract GitcoinPassportDecoder is
emit SchemaSet(_schemaUID);
}

/**
* @dev Sets the maximum allowed age of the score
* @param _maxScoreAge Max number of days for max score age
*/
function setMaxScoreAge(uint256 _maxScoreAge) public {
maxScoreAge = block.timestamp + (_maxScoreAge * 86400);
}

/**
* @dev Adds a new provider to the end of the providerVersions mapping
* @param providers provider name
Expand Down Expand Up @@ -321,7 +332,7 @@ contract GitcoinPassportDecoder is
* @dev Retrieves the user's Score attestation via the GitcoinResolver and returns it
* @param userAddress User's address
*/
function getScore(address userAddress) public view returns (Score memory) {
function getScore_(address userAddress) public view returns (Score memory) {
// Get the attestation UID from the user's attestations
bytes32 attestationUID = gitcoinResolver.getUserAttestation(
userAddress,
Expand Down Expand Up @@ -356,4 +367,60 @@ contract GitcoinPassportDecoder is
// Return the score attestation
return score;
}

/**
* @dev Retrieves the user's Score attestation via the GitcoinResolver and returns it
* @param user The ETH address of the recipient
*/
function getScore(address user) public view returns (uint256) {
Score memory score;

IGitcoinResolver.CachedScore memory cachedScore = gitcoinResolver.getCachedScore(user);

bool hasCachedScore = cachedScore.issuanceDate != 0 && cachedScore.expirationDate != 0;

if (!hasCachedScore) {
// fallback for reading the score from the attestation
bytes32 attestationUID = gitcoinResolver.getUserAttestation(
user,
scoreSchemaUID
);

// Check if the attestation UID exists within the GitcoinResolver. When an attestation is revoked that attestation UID is set to 0.
if (attestationUID == 0) {
revert AttestationNotFound();
}

// Get the attestation from the user's attestation UID
Attestation memory attestation = getAttestation(attestationUID);

// Decode the attestion output
(score.score, score.scorerID, score.decimals) = abi.decode(
attestation.data,
(uint256, uint32, uint8)
);

// Check for expiration time
if (
attestation.expirationTime > 0 &&
attestation.expirationTime <= maxScoreAge
) {
revert AttestationExpired(attestation.expirationTime);
}

// Return the score value
return score.score;
}

// Check for expiration time
if (
cachedScore.expirationDate > 0 &&
cachedScore.expirationDate <= maxScoreAge
) {
revert AttestationExpired(cachedScore.expirationDate);
}

// Return the score value
return cachedScore.score;
}
}
2 changes: 1 addition & 1 deletion contracts/IGitcoinPassportDecoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ interface IGitcoinPassportDecoder {
address userAddress
) external returns (Credential[] memory);

function getScore(address userAddress) external returns (Score memory);
function getScore(address user) external view returns (uint256);
}
Loading

0 comments on commit c599895

Please sign in to comment.