Skip to content

Commit

Permalink
ENG-0000 feat(protocol): Add protocol package versioning (#1019)
Browse files Browse the repository at this point in the history
* Add versioning in-line with the graphql package.
* Add tsup for building.
* Fix tests

---------

Co-authored-by: Simonas Karuzas <[email protected]>
  • Loading branch information
0xjojikun and simonas-notcat authored Jan 8, 2025
1 parent 4b4f7f0 commit f14951b
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 66 deletions.
11 changes: 7 additions & 4 deletions packages/protocol/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@

```typescript
import { MultiVault } from '0xIntuition/protocol'
import { base } from 'viem/chains'
import { createPublicClient, http } from 'viem'
import { base } from 'viem/chains'

const public = createPublicClient({
const publicClient = createPublicClient({
chain: base,
transport: http(),
})

const wallet = createWalletClient({
const walletClient = createWalletClient({
chain: base,
transport: custom(window.ethereum!),
})

const multiVault = new Multivault({ public, wallet })
const multiVault = new Multivault({
publicClient,
walletClient,
})

const { vaultId, events } = await multiVault.createAtom('hello')

Expand Down
45 changes: 31 additions & 14 deletions packages/protocol/package.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,52 @@
{
"name": "@0xintuition/protocol",
"version": "0.1.2",
"version": "0.1.3",
"description": "Protocol",
"main": "src/index.js",
"type": "module",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": "./src/index.js"
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.js"
}
},
"types": "src/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "tsup",
"dev": "tsup --watch",
"publish-dry": "pnpm build && pnpm publish --dry-run --no-git-checks --ignore-scripts",
"publish-next": "pnpm build && pnpm publish --tag next --no-git-checks --ignore-scripts",
"publish-latest": "pnpm build && pnpm publish --tag latest --no-git-checks --ignore-scripts",
"version:patch": "pnpm version patch --no-git-tag-version",
"version:minor": "pnpm version minor --no-git-tag-version",
"version:major": "pnpm version major --no-git-tag-version",
"version:beta": "pnpm version prerelease --preid beta --no-git-tag-version",
"lint:fix": "pnpm lint --fix",
"generate": "node ./scripts/generate.mjs"
"generate": "node ./scripts/generate.mjs",
"test": "vitest"
},
"files": [
"dist/**/*",
"src/**/*",
"README.md",
"LICENSE"
],
"repository": {
"type": "git",
"url": "https://github.com/0xIntuition/intuition-ts",
"directory": "packages/protocol"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"dependencies": {
"viem": "2.18.8"
"viem": "2.21.8"
},
"devDependencies": {
"@viem/anvil": "^0.0.10",
"typescript": "^5.4.5",
"vite": "^5.2.11",
"vitest": "^1.3.1"
"vitest": "^1.3.1",
"tsup": "^6.7.0",
"concurrently": "^8.2.2"
}
}
2 changes: 1 addition & 1 deletion packages/protocol/src/deployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { base, baseSepolia } from 'viem/chains'

const deployments: Record<number, Address> = {}

deployments[base.id] = '0x73Edf2A6Aca5AC52041D1D14deB3157A33b9Ab6d'
deployments[base.id] = '0x430BbF52503Bd4801E51182f4cB9f8F534225DE5'
deployments[baseSepolia.id] = '0x1A6950807E33d5bC9975067e6D6b5Ea4cD661665'

export { deployments }
82 changes: 40 additions & 42 deletions packages/protocol/src/multivault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,24 @@ export class Multivault {

constructor(
private client: {
public: PublicClient<Transport, Chain>
wallet: WalletClient<Transport, Chain, Account>
publicClient: PublicClient<Transport, Chain>
walletClient: WalletClient<Transport, Chain, Account>
},
address?: Address,
) {
const deployment = deployments[this.client.public.chain.id]
const deployment = deployments[this.client.publicClient.chain.id]

if (address === undefined && deployment === undefined) {
throw new Error(
`Multivault not deployed on chain: ${this.client.public.chain.id}`,
`Multivault not deployed on chain: ${this.client.publicClient.chain.id}`,
)
}
this.contract = getContract({
abi,
client,
client: {
wallet: this.client.walletClient,
public: this.client.publicClient,
},
address: address || deployment,
})
}
Expand All @@ -52,6 +55,9 @@ export class Multivault {
(err) => err instanceof ContractFunctionRevertedError,
)
if (revertError instanceof ContractFunctionRevertedError) {
if (!revertError.data?.errorName) {
throw revertError
}
const errorName = revertError.data?.errorName ?? ''
throw new Error(errorName)
}
Expand Down Expand Up @@ -222,7 +228,7 @@ export class Multivault {
* @param owner owner to get corresponding shares for
*/
public async maxRedeem(vaultId: bigint, owner?: Address) {
const address = owner || this.client.wallet.account.address
const address = owner || this.client.walletClient.account.address
return await this.contract.read.maxRedeem([address, vaultId])
}

Expand Down Expand Up @@ -446,7 +452,7 @@ export class Multivault {
try {
await this.contract.simulate.createAtom([toHex(uri)], {
value: costWithDeposit,
account: this.client.wallet.account.address,
account: this.client.walletClient.account.address,
})
} catch (e) {
this._throwRevertedError(e as BaseError)
Expand All @@ -467,9 +473,8 @@ export class Multivault {
hash: `0x${string}`
events: ParseEventLogsReturnType
}> {
const { logs, status } = await this.client.public.waitForTransactionReceipt(
{ hash },
)
const { logs, status } =
await this.client.publicClient.waitForTransactionReceipt({ hash })

if (status === 'reverted') {
throw new Error('Transaction reverted')
Expand Down Expand Up @@ -509,7 +514,7 @@ export class Multivault {
[atomUris.map((uri) => toHex(uri))],
{
value: costWithDeposit,
account: this.client.wallet.account.address,
account: this.client.walletClient.account.address,
},
)
} catch (e) {
Expand All @@ -523,9 +528,8 @@ export class Multivault {
},
)

const { logs, status } = await this.client.public.waitForTransactionReceipt(
{ hash },
)
const { logs, status } =
await this.client.publicClient.waitForTransactionReceipt({ hash })

if (status === 'reverted') {
throw new Error('Transaction reverted')
Expand Down Expand Up @@ -606,7 +610,7 @@ export class Multivault {
[subjectId, predicateId, objectId],
{
value: costWithDeposit,
account: this.client.wallet.account.address,
account: this.client.walletClient.account.address,
},
)
} catch (e) {
Expand All @@ -629,9 +633,8 @@ export class Multivault {
hash: `0x${string}`
events: ParseEventLogsReturnType
}> {
const { logs, status } = await this.client.public.waitForTransactionReceipt(
{ hash },
)
const { logs, status } =
await this.client.publicClient.waitForTransactionReceipt({ hash })

if (status === 'reverted') {
throw new Error('Transaction reverted')
Expand Down Expand Up @@ -675,7 +678,7 @@ export class Multivault {
[subjectIds, predicateIds, objectIds],
{
value: cost,
account: this.client.wallet.account.address,
account: this.client.walletClient.account.address,
},
)
} catch (e) {
Expand All @@ -687,9 +690,8 @@ export class Multivault {
{ value: cost },
)

const { logs, status } = await this.client.public.waitForTransactionReceipt(
{ hash },
)
const { logs, status } =
await this.client.publicClient.waitForTransactionReceipt({ hash })

if (status === 'reverted') {
throw new Error('Transaction reverted')
Expand Down Expand Up @@ -719,12 +721,12 @@ export class Multivault {
assets: bigint,
receiver?: Address,
) {
const address = receiver || this.client.wallet.account.address
const address = receiver || this.client.walletClient.account.address

try {
await this.contract.simulate.depositAtom([address, vaultId], {
value: assets,
account: this.client.wallet.account.address,
account: this.client.walletClient.account.address,
})
} catch (e) {
this._throwRevertedError(e as BaseError)
Expand All @@ -734,9 +736,8 @@ export class Multivault {
value: assets,
})

const { logs, status } = await this.client.public.waitForTransactionReceipt(
{ hash },
)
const { logs, status } =
await this.client.publicClient.waitForTransactionReceipt({ hash })

if (status === 'reverted') {
throw new Error('Transaction reverted')
Expand All @@ -762,11 +763,11 @@ export class Multivault {
* @returns transaction assets, transaction hash and events
*/
public async redeemAtom(vaultId: bigint, shares: bigint, receiver?: Address) {
const address = receiver || this.client.wallet.account.address
const address = receiver || this.client.walletClient.account.address

try {
await this.contract.simulate.redeemAtom([shares, address, vaultId], {
account: this.client.wallet.account.address,
account: this.client.walletClient.account.address,
})
} catch (e) {
this._throwRevertedError(e as BaseError)
Expand All @@ -778,9 +779,8 @@ export class Multivault {
vaultId,
])

const { logs, status } = await this.client.public.waitForTransactionReceipt(
{ hash },
)
const { logs, status } =
await this.client.publicClient.waitForTransactionReceipt({ hash })

if (status === 'reverted') {
throw new Error('Transaction reverted')
Expand Down Expand Up @@ -810,12 +810,12 @@ export class Multivault {
assets: bigint,
receiver?: Address,
) {
const address = receiver || this.client.wallet.account.address
const address = receiver || this.client.walletClient.account.address

try {
await this.contract.simulate.depositTriple([address, vaultId], {
value: assets,
account: this.client.wallet.account.address,
account: this.client.walletClient.account.address,
})
} catch (e) {
this._throwRevertedError(e as BaseError)
Expand All @@ -825,9 +825,8 @@ export class Multivault {
value: assets,
})

const { logs, status } = await this.client.public.waitForTransactionReceipt(
{ hash },
)
const { logs, status } =
await this.client.publicClient.waitForTransactionReceipt({ hash })

if (status === 'reverted') {
throw new Error('Transaction reverted')
Expand Down Expand Up @@ -857,11 +856,11 @@ export class Multivault {
shares: bigint,
receiver?: Address,
) {
const address = receiver || this.client.wallet.account.address
const address = receiver || this.client.walletClient.account.address

try {
await this.contract.simulate.redeemTriple([shares, address, vaultId], {
account: this.client.wallet.account.address,
account: this.client.walletClient.account.address,
})
} catch (e) {
this._throwRevertedError(e as BaseError)
Expand All @@ -873,9 +872,8 @@ export class Multivault {
vaultId,
])

const { logs, status } = await this.client.public.waitForTransactionReceipt(
{ hash },
)
const { logs, status } =
await this.client.publicClient.waitForTransactionReceipt({ hash })

if (status === 'reverted') {
throw new Error('Transaction reverted')
Expand Down
4 changes: 2 additions & 2 deletions packages/protocol/tests/multivault.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ beforeAll(async () => {
address = await deployAndInit()
multiVault = new Multivault(
{
public: publicClient,
wallet: walletClient,
publicClient,
walletClient,
},
address,
)
Expand Down
10 changes: 10 additions & 0 deletions packages/protocol/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from 'tsup'

export default defineConfig({
entry: ['src/index.ts'],
format: ['cjs', 'esm'],
dts: true,
splitting: false,
sourcemap: true,
clean: true,
})
Loading

0 comments on commit f14951b

Please sign in to comment.