Skip to content

Commit

Permalink
CLI improvements (#3)
Browse files Browse the repository at this point in the history
* Improve CLI

* Redo readme
  • Loading branch information
krzkaczor authored Jul 25, 2024
1 parent fe14716 commit c8c5894
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 14 deletions.
37 changes: 32 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
# Spark Spells GitHub Action
# Spell Caster

Execute a spell on a persisted, forked network with a shareable link.
Execute a [Spark spell](https://github.com/marsfoundation/spark-spells) on a forked network with a shareable links.

## Running
Spell Caster can be run as CLI (for local testing) or configured as GitHub Action.

## Running as CLI

```bash
bun install # only first time
bun src/index.ts SparkEthereum_20240627
# fill out .env based on .env.example

#bun src/index.ts --root <spark-spells-root-path> <spell-name>
bun src/index.ts --root ../spark-spell SparkEthereum_20240627
```

## Running as Github Action

Presents results as GitHub PR comment.

```yml
- name: Spell Caster
uses: marsfoundation/spell-caster@action
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
TENDERLY_API_KEY: ${{ secrets.TENDERLY_ACCESS_KEY }}
TENDERLY_PROJECT: ${{ secrets.TENDERLY_PROJECT }}
TENDERLY_ACCOUNT: ${{ secrets.TENDERLY_USER }}
```
## Developing
```sh
bun fix # to run linter, tests and typecheck
```
```

### Bun support

GitHub Runners don't support bun as runner environment so we use bun for testing and bundling but not as runtime dependency.

### GH Action

New release of github action (pushed to `action`) branch is done after each commit to `main` branch.
2 changes: 1 addition & 1 deletion src/bin/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { getRequiredGithubInput } from '../config/environments/action'
import { findPendingSpells } from '../spells/findPendingSpells'

async function main(): Promise<void> {
const config = getConfig(getRequiredGithubInput)
const config = getConfig(getRequiredGithubInput, process.cwd())

const allPendingSpellNames = findPendingSpells(process.cwd())
core.info(`Pending spells: ${allPendingSpellNames.join(', ')}`)
Expand Down
22 changes: 18 additions & 4 deletions src/bin/cli.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import assert from 'node:assert'
import { parseArgs } from 'node:util'
import { forkAndExecuteSpell } from '..'
import { getConfig } from '../config'
import { getRequiredShellEnv } from '../config/environments/cli'
import { ensureAbsolutePath } from '../utils/fs'

async function main() {
const args = parseArgs({
options: {
root: {
type: 'string',
},
},
allowPositionals: true,
strict: true,
})
const rootPath = args.values.root
const spellName = args.positionals[0]

async function main(spellName?: string) {
assert(spellName, 'Pass spell name as an argument ex. SparkEthereum_20240627')
assert(rootPath, 'Pass root path as an argument ex. --root /path/to/spark-spells')

const config = getConfig(getRequiredShellEnv)
const config = getConfig(getRequiredShellEnv, ensureAbsolutePath(rootPath))

console.log(`Executing spell ${spellName}`)
const { forkRpc, appUrl } = await forkAndExecuteSpell(spellName, config)
Expand All @@ -15,5 +30,4 @@ async function main(spellName?: string) {
console.log(`Spark App URL: ${appUrl}`)
}

const arg1 = process.argv[2]
await main(arg1)
await main()
4 changes: 3 additions & 1 deletion src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface Config {
tenderly: TenderlyConfig
networks: Record<string, NetworkConfig>
deployer: Address
spellsRepoPath: string
}

export interface NetworkConfig {
Expand All @@ -19,7 +20,7 @@ export interface TenderlyConfig {
apiKey: string
}

export function getConfig(getEnvVariable: (key: string) => string): Config {
export function getConfig(getEnvVariable: (key: string) => string, spellsRepoPath: string): Config {
return {
tenderly: {
account: getEnvVariable('TENDERLY_ACCOUNT'),
Expand All @@ -39,5 +40,6 @@ export function getConfig(getEnvVariable: (key: string) => string): Config {
},
},
deployer: zeroAddress,
spellsRepoPath,
}
}
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ export async function forkAndExecuteSpell(spellName: string, config: Config): Pr
})
const ethereumClient = new EthereumClient(rpc.adminRpcUrl, forkChainId, config.deployer)

const spellAddress = await deployContract(spellName, rpc.adminRpcUrl, config.deployer)
const spellAddress = await deployContract({
contractName: spellName,
rpc: rpc.adminRpcUrl,
from: config.deployer,
cwd: config.spellsRepoPath,
})

await executeSpell({ spellAddress, network: chain, ethereumClient })

Expand Down
9 changes: 7 additions & 2 deletions src/periphery/forge/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { $ } from 'dax-sh'
import { Address } from 'viem'

export async function deployContract(contractName: string, rpc: string, from: Address): Promise<Address> {
const result = await $`forge create --rpc-url ${rpc} --from ${from} ${contractName} --unlocked --json`.json()
export async function deployContract({
contractName,
rpc,
from,
cwd,
}: { contractName: string; rpc: string; from: Address; cwd: string }): Promise<Address> {
const result = await $`forge create --rpc-url ${rpc} --from ${from} ${contractName} --unlocked --json`.cwd(cwd).json()
return result.deployedTo
}
5 changes: 5 additions & 0 deletions src/utils/fs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as path from 'node:path'

export function ensureAbsolutePath(p: string): string {
return path.resolve(p)
}

0 comments on commit c8c5894

Please sign in to comment.