Skip to content

Commit

Permalink
Merge pull request #107 from monax/writer-params-pattern
Browse files Browse the repository at this point in the history
chore: Standardise writer params pattern
  • Loading branch information
Silas Davis authored Oct 24, 2023
2 parents baa5eea + 82ec2d5 commit e63556c
Show file tree
Hide file tree
Showing 49 changed files with 250 additions and 234 deletions.
9 changes: 4 additions & 5 deletions sdk/src/contracts/collections/features/acceptTerms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ export class AcceptTerms extends ContractFunction<

protected async acceptTerms(walletClient: Signer, params?: WriteParameters): Promise<AcceptTermsResponse> {
const v1 = this.partition('v1');
const fullParams = { account: walletClient.account, ...params };

try {
const { request } = await this.reader(this.abi(v1)).simulate.acceptTerms(params);
const { request } = await this.reader(this.abi(v1)).simulate.acceptTerms(fullParams);
const hash = await walletClient.writeContract(request);
return this.base.publicClient.waitForTransactionReceipt({
hash,
Expand All @@ -53,12 +54,10 @@ export class AcceptTerms extends ContractFunction<

async estimateGas(walletClient: Signer, params?: WriteParameters): Promise<bigint> {
const v1 = this.partition('v1');
const fullParams = { account: walletClient.account, ...params };

try {
const estimate = await this.reader(this.abi(v1)).estimateGas.acceptTerms({
account: walletClient.account,
...params,
});
const estimate = await this.reader(this.abi(v1)).estimateGas.acceptTerms(fullParams);
return estimate;
} catch (err) {
throw SdkError.from(err, SdkErrorCode.CHAIN_ERROR);
Expand Down
13 changes: 6 additions & 7 deletions sdk/src/contracts/collections/features/acceptTermsFor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Addressish, asAddress } from '@monaxlabs/phloem/dist/types';
import { encodeFunctionData, GetTransactionReceiptReturnType, Hex } from 'viem';
import { GetTransactionReceiptReturnType, Hex, encodeFunctionData } from 'viem';
import { CollectionContract } from '../..';
import { SdkError, SdkErrorCode } from '../errors';
import type { Signer, WriteParameters } from '../types';
import { FeatureFunctionsMap } from './feature-functions.gen';
import { asCallableClass, ContractFunction } from './features';
import { ContractFunction, asCallableClass } from './features';

const AcceptTermsForFunctions = {
v1: 'acceptTerms(address)[]',
Expand Down Expand Up @@ -44,9 +44,10 @@ export class AcceptTermsFor extends ContractFunction<
): Promise<AcceptTermsForResponse> {
const v1 = this.partition('v1');
const wallet = await asAddress(acceptor);
const fullParams = { account: walletClient.account, ...params };

try {
const { request } = await this.reader(this.abi(v1)).simulate.acceptTerms([wallet as Hex], params);
const { request } = await this.reader(this.abi(v1)).simulate.acceptTerms([wallet as Hex], fullParams);
const hash = await walletClient.writeContract(request);
return this.base.publicClient.waitForTransactionReceipt({
hash,
Expand All @@ -59,12 +60,10 @@ export class AcceptTermsFor extends ContractFunction<
async estimateGas(walletClient: Signer, acceptor: Addressish, params?: WriteParameters): Promise<bigint> {
const v1 = this.partition('v1');
const wallet = await asAddress(acceptor);
const fullParams = { account: walletClient.account, ...params };

try {
const estimate = await this.reader(this.abi(v1)).estimateGas.acceptTerms([wallet as Hex], {
account: walletClient.account,
...params,
});
const estimate = await this.reader(this.abi(v1)).estimateGas.acceptTerms([wallet as Hex], fullParams);
return estimate;
} catch (err) {
throw SdkError.from(err, SdkErrorCode.CHAIN_ERROR);
Expand Down
9 changes: 4 additions & 5 deletions sdk/src/contracts/collections/features/acceptTermsForMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ export class AcceptTermsForMany extends ContractFunction<
): Promise<AcceptTermsForManyResponse> {
const v1 = this.partition('v1');
const wallets = await Promise.all(acceptors.map((a) => asAddress(a)));
const fullParams = { account: walletClient.account, ...params };

try {
const { request } = await this.reader(this.abi(v1)).simulate.batchAcceptTerms([wallets as Hex[]], params);
const { request } = await this.reader(this.abi(v1)).simulate.batchAcceptTerms([wallets as Hex[]], fullParams);
const hash = await walletClient.writeContract(request);
return this.base.publicClient.waitForTransactionReceipt({
hash,
Expand All @@ -58,12 +59,10 @@ export class AcceptTermsForMany extends ContractFunction<
async estimateGas(walletClient: Signer, acceptors: Addressish[], params?: WriteParameters): Promise<bigint> {
const v1 = this.partition('v1');
const wallets = await Promise.all(acceptors.map((a) => asAddress(a)));
const fullParams = { account: walletClient.account, ...params };

try {
const estimate = await this.reader(this.abi(v1)).estimateGas.batchAcceptTerms([wallets as Hex[]], {
account: walletClient.account,
...params,
});
const estimate = await this.reader(this.abi(v1)).estimateGas.batchAcceptTerms([wallets as Hex[]], fullParams);
return estimate;
} catch (err) {
throw SdkError.from(err, SdkErrorCode.CHAIN_ERROR);
Expand Down
23 changes: 11 additions & 12 deletions sdk/src/contracts/collections/features/acceptTermsWithSignature.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Addressish, asAddress } from '@monaxlabs/phloem/dist/types';
import { encodeFunctionData, GetTransactionReceiptReturnType, Hex } from 'viem';
import { GetTransactionReceiptReturnType, Hex, encodeFunctionData } from 'viem';
import { CollectionContract, Signer, WriteParameters } from '../..';
import { SdkError, SdkErrorCode } from '../errors';
import { FeatureFunctionsMap } from './feature-functions.gen';
import { asCallableClass, ContractFunction } from './features';
import { ContractFunction, asCallableClass } from './features';

const AcceptTermsWithSignatureFunctions = {
v1: 'acceptTerms(address,bytes)[]',
Expand Down Expand Up @@ -56,12 +56,13 @@ export class AcceptTermsWithSignature extends ContractFunction<
): Promise<AcceptTermsWithSignatureResponse> {
const { v1, v2 } = this.partitions;
const wallet = await asAddress(acceptor);
const fullParams = { account: walletClient.account, ...params };

try {
if (v1) {
const { request } = await this.reader(this.abi(v1)).simulate.acceptTerms(
[wallet as Hex, signature as Hex],
params,
fullParams,
);
const hash = await walletClient.writeContract(request);
return this.base.publicClient.waitForTransactionReceipt({
Expand All @@ -70,7 +71,7 @@ export class AcceptTermsWithSignature extends ContractFunction<
} else if (v2) {
const { request } = await this.reader(this.abi(v2)).simulate.storeTermsAccepted(
[wallet as Hex, signature as Hex],
params,
fullParams,
);
const hash = await walletClient.writeContract(request);
return this.base.publicClient.waitForTransactionReceipt({
Expand All @@ -92,21 +93,19 @@ export class AcceptTermsWithSignature extends ContractFunction<
): Promise<bigint> {
const { v1, v2 } = this.partitions;
const wallet = await asAddress(acceptor);
const fullParams = { account: walletClient.account, ...params };

try {
if (v1) {
const estimate = await this.reader(this.abi(v1)).estimateGas.acceptTerms([wallet as Hex, signature as Hex], {
account: walletClient.account,
...params,
});
const estimate = await this.reader(this.abi(v1)).estimateGas.acceptTerms(
[wallet as Hex, signature as Hex],
fullParams,
);
return estimate;
} else if (v2) {
const estimate = await this.reader(this.abi(v2)).estimateGas.storeTermsAccepted(
[wallet as Hex, signature as Hex],
{
account: walletClient.account,
...params,
},
fullParams,
);
return estimate;
}
Expand Down
13 changes: 6 additions & 7 deletions sdk/src/contracts/collections/features/approve.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Addressish, asAddress } from '@monaxlabs/phloem/dist/types';
import { encodeFunctionData, GetTransactionReceiptReturnType, Hex } from 'viem';
import { GetTransactionReceiptReturnType, Hex, encodeFunctionData } from 'viem';
import { CollectionContract } from '../..';
import { SdkError, SdkErrorCode } from '../errors';
import type { Signer, WriteParameters } from '../types';
import { FeatureFunctionsMap } from './feature-functions.gen';
import { asCallableClass, ContractFunction } from './features';
import { ContractFunction, asCallableClass } from './features';

const ApproveFunctions = {
nft: 'approve(address,uint256)[]',
Expand Down Expand Up @@ -44,9 +44,10 @@ export class Approve extends ContractFunction<ApproveInterfaces, ApprovePartitio
): Promise<ApproveResponse> {
const nft = this.partition('nft');
const to = await asAddress(toAddress);
const fullParams = { account: walletClient.account, ...params };

try {
const { request } = await this.reader(this.abi(nft)).simulate.approve([to as Hex, tokenId], params);
const { request } = await this.reader(this.abi(nft)).simulate.approve([to as Hex, tokenId], fullParams);
const hash = await walletClient.writeContract(request);
return this.base.publicClient.waitForTransactionReceipt({
hash,
Expand All @@ -59,12 +60,10 @@ export class Approve extends ContractFunction<ApproveInterfaces, ApprovePartitio
async estimateGas(walletClient: Signer, { toAddress, tokenId }: ApproveArgs, params?: WriteParameters) {
const nft = this.partition('nft');
const to = await asAddress(toAddress);
const fullParams = { account: walletClient.account, ...params };

try {
const estimate = await this.reader(this.abi(nft)).estimateGas.approve([to as Hex, tokenId], {
account: walletClient.account,
...params,
});
const estimate = await this.reader(this.abi(nft)).estimateGas.approve([to as Hex, tokenId], fullParams);
return estimate;
} catch (err) {
throw SdkError.from(err, SdkErrorCode.CHAIN_ERROR);
Expand Down
24 changes: 14 additions & 10 deletions sdk/src/contracts/collections/features/batchIssue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ export class BatchIssue extends ContractFunction<
): Promise<BatchIssueResponse> {
const sft = this.partition('sft');
const wallets = await Promise.all(receivers.map((receiver) => asAddress(receiver)));
const fullParams = { account: walletClient.account, ...params };

try {
const { request } = await this.reader(this.abi(sft)).simulate.batchIssue(
[wallets as Hex[], tokenIds, quantities],
params,
fullParams,
);
const hash = await walletClient.writeContract(request);
return this.base.publicClient.waitForTransactionReceipt({
Expand All @@ -86,9 +87,13 @@ export class BatchIssue extends ContractFunction<
): Promise<BatchIssueResponse> {
const nft = this.partition('nft');
const wallets = await Promise.all(receivers.map((receiver) => asAddress(receiver)));
const fullParams = { account: walletClient.account, ...params };

try {
const { request } = await this.reader(this.abi(nft)).simulate.batchIssue([wallets as Hex[], quantities], params);
const { request } = await this.reader(this.abi(nft)).simulate.batchIssue(
[wallets as Hex[], quantities],
fullParams,
);
const hash = await walletClient.writeContract(request);
return this.base.publicClient.waitForTransactionReceipt({
hash,
Expand Down Expand Up @@ -116,14 +121,12 @@ export class BatchIssue extends ContractFunction<
): Promise<bigint> {
const sft = this.partition('sft');
const wallets = await Promise.all(receivers.map((receiver) => asAddress(receiver)));
const fullParams = { account: walletClient.account, ...params };

try {
const estimate = await this.reader(this.abi(sft)).estimateGas.batchIssue(
[wallets as Hex[], tokenIds, quantities],
{
account: walletClient.account,
...params,
},
fullParams,
);
return estimate;
} catch (err) {
Expand All @@ -138,12 +141,13 @@ export class BatchIssue extends ContractFunction<
): Promise<bigint> {
const nft = this.partition('nft');
const wallets = await Promise.all(receivers.map((receiver) => asAddress(receiver)));
const fullParams = { account: walletClient.account, ...params };

try {
const estimate = await this.reader(this.abi(nft)).estimateGas.batchIssue([wallets as Hex[], quantities], {
account: walletClient.account,
...params,
});
const estimate = await this.reader(this.abi(nft)).estimateGas.batchIssue(
[wallets as Hex[], quantities],
fullParams,
);
return estimate;
} catch (err) {
throw SdkError.from(err, SdkErrorCode.CHAIN_ERROR, { receivers, quantities });
Expand Down
13 changes: 6 additions & 7 deletions sdk/src/contracts/collections/features/batchIssueWithTokenUri.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Address, Addressish, asAddress } from '@monaxlabs/phloem/dist/types';
import { encodeFunctionData, GetTransactionReceiptReturnType, Hex } from 'viem';
import { GetTransactionReceiptReturnType, Hex, encodeFunctionData } from 'viem';
import { Signer, WriteParameters, ZERO_ADDRESS } from '../..';
import { CollectionContract } from '../collections';
import { SdkError, SdkErrorCode } from '../errors';
import { FeatureFunctionsMap } from './feature-functions.gen';
import { asCallableClass, ContractFunction } from './features';
import { ContractFunction, asCallableClass } from './features';

const BatchIssueWithTokenUriFunctions = {
nft: 'batchIssueWithTokenURI(address[],string[])[]',
Expand Down Expand Up @@ -54,11 +54,12 @@ export class BatchIssueWithTokenUri extends ContractFunction<
this.validateArgs(args);
const nft = this.partition('nft');
const wallets = await Promise.all(args.receivers.map((receiver) => asAddress(receiver)));
const fullParams = { account: walletClient.account, ...params };

try {
const { request } = await this.reader(this.abi(nft)).simulate.batchIssueWithTokenURI(
[wallets as Hex[], args.tokenURIs],
params,
fullParams,
);
const hash = await walletClient.writeContract(request);
return this.base.publicClient.waitForTransactionReceipt({
Expand All @@ -73,14 +74,12 @@ export class BatchIssueWithTokenUri extends ContractFunction<
this.validateArgs(args);
const nft = this.partition('nft');
const wallets = await Promise.all(args.receivers.map((receiver) => asAddress(receiver)));
const fullParams = { account: walletClient.account, ...params };

try {
const estimate = await this.reader(this.abi(nft)).estimateGas.batchIssueWithTokenURI(
[wallets as Hex[], args.tokenURIs],
{
account: walletClient.account,
...params,
},
fullParams,
);
return estimate;
} catch (err) {
Expand Down
18 changes: 8 additions & 10 deletions sdk/src/contracts/collections/features/batchIssueWithinPhase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ export class BatchIssueWithinPhase extends ContractFunction<
): Promise<BatchIssueWithinPhaseResponse> {
const sft = this.partition('sft');
const wallets = await Promise.all(receivers.map((receiver) => asAddress(receiver)));
const fullParams = { account: walletClient.account, ...params };

try {
const { request } = await this.reader(this.abi(sft)).simulate.batchIssueWithinPhase(
[wallets as Hex[], tokenIds, quantities],
params,
fullParams,
);
const hash = await walletClient.writeContract(request);
return this.base.publicClient.waitForTransactionReceipt({
Expand All @@ -84,11 +85,12 @@ export class BatchIssueWithinPhase extends ContractFunction<
): Promise<BatchIssueWithinPhaseResponse> {
const nft = this.partition('nft');
const wallets = await Promise.all(receivers.map((receiver) => asAddress(receiver)));
const fullParams = { account: walletClient.account, ...params };

try {
const { request } = await this.reader(this.abi(nft)).simulate.batchIssueWithinPhase(
[wallets as Hex[], quantities],
params,
fullParams,
);
const hash = await walletClient.writeContract(request);
return this.base.publicClient.waitForTransactionReceipt({
Expand Down Expand Up @@ -117,14 +119,12 @@ export class BatchIssueWithinPhase extends ContractFunction<
): Promise<bigint> {
const sft = this.partition('sft');
const wallets = await Promise.all(receivers.map((receiver) => asAddress(receiver)));
const fullParams = { account: walletClient.account, ...params };

try {
const estimate = await this.reader(this.abi(sft)).estimateGas.batchIssueWithinPhase(
[wallets as Hex[], tokenIds, quantities],
{
account: walletClient.account,
...params,
},
fullParams,
);
return estimate;
} catch (err) {
Expand All @@ -139,14 +139,12 @@ export class BatchIssueWithinPhase extends ContractFunction<
): Promise<bigint> {
const nft = this.partition('nft');
const wallets = await Promise.all(receivers.map((receiver) => asAddress(receiver)));
const fullParams = { account: walletClient.account, ...params };

try {
const estimate = await this.reader(this.abi(nft)).estimateGas.batchIssueWithinPhase(
[wallets as Hex[], quantities],
{
account: walletClient.account,
...params,
},
fullParams,
);
return estimate;
} catch (err) {
Expand Down
Loading

0 comments on commit e63556c

Please sign in to comment.