Skip to content

Commit

Permalink
🐛 Add debug command (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
vanruch authored Jan 26, 2021
1 parent cff738e commit 7470a74
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 15 deletions.
20 changes: 14 additions & 6 deletions packages/example/deployments.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
{
"kovan": {
"dai": {
"txHash": "0xa5395bb2ad241d3a7a1813e9191d208a0b974e8eb4585c8eb346f85da27e3c2e",
"address": "0x2f9ac14463db2B3283aE99FA9158a01968585DDd"
"txHash": "0x7c058a68e08739b173f5acace0e6fa29dd95aafc5601733fef46bfe4de37ca6f",
"address": "0x0a3BDD55AbA03BE21365A164bac541845f2412AF"
},
"btc": {
"txHash": "0x6e3305488edc15414943574008b5dda95c00057432adeb1e4e0cb9f962de169e",
"address": "0xC05e6f465aaB3d42aC98C9e860275056872311C1"
"txHash": "0x795a6145e6f2fd8574cd0c56fc4f514b826cf61b467abb4c5ee805f1849edd7e",
"address": "0x28562C8c670BdA5AA186156Fff49F07C2Ff0124F"
},
"market": {
"txHash": "0x042584491c4c4323c4ff80807b2d0ebc2259772a51c4df0122a4e8c08a99d098",
"address": "0x945989f49B4Ea0756e97ef1B8fBFE81DD5c9142E"
"txHash": "0xe8bf839c398e01d19b366bf9ac2fbd07c9c142fd2b8de4ca65218c4e4604dfb4",
"address": "0x51Ef1C90C02bE44d8609EAc9A3Fe30b7B907fB9c"
},
"dai_proxy": {
"txHash": "0xe2b80e23d3a90278e0f0e623d74802c00084872e15ad5950480d2f0004c4f6fc",
"address": "0xD6c9fb590c15EA0B8FF016934488459E1578C66c"
},
"btc_proxy": {
"txHash": "0x28e6d21a3e8f224967ba3a7d09c9a1298f81f8cad450085949530826399727a9",
"address": "0x92146cB5fCC0fCA0d1A0E24f28Ebc447D73983E1"
}
}
}
4 changes: 3 additions & 1 deletion packages/example/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { contract, createProxy, deploy, runIf } from 'ethereum-mars'
import { contract, createProxy, deploy, runIf, debug } from 'ethereum-mars'
import { Market, Token, UpgradeabilityProxy } from '../build/artifacts'

deploy({}, (deployer) => {
Expand All @@ -8,6 +8,8 @@ deploy({}, (deployer) => {
const apple = proxy(appleImplementation, 'initialize', [100])
const orange = proxy(orangeImplementation, 'initialize', [200])
const market = contract(Market, [apple, orange])
debug('Apple', apple)
debug('Allowances', [apple.allowance(deployer, market), orange.allowance(deployer, market)])
runIf(apple.allowance(deployer, market).equals(0), () => apple.approve(market, 100)).else(() =>
orange.approve(market, 100)
)
Expand Down
6 changes: 6 additions & 0 deletions packages/mars/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type Action =
| EncodeAction
| StartConditionalAction
| EndConditionalAction
| DebugAction

export interface DeployAction {
type: 'DEPLOY'
Expand Down Expand Up @@ -53,3 +54,8 @@ export interface EncodeAction {
params: any[]
resolve: (value: Buffer) => void
}

export interface DebugAction {
type: 'DEBUG'
messages: any[]
}
58 changes: 51 additions & 7 deletions packages/mars/src/execute/execute.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { Action, DeployAction, EncodeAction, ReadAction, StartConditionalAction, TransactionAction } from '../actions'
import { Contract, providers, utils } from 'ethers'
import { AbiSymbol, Address, Bytecode, Name } from '../symbols'
import chalk from 'chalk'
import {
Action,
DebugAction,
DeployAction,
EncodeAction,
ReadAction,
StartConditionalAction,
TransactionAction,
} from '../actions'
import { BigNumber, Contract, providers, utils } from 'ethers'
import { AbiSymbol, Address, ArtifactSymbol, Bytecode, Name } from '../symbols'
import { Future, resolveBytesLike } from '../values'
import { getDeployTx } from './getDeployTx'
import { sendTransaction, TransactionOptions } from './sendTransaction'
Expand Down Expand Up @@ -49,6 +58,8 @@ async function executeAction(action: Action, options: ExecuteOptions) {
return executeEncode(action)
case 'CONDITIONAL_START':
return executeConditionalStart(action)
case 'DEBUG':
return executeDebug(action)
}
}

Expand Down Expand Up @@ -122,13 +133,21 @@ async function executeRead(action: ReadAction, options: ExecuteOptions) {
async function executeTransaction(action: TransactionAction, globalOptions: ExecuteOptions) {
const options = { ...globalOptions, ...action.options }
const params = action.params.map((param) => resolveValue(param))
const { txHash } = await sendTransaction(`${action.name}.${action.method.name}`, options, {
to: resolveValue(action.address),
data: new utils.Interface([action.method]).encodeFunctionData(action.method.name, params),
})
const { txHash } = await sendTransaction(
`${action.name}.${action.method.name}(${printableTransactionParams(params)})`,
options,
{
to: resolveValue(action.address),
data: new utils.Interface([action.method]).encodeFunctionData(action.method.name, params),
}
)
action.resolve(resolveBytesLike(txHash))
}

function printableTransactionParams(params: unknown[]) {
return params.map(printableToString).join(', ')
}

async function executeEncode(action: EncodeAction) {
const params = action.params.map((param) => resolveValue(param))
const result = new utils.Interface([action.method]).encodeFunctionData(action.method.name, params)
Expand All @@ -143,3 +162,28 @@ function resolveValue(value: unknown) {
}
return resolved
}

function executeDebug({ messages }: DebugAction) {
console.log(chalk.yellow('🛠', ...messages.map(printableToString)))
}

export function printableToString(data: unknown): string | number | boolean | null | undefined {
const resolved = data instanceof Future ? Future.resolve(data) : data
if (!resolved || typeof resolved !== 'object') {
return resolved
}
if (resolved instanceof BigNumber) {
return resolved.toString()
}
if (ArtifactSymbol in resolved && Address in resolved) {
return `${resolved[ArtifactSymbol][Name]}#${Future.resolve(resolved[Address])}`
}
if (Array.isArray(resolved)) {
return JSON.stringify(resolved.map(printableToString))
}
return JSON.stringify(
Object.fromEntries(Object.entries(resolved).map(([key, value]) => [key, printableToString(value)])),
null,
2
)
}
1 change: 1 addition & 0 deletions packages/mars/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export { contract } from './syntax/contract'
export { createProxy } from './syntax/createProxy'
export { createArtifact, ArtifactFrom } from './syntax/artifact'
export { runIf } from './syntax/conditionals'
export { debug } from './syntax/debug'
export * from './values'
10 changes: 10 additions & 0 deletions packages/mars/src/syntax/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { context } from '../context'

export function debug(...messages: any[]) {
context.ensureEnabled()

context.actions.push({
type: 'DEBUG',
messages,
})
}
40 changes: 40 additions & 0 deletions packages/mars/test/syntax/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { expect } from 'chai'
import { printableToString } from '../../src/execute/execute'
import { BigNumber } from 'ethers'
import { testDeploy } from '../utils/testDeploy'
import { contract, Future } from '../../src'
import { SimpleContract } from '../fixtures/exampleArtifacts'
import { Address } from '../../src/symbols'
import fs from 'fs'

describe('Debug', () => {
describe('object conversions', () => {
it('BigNumber', () => {
expect(printableToString(BigNumber.from('123'))).to.equal('123')
})

it('For contracts prints contract solidity name and address', async () => {
const deployed = await testDeploy(() => contract('name', SimpleContract))
const address = Future.resolve(deployed.result[Address])
expect(printableToString(deployed.result)).to.equal(`SimpleContract#${address}`)
fs.unlinkSync('./test/deployments.json')
})

it('Array', () => {
expect(printableToString([BigNumber.from('123'), '321'])).to.deep.equal('["123","321"]')
})

it('Object', () => {
expect(
printableToString({
foo: BigNumber.from('123'),
bar: 'bar',
})
).to.deep.equal('{\n "foo": "123",\n "bar": "bar"\n}')
})

it('Future', async () => {
expect(printableToString(new Future(() => BigNumber.from('123')))).to.equal('123')
})
})
})
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"strict": true,
"target": "ES2017",
"target": "ES2019",
"moduleResolution": "node",
"resolveJsonModule": true,
"esModuleInterop": true,
Expand Down

0 comments on commit 7470a74

Please sign in to comment.