Skip to content

Commit

Permalink
chore: added user vaults example
Browse files Browse the repository at this point in the history
  • Loading branch information
mishuagopian committed Jan 12, 2024
1 parent 479372f commit 94d3903
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
10 changes: 7 additions & 3 deletions examples/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,18 @@ const approveToken = async (user: Gondi, to: Address) => {
}
};

const approveNFT = async (user: Gondi, to: Address) => {
export const approveNFT = async (
user: Gondi,
to: Address,
collection: Address | undefined = testCollection.contractAddress,
) => {
const isApprovedAlready = await user.isApprovedNFTForAll({
nftAddress: testCollection.contractAddress,
nftAddress: collection,
to,
});
if (!isApprovedAlready) {
const approveNFT = await user.approveNFTForAll({
nftAddress: testCollection.contractAddress,
nftAddress: collection,
to,
});
await approveNFT.waitTxInBlock();
Expand Down
73 changes: 73 additions & 0 deletions examples/userVaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Address, isAddress } from 'viem';

import { approveNFT, testCollection, testTokenId, users } from './common';

const USER_VAULT_CONTRACT_V5 = process.env.USER_VAULT_CONTRACT_V5 ?? '';
const ANOTHER_COLLECTION = process.env.TEST_COLLECTION_2 as Address;

const userVaults = async () => {
const gondi = users[1];

if (!isAddress(ANOTHER_COLLECTION) || !isAddress(USER_VAULT_CONTRACT_V5)) {
throw new Error(`
TEST_COLLECTION_2 OR USER_VAULT_CONTRACT_5 were not correctly provided.
Please, provide addresses to run user vaults example.
`);
}
await approveNFT(gondi, USER_VAULT_CONTRACT_V5, testCollection.contractAddress);
await approveNFT(gondi, USER_VAULT_CONTRACT_V5, ANOTHER_COLLECTION);

console.log('Creating vault with nfts...');
const createTxn = await gondi.createUserVault({
nfts: [
// Assuming users[0] has same tokenId for collections
{ collection: ANOTHER_COLLECTION, tokenIds: [testTokenId] },
{ collection: testCollection.contractAddress, tokenIds: [testTokenId] },
],
});
const vaultId = createTxn.vaultId;
console.log(`successfully created vaultId ${vaultId}.`);

const createResult = await createTxn.waitTxInBlock();
console.log(`successfully deposited ${createResult.length} nfts to vaultId ${vaultId}.`);

for (let i = 0; i < createResult.length; i++) {
const element = createResult[i];
const owner = await gondi.getOwner({
nftAddress: element.collection,
tokenId: element.tokenId,
});
console.log(`-- ${i + 1}: ${element.collection} ${element.tokenId} - owner: ${owner}`);
}

console.log('Burning and withdrawing nfts...');

const burnTxn = await gondi.burnUserVaultAndWithdraw({
vaultId,
collections: [ANOTHER_COLLECTION, testCollection.contractAddress],
tokenIds: [testTokenId, testTokenId],
});

const burnResult = await burnTxn.waitTxInBlock();
console.log(`successfully burned vaultId ${vaultId}.`);

const withdrawEvents = burnResult.events;
console.log(`successfully withdrawn ${withdrawEvents.length} nfts from vaultId ${vaultId}.`);

for (let j = 0; j < withdrawEvents.length; j++) {
const event = withdrawEvents[j];
const owner = await gondi.getOwner({ nftAddress: event.collection, tokenId: event.tokenId });
console.log(`-- ${j + 1}: ${event.collection} ${event.tokenId} - owner: ${owner}`);
}
};

async function main() {
try {
await userVaults();
} catch (e) {
console.log('Error:');
console.log(e);
}
}

main();
5 changes: 4 additions & 1 deletion src/contracts/UserVaultV5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ export class UserVaultV5 extends Contract<typeof userVaultABIV5> {
const filter = await this.contract.createEventFilter.ERC721Withdrawn();
const events = filterLogs(receipt, filter);
if (events.length !== tokenIds.length) throw new Error('Withdrawn count mismatch');
return { events, ...receipt };
return {
events: events.map((event) => event.args),
...receipt,
};
},
};
}
Expand Down

0 comments on commit 94d3903

Please sign in to comment.