Skip to content

Commit

Permalink
refactor(sdk): better viem contract parameter typing
Browse files Browse the repository at this point in the history
  • Loading branch information
sammccord committed Jul 9, 2024
1 parent 2b92dff commit d25b097
Show file tree
Hide file tree
Showing 18 changed files with 571 additions and 412 deletions.
139 changes: 71 additions & 68 deletions packages/evm/artifacts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,74 +253,6 @@ export function prepareERC1155Payload({
);
}

export interface ERC1155TransferPayload {
tokenId: bigint;
amount: bigint;
asset: Address;
target: Address;
}

export function prepareERC1155Transfer({
tokenId,
amount,
asset,
target,
}: ERC1155TransferPayload) {
return encodeAbiParameters(
parseAbiParameters([
'Transfer request',
'struct Transfer { uint8 assetType; address asset; address target; bytes data; }',
]),
[
{
assetType: 2,
asset,
data: prepareERC1155Payload({ tokenId, amount }),
target,
},
],
);
}

export interface FungiblePayload { amount: bigint }

export function prepareFungiblePayload({ amount }: FungiblePayload) {
return encodeAbiParameters(
parseAbiParameters([
'FungiblePayload payload',
'struct FungiblePayload { uint256 amount; }',
]),
[{ amount }],
);
}

export interface FungibleTransferPayload {
amount: bigint;
asset: Address;
target: Address;
}

export function prepareFungibleTransfer({
amount,
asset,
target,
}: FungibleTransferPayload) {
return encodeAbiParameters(
parseAbiParameters([
'Transfer request',
'struct Transfer { uint8 assetType; address asset; address target; bytes data; }',
]),
[
{
assetType: asset == zeroAddress ? 0 : 1,
asset,
data: prepareFungiblePayload({ amount }),
target,
},
],
);
}

export interface PointsIncentivePayload {
venue: Address;
selector: Hex;
Expand Down Expand Up @@ -548,6 +480,10 @@ export const prepareClaimPayload = ({ target, data = zeroHash }: ClaimPayload) =
)
}

/*
* Transfer Payloads
*/

export enum AssetType {
ETH,
ERC20,
Expand All @@ -573,3 +509,70 @@ export const prepareTransferPayload =({ assetType, address, target, data }: Tran
)
}

export interface ERC1155TransferPayload {
tokenId: bigint;
amount: bigint;
asset: Address;
target: Address;
}

export function prepareERC1155Transfer({
tokenId,
amount,
asset,
target,
}: ERC1155TransferPayload) {
return encodeAbiParameters(
parseAbiParameters([
'Transfer request',
'struct Transfer { uint8 assetType; address asset; address target; bytes data; }',
]),
[
{
assetType: AssetType.ERC1155,
asset,
data: prepareERC1155Payload({ tokenId, amount }),
target,
},
],
);
}

export interface FungiblePayload { amount: bigint }

export function prepareFungiblePayload({ amount }: FungiblePayload) {
return encodeAbiParameters(
parseAbiParameters([
'FungiblePayload payload',
'struct FungiblePayload { uint256 amount; }',
]),
[{ amount }],
);
}

export interface FungibleTransferPayload {
amount: bigint;
asset: Address;
target: Address;
}

export function prepareFungibleTransfer({
amount,
asset,
target,
}: FungibleTransferPayload) {
return encodeAbiParameters(
parseAbiParameters([
'Transfer request',
'struct Transfer { uint8 assetType; address asset; address target; bytes data; }',
]),
[
{
assetType: asset == zeroAddress ? AssetType.ETH : AssetType.ERC20,
asset,
data: prepareFungiblePayload({ amount }),
target,
},
],
);
}
46 changes: 26 additions & 20 deletions packages/sdk/src/Actions/ContractAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type {
GenericDeployableParams,
} from '../Deployable/Deployable';
import { DeployableTarget } from '../Deployable/DeployableTarget';
import type { CallParams } from '../utils';
import type { ReadParams, WriteParams } from '../utils';

export type { ContractActionPayload };

