Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@wagmi/cli

Package Overview
Dependencies
Maintainers
2
Versions
159
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wagmi/cli - npm Package Compare versions

Comparing version 2.0.0-beta.8 to 2.0.0-beta.9

dist/esm/plugins/actions.js

1

dist/esm/exports/plugins.js

@@ -0,1 +1,2 @@

export { actions } from '../plugins/actions.js';
export { blockExplorer, } from '../plugins/blockExplorer.js';

@@ -2,0 +3,0 @@ export { etherscan } from '../plugins/etherscan.js';

import { pascalCase } from 'change-case';
import {} from '../config.js';
import {} from '../types.js';
export function react(_config = {}) {
import { getAddressDocString } from '../utils/getAddressDocString.js';
export function react(config = {}) {
return {

@@ -11,2 +12,3 @@ name: 'React',

const pure = '/*#__PURE__*/';
const hookNames = new Set();
for (const contract of contracts) {

@@ -16,28 +18,133 @@ let hasReadFunction = false;

let hasEvent = false;
for (const component of contract.abi) {
if (component.type === 'function')
if (component.stateMutability === 'view' ||
component.stateMutability === 'pure')
const readItems = [];
const writeItems = [];
const eventItems = [];
for (const item of contract.abi) {
if (item.type === 'function')
if (item.stateMutability === 'view' ||
item.stateMutability === 'pure') {
hasReadFunction = true;
else
readItems.push(item);
}
else {
hasWriteFunction = true;
else if (component.type === 'event')
writeItems.push(item);
}
else if (item.type === 'event') {
hasEvent = true;
// Exit early if all flags are `true`
if (hasReadFunction && hasWriteFunction && hasEvent)
break;
eventItems.push(item);
}
}
let innerContent;
if (contract.meta.addressName)
innerContent = `{ abi: ${contract.meta.abiName}, address: ${contract.meta.addressName} }`;
innerContent = `abi: ${contract.meta.abiName}, address: ${contract.meta.addressName}`;
else
innerContent = `{ abi: ${contract.meta.abiName} }`;
innerContent = `abi: ${contract.meta.abiName}`;
if (hasReadFunction) {
imports.add('createReadContract');
content.push(`export const useRead${pascalCase(contract.name)} = ${pure} createReadContract(${innerContent})`);
const hookName = getHookName(config, hookNames, 'read', contract.name);
const docString = genDocString('useReadContract', contract);
const functionName = 'createUseReadContract';
imports.add(functionName);
content.push(`${docString}
export const ${hookName} = ${pure} ${functionName}({ ${innerContent} })`);
const names = new Set();
for (const item of readItems) {
if (item.type !== 'function')
continue;
if (item.stateMutability !== 'pure' &&
item.stateMutability !== 'view')
continue;
// Skip overrides since they are captured by same hook
if (names.has(item.name))
continue;
names.add(item.name);
const hookName = getHookName(config, hookNames, 'read', contract.name, item.name);
const docString = genDocString('useReadContract', contract, {
name: 'functionName',
value: item.name,
});
content.push(`${docString}
export const ${hookName} = ${pure} ${functionName}({ ${innerContent}, functionName: '${item.name}' })`);
}
}
if (hasWriteFunction) {
imports.add('createWriteContract');
content.push(`export const useWrite${pascalCase(contract.name)} = ${pure} createWriteContract(${innerContent})`);
{
const hookName = getHookName(config, hookNames, 'write', contract.name);
const docString = genDocString('useWriteContract', contract);
const functionName = 'createUseWriteContract';
imports.add(functionName);
content.push(`${docString}
export const ${hookName} = ${pure} ${functionName}({ ${innerContent} })`);
const names = new Set();
for (const item of writeItems) {
if (item.type !== 'function')
continue;
if (item.stateMutability !== 'nonpayable' &&
item.stateMutability !== 'payable')
continue;
// Skip overrides since they are captured by same hook
if (names.has(item.name))
continue;
names.add(item.name);
const hookName = getHookName(config, hookNames, 'write', contract.name, item.name);
const docString = genDocString('useWriteContract', contract, {
name: 'functionName',
value: item.name,
});
content.push(`${docString}
export const ${hookName} = ${pure} ${functionName}({ ${innerContent}, functionName: '${item.name}' })`);
}
}
{
const hookName = getHookName(config, hookNames, 'simulate', contract.name);
const docString = genDocString('useSimulateContract', contract);
const functionName = 'createUseSimulateContract';
imports.add(functionName);
content.push(`${docString}
export const ${hookName} = ${pure} ${functionName}({ ${innerContent} })`);
const names = new Set();
for (const item of writeItems) {
if (item.type !== 'function')
continue;
if (item.stateMutability !== 'nonpayable' &&
item.stateMutability !== 'payable')
continue;
// Skip overrides since they are captured by same hook
if (names.has(item.name))
continue;
names.add(item.name);
const hookName = getHookName(config, hookNames, 'simulate', contract.name, item.name);
const docString = genDocString('useSimulateContract', contract, {
name: 'functionName',
value: item.name,
});
content.push(`${docString}
export const ${hookName} = ${pure} ${functionName}({ ${innerContent}, functionName: '${item.name}' })`);
}
}
}
if (hasEvent) {
const hookName = getHookName(config, hookNames, 'watch', contract.name);
const docString = genDocString('useWatchContractEvent', contract);
const functionName = 'createUseWatchContractEvent';
imports.add(functionName);
content.push(`${docString}
export const ${hookName} = ${pure} ${functionName}({ ${innerContent} })`);
const names = new Set();
for (const item of eventItems) {
if (item.type !== 'event')
continue;
// Skip overrides since they are captured by same hook
if (names.has(item.name))
continue;
names.add(item.name);
const hookName = getHookName(config, hookNames, 'watch', contract.name, item.name);
const docString = genDocString('useWatchContractEvent', contract, {
name: 'eventName',
value: item.name,
});
content.push(`${docString}
export const ${hookName} = ${pure} ${functionName}({ ${innerContent}, functionName: '${item.name}' })`);
}
}
}

@@ -54,2 +161,53 @@ const importValues = [...imports.values()];

}
function genDocString(hookName, contract, item) {
let description = `Wraps __{@link ${hookName}}__ with \`abi\` set to __{@link ${contract.meta.abiName}}__`;
if (item)
description += ` and \`${item.name}\` set to \`"${item.value}"\``;
const docString = getAddressDocString({ address: contract.address });
if (docString)
return `/**
* ${description}
*
${docString}
*/`;
return `/**
* ${description}
*/`;
}
function getHookName(config, hookNames, type, contractName, itemName) {
const ContractName = pascalCase(contractName);
const ItemName = itemName ? pascalCase(itemName) : undefined;
let hookName;
if (typeof config.getHookName === 'function')
hookName = config.getHookName({
type,
contractName: ContractName,
itemName: ItemName,
});
else if (typeof config.getHookName === 'string') {
switch (type) {
case 'read':
hookName = `use${ContractName}${ItemName ?? 'Read'}`;
break;
case 'simulate':
hookName = `usePrepare${ContractName}${ItemName ?? 'Write'}`;
break;
case 'watch':
hookName = `use${ContractName}${ItemName ?? ''}Event`;
break;
case 'write':
hookName = `use${ContractName}${ItemName ?? 'Write'}`;
break;
}
}
else {
hookName = `use${pascalCase(type)}${ContractName}${ItemName ?? ''}`;
if (type === 'watch')
hookName = `${hookName}Event`;
}
if (hookNames.has(hookName))
throw new Error(`Hook name "${hookName}" must be unique for contract "${contractName}". Try using \`getHookName\` to create a unique name.`);
hookNames.add(hookName);
return hookName;
}
//# sourceMappingURL=react.js.map

2

dist/esm/version.js

@@ -1,2 +0,2 @@

export const version = '2.0.0-beta.8';
export const version = '2.0.0-beta.9';
//# sourceMappingURL=version.js.map

@@ -0,1 +1,2 @@

export { actions, type ActionsConfig } from '../plugins/actions.js';
export { blockExplorer, type BlockExplorerConfig, } from '../plugins/blockExplorer.js';

@@ -2,0 +3,0 @@ export { etherscan, type EtherscanConfig } from '../plugins/etherscan.js';

import { type Plugin } from '../config.js';
import { type Evaluate, type RequiredBy } from '../types.js';
export type ReactConfig = {
foo?: string | undefined;
getHookName?: 'legacy' | ((options: {
contractName: string;
itemName?: string | undefined;
type: 'read' | 'simulate' | 'watch' | 'write';
}) => `use${string}`);
};
type ReactResult = Evaluate<RequiredBy<Plugin, 'run'>>;
export declare function react(_config?: ReactConfig): ReactResult;
export declare function react(config?: ReactConfig): ReactResult;
export {};
//# sourceMappingURL=react.d.ts.map

@@ -1,2 +0,2 @@

export declare const version = "2.0.0-beta.8";
export declare const version = "2.0.0-beta.9";
//# sourceMappingURL=version.d.ts.map
{
"name": "@wagmi/cli",
"description": "Manage and generate code from Ethereum ABIs",
"version": "2.0.0-beta.8",
"version": "2.0.0-beta.9",
"license": "MIT",

@@ -79,3 +79,3 @@ "repository": {

"prettier": "^3.0.3",
"viem": "2.0.0-beta.15",
"viem": "2.0.0-beta.16",
"zod": "^3.22.2"

@@ -82,0 +82,0 @@ },

@@ -0,1 +1,3 @@

export { actions, type ActionsConfig } from '../plugins/actions.js'
export {

@@ -2,0 +4,0 @@ blockExplorer,

import { pascalCase } from 'change-case'
import { type Plugin } from '../config.js'
import { type Contract, type Plugin } from '../config.js'
import { type Evaluate, type RequiredBy } from '../types.js'
import { getAddressDocString } from '../utils/getAddressDocString.js'
export type ReactConfig = { foo?: string | undefined }
export type ReactConfig = {
getHookName?:
| 'legacy' // TODO: Deprecate `'legacy'` option
| ((options: {
contractName: string
itemName?: string | undefined
type: 'read' | 'simulate' | 'watch' | 'write'
}) => `use${string}`)
}
type ReactResult = Evaluate<RequiredBy<Plugin, 'run'>>
export function react(_config: ReactConfig = {}): ReactResult {
export function react(config: ReactConfig = {}): ReactResult {
return {

@@ -18,2 +27,3 @@ name: 'React',

const hookNames = new Set<string>()
for (const contract of contracts) {

@@ -23,13 +33,21 @@ let hasReadFunction = false

let hasEvent = false
for (const component of contract.abi) {
if (component.type === 'function')
const readItems = []
const writeItems = []
const eventItems = []
for (const item of contract.abi) {
if (item.type === 'function')
if (
component.stateMutability === 'view' ||
component.stateMutability === 'pure'
)
item.stateMutability === 'view' ||
item.stateMutability === 'pure'
) {
hasReadFunction = true
else hasWriteFunction = true
else if (component.type === 'event') hasEvent = true
// Exit early if all flags are `true`
if (hasReadFunction && hasWriteFunction && hasEvent) break
readItems.push(item)
} else {
hasWriteFunction = true
writeItems.push(item)
}
else if (item.type === 'event') {
hasEvent = true
eventItems.push(item)
}
}

@@ -39,21 +57,179 @@

if (contract.meta.addressName)
innerContent = `{ abi: ${contract.meta.abiName}, address: ${contract.meta.addressName} }`
else innerContent = `{ abi: ${contract.meta.abiName} }`
innerContent = `abi: ${contract.meta.abiName}, address: ${contract.meta.addressName}`
else innerContent = `abi: ${contract.meta.abiName}`
if (hasReadFunction) {
imports.add('createReadContract')
const hookName = getHookName(config, hookNames, 'read', contract.name)
const docString = genDocString('useReadContract', contract)
const functionName = 'createUseReadContract'
imports.add(functionName)
content.push(
`export const useRead${pascalCase(
`${docString}
export const ${hookName} = ${pure} ${functionName}({ ${innerContent} })`,
)
const names = new Set<string>()
for (const item of readItems) {
if (item.type !== 'function') continue
if (
item.stateMutability !== 'pure' &&
item.stateMutability !== 'view'
)
continue
// Skip overrides since they are captured by same hook
if (names.has(item.name)) continue
names.add(item.name)
const hookName = getHookName(
config,
hookNames,
'read',
contract.name,
)} = ${pure} createReadContract(${innerContent})`,
)
item.name,
)
const docString = genDocString('useReadContract', contract, {
name: 'functionName',
value: item.name,
})
content.push(
`${docString}
export const ${hookName} = ${pure} ${functionName}({ ${innerContent}, functionName: '${item.name}' })`,
)
}
}
if (hasWriteFunction) {
imports.add('createWriteContract')
{
const hookName = getHookName(
config,
hookNames,
'write',
contract.name,
)
const docString = genDocString('useWriteContract', contract)
const functionName = 'createUseWriteContract'
imports.add(functionName)
content.push(
`${docString}
export const ${hookName} = ${pure} ${functionName}({ ${innerContent} })`,
)
const names = new Set<string>()
for (const item of writeItems) {
if (item.type !== 'function') continue
if (
item.stateMutability !== 'nonpayable' &&
item.stateMutability !== 'payable'
)
continue
// Skip overrides since they are captured by same hook
if (names.has(item.name)) continue
names.add(item.name)
const hookName = getHookName(
config,
hookNames,
'write',
contract.name,
item.name,
)
const docString = genDocString('useWriteContract', contract, {
name: 'functionName',
value: item.name,
})
content.push(
`${docString}
export const ${hookName} = ${pure} ${functionName}({ ${innerContent}, functionName: '${item.name}' })`,
)
}
}
{
const hookName = getHookName(
config,
hookNames,
'simulate',
contract.name,
)
const docString = genDocString('useSimulateContract', contract)
const functionName = 'createUseSimulateContract'
imports.add(functionName)
content.push(
`${docString}
export const ${hookName} = ${pure} ${functionName}({ ${innerContent} })`,
)
const names = new Set<string>()
for (const item of writeItems) {
if (item.type !== 'function') continue
if (
item.stateMutability !== 'nonpayable' &&
item.stateMutability !== 'payable'
)
continue
// Skip overrides since they are captured by same hook
if (names.has(item.name)) continue
names.add(item.name)
const hookName = getHookName(
config,
hookNames,
'simulate',
contract.name,
item.name,
)
const docString = genDocString('useSimulateContract', contract, {
name: 'functionName',
value: item.name,
})
content.push(
`${docString}
export const ${hookName} = ${pure} ${functionName}({ ${innerContent}, functionName: '${item.name}' })`,
)
}
}
}
if (hasEvent) {
const hookName = getHookName(
config,
hookNames,
'watch',
contract.name,
)
const docString = genDocString('useWatchContractEvent', contract)
const functionName = 'createUseWatchContractEvent'
imports.add(functionName)
content.push(
`export const useWrite${pascalCase(
`${docString}
export const ${hookName} = ${pure} ${functionName}({ ${innerContent} })`,
)
const names = new Set<string>()
for (const item of eventItems) {
if (item.type !== 'event') continue
// Skip overrides since they are captured by same hook
if (names.has(item.name)) continue
names.add(item.name)
const hookName = getHookName(
config,
hookNames,
'watch',
contract.name,
)} = ${pure} createWriteContract(${innerContent})`,
)
item.name,
)
const docString = genDocString('useWatchContractEvent', contract, {
name: 'eventName',
value: item.name,
})
content.push(
`${docString}
export const ${hookName} = ${pure} ${functionName}({ ${innerContent}, functionName: '${item.name}' })`,
)
}
}

@@ -73,1 +249,68 @@ }

}
function genDocString(
hookName: string,
contract: Contract,
item?: { name: string; value: string },
) {
let description = `Wraps __{@link ${hookName}}__ with \`abi\` set to __{@link ${contract.meta.abiName}}__`
if (item) description += ` and \`${item.name}\` set to \`"${item.value}"\``
const docString = getAddressDocString({ address: contract.address })
if (docString)
return `/**
* ${description}
*
${docString}
*/`
return `/**
* ${description}
*/`
}
function getHookName(
config: ReactConfig,
hookNames: Set<string>,
type: 'read' | 'simulate' | 'watch' | 'write',
contractName: string,
itemName?: string | undefined,
) {
const ContractName = pascalCase(contractName)
const ItemName = itemName ? pascalCase(itemName) : undefined
let hookName
if (typeof config.getHookName === 'function')
hookName = config.getHookName({
type,
contractName: ContractName,
itemName: ItemName,
})
else if (typeof config.getHookName === 'string') {
switch (type) {
case 'read':
hookName = `use${ContractName}${ItemName ?? 'Read'}`
break
case 'simulate':
hookName = `usePrepare${ContractName}${ItemName ?? 'Write'}`
break
case 'watch':
hookName = `use${ContractName}${ItemName ?? ''}Event`
break
case 'write':
hookName = `use${ContractName}${ItemName ?? 'Write'}`
break
}
} else {
hookName = `use${pascalCase(type)}${ContractName}${ItemName ?? ''}`
if (type === 'watch') hookName = `${hookName}Event`
}
if (hookNames.has(hookName))
throw new Error(
`Hook name "${hookName}" must be unique for contract "${contractName}". Try using \`getHookName\` to create a unique name.`,
)
hookNames.add(hookName)
return hookName
}

@@ -1,1 +0,1 @@

export const version = '2.0.0-beta.8'
export const version = '2.0.0-beta.9'

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc