newton-testing-utils 0.1.33

newton provero testing utils
Documentation
#![allow(dead_code)]
//! Policy testing utils

/// Sample policy parsed intent data for the policy
/// Note: "from" must match FIRST_ADDRESS (anvil account 0) for executeIntent validation
pub const TEST_POLICY_PARSED_INTENT: &str = r#"
{
    "from": "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266",
    "to": "0x8f86403a4de0bb5791fa46b8e795c547942fe4cf",
    "value": "10000000000000000",
    "data": "0xcce7ec130000000000000000000000008f86403a4de0bb5791fa46b8e795c547942fe4cf0000000000000000000000000000000000000000000000000000002e90edd000",
    "chain_id": "31337",
    "function_signature": "0x66756e6374696f6e20627579286164647265737320746f6b656e2c2075696e7432353620616d6f756e7429",
    "function": {
        "type": "function",
        "name": "buy",
        "inputs": [
            {
                "name": "token",
                "type": "address"
            },
            {
                "name": "amount",
                "type": "uint256"
            }
        ],
        "outputs": [],
        "stateMutability": "nonpayable"
    },
    "decoded_function_signature": "function buy(address token, uint256 amount)",
    "decoded_function_arguments": [
        "0x8f86403a4de0bb5791fa46b8e795c547942fe4cf",
        "200000000000"
    ]
}
"#;

/// Sample policy schema for the policy params
pub const TEST_POLICY_SCHEMA: &str = r#"
{
  "type": "object",
  "additionalProperties": false,
  "required": ["admin", "allowed_actions", "token_whitelist"],
  "properties": {
    "admin": {
      "type": "string",
      "minLength": 42,
      "maxLength": 42,
      "pattern": "^0x[a-fA-F0-9]{40}$"
    },
    "allowed_actions": {
      "type": "object",
      "additionalProperties": {
        "type": "object",
        "additionalProperties": false,
        "required": ["address", "function_name", "max_limit"],
        "properties": {
          "address": {
            "type": "string",
            "minLength": 42,
            "maxLength": 42,
            "pattern": "^0x[a-fA-F0-9]{40}$"
          },
          "function_name": {
            "type": "string",
            "minLength": 1,
            "maxLength": 128
          },
          "max_limit": {
            "type": "integer",
            "minimum": 0
          }
        }
      }
    },
    "token_whitelist": {
      "type": "object",
      "additionalProperties": {
        "type": "object",
        "additionalProperties": false,
        "required": ["address", "max_limit", "symbol"],
        "properties": {
          "address": {
            "type": "string",
            "minLength": 42,
            "maxLength": 42,
            "pattern": "^0x[a-fA-F0-9]{40}$"
          },
          "max_limit": {
            "type": "integer",
            "minimum": 0
          },
          "symbol": {
            "type": "string",
            "minLength": 1,
            "maxLength": 64
          }
        }
      }
    }
  }
}
"#;

/// Sample policy data for the policy
pub const TEST_POLICY_DATA: &str = r#"
{
    "data": {
        "base_symbol": "BTC",
        "confidence": "3497946920u64",
        "exponent": -8,
        "price": "10880616000000",
        "publish_time": 1756615161,
        "quote_symbol": "USD"
    },
    "params": {
        "admin": "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266",
        "allowed_actions": {
            "31337": {
                "address": "0x8f86403a4de0bb5791fa46b8e795c547942fe4cf",
                "function_name": "buy",
                "max_limit": 1000000000000000000
            },
            "11155111": {
                "address": "0x8f86403a4de0bb5791fa46b8e795c547942fe4cf",
                "function_name": "buy",
                "max_limit": 1000000000000000000
            }
        },
        "token_whitelist": {
            "31337": {
                "address": "0x8f86403a4de0bb5791fa46b8e795c547942fe4cf",
                "max_limit": 1000000000000000000,
                "symbol": "NEWT"
            },
            "11155111": {
                "address": "0x29f2D40B0605204364af54EC677bD022dA425d03",
                "max_limit": 1000000000000000000,
                "symbol": "WBTC"
            }
        }
    }
}
"#;

