Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
samteb committed Jan 29, 2024
1 parent 60579e0 commit 67862e5
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 53 deletions.
17 changes: 7 additions & 10 deletions apps/authz/src/opa/rego/__test__/criteria/gas_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,22 @@ moreGasFee = gasFee + 1000000000

lessGasFee = gasFee - 1000000000

test_gas {
checkGasCondition(wildcard) with input as request
test_checkGasFeeAmount {
checkGasFeeAmount({"currency": wildcard, "operator": "gt", "value": lessGasFee}) with input as request
with data.entities as entities

checkGasCondition({"operator": "gt", "value": lessGasFee}) with input as request
checkGasFeeAmount({"currency": wildcard, "operator": "lt", "value": moreGasFee}) with input as request
with data.entities as entities

checkGasCondition({"operator": "lt", "value": moreGasFee}) with input as request
checkGasFeeAmount({"currency": wildcard, "operator": "gte", "value": gasFee}) with input as request
with data.entities as entities

checkGasCondition({"operator": "gte", "value": gasFee}) with input as request
checkGasFeeAmount({"currency": wildcard, "operator": "lte", "value": gasFee}) with input as request
with data.entities as entities

checkGasCondition({"operator": "lte", "value": gasFee}) with input as request
checkGasFeeAmount({"currency": wildcard, "operator": "eq", "value": gasFee}) with input as request
with data.entities as entities

checkGasCondition({"operator": "eq", "value": gasFee}) with input as request
with data.entities as entities

checkGasCondition({"operator": "neq", "value": moreGasFee}) with input as request
checkGasFeeAmount({"currency": wildcard, "operator": "neq", "value": moreGasFee}) with input as request
with data.entities as entities
}
79 changes: 67 additions & 12 deletions apps/authz/src/opa/rego/__test__/criteria/tokenAllowance_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,79 @@ package main

import future.keywords.in

test_checkTokenAllowanceIntent {
tokenAllowanceRequest = {
"action": "signTransaction",
"intent": {
"type": "approveTokenAllowance",
"from": "eip155:eoa:0xddcf208f219a6e6af072f2cfdc615b2c1805f98e",
"spender": "eip155:137:0xa45e21e9370ba031c5e1f47dedca74a7ce2ed7a3",
"contract": "eip155:137/erc20:0x2791bca1f2de4661ed88a30c99a7a9449aa84174",
"amount": "1000000000000000000",
},
}
tokenAllowanceRequest = {
"action": "signTransaction",
"intent": {
"type": "approveTokenAllowance",
"from": "eip155:eoa:0xddcf208f219a6e6af072f2cfdc615b2c1805f98e",
"spender": "eip155:137:0xa45e21e9370ba031c5e1f47dedca74a7ce2ed7a3",
"token": "eip155:137/erc20:0x2791bca1f2de4661ed88a30c99a7a9449aa84174",
"amount": "1000000000000000000",
},
"prices": {
"fiat:usd": "0.99",
"fiat:eur": "1.10",
},
}

test_tokenAllowance {
checkTokenAllowanceIntent({"approveTokenAllowance"}) with input as tokenAllowanceRequest
with data.entities as entities

checkTokenAllowanceSpender({"eip155:137:0xa45e21e9370ba031c5e1f47dedca74a7ce2ed7a3"}) with input as tokenAllowanceRequest
with data.entities as entities

checkTokenAllowanceSpender(wildcard) with input as tokenAllowanceRequest
checkTokenAllowanceAddress({"eip155:137/erc20:0x2791bca1f2de4661ed88a30c99a7a9449aa84174"}) with input as tokenAllowanceRequest
with data.entities as entities

checkTokenAllowanceAddress(wildcard) with input as tokenAllowanceRequest with data.entities as entities

checkTokenAllowanceSpender(wildcard) with input as tokenAllowanceRequest with data.entities as entities
}

test_tokenAllowanceValue {
res = tokenAllowanceAmount("fiat:usd") with input as tokenAllowanceRequest
with data.entities as entities

res == 1000000000000000000 * 0.99
}

test_checkTokenAllowanceByAmount {
checkTokenAllowanceAmount({"currency": wildcard, "operator": "eq", "value": one_matic}) with input as tokenAllowanceRequest
with data.entities as entities

checkTokenAllowanceAmount({"currency": wildcard, "operator": "neq", "value": ten_matic}) with input as tokenAllowanceRequest
with data.entities as entities

checkTokenAllowanceAmount({"currency": wildcard, "operator": "gt", "value": half_matic}) with input as tokenAllowanceRequest
with data.entities as entities

checkTokenAllowanceAmount({"currency": wildcard, "operator": "lt", "value": ten_matic}) with input as tokenAllowanceRequest
with data.entities as entities

checkTokenAllowanceAmount({"currency": wildcard, "operator": "gte", "value": one_matic}) with input as tokenAllowanceRequest
with data.entities as entities

checkTokenAllowanceAmount({"currency": wildcard, "operator": "lte", "value": one_matic}) with input as tokenAllowanceRequest
with data.entities as entities
}

test_checkTokenAllowanceByValue {
checkTokenAllowanceAmount({"currency": "fiat:usd", "operator": "eq", "value": one_matic_value}) with input as tokenAllowanceRequest
with data.entities as entities

checkTokenAllowanceAmount({"currency": "fiat:usd", "operator": "neq", "value": ten_matic_value}) with input as tokenAllowanceRequest
with data.entities as entities

checkTokenAllowanceAmount({"currency": "fiat:usd", "operator": "gt", "value": half_matic_value}) with input as tokenAllowanceRequest
with data.entities as entities

checkTokenAllowanceAmount({"currency": "fiat:usd", "operator": "lt", "value": ten_matic_value}) with input as tokenAllowanceRequest
with data.entities as entities

checkTokenAllowanceAmount({"currency": "fiat:usd", "operator": "gte", "value": one_matic_value}) with input as tokenAllowanceRequest
with data.entities as entities

checkTokenAllowanceAmount({"currency": "fiat:usd", "operator": "lte", "value": one_matic_value}) with input as tokenAllowanceRequest
with data.entities as entities
}
12 changes: 0 additions & 12 deletions apps/authz/src/opa/rego/__test__/criteria/transferToken_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@ package main

import future.keywords.in

half_matic = "500000000000000000"

one_matic = "1000000000000000000"

ten_matic = "10000000000000000000"

half_matic_value = "495000000000000000"

one_matic_value = "990000000000000000"

ten_matic_value = "9900000000000000000"

test_transferNative {
nativeRequest = {
"action": "signTransaction",
Expand Down
12 changes: 12 additions & 0 deletions apps/authz/src/opa/rego/__test__/main_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ package main

import future.keywords.in

half_matic = "500000000000000000"

one_matic = "1000000000000000000"

ten_matic = "10000000000000000000"

half_matic_value = "495000000000000000"

one_matic_value = "990000000000000000"

ten_matic_value = "9900000000000000000"

twentyHoursAgo = (nowSeconds - ((20 * 60) * 60)) * 1000 # in ms

elevenHoursAgo = (nowSeconds - ((11 * 60) * 60)) * 1000 # in ms
Expand Down
39 changes: 20 additions & 19 deletions apps/authz/src/opa/rego/lib/criteria/gas.rego
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,43 @@ import future.keywords.in

gasFee = (to_number(input.transactionRequest.maxFeePerGas) + to_number(input.transactionRequest.maxPriorityFeePerGas)) * to_number(input.transactionRequest.gas)

checkGasCondition(condition) {
condition == wildcard
gasFeeAmount(currency) = result {
currency == wildcard
result = gasFee
}

checkGasCondition(condition) {
gasFeeAmount(currency) = result {
currency != wildcard
result = gasFee * to_number(input.prices[currency])
}

checkGasFeeAmount(condition) {
condition != wildcard
condition.operator == "eq"
to_number(condition.value) == gasFee
to_number(condition.value) == gasFeeAmount(condition.currency)
}

checkGasCondition(condition) {
condition != wildcard
checkGasFeeAmount(condition) {
condition.operator == "neq"
to_number(condition.value) != gasFee
to_number(condition.value) != gasFeeAmount(condition.currency)
}

checkGasCondition(condition) {
condition != wildcard
checkGasFeeAmount(condition) {
condition.operator == "gt"
to_number(condition.value) < gasFee
to_number(condition.value) < gasFeeAmount(condition.currency)
}

checkGasCondition(condition) {
condition != wildcard
checkGasFeeAmount(condition) {
condition.operator == "lt"
to_number(condition.value) > gasFee
to_number(condition.value) > gasFeeAmount(condition.currency)
}

checkGasCondition(condition) {
condition != wildcard
checkGasFeeAmount(condition) {
condition.operator == "gte"
to_number(condition.value) <= gasFee
to_number(condition.value) <= gasFeeAmount(condition.currency)
}

checkGasCondition(condition) {
condition != wildcard
checkGasFeeAmount(condition) {
condition.operator == "lte"
to_number(condition.value) >= gasFee
to_number(condition.value) >= gasFeeAmount(condition.currency)
}
49 changes: 49 additions & 0 deletions apps/authz/src/opa/rego/lib/criteria/tokenAllowance.rego
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,52 @@ checkTokenAllowanceSpender(values) {
input.intent.spender != wildcard
input.intent.spender in values
}

tokenAllowanceAmount(currency) = result {
currency == wildcard
result = to_number(input.intent.amount)
}

tokenAllowanceAmount(currency) = result {
currency != wildcard
result = to_number(input.intent.amount) * to_number(input.prices[currency])
}

checkTokenAllowanceAddress(values) {
values == wildcard
}

checkTokenAllowanceAddress(values) {
values != wildcard
input.intent.token in values
}

checkTokenAllowanceAmount(condition) {
condition.operator == "eq"
to_number(condition.value) == tokenAllowanceAmount(condition.currency)
}

checkTokenAllowanceAmount(condition) {
condition.operator == "neq"
to_number(condition.value) != tokenAllowanceAmount(condition.currency)
}

checkTokenAllowanceAmount(condition) {
condition.operator == "gt"
to_number(condition.value) < tokenAllowanceAmount(condition.currency)
}

checkTokenAllowanceAmount(condition) {
condition.operator == "lt"
to_number(condition.value) > tokenAllowanceAmount(condition.currency)
}

checkTokenAllowanceAmount(condition) {
condition.operator == "gte"
to_number(condition.value) <= tokenAllowanceAmount(condition.currency)
}

checkTokenAllowanceAmount(condition) {
condition.operator == "lte"
to_number(condition.value) >= tokenAllowanceAmount(condition.currency)
}

0 comments on commit 67862e5

Please sign in to comment.