-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
f48adb5
commit 70b596d
Showing
7 changed files
with
892 additions
and
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,26 @@ | ||
import { vocab } from '@slangroom/ethereum/tokens'; | ||
import { ethereumEndpointVocab, ethereumBalanceVocab } from '@slangroom/ethereum/tokens'; | ||
import { Lexer } from '@slangroom/deps/chevrotain'; | ||
|
||
const FsLexer = new Lexer(vocab); | ||
const EthereumEndpointLexer = new Lexer(ethereumEndpointVocab); | ||
const EthereumBalanceLexer = new Lexer(ethereumBalanceVocab); | ||
|
||
/** | ||
* Lexes the given statement for filesystems statements. | ||
* Lexes the given statement for Ethereum endpoint statements: | ||
* | ||
* Given I have a ethereum endpoint named 'x' | ||
* | ||
* @param statement is the statement ignored by Zenroom. | ||
* @returns the tokens of the lexed result. | ||
**/ | ||
export const lex = (statement: string) => FsLexer.tokenize(statement); | ||
export const lexEthereumEndpoint = (statement: string) => EthereumEndpointLexer.tokenize(statement); | ||
|
||
/** | ||
* Lexes the given statement for reading the Ethereum balance: | ||
* | ||
* Given I read the ethereum balance for 'x' | ||
* | ||
* @param statement is the statement ignored by Zenroom. | ||
* @returns the tokens of the lexed result. | ||
**/ | ||
|
||
export const lexEthereumBalance = (statement: string) => EthereumBalanceLexer.tokenize(statement); |
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 |
---|---|---|
@@ -1,26 +1,35 @@ | ||
import { visitEthereumEndpointStatement, visitEthereumReadBalanceStatement } from '@slangroom/ethereum/visitor'; | ||
import { | ||
visitEthereumEndpointStatement, | ||
visitEthereumReadBalanceStatement, | ||
} from '@slangroom/ethereum/visitor'; | ||
import { BeforePlugin } from '@slangroom/core/plugin'; | ||
import { Web3 } from 'web3' | ||
import { Web3 } from 'web3'; | ||
|
||
let web3: Web3 | null = null | ||
let web3: Web3 | null = null; | ||
|
||
export const GivenIHaveAEthereumEndpointNamed = new BeforePlugin(async ({ statement, params }) => { | ||
const endpointName = visitEthereumEndpointStatement(statement); | ||
console.log(endpointName) | ||
// TODO: if `visit()` fails, exit (return) | ||
if (!endpointName) return; | ||
|
||
const endpoint = ((params?.data || {})[endpointName] || (params?.keys || {})[endpointName] || endpointName) as string; | ||
web3 = new Web3(endpoint); | ||
const endpoint = ((params?.data || {})[endpointName] || | ||
(params?.keys || {})[endpointName] || | ||
endpointName) as string; | ||
web3 = new Web3(endpoint); | ||
}); | ||
|
||
export const GivenIReadTheEhtereumBalanceFor = new BeforePlugin(async ({ statement, params }) => { | ||
const addressName = visitEthereumReadBalanceStatement(statement); | ||
// TODO: if `visit()` fails, exit (return) | ||
if (!addressName) return; | ||
|
||
const address = ((params?.data || {})[addressName] || (params?.keys || {})[addressName] || addressName) as string; | ||
if(web3 == null) throw Error("No connection to a client") | ||
const address = ((params?.data || {})[addressName] || | ||
(params?.keys || {})[addressName] || | ||
addressName) as string; | ||
if (!web3) throw Error('No connection to a client'); | ||
const balance = await web3.eth.getBalance(address); | ||
return { ethereum_balance: balance.toString() } | ||
|
||
return { ethereum_balance: balance.toString() }; | ||
}); | ||
export const allPlugins = new Set([GivenIHaveAEthereumEndpointNamed]); | ||
|
||
export const allPlugins = new Set([ | ||
GivenIHaveAEthereumEndpointNamed, | ||
GivenIReadTheEhtereumBalanceFor, | ||
]); |
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 |
---|---|---|
@@ -1,19 +1,66 @@ | ||
import { Whitespace, Comment, Identifier, Given, I, Named, HaveA } from '@slangroom/shared'; | ||
import { | ||
Whitespace, | ||
Comment, | ||
Identifier, | ||
Given, | ||
I, | ||
Named, | ||
For, | ||
The, | ||
Have, | ||
Read, | ||
A, | ||
} from '@slangroom/shared'; | ||
import { createToken } from '@slangroom/deps/chevrotain'; | ||
|
||
/** | ||
* The "save the" statement, used to write files to the filesystems. | ||
*/ | ||
export const EthereumEndpoint = createToken({ | ||
name: 'EthereumEndpoint', | ||
pattern: /ethereum endpoint/, | ||
export const Ethereum = createToken({ | ||
name: 'Ethereum', | ||
pattern: /ethereum/i, | ||
}); | ||
|
||
export const Endpoint = createToken({ | ||
name: 'Endpoint', | ||
pattern: /endpoint/i, | ||
}); | ||
|
||
export const EthereumBalance = createToken({ | ||
name: 'EthereumBalance', | ||
pattern: /ethereum balance/, | ||
export const Balance = createToken({ | ||
name: 'Balance', | ||
pattern: /balance/i, | ||
}); | ||
|
||
/** | ||
* The vocabulary for the statements that specifies endpoints for Ethereum: | ||
* | ||
* Given I have a ethereum endpoint named 'x' | ||
*/ | ||
export const ethereumEndpointVocab = [ | ||
Whitespace, | ||
Comment, | ||
Given, | ||
I, | ||
Have, | ||
A, | ||
Ethereum, | ||
Endpoint, | ||
Named, | ||
Identifier, | ||
]; | ||
|
||
/** | ||
* Vocabulary to perform filesystems actions. | ||
* The vocabulary for statements that reads the balance of a given Ethereum | ||
* address: | ||
* | ||
* Given I read the ethereum balance for 'x' | ||
*/ | ||
export const vocab = [Whitespace, Comment, Given, I, Identifier, EthereumEndpoint, EthereumBalance, Named, HaveA]; | ||
export const ethereumBalanceVocab = [ | ||
Whitespace, | ||
Comment, | ||
Given, | ||
I, | ||
Read, | ||
The, | ||
Ethereum, | ||
Balance, | ||
For, | ||
Identifier, | ||
]; |
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 |
---|---|---|
@@ -1,41 +1,55 @@ | ||
import { BaseEthVisitor, parseEthereumReadBalanceStatement, parseEthereumEndpointStatement, | ||
type EthereumEndpointStatementCtx, type EthereumReadBalanceStatementCtx } from '@slangroom/ethereum/parser'; | ||
import { | ||
BaseEthereumEndpointParser, | ||
BaseEthereumBalanceParser, | ||
parseEthereumReadBalanceStatement, | ||
parseEthereumEndpointStatement, | ||
type EthereumEndpointStatementCtx, | ||
type EthereumReadBalanceStatementCtx, | ||
} from '@slangroom/ethereum/parser'; | ||
import type { CstNode } from '@slangroom/deps/chevrotain'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions | ||
interface Visitor { | ||
visitEthereumEndpointConnect(params: CstNode): string; | ||
visitEthereumReadBalance(params: CstNode): string; | ||
interface EthereumBalanceVisitor { | ||
visit(params: CstNode): string; | ||
} | ||
|
||
class Visitor extends BaseEthVisitor { | ||
class EthereumEndpointVisitor extends BaseEthereumEndpointParser { | ||
constructor() { | ||
super(); | ||
this.validateVisitor(); | ||
} | ||
|
||
ethereumEndpointStatement(ctx: EthereumEndpointStatementCtx) { | ||
return ctx.endpointName[0].image.slice(1, -1) | ||
return ctx.endpointName[0].image.slice(1, -1); | ||
} | ||
} | ||
|
||
export const ethereumEndpointVisitor = new EthereumEndpointVisitor(); | ||
|
||
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions | ||
interface EthereumBalanceVisitor { | ||
visit(params: CstNode): string; | ||
} | ||
|
||
class EthereumBalanceVisitor extends BaseEthereumBalanceParser { | ||
constructor() { | ||
super(); | ||
this.validateVisitor(); | ||
} | ||
|
||
ethereumReadBalanceStatement(ctx: EthereumReadBalanceStatementCtx) { | ||
return ctx.addressName[0].image.slice(1, -1) | ||
return ctx.addressName[0].image.slice(1, -1); | ||
} | ||
} | ||
|
||
export const EthVisitor = new Visitor(); | ||
export const ethereumBalanceVisitor = new EthereumBalanceVisitor(); | ||
|
||
/** | ||
* Visits the given statement for filesystems statements. | ||
* | ||
* @param statement is the statement ignored by Zenroom. | ||
* @returns the AST of the parsed CST. | ||
**/ | ||
export const visitEthereumEndpointStatement = (statement: string) => { | ||
const cst = parseEthereumEndpointStatement(statement); | ||
return EthVisitor.visitEthereumEndpointConnect(cst); | ||
return ethereumEndpointVisitor.visit(cst); | ||
}; | ||
|
||
export const visitEthereumReadBalanceStatement = (statement: string) => { | ||
const cst = parseEthereumReadBalanceStatement(statement); | ||
return EthVisitor.visitEthereumReadBalance(cst); | ||
return ethereumBalanceVisitor.visit(cst); | ||
}; |
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.