Skip to content

Commit

Permalink
support walletconnect - update nft collection allowance
Browse files Browse the repository at this point in the history
  • Loading branch information
ezra-sg committed Dec 19, 2023
1 parent c66268e commit 494ec6f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
22 changes: 9 additions & 13 deletions src/antelope/stores/allowances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,24 +392,20 @@ export const useAllowancesStore = defineStore(store_name, {
useFeedbackStore().setLoading('updateNftCollectionAllowance');

try {
const nftContract = await useContractStore().getContract(CURRENT_CONTEXT, nftContractAddress);
const nftContractInstance = await nftContract?.getContractInstance();
const authenticator = useAccountStore().getEVMAuthenticator(CURRENT_CONTEXT);
const tx = await authenticator.updateNftCollectionAllowance(operator, nftContractAddress, allowed) as TransactionResponse;

if (!nftContractInstance) {
throw new AntelopeError('antelope.utils.error_contract_instance');
}
const account = useAccountStore().loggedAccount as EvmAccountModel;

const tx = await nftContractInstance.setApprovalForAll(operator, allowed) as TransactionResponse;
const returnTx = this.subscribeForTransactionReceipt(account, tx);

tx.wait().then(() => {
setTimeout(() => {
this.fetchAllowancesForAccount(owner).then(() => {
useFeedbackStore().unsetLoading('updateNftCollectionAllowance');
});
}, 3000); // give the indexer time to update allowance data
returnTx.then((r) => {
r.wait().finally(() => {
useFeedbackStore().unsetLoading('updateNftCollectionAllowance');
});
});

return tx;
return returnTx;
} catch (error) {
const trxError = getAntelope().config.transactionError('antelope.evm.error_updating_allowance', error);
getAntelope().config.transactionErrorHandler(trxError, 'updateNftCollectionAllowance');
Expand Down
2 changes: 1 addition & 1 deletion src/antelope/stores/utils/abi/erc721.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,5 +365,5 @@ export const erc721ApproveAbi = [{
'outputs': [],
'stateMutability': 'nonpayable',
'type': 'function',
}] as unknown as EvmABI; // eztodo fix this
}] as unknown as EvmABI;

37 changes: 37 additions & 0 deletions src/antelope/wallets/authenticators/EVMAuthenticator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useChainStore } from 'src/antelope/stores/chain';
import { useEVMStore } from 'src/antelope/stores/evm';
import { createTraceFunction, isTracingAll, useFeedbackStore } from 'src/antelope/stores/feedback';
import { usePlatformStore } from 'src/antelope/stores/platform';
import { setApprovalForAllAbi } from 'src/antelope/stores/utils/abi/setApprovalForAllAbi';
import { AntelopeError, NftTokenInterface, ERC1155_TYPE, ERC721_TYPE, EvmABI, EvmABIEntry, EvmFunctionParam, EvmTransactionResponse, ExceptionError, TokenClass, addressString, erc20Abi, erc721Abi, escrowAbiWithdraw, stlosAbiDeposit, stlosAbiWithdraw, wtlosAbiDeposit, wtlosAbiWithdraw, erc1155Abi, erc20AbiApprove, erc721ApproveAbi } from 'src/antelope/types';

export abstract class EVMAuthenticator {
Expand Down Expand Up @@ -373,6 +374,10 @@ export abstract class EVMAuthenticator {

/**
* This method creates a Transaction to update an ERC20 allowance by calling the approve function
* @param spender address of the spender
* @param tokenContractAddress address of the ERC20 token contract
* @param allowance amount of tokens to allow
*
* @returns transaction response
*/
async updateErc20Allowance(
Expand All @@ -396,6 +401,10 @@ export abstract class EVMAuthenticator {

/**
* This method creates a Transaction to update an ERC721 allowance by calling the approve function
* @param operator address of the operator
* @param nftContractAddress address of the ERC721 token contract
* @param tokenId id of the token to set allowance for
*
* @returns transaction response
*/
async updateSingleErc721Allowance(
Expand All @@ -417,6 +426,34 @@ export abstract class EVMAuthenticator {
});
}

/**
* This method creates a Transaction to update an NFT collection (ERC721 or ERC1155) allowance
* by calling the setApprovalForAll function
* @param operator address of the operator
* @param nftContractAddress address of the ERC721 token contract
* @param allowed boolean to set allowance
*
* @returns transaction response
*/
async updateNftCollectionAllowance(
operator: string,
nftContractAddress: string,
allowed: boolean,
): Promise<EvmTransactionResponse | WriteContractResult> {
this.trace('updateNftCollectionAllowance', operator, nftContractAddress, allowed);

return this.signCustomTransaction(
nftContractAddress,
setApprovalForAllAbi,
[
operator,
allowed,
],
).catch((error) => {
throw this.handleCatchError(error as never);
});
}

/**
* This method creates and throws an AntelopeError with the corresponding message.
* It is useful to handle specific error codes that may indicate a particular known error situation.
Expand Down

0 comments on commit 494ec6f

Please sign in to comment.