Expand All @@ -28,46 +28,48 @@ export class ContractAction extends DeployableTarget<ContractActionPayload> {
public override readonly base = ContractAction.base;

public async chainId(
params: CallParams<typeof readContractActionChainId> = {},
params?: ReadParams<typeof contractActionAbi, 'chainId'>,
) {
return readContractActionChainId(this._config, {
address: this.assertValidAddress(),
...this.optionallyAttachAccount(),
...params,
// biome-ignore lint/suspicious/noExplicitAny: Accept any shape of valid wagmi/viem parameters, wagmi does the same thing internally
...(params as any),
});
}

public async target(
params: CallParams<typeof readContractActionTarget> = {},
) {
public async target(params?: ReadParams<typeof contractActionAbi, 'target'>) {
return readContractActionTarget(this._config, {
address: this.assertValidAddress(),
...this.optionallyAttachAccount(),
...params,
// biome-ignore lint/suspicious/noExplicitAny: Accept any shape of valid wagmi/viem parameters, wagmi does the same thing internally
...(params as any),
});
}

public async selector(
params: CallParams<typeof readContractActionSelector> = {},
params?: ReadParams<typeof contractActionAbi, 'selector'>,
) {
return readContractActionSelector(this._config, {
address: this.assertValidAddress(),
...this.optionallyAttachAccount(),
...params,
// biome-ignore lint/suspicious/noExplicitAny: Accept any shape of valid wagmi/viem parameters, wagmi does the same thing internally
...(params as any),
});
}

public async value(params: CallParams<typeof readContractActionValue> = {}) {
public async value(params?: ReadParams<typeof contractActionAbi, 'value'>) {
return readContractActionValue(this._config, {
address: this.assertValidAddress(),
...this.optionallyAttachAccount(),
...params,
// biome-ignore lint/suspicious/noExplicitAny: Accept any shape of valid wagmi/viem parameters, wagmi does the same thing internally
...(params as any),
});
}

public async execute(
data: Hex,
params: CallParams<typeof writeContractActionExecute> = {},
params?: WriteParams<typeof contractActionAbi, 'execute'>,
) {
return this.awaitResult(
this.executeRaw(data, params),
Expand All @@ -78,47 +80,51 @@ export class ContractAction extends DeployableTarget<ContractActionPayload> {

public async executeRaw(
data: Hex,
params: CallParams<typeof writeContractActionExecute> = {},
params?: WriteParams<typeof contractActionAbi, 'execute'>,
) {
return writeContractActionExecute(this._config, {
address: this.assertValidAddress(),
...this.optionallyAttachAccount(),
...params,
// biome-ignore lint/suspicious/noExplicitAny: Accept any shape of valid wagmi/viem parameters, wagmi does the same thing internally
...(params as any),
args: [data],
});
}

public async prepare(
data: Hex,
params: CallParams<typeof readContractActionPrepare> = {},
params?: ReadParams<typeof contractActionAbi, 'prepare'>,
) {
return readContractActionPrepare(this._config, {
address: this.assertValidAddress(),
args: [data],
...this.optionallyAttachAccount(),
...params,
// biome-ignore lint/suspicious/noExplicitAny: Accept any shape of valid wagmi/viem parameters, wagmi does the same thing internally
...(params as any),
});
}

public async supportsInterface(
interfaceId: Hex,
params: CallParams<typeof readContractActionSupportsInterface> = {},
params?: ReadParams<typeof contractActionAbi, 'supportsInterface'>,
) {
return readContractActionSupportsInterface(this._config, {
address: this.assertValidAddress(),
...this.optionallyAttachAccount(),
...params,
// biome-ignore lint/suspicious/noExplicitAny: Accept any shape of valid wagmi/viem parameters, wagmi does the same thing internally
...(params as any),
args: [interfaceId],
});
}

public async getComponentInterface(
params: CallParams<typeof readContractActionGetComponentInterface> = {},
params?: ReadParams<typeof contractActionAbi, 'getComponentInterface'>,
) {
return readContractActionGetComponentInterface(this._config, {
address: this.assertValidAddress(),
...this.optionallyAttachAccount(),
...params,
// biome-ignore lint/suspicious/noExplicitAny: Accept any shape of valid wagmi/viem parameters, wagmi does the same thing internally
...(params as any),
args: [],
});
}
Expand Down
31 changes: 18 additions & 13 deletions packages/sdk/src/Actions/ERC721MintAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {
DeployableOptions,
GenericDeployableParams,
} from '../Deployable/Deployable';
import type { CallParams } from '../utils';
import type { ReadParams, WriteParams } from '../utils';
import { ContractAction } from './ContractAction';

export type { ERC721MintActionPayload };
Expand All @@ -27,7 +27,7 @@ export class ERC721MintAction extends ContractAction {

public override async execute(
data: Hex,
params: CallParams<typeof writeErc721MintActionExecute> = {},
params?: WriteParams<typeof erc721MintActionAbi, 'execute'>,
) {
return this.awaitResult(
this.executeRaw(data, params),
Expand All @@ -38,31 +38,33 @@ export class ERC721MintAction extends ContractAction {

public override async executeRaw(
data: Hex,
params: CallParams<typeof writeErc721MintActionExecute> = {},
params?: WriteParams<typeof erc721MintActionAbi, 'execute'>,
) {
return writeErc721MintActionExecute(this._config, {
address: this.assertValidAddress(),
args: [data],
...this.optionallyAttachAccount(),
...params,
// biome-ignore lint/suspicious/noExplicitAny: Accept any shape of valid wagmi/viem parameters, wagmi does the same thing internally
...(params as any),
});
}

public override async prepare(
data: Hex,
params: CallParams<typeof readErc721MintActionPrepare> = {},
params?: ReadParams<typeof erc721MintActionAbi, 'prepare'>,
) {
return readErc721MintActionPrepare(this._config, {
address: this.assertValidAddress(),
args: [data],
...this.optionallyAttachAccount(),
...params,
// biome-ignore lint/suspicious/noExplicitAny: Accept any shape of valid wagmi/viem parameters, wagmi does the same thing internally
...(params as any),
});
}

public async validate(
data: Hex,
params: CallParams<typeof writeErc721MintActionValidate> = {},
params?: WriteParams<typeof erc721MintActionAbi, 'validate'>,
) {
return this.awaitResult(
this.validateRaw(data, params),
Expand All @@ -73,35 +75,38 @@ export class ERC721MintAction extends ContractAction {

public async validateRaw(
data: Hex,
params: CallParams<typeof writeErc721MintActionValidate> = {},
params?: WriteParams<typeof erc721MintActionAbi, 'validate'>,
) {
return writeErc721MintActionValidate(this._config, {
address: this.assertValidAddress(),
args: [data],
...this.optionallyAttachAccount(),
...params,
// biome-ignore lint/suspicious/noExplicitAny: Accept any shape of valid wagmi/viem parameters, wagmi does the same thing internally
...(params as any),
});
}

public override async supportsInterface(
interfaceId: Hex,
params: CallParams<typeof readErc721MintActionSupportsInterface> = {},
params?: ReadParams<typeof erc721MintActionAbi, 'supportsInterface'>,
) {
return readErc721MintActionSupportsInterface(this._config, {
address: this.assertValidAddress(),
...this.optionallyAttachAccount(),
...params,
// biome-ignore lint/suspicious/noExplicitAny: Accept any shape of valid wagmi/viem parameters, wagmi does the same thing internally
...(params as any),
args: [interfaceId],
});
}

public override async getComponentInterface(
params: CallParams<typeof readErc721MintActionGetComponentInterface> = {},
params?: ReadParams<typeof erc721MintActionAbi, 'getComponentInterface'>,
) {
return readErc721MintActionGetComponentInterface(this._config, {
address: this.assertValidAddress(),
...this.optionallyAttachAccount(),
...params,
// biome-ignore lint/suspicious/noExplicitAny: Accept any shape of valid wagmi/viem parameters, wagmi does the same thing internally
...(params as any),
args: [],
});
}
Expand Down
Loading

0 comments on commit d25b097

Please sign in to comment.