Skip to content

Commit

Permalink
Merge branch 'dev' into p-883-remove-shard_vault
Browse files Browse the repository at this point in the history
  • Loading branch information
Kailai-Wang committed Jun 24, 2024
2 parents 452570d + 2810649 commit 9db99df
Show file tree
Hide file tree
Showing 55 changed files with 1,715 additions and 1,327 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,10 @@ library Utils {

return (success, value);
}
function isStringsEqual(
string memory a,
string memory b
) internal pure returns (bool) {
return keccak256(abi.encodePacked(a)) == keccak256(abi.encodePacked(b));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2020-2024 Trust Computing GmbH.
// This file is part of Litentry.
//
// Litentry is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Litentry is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Litentry. If not, see <https://www.gnu.org/licenses/>.

// SPDX-License-Identifier: GPL-3.0-or-later

pragma solidity ^0.8.8;

library Constants {
uint256 constant decimals_factor = 1000;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2020-2024 Trust Computing GmbH.
// This file is part of Litentry.
//
// Litentry is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Litentry is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Litentry. If not, see <https://www.gnu.org/licenses/>.

// SPDX-License-Identifier: GPL-3.0-or-later

pragma solidity ^0.8.8;

import "../libraries/Http.sol";
import "../libraries/Utils.sol";
library GeniidataClient {
function getTokenBalance(
string[] memory secrets,
string memory url,
string memory identityString,
string memory tokenName,
uint8 tokenDecimals
) internal returns (uint256) {
string memory encodePackedUrl = string(
abi.encodePacked(
url,
"?tick=",
tokenName,
"&address=",
identityString
)
);
HttpHeader[] memory headers = new HttpHeader[](1);
headers[0] = HttpHeader("api-key", secrets[1]);

// https://geniidata.readme.io/reference/get-brc20-tick-list-copy
(bool success, string memory value) = Http.GetString(
encodePackedUrl,
"/data/list/0/available_balance",
headers
);

if (success) {
(bool parseDecimalSuccess, uint256 result) = Utils.parseDecimal(
value,
tokenDecimals
);
if (parseDecimalSuccess) {
return result;
}
}
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ import "../libraries/Utils.sol";
library NoderealClient {
function getTokenBalance(
string memory url,
string[] memory secrets,
string memory tokenContractAddress,
string memory account
) internal returns (bool, uint256) {
HttpHeader[] memory headers = new HttpHeader[](0);
string memory request;

string memory encodePackedUrl = string(
abi.encodePacked(url, secrets[0])
);
if (
keccak256(bytes(tokenContractAddress)) == keccak256("Native Token")
) {
Expand All @@ -54,7 +59,7 @@ library NoderealClient {
return (false, 0);
}
(bool result, string memory balance) = Http.PostString(
url,
encodePackedUrl,
"/result",
request,
headers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,56 @@ import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.9.0/contr
import "../libraries/AssertionLogic.sol";
import "../libraries/Identities.sol";
import "../DynamicAssertion.sol";
import "./Constants.sol";

abstract contract TokenHoldingAmount is DynamicAssertion {
uint256 constant decimals_factor = 1000;
function execute(
Identity[] memory identities,
string[] memory secrets,
bytes memory /*params*/
)
public
override
returns (
string memory,
string memory,
string[] memory,
string memory,
bool
)
{
string
memory description = "The amount of a particular token you are holding";
string memory assertion_type = "Token Holding Amount";
schema_url = "https://raw.githubusercontent.com/litentry/vc-jsonschema/main/dist/schemas/25-token-holding-amount/1-1-0.json";

uint256 balance = queryTotalBalance(identities, secrets);

(uint256 index, uint256 min, int256 max) = calculateRange(balance);

string[] memory assertions = assembleAssertions(min, max);
mapping(string => string) internal tokenNames;
mapping(string => uint256[]) internal tokenRanges;
function execute(
Identity[] memory identities,
string[] memory secrets,
bytes memory params
)
public
override
returns (
string memory,
string memory,
string[] memory,
string memory,
bool
)
{
string
memory description = "The amount of a particular token you are holding";
string memory assertion_type = "Token Holding Amount";
schema_url = "https://raw.githubusercontent.com/litentry/vc-jsonschema/main/dist/schemas/25-token-holding-amount/1-1-0.json";

string memory tokenLowercaseName = abi.decode(params, (string));

if (
keccak256(abi.encodePacked(tokenNames[tokenLowercaseName])) ==
keccak256(abi.encodePacked(""))
) {
revert("Token not supported or not found");
}

uint256 balance = queryTotalBalance(
identities,
secrets,
tokenNames[tokenLowercaseName]
);

(uint256 index, uint256 min, int256 max) = calculateRange(
balance,
tokenRanges[tokenLowercaseName]
);

string[] memory assertions = assembleAssertions(
min,
max,
tokenNames[tokenLowercaseName]
);

bool result = index > 0 || balance > 0;

Expand All @@ -57,7 +80,8 @@ abstract contract TokenHoldingAmount is DynamicAssertion {

function queryTotalBalance(
Identity[] memory identities,
string[] memory secrets
string[] memory secrets,
string memory tokenName
) internal virtual returns (uint256) {
uint256 total_balance = 0;
uint256 identitiesLength = identities.length;
Expand All @@ -68,7 +92,12 @@ abstract contract TokenHoldingAmount is DynamicAssertion {
for (uint32 j = 0; j < networksLength; j++) {
uint32 network = identity.networks[j];
if (isSupportedNetwork(network)) {
total_balance += queryBalance(identity, network, secrets);
total_balance += queryBalance(
identity,
network,
secrets,
tokenName
);
}
}
}
Expand All @@ -77,16 +106,17 @@ abstract contract TokenHoldingAmount is DynamicAssertion {
}

function calculateRange(
uint256 balance
uint256 balance,
uint256[] memory ranges
) private pure returns (uint256, uint256, int256) {
uint256[] memory ranges = getTokenRanges();
uint256 index = ranges.length - 1;
uint256 min = 0;
int256 max = 0;

for (uint32 i = 1; i < ranges.length; i++) {
if (
balance * decimals_factor < ranges[i] * 10 ** getTokenDecimals()
balance * Constants.decimals_factor <
ranges[i] * 10 ** getTokenDecimals()
) {
index = i - 1;
break;
Expand All @@ -106,7 +136,8 @@ abstract contract TokenHoldingAmount is DynamicAssertion {

function assembleAssertions(
uint256 min,
int256 max
int256 max,
string memory tokenName
) private pure returns (string[] memory) {
string memory variable = "$holding_amount";
AssertionLogic.CompositeCondition memory cc = AssertionLogic
Expand All @@ -119,22 +150,22 @@ abstract contract TokenHoldingAmount is DynamicAssertion {
0,
"$token",
AssertionLogic.Op.Equal,
getTokenName()
tokenName
);
AssertionLogic.andOp(
cc,
1,
variable,
AssertionLogic.Op.GreaterEq,
Strings.toString(min / decimals_factor)
Strings.toString(min / Constants.decimals_factor)
);
if (max > 0) {
AssertionLogic.andOp(
cc,
2,
variable,
AssertionLogic.Op.LessThan,
Strings.toString(uint256(max) / decimals_factor)
Strings.toString(uint256(max) / Constants.decimals_factor)
);
}

Expand All @@ -144,10 +175,6 @@ abstract contract TokenHoldingAmount is DynamicAssertion {
return assertions;
}

function getTokenName() internal pure virtual returns (string memory);

function getTokenRanges() internal pure virtual returns (uint256[] memory);

function getTokenDecimals() internal pure virtual returns (uint8);

function isSupportedNetwork(
Expand All @@ -157,6 +184,7 @@ abstract contract TokenHoldingAmount is DynamicAssertion {
function queryBalance(
Identity memory identity,
uint32 network,
string[] memory secrets
string[] memory secrets,
string memory tokenName
) internal virtual returns (uint256);
}
Loading

0 comments on commit 9db99df

Please sign in to comment.