/// Sample policy rego program for the policy
pub const TEST_POLICY_REGO: &str = r#"
# MockERC20 Token Buy Policy
# --------------------------------
#
# For more information see:
#
#	* Rego comparison to other systems: https://www.openpolicyagent.org/docs/latest/comparison-to-other-systems/
#	* Rego Iteration: https://www.openpolicyagent.org/docs/latest/#iteration

package mockerc20

# By default, deny requests.
default allow := false

# Allow admins to do anything.
allow if user_is_admin

# user_is_admin is true if sender is the admin
user_is_admin if {
	data.params.admin == input.from
}

# Allow the action if the user is granted permission to perform the action.
allow if {
	base_symbol == "BTC"
	price > amount_out
	quote_symbol == "USD"
	token == lower(whitelist_token.address)
	function_name == allowed_action.function_name
	contract == lower(allowed_action.address)
	allowed_action.max_limit > amount_out
}

contract := lower(input.to)
function_name := input.function.name
token := lower(input.decoded_function_arguments[0])
amount_out := to_number(input.decoded_function_arguments[1])
allowed_action := data.params.allowed_actions[input.chain_id]
whitelist_token := data.params.token_whitelist[input.chain_id]
base_symbol := data.data.base_symbol
price := to_number(data.data.price)
quote_symbol := data.data.quote_symbol
"#;

/// Sample policy rego output for the policy
pub const TEST_POLICY_REGO_OUTPUT: &str = r#"
{
  "result": [
    {
      "expressions": [
        {
          "value": {
            "data": {
              "base_symbol": "BTC",
              "confidence": "3497946920u64",
              "exponent": -8,
              "price": "10880616000000",
              "publish_time": 1756615161,
              "quote_symbol": "USD"
            },
            "example": {
              "allow": true,
              "allowed_action": {
                "address": "0x8f86403a4de0bb5791fa46b8e795c547942fe4cf",
                "function_name": "buy",
                "max_limit": 1000000000000000000
              },
              "amount_out": 200000000000,
              "base_symbol": "BTC",
              "function_name": "buy",
              "if": true,
              "price": 10880616000000,
              "quote_symbol": "USD",
              "token": "0x8f86403a4de0bb5791fa46b8e795c547942fe4cf",
              "user_is_admin": true,
              "whitelist_token": {
                "address": "0x8f86403a4de0bb5791fa46b8e795c547942fe4cf",
                "max_limit": 1000000000000000000,
                "symbol": "NEWT"
              }
            },
            "params": {
              "admin": "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266",
              "allowed_actions": {
                "11155111": {
                  "address": "0x8f86403a4de0bb5791fa46b8e795c547942fe4cf",
                  "function_name": "buy",
                  "max_limit": 1000000000000000000
                },
                "31337": {
                  "address": "0x8f86403a4de0bb5791fa46b8e795c547942fe4cf",
                  "function_name": "buy",
                  "max_limit": 1000000000000000000
                }
              },
              "token_whitelist": {
                "11155111": {
                  "address": "0x29f2D40B0605204364af54EC677bD022dA425d03",
                  "max_limit": 1000000000000000000,
                  "symbol": "WBTC"
                },
                "31337": {
                  "address": "0x8f86403a4de0bb5791fa46b8e795c547942fe4cf",
                  "max_limit": 1000000000000000000,
                  "symbol": "NEWT"
                }
              }
            }
          },
          "text": "data",
          "location": {
            "row": 1,
            "col": 1
          }
        }
      ]
    }
  ]
}
"#;

/// Sample policy wasm args for the policy
pub const TEST_POLICY_WASM_ARGS: &str = r#"
{
  "base_symbol": "BTC",
  "quote_symbol": "USD",
  "feed_id": "0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43"
}
"#;