Skip to content

Commit

Permalink
fix: P-885 add $network clause in solidity VC (#3026)
Browse files Browse the repository at this point in the history
Co-authored-by: higherordertech <higherordertech>
  • Loading branch information
higherordertech authored Aug 28, 2024
1 parent 59506f9 commit 61afbb7
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ contract A20 is DynamicAssertion {
}

Logging.info("begin create assertion for A20");
AssertionLogic.Condition memory condition = AssertionLogic.Condition(
"$has_joined",
AssertionLogic.Op.Equal,
"true"
);
AssertionLogic.Condition memory condition = AssertionLogic
.newConditionWithoutSubCc(
"$has_joined",
AssertionLogic.Op.Equal,
"true"
);
string[] memory assertions = new string[](1);
assertions[0] = AssertionLogic.toString(condition);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,40 @@ library AssertionLogic {
string src;
Op op;
string dst;
CompositeCondition cc;
}

struct CompositeCondition {
Condition[] conditions;
bool isAnd; // true for 'And', false for 'Or'
}

function newConditionWithoutSubCc(
string memory src,
Op op,
string memory dst
) internal pure returns (Condition memory) {
CompositeCondition memory subCc;
return Condition(src, op, dst, subCc);
}

function addCondition(
CompositeCondition memory cc,
uint256 i,
string memory src,
Op op,
string memory dst
) internal pure {
cc.conditions[i] = Condition(src, op, dst);
CompositeCondition memory subCc;
cc.conditions[i] = Condition(src, op, dst, subCc);
}

function addCompositeCondition(
CompositeCondition memory cc,
uint256 i,
CompositeCondition memory subCc
) internal pure {
cc.conditions[i] = Condition("", Op.Equal, "", subCc);
}

function andOp(
Expand Down Expand Up @@ -85,12 +104,15 @@ library AssertionLogic {
abi.encodePacked(result, cc.isAnd ? '"and":[' : '"or":[')
);
for (uint256 i = 0; i < cc.conditions.length; i++) {
Condition memory c = cc.conditions[i];
if (i > 0) {
result = string(abi.encodePacked(result, ","));
}
result = string(
abi.encodePacked(result, toString(cc.conditions[i]))
);
if (c.cc.conditions.length > 0) {
result = string(abi.encodePacked(result, toString(c.cc)));
} else {
result = string(abi.encodePacked(result, toString(c)));
}
}
result = string(abi.encodePacked(result, "]"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,64 @@ library Identities {
chain = "combo";
}
}

function get_network_name(
uint32 network
) internal pure returns (string memory) {
if (network == Web3Networks.Polkadot) {
return "Polkadot";
}
if (network == Web3Networks.Kusama) {
return "Kusama";
}
if (network == Web3Networks.Litentry) {
return "Litentry";
}
if (network == Web3Networks.Litmus) {
return "Litmus";
}
if (network == Web3Networks.LitentryRococo) {
return "LitentryRococo";
}
if (network == Web3Networks.Khala) {
return "Khala";
}
if (network == Web3Networks.SubstrateTestnet) {
return "SubstrateTestnet";
}
if (network == Web3Networks.Ethereum) {
return "Ethereum";
}
if (network == Web3Networks.Bsc) {
return "Bsc";
}
if (network == Web3Networks.Polygon) {
return "Polygon";
}
if (network == Web3Networks.Arbitrum) {
return "Arbitrum";
}
if (network == Web3Networks.Solana) {
return "Solana";
}
if (network == Web3Networks.Combo) {
return "Combo";
}
if (network == Web3Networks.BitcoinP2tr) {
return "BitcoinP2tr";
}
if (network == Web3Networks.BitcoinP2pkh) {
return "BitcoinP2pkh";
}
if (network == Web3Networks.BitcoinP2sh) {
return "BitcoinP2sh";
}
if (network == Web3Networks.BitcoinP2wpkh) {
return "BitcoinP2wpkh";
}
if (network == Web3Networks.BitcoinP2wsh) {
return "BitcoinP2wsh";
}
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ abstract contract TokenHoldingAmount is DynamicAssertion {
string memory variable = "$holding_amount";
AssertionLogic.CompositeCondition memory cc = AssertionLogic
.CompositeCondition(
new AssertionLogic.Condition[](max > 0 && balance > 0 ? 3 : 2),
new AssertionLogic.Condition[](max > 0 && balance > 0 ? 4 : 3),
true
);
AssertionLogic.andOp(
Expand All @@ -160,9 +160,26 @@ abstract contract TokenHoldingAmount is DynamicAssertion {
AssertionLogic.Op.Equal,
tokenName
);

AssertionLogic.CompositeCondition memory networkCc = AssertionLogic
.CompositeCondition(
new AssertionLogic.Condition[](token.networks.length),
false
);
AssertionLogic.addCompositeCondition(cc, 1, networkCc);
for (uint256 i = 0; i < token.networks.length; i++) {
AssertionLogic.andOp(
networkCc,
i,
"$network",
AssertionLogic.Op.Equal,
Identities.get_network_name(token.networks[i].network)
);
}

AssertionLogic.andOp(
cc,
1,
2,
variable,
min == 0
? AssertionLogic.Op.GreaterThan
Expand All @@ -172,7 +189,7 @@ abstract contract TokenHoldingAmount is DynamicAssertion {
if (max > 0 && balance > 0) {
AssertionLogic.andOp(
cc,
2,
3,
variable,
AssertionLogic.Op.LessThan,
StringShift.toShiftedString(
Expand Down
Loading

0 comments on commit 61afbb7

Please sign in to comment.