Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add plus operation #1582

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/strategies/math/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Currently supported operations are:
| `a-if-gt-b` | 3 | (x, a, b) = x > b ? a : x |
| `a-if-gte-b` | 3 | (x, a, b) = x >= b ? a : x |
| `minus` | 2 | x - a |
| `plus` | 2 | x + a |
| `divide` | 2 | x / a |

## Examples
Expand Down
37 changes: 36 additions & 1 deletion src/strategies/math/examples.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"name": "Example simple contract call plus / minus",
"name": "Example simple contract call minus",
"strategy": {
"name": "math",
"params": {
Expand Down Expand Up @@ -34,6 +34,41 @@
],
"snapshot": 16847746
},
{
"name": "Example simple contract call plus",
"strategy": {
"name": "math",
"params": {
"symbol": "MATH",
"operands": [
{
"type": "strategy",
"strategy": {
"network": "1",
"name": "erc20-balance-of",
"params": {
"address": "0xdAC17F958D2ee523a2206206994597C13D831ec7",
"symbol": "USDT",
"decimals": 6
}
}
},
{
"type": "constant",
"value": 6
}
],
"operation": "plus"
}
},
"network": "1",
"addresses": [
"0x5C52cC7c96bDE8594e5B77D5b76d042CB5FaE5f2",
"0x4D19C0a5357bC48be0017095d3C871D9aFC3F21d",
"0xf59869753f41Db720127Ceb8DbB8afAF89030De4"
],
"snapshot": 16847746
},
{
"name": "Example nested query",
"strategy": {
Expand Down
10 changes: 10 additions & 0 deletions src/strategies/math/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ function resolveOperation(
);
return Object.fromEntries(arr);
}
case Operation.PLUS: {
return Object.fromEntries(
Object.entries(resolvedOperands[0]).map(
([address, score]: [string, number]) => [
address,
score + resolvedOperands[1][address]
]
)
);
}
case Operation.Divide: {
const arr = Object.entries(resolvedOperands[0]).map(
([address, score]: [string, number]) => [
Expand Down
3 changes: 3 additions & 0 deletions src/strategies/math/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export enum Operation {
AIfGteB = 'a-if-gte-b',
Multiply = 'multiply',
MINUS = 'minus',
PLUS = 'plus',
Divide = 'divide'
}

Expand Down Expand Up @@ -66,6 +67,7 @@ const operandCountByOperation: Record<Operation, number> = {
[Operation.AIfGtB]: 3,
[Operation.AIfGteB]: 3,
[Operation.MINUS]: 2,
[Operation.PLUS]: 2,
[Operation.Divide]: 2
};

Expand All @@ -87,6 +89,7 @@ export function validateOptions(rawOptions: OptionalOptions): Options {
rawOptions.operation !== Operation.AIfGteB &&
rawOptions.operation !== Operation.Multiply &&
rawOptions.operation !== Operation.MINUS &&
rawOptions.operation !== Operation.PLUS &&
rawOptions.operation !== Operation.Divide
) {
throw new Error('Invalid `operation`');
Expand Down