Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK] Fix setting allowance or approvals gaslessly #450

Merged
merged 3 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shaggy-rings-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thirdweb-dev/sdk": patch
---

Fix setting allowance or approvals gaslessly
4 changes: 2 additions & 2 deletions packages/sdk/src/evm/common/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export async function setErc20Allowance(
signer || provider,
currencyAddress,
ERC20Abi,
{},
contractToApprove.options,
);

const owner = await contractToApprove.getSignerAddress();
Expand All @@ -151,7 +151,7 @@ export async function approveErc20Allowance(
signer || provider,
currencyAddress,
ERC20Abi,
{},
contractToApprove.options,
);
const owner = await contractToApprove.getSignerAddress();
const spender = contractToApprove.readContract.address;
Expand Down
17 changes: 8 additions & 9 deletions packages/sdk/src/evm/common/marketplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
InterfaceId_IERC1155,
InterfaceId_IERC721,
} from "../constants/contract";
import { SignerOrProvider } from "../core";
import { ContractWrapper } from "../core/classes/contract-wrapper";
import {
NewAuctionListing,
Expand Down Expand Up @@ -76,24 +75,24 @@ export async function isTokenApprovedForTransfer(
/**
* Checks if the marketplace is approved to make transfers on the assetContract
* If not, it tries to set the approval.
* @param signerOrProvider
* @param contractWrapper
* @param marketplaceAddress
* @param assetContract
* @param tokenId
* @param from
*/
export async function handleTokenApproval(
signerOrProvider: SignerOrProvider,
contractWrapper: ContractWrapper<any>,
marketplaceAddress: string,
assetContract: string,
tokenId: BigNumberish,
from: string,
): Promise<void> {
const erc165 = new ContractWrapper<IERC165>(
signerOrProvider,
contractWrapper.getSignerOrProvider(),
assetContract,
ERC165Abi,
{},
contractWrapper.options,
);
const isERC721 = await erc165.readContract.supportsInterface(
InterfaceId_IERC721,
Expand All @@ -104,10 +103,10 @@ export async function handleTokenApproval(
// check for token approval
if (isERC721) {
const asset = new ContractWrapper<IERC721>(
signerOrProvider,
contractWrapper.getSignerOrProvider(),
assetContract,
ERC721Abi,
{},
contractWrapper.options,
);
const approved = await asset.readContract.isApprovedForAll(
from,
Expand All @@ -127,10 +126,10 @@ export async function handleTokenApproval(
}
} else if (isERC1155) {
const asset = new ContractWrapper<IERC1155>(
signerOrProvider,
contractWrapper.getSignerOrProvider(),
assetContract,
ERC1155Abi,
{},
contractWrapper.options,
);

const approved = await asset.readContract.isApprovedForAll(
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/evm/core/classes/marketplace-auction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export class MarketplaceAuction {
validateNewListingParam(listing);

await handleTokenApproval(
this.contractWrapper.getSignerOrProvider(),
this.contractWrapper,
this.getAddress(),
listing.assetContractAddress,
listing.tokenId,
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/evm/core/classes/marketplace-direct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export class MarketplaceDirect {
validateNewListingParam(listing);

await handleTokenApproval(
this.contractWrapper.getSignerOrProvider(),
this.contractWrapper,
this.getAddress(),
listing.assetContractAddress,
listing.tokenId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import EventEmitter from "eventemitter3";
export class RPCConnectionHandler extends EventEmitter {
private provider: providers.Provider;
private signer: Signer | undefined;
protected readonly options: SDKOptionsOutput;
public readonly options: SDKOptionsOutput;

constructor(network: NetworkOrSignerOrProvider, options: SDKOptions) {
super();
Expand Down