-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
594 additions
and
394 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { Conditions as WASMConditions } from '@nucypher/nucypher-core'; | ||
import { ethers } from 'ethers'; | ||
import { SemVer } from 'semver'; | ||
|
||
import { objectEquals, toJSON } from '../utils'; | ||
|
||
import { | ||
Condition, | ||
ContractCondition, | ||
RpcCondition, | ||
TimeCondition, | ||
} from './base'; | ||
import { BLOCKTIME_METHOD } from './base/time'; | ||
import { CompoundCondition } from './compound-condition'; | ||
import { ConditionContext } from './context'; | ||
|
||
export type ConditionExpressionJSON = { | ||
version: string; | ||
condition: Record<string, unknown>; | ||
}; | ||
|
||
export class ConditionExpression { | ||
static VERSION = '1.0.0'; | ||
|
||
constructor( | ||
public readonly condition: Condition, | ||
public readonly version: string = ConditionExpression.VERSION | ||
) {} | ||
|
||
public toObj(): ConditionExpressionJSON { | ||
const conditionData = this.condition.toObj(); | ||
return { | ||
version: this.version, | ||
condition: conditionData, | ||
}; | ||
} | ||
|
||
public static fromObj(obj: ConditionExpressionJSON): ConditionExpression { | ||
const receivedVersion = new SemVer(obj.version); | ||
const currentVersion = new SemVer(ConditionExpression.VERSION); | ||
if (receivedVersion.major > currentVersion.major) { | ||
throw new Error( | ||
`Version provided, ${obj.version}, is incompatible with current version, ${ConditionExpression.VERSION}` | ||
); | ||
} | ||
|
||
const underlyingConditionData = obj.condition; | ||
let condition: Condition | undefined; | ||
|
||
if (underlyingConditionData.operator) { | ||
condition = new CompoundCondition(underlyingConditionData); | ||
} else if (underlyingConditionData.method) { | ||
if (underlyingConditionData.method === BLOCKTIME_METHOD) { | ||
condition = new TimeCondition(underlyingConditionData); | ||
} else if (underlyingConditionData.contractAddress) { | ||
condition = new ContractCondition(underlyingConditionData); | ||
} else if ( | ||
(underlyingConditionData.method as string).startsWith('eth_') | ||
) { | ||
condition = new RpcCondition(underlyingConditionData); | ||
} | ||
} | ||
|
||
if (!condition) { | ||
throw new Error( | ||
`Invalid condition: unrecognized condition data ${JSON.stringify( | ||
underlyingConditionData | ||
)}` | ||
); | ||
} | ||
|
||
return new ConditionExpression(condition, obj.version); | ||
} | ||
|
||
public toJson(): string { | ||
return toJSON(this.toObj()); | ||
} | ||
|
||
public static fromJSON(json: string): ConditionExpression { | ||
return ConditionExpression.fromObj(JSON.parse(json)); | ||
} | ||
|
||
public toWASMConditions(): WASMConditions { | ||
return new WASMConditions(toJSON(this.toObj())); | ||
} | ||
|
||
public buildContext( | ||
provider: ethers.providers.Web3Provider | ||
): ConditionContext { | ||
return new ConditionContext([this.condition], provider); | ||
} | ||
|
||
public equals(other: ConditionExpression): boolean { | ||
return ( | ||
this.version === other.version && | ||
objectEquals(this.condition.toObj(), other.condition.toObj()) | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
b67cbc9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bundled size for the package is listed below:
build/main/src/kits: 19.53 KB
build/main/src/characters: 89.84 KB
build/main/src/policies: 19.53 KB
build/main/src/sdk/strategy: 35.16 KB
build/main/src/sdk: 50.78 KB
build/main/src/conditions/predefined: 19.53 KB
build/main/src/conditions/base: 54.69 KB
build/main/src/conditions/context: 42.97 KB
build/main/src/conditions: 156.25 KB
build/main/src/agents: 31.25 KB
build/main/src: 437.50 KB
build/main/types/ethers-contracts/factories: 82.03 KB
build/main/types/ethers-contracts: 156.25 KB
build/main/types: 160.16 KB
build/main/test: 46.88 KB
build/main: 699.22 KB
build/module/src/kits: 19.53 KB
build/module/src/characters: 89.84 KB
build/module/src/policies: 19.53 KB
build/module/src/sdk/strategy: 31.25 KB
build/module/src/sdk: 46.88 KB
build/module/src/conditions/predefined: 19.53 KB
build/module/src/conditions/base: 54.69 KB
build/module/src/conditions/context: 42.97 KB
build/module/src/conditions: 156.25 KB
build/module/src/agents: 31.25 KB
build/module/src: 429.69 KB
build/module/types/ethers-contracts/factories: 82.03 KB
build/module/types/ethers-contracts: 156.25 KB
build/module/types: 160.16 KB
build/module/test: 42.97 KB
build/module: 687.50 KB
build: 1.36 MB