Skip to content

Commit

Permalink
fix: contract logging
Browse files Browse the repository at this point in the history
for help with debugging, all contract writes now log their arguments to the debug console
  • Loading branch information
jackmellis committed Nov 23, 2023
1 parent 39621bc commit 34e5edf
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 96 deletions.
49 changes: 47 additions & 2 deletions packages/trade/src/approve/approve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
import { MaxUint256, PERMIT2, Zero } from '@nftx/constants';
import { ValidationError } from '@nftx/errors';
import config from '@nftx/config';
import { zeroAddress } from 'viem';
import { encodeAbiParameters, zeroAddress } from 'viem';

function approvePunk({
tokenId,
Expand Down Expand Up @@ -156,7 +156,7 @@ async function approvePermit2({
details: {
token: tokenAddress,
amount,
expiration: deadline,
expiration: Number(deadline),
nonce,
},
spender: spenderAddress,
Expand All @@ -176,6 +176,50 @@ async function approvePermit2({
wait: async () => {
const signature = await promise;

const permit2encoded = encodeAbiParameters(
[
{
name: 'owner',
type: 'address',
},
{
name: 'permitSingle',
type: 'tuple',
components: [
{
name: 'details',
type: 'tuple',
components: [
{
name: 'token',
type: 'address',
},
{
name: 'amount',
type: 'uint160',
},
{
name: 'expiration',
type: 'uint48',
},
{
name: 'nonce',
type: 'uint48',
},
],
},
{ name: 'spender', type: 'address' },
{ name: 'sigDeadline', type: 'uint256' },
],
},
{
name: 'signature',
type: 'bytes',
},
],
[userAddress, permitSingleStruct, signature]
);

return {
blockHash: '0x',
blockNumber: 0n,
Expand All @@ -197,6 +241,7 @@ async function approvePermit2({
expiration: permitSingleStruct.details.expiration,
sigDeadline: permitSingleStruct.sigDeadline,
nonce: permitSingleStruct.details.nonce,
permit2encoded,
},
};
},
Expand Down
73 changes: 73 additions & 0 deletions packages/trade/src/approve/approveContracts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { MaxUint256, PERMIT2 } from '@nftx/constants';
import { ApproveContract, TokenId } from '@nftx/types';
import { getChainConstant, getUniqueTokenIds, isCryptoPunk } from '@nftx/utils';

type GetApproveContract = Pick<
ApproveContract,
'label' | 'spenderAddress' | 'tokenAddress'
> &
Partial<Pick<ApproveContract, 'amount' | 'standard' | 'tokenIds'>> & {
network: number;
usePermit2?: boolean;
};

export const getApproveContracts = ({
label,
network,
spenderAddress,
tokenAddress,
amount,
standard,
tokenIds,
usePermit2,
}: GetApproveContract): ApproveContract[] => {
if (isCryptoPunk(tokenAddress)) {
return (
tokenIds?.map((tokenIdOrTuple) => {
const tokenIds = getUniqueTokenIds([tokenIdOrTuple as TokenId]);

return {
type: 'on-chain',
label: `${label} [${tokenIds}]`,
tokenIds,
spenderAddress,
tokenAddress,
standard,
};
}) || []
);
// Permit2 is only available for ERC20 tokens
} else if (usePermit2 && (standard === 'ERC20' || standard == null)) {
return [
{
label: 'Approve Permit2',
type: 'on-chain',
spenderAddress: getChainConstant(PERMIT2, network),
tokenAddress,
standard: 'ERC20',
amount: MaxUint256,
},
{
type: 'permit2',
label,
spenderAddress,
tokenAddress,
amount,
standard,
tokenIds,
},
];
} else {
return [
{
type: 'on-chain',
label,
spenderAddress,
tokenAddress,
amount,
standard,
tokenIds,
},
];
}
};
1 change: 1 addition & 0 deletions packages/trade/src/approve/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as approve } from './approve';
export { default as isApproved } from './isApproved';
export * from './approveContracts';
7 changes: 0 additions & 7 deletions packages/trade/src/pools/createVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ const makeCreateVault = ({
signer,
});

console.debug({
method: 'createVault',
address,
value,
params,
});

return contract.write.createVault({
args: [params],
value,
Expand Down
8 changes: 0 additions & 8 deletions packages/trade/src/positions/createInventoryPosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ const createInventoryPosition = ({
signer,
});

console.debug({
method: 'depositWithNFT',
vaultId,
nftIds,
nftAmounts,
userAddress,
});

return contract.write.depositWithNFT({
args: [BigInt(vaultId), nftIds, nftAmounts, userAddress],
});
Expand Down
8 changes: 0 additions & 8 deletions packages/trade/src/positions/withdrawInventory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ const withdrawInventory = ({
provider: Provider;
signer: Signer;
}) => {
console.debug({
method: 'withdraw',
positionId,
vTokenShares,
nftIds,
vTokenPremiumLimit,
});

const contract = getContract({
abi: InventoryStaking,
address: getChainConstant(INVENTORY_STAKING, network),
Expand Down
6 changes: 0 additions & 6 deletions packages/trade/src/positions/withdrawLiquidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ const withdrawLiquidity = ({
signer,
});

console.debug({
method: 'removeLiquidity',
params,
value,
});

return contract.write.removeLiquidity({
args: [params],
value,
Expand Down
11 changes: 0 additions & 11 deletions packages/trade/src/trade/buy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,6 @@ const buy = ({
const vTokenPremiumLimit = BigInt(params.premiumLimit);
const deductRoyalty = false;

console.debug({
method: 'buyNFTsWithETH',
vaultId,
idsOut,
calldata,
to,
vTokenPremiumLimit,
deductRoyalty,
value: BigInt(params.value),
});

return contract.write.buyNFTsWithETH({
args: [vaultId, idsOut, calldata, to, vTokenPremiumLimit, deductRoyalty],
value: BigInt(params.value),
Expand Down
2 changes: 0 additions & 2 deletions packages/trade/src/trade/mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ const mint = async ({
const to = params.to;
const value = BigInt(params.value);

console.debug({ method: 'mint', tokenIds, amounts, depositor, to, value });

// Not sure if vaultAddress is correct
return contract.write.mint({
args: [tokenIds, amounts, depositor, to],
Expand Down
10 changes: 0 additions & 10 deletions packages/trade/src/trade/redeem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,6 @@ const redeem = async ({
const value = BigInt(params.value);
const vTokenPremiumLimit = BigInt(params.premiumLimit);

console.debug({
method: 'redeem',
tokenIds,
to,
wethAmount,
vTokenPremiumLimit,
forceFees,
value,
});

return contract.write.redeem({
args: [tokenIds, to, wethAmount, vTokenPremiumLimit, forceFees],
value,
Expand Down
21 changes: 0 additions & 21 deletions packages/trade/src/trade/sell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,14 @@ const sell = ({
const to = params.to;
// TODO: handle royalties?
const deductRoyalty = false;
const value = BigInt(params.value);

if (params.standard === 'ERC1155') {
console.debug({
method: 'sell1155',
vaultId,
idsIn,
amounts,
calldata,
to,
deductRoyalty,
});

return contract.write.sell1155({
args: [vaultId, idsIn, amounts, calldata, to, deductRoyalty],
// value,
});
}

console.debug({
method: 'sell721',
vaultId,
idsIn,
calldata,
to,
deductRoyalty,
value,
});

return contract.write.sell721({
args: [vaultId, idsIn, calldata, to, deductRoyalty],
// value,
Expand Down
21 changes: 0 additions & 21 deletions packages/trade/src/trade/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,12 @@ const swap = ({
const value = BigInt(params.value);

if (params.standard === 'ERC1155') {
console.debug({
method: 'swap1155',
vaultId,
idsIn,
amounts,
idsOut,
vTokenPremiumLimit,
to,
value,
});

return contract.write.swap1155({
args: [vaultId, idsIn, amounts, idsOut, vTokenPremiumLimit, to],
value,
});
}

console.debug({
method: 'swap721',
vaultId,
idsIn,
idsOut,
vTokenPremiumLimit,
to,
value,
});

return contract.write.swap721({
args: [vaultId, idsIn, idsOut, vTokenPremiumLimit, to],
value,
Expand Down
7 changes: 7 additions & 0 deletions packages/utils/src/web3/getContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ function getContract<T extends Abi>({
const [userAddress] = await signer.getAddresses();
const account = userAddress;

console.debug({
method: functionName,
contractAddress: address,
account,
...args,
});

const { request } = await provider.simulateContract({
address,
abi,
Expand Down

0 comments on commit 34e5edf

Please sign in to comment.