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(contract): Add address utility to contract #828

Merged
merged 4 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
31 changes: 31 additions & 0 deletions .changeset/neat-sloths-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
"@tevm/contract": major
---

- Changed name of `TevmContract` to `Contract`
- Changed name of `createTevmContract` to `createContract`
- Added `Script` to be a Contract with bytecode
- Removed bytecode from `Contract`
- Added a new `withAddress` method for adding an address to a contract
- Removed need to explicitly pass in undefined for optional params

## withAddress

Before we had to spred contracts like this:

```typescript
client.readContract({
address: contractAddress,
...Erc20Contract.read.balanceOf(userAddress)
})
```

Now Tevm can create a contract with an address attatched


```typescript
client.readContract(
Erc20Contract.withAddress(contractAddress).read.balanceOf(userAddress)
)
```

6 changes: 3 additions & 3 deletions bundler/base/TestContract.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TevmContract } from '@tevm/contract'
import { Contract } from '@tevm/contract'
const _abiTestContract = [
{
constant: true,
Expand Down Expand Up @@ -119,10 +119,10 @@ const _abiTestContract = [
const _chainAddressMapTestContract = { '10': '0x123' } as const
const _nameTestContract = 'TestContract' as const
/**
* TestContract TevmContract
* TestContract Contract
* @etherscan-10 https://optimistic.etherscan.io/address/0x123
*/
export const TestContract: TevmContract<
export const TestContract: Contract<
typeof _nameTestContract,
typeof _chainAddressMapTestContract,
typeof _abiTestContract
Expand Down
36 changes: 18 additions & 18 deletions bundler/base/src/bundler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,13 @@ describe(bundler.name, () => {
"evmVersion": "homestead",
},
},
"code": "import { TevmContract } from '@tevm/contract'
"code": "import { Contract } from '@tevm/contract'
const _abiTestContract = [] as const;
const _nameTestContract = \\"TestContract\\" as const;
/**
* TestContract TevmContract
* TestContract Contract
*/
export const TestContract: TevmContract<typeof _nameTestContract, typeof _abiTestContract, undefined, undefined>;",
export const TestContract: Contract<typeof _nameTestContract, typeof _abiTestContract>;",
"modules": {
"module1": {
"code": "import { TestContract } from 'module2'
Expand Down Expand Up @@ -384,13 +384,13 @@ describe(bundler.name, () => {
"evmVersion": "homestead",
},
},
"code": "import { TevmContract } from '@tevm/contract'
"code": "import { Contract } from '@tevm/contract'
const _abiTestContract = [] as const;
const _nameTestContract = \\"TestContract\\" as const;
/**
* TestContract TevmContract
* TestContract Contract
*/
export const TestContract: TevmContract<typeof _nameTestContract, typeof _abiTestContract, undefined, undefined>;",
export const TestContract: Contract<typeof _nameTestContract, typeof _abiTestContract>;",
"modules": {
"module1": {
"code": "import { TestContract } from 'module2'
Expand Down Expand Up @@ -478,9 +478,9 @@ describe(bundler.name, () => {
"evmVersion": "homestead",
},
},
"code": "import { createTevmContract } from '@tevm/contract'
"code": "import { createContract } from '@tevm/contract'
const _TestContract = {\\"name\\":\\"TestContract\\",\\"humanReadableAbi\\":[]} as const
export const TestContract = createTevmContract(_TestContract)",
export const TestContract = createContract(_TestContract)",
"modules": {
"module1": {
"code": "import { TestContract } from 'module2'
Expand Down Expand Up @@ -568,9 +568,9 @@ describe(bundler.name, () => {
"evmVersion": "homestead",
},
},
"code": "import { createTevmContract } from '@tevm/contract'
"code": "import { createContract } from '@tevm/contract'
const _TestContract = {\\"name\\":\\"TestContract\\",\\"humanReadableAbi\\":[]} as const
export const TestContract = createTevmContract(_TestContract)",
export const TestContract = createContract(_TestContract)",
"modules": {
"module1": {
"code": "import { TestContract } from 'module2'
Expand Down Expand Up @@ -658,9 +658,9 @@ describe(bundler.name, () => {
"evmVersion": "homestead",
},
},
"code": "const { createTevmContract } = require('@tevm/contract')
"code": "const { createContract } = require('@tevm/contract')
const _TestContract = {\\"name\\":\\"TestContract\\",\\"humanReadableAbi\\":[]}
module.exports.TestContract = createTevmContract(_TestContract)",
module.exports.TestContract = createContract(_TestContract)",
"modules": {
"module1": {
"code": "import { TestContract } from 'module2'
Expand Down Expand Up @@ -748,9 +748,9 @@ describe(bundler.name, () => {
"evmVersion": "homestead",
},
},
"code": "const { createTevmContract } = require('@tevm/contract')
"code": "const { createContract } = require('@tevm/contract')
const _TestContract = {\\"name\\":\\"TestContract\\",\\"humanReadableAbi\\":[]}
module.exports.TestContract = createTevmContract(_TestContract)",
module.exports.TestContract = createContract(_TestContract)",
"modules": {
"module1": {
"code": "import { TestContract } from 'module2'
Expand Down Expand Up @@ -838,9 +838,9 @@ describe(bundler.name, () => {
"evmVersion": "homestead",
},
},
"code": "import { createTevmContract } from '@tevm/contract'
"code": "import { createContract } from '@tevm/contract'
const _TestContract = {\\"name\\":\\"TestContract\\",\\"humanReadableAbi\\":[]}
export const TestContract = createTevmContract(_TestContract)",
export const TestContract = createContract(_TestContract)",
"modules": {
"module1": {
"code": "import { TestContract } from 'module2'
Expand Down Expand Up @@ -928,9 +928,9 @@ describe(bundler.name, () => {
"evmVersion": "homestead",
},
},
"code": "import { createTevmContract } from '@tevm/contract'
"code": "import { createContract } from '@tevm/contract'
const _TestContract = {\\"name\\":\\"TestContract\\",\\"humanReadableAbi\\":[]}
export const TestContract = createTevmContract(_TestContract)",
export const TestContract = createContract(_TestContract)",
"modules": {
"module1": {
"code": "import { TestContract } from 'module2'
Expand Down
10 changes: 5 additions & 5 deletions bundler/bun/docs/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

### Functions

- [tevmBunPlugin](undefined)
- [tevmBunPlugin](modules.md#tevmbunplugin)

## Functions

### tevmBunPlugin

▸ **tevmBunPlugin**(`SolcVersions`): BunPlugin
▸ **tevmBunPlugin**(`SolcVersions`): `BunPlugin`

@tevm/bun-plugin is a bun plugin that allows you to import solidity files into your typescript files
and have them compiled to typescript on the fly.
Expand All @@ -21,12 +21,12 @@ and have them compiled to typescript on the fly.

| Name | Type |
| :------ | :------ |
| `SolcVersions` | Object |
| `SolcVersions.solc?` | SolcVersions$1 |
| `SolcVersions` | `Object` |
| `SolcVersions.solc?` | `SolcVersions$1` |

#### Returns

BunPlugin
`BunPlugin`

**`Example`**

Expand Down
6 changes: 3 additions & 3 deletions bundler/bundler-cache/TestContract.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TevmContract } from '@tevm/contract'
import { Contract } from '@tevm/contract'
const _abiTestContract = [
{
constant: true,
Expand Down Expand Up @@ -119,10 +119,10 @@ const _abiTestContract = [
const _chainAddressMapTestContract = { '10': '0x123' } as const
const _nameTestContract = 'TestContract' as const
/**
* TestContract TevmContract
* TestContract Contract
* @etherscan-10 https://optimistic.etherscan.io/address/0x123
*/
export const TestContract: TevmContract<
export const TestContract: Contract<
typeof _nameTestContract,
typeof _chainAddressMapTestContract,
typeof _abiTestContract
Expand Down
6 changes: 3 additions & 3 deletions bundler/compiler/TestContract.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TevmContract } from '@tevm/contract'
import { Contract } from '@tevm/contract'
const _abiTestContract = [
{
constant: true,
Expand Down Expand Up @@ -119,10 +119,10 @@ const _abiTestContract = [
const _chainAddressMapTestContract = { '10': '0x123' } as const
const _nameTestContract = 'TestContract' as const
/**
* TestContract TevmContract
* TestContract Contract
* @etherscan-10 https://optimistic.etherscan.io/address/0x123
*/
export const TestContract: TevmContract<
export const TestContract: Contract<
typeof _nameTestContract,
typeof _chainAddressMapTestContract,
typeof _abiTestContract
Expand Down
6 changes: 3 additions & 3 deletions bundler/resolutions/TestContract.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TevmContract } from '@tevm/contract'
import { Contract } from '@tevm/contract'
const _abiTestContract = [
{
constant: true,
Expand Down Expand Up @@ -119,10 +119,10 @@ const _abiTestContract = [
const _chainAddressMapTestContract = { '10': '0x123' } as const
const _nameTestContract = 'TestContract' as const
/**
* TestContract TevmContract
* TestContract Contract
* @etherscan-10 https://optimistic.etherscan.io/address/0x123
*/
export const TestContract: TevmContract<
export const TestContract: Contract<
typeof _nameTestContract,
typeof _chainAddressMapTestContract,
typeof _abiTestContract
Expand Down
18 changes: 9 additions & 9 deletions bundler/runtime/docs/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

### Type Aliases

- [ModuleType](undefined)
- [ModuleType](modules.md#moduletype)

### Functions

- [generateRuntime](undefined)
- [generateRuntime](modules.md#generateruntime)

## Type Aliases

### ModuleType

Ƭ **ModuleType**: "cjs" \| "dts" \| "ts" \| "mjs"
Ƭ **ModuleType**: ``"cjs"`` \| ``"dts"`` \| ``"ts"`` \| ``"mjs"``

#### Defined in

Expand All @@ -26,20 +26,20 @@

### generateRuntime

▸ **generateRuntime**(`artifacts`, `moduleType`, `includeBytecode`): Effect\<never, never, string\>
▸ **generateRuntime**(`artifacts`, `moduleType`, `includeBytecode`): `Effect`\<`never`, `never`, `string`\>

#### Parameters

| Name | Type |
| :------ | :------ |
| `artifacts` | Artifacts$1 |
| `moduleType` | ModuleType |
| `includeBytecode` | boolean |
| `artifacts` | `Artifacts$1` |
| `moduleType` | [`ModuleType`](modules.md#moduletype) |
| `includeBytecode` | `boolean` |

#### Returns

Effect\<never, never, string\>
`Effect`\<`never`, `never`, `string`\>

#### Defined in

[generateRuntime.js:17](https://github.com/evmts/tevm-monorepo/blob/main/bundler/runtime/src/generateRuntime.js#L17)
[generateRuntime.js:25](https://github.com/evmts/tevm-monorepo/blob/main/bundler/runtime/src/generateRuntime.js#L25)
19 changes: 14 additions & 5 deletions bundler/runtime/src/generateRuntime.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ import { generateTevmBody } from './generateTevmBody.js'
import { die, map } from 'effect/Effect'

const importsByModuleType = {
cjs: `const { createTevmContract } = require('@tevm/contract')`,
dts: `import { TevmContract } from '@tevm/contract'`,
ts: `import { createTevmContract } from '@tevm/contract'`,
mjs: `import { createTevmContract } from '@tevm/contract'`,
contract: {
cjs: `const { createContract } = require('@tevm/contract')`,
dts: `import { Contract } from '@tevm/contract'`,
ts: `import { createContract } from '@tevm/contract'`,
mjs: `import { createContract } from '@tevm/contract'`,
},
script: {
cjs: `const { createScript } = require('@tevm/contract')`,
dts: `import { Script } from '@tevm/contract'`,
ts: `import { createScript } from '@tevm/contract'`,
mjs: `import { createScript } from '@tevm/contract'`,
},
}

/**
Expand All @@ -18,7 +26,8 @@ export const generateRuntime = (artifacts, moduleType, includeBytecode) => {
if (!artifacts || Object.keys(artifacts).length === 0) {
return die('No artifacts provided to generateRuntime')
}
const imports = importsByModuleType[moduleType]
const imports =
importsByModuleType[includeBytecode ? 'script' : 'contract'][moduleType]
if (!imports) {
return die(
`Unknown module type: ${moduleType}. Valid module types include ${Object.keys(
Expand Down
20 changes: 10 additions & 10 deletions bundler/runtime/src/generateRuntime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('generateRuntime', () => {
expect(() =>
runSync(generateRuntime(artifacts, 'invalidType' as any, false)),
).toThrowErrorMatchingInlineSnapshot(
'"Unknown module type: invalidType. Valid module types include cjs, dts, ts, mjs"',
'"Unknown module type: invalidType. Valid module types include contract, script"',
)
})

Expand All @@ -48,54 +48,54 @@ describe('generateRuntime', () => {
it('should handle commonjs module type', () => {
const result = runSync(generateRuntime(artifacts, 'cjs', false))
expect(result).toMatchInlineSnapshot(`
"const { createTevmContract } = require('@tevm/contract')
"const { createContract } = require('@tevm/contract')
const _MyContract = {\\"name\\":\\"MyContract\\",\\"humanReadableAbi\\":[\\"constructor() payable\\"]}
/**
* MyContract
* @property balanceOf(address) Returns the amount of tokens owned by account
*/
module.exports.MyContract = createTevmContract(_MyContract)"
module.exports.MyContract = createContract(_MyContract)"
`)
})

it('should handle dts module type', () => {
const result = runSync(generateRuntime(artifacts, 'dts', false))
expect(result).toMatchInlineSnapshot(`
"import { TevmContract } from '@tevm/contract'
"import { Contract } from '@tevm/contract'
const _abiMyContract = [\\"constructor() payable\\"] as const;
const _nameMyContract = \\"MyContract\\" as const;
/**
* MyContract TevmContract
* MyContract Contract
* @notice MyContract
* @property balanceOf(address) Returns the amount of tokens owned by account
*/
export const MyContract: TevmContract<typeof _nameMyContract, typeof _abiMyContract, undefined, undefined>;"
export const MyContract: Contract<typeof _nameMyContract, typeof _abiMyContract>;"
`)
})

it('should handle ts module type', () => {
const result = runSync(generateRuntime(artifacts, 'ts', false))
expect(result).toMatchInlineSnapshot(`
"import { createTevmContract } from '@tevm/contract'
"import { createContract } from '@tevm/contract'
const _MyContract = {\\"name\\":\\"MyContract\\",\\"humanReadableAbi\\":[\\"constructor() payable\\"]} as const
/**
* MyContract
* @property balanceOf(address) Returns the amount of tokens owned by account
*/
export const MyContract = createTevmContract(_MyContract)"
export const MyContract = createContract(_MyContract)"
`)
})

it('should handle mjs module type', () => {
const result = runSync(generateRuntime(artifacts, 'mjs', false))
expect(result).toMatchInlineSnapshot(`
"import { createTevmContract } from '@tevm/contract'
"import { createContract } from '@tevm/contract'
const _MyContract = {\\"name\\":\\"MyContract\\",\\"humanReadableAbi\\":[\\"constructor() payable\\"]}
/**
* MyContract
* @property balanceOf(address) Returns the amount of tokens owned by account
*/
export const MyContract = createTevmContract(_MyContract)"
export const MyContract = createContract(_MyContract)"
`)
})
})
Loading
Loading