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 0cefb17
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
5 changes: 5 additions & 0 deletions examples/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const RPC = process.env.RPC_URL;
const MULTI_SOURCE_LOAN_CONTRACT_V5 = process.env.MULTI_SOURCE_LOAN_CONTRACT_V5 ?? '';
const SEAPORT_CONTRACT_ADDRESS = '0x00000000000000ADc04C56Bf30aC9d3c0aAF14dC';
const LEVERAGE_CONTRACT = process.env.LEVERAGE_ADDRESS ?? '';
const USER_VAULT_CONTRACT_V5 = process.env.USER_VAULT_CONTRACT_V5 ?? '';

export const MAX_NUMBER =
115792089237316195423570985008687907853269984665640564039457584007913129639935n;
Expand Down Expand Up @@ -152,6 +153,10 @@ for (const [i, user] of users.entries()) {
await approveForUser(user, LEVERAGE_CONTRACT);
}

if (isAddress(USER_VAULT_CONTRACT_V5)) {
await approveForUser(user, USER_VAULT_CONTRACT_V5);
}

await approveForUser(user, SEAPORT_CONTRACT_ADDRESS);
}

Expand Down
68 changes: 68 additions & 0 deletions examples/userVaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Address } from 'viem';

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

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

const anotherCollectionAddress = process.env.TEST_COLLECTION_2 as unknown as Address | undefined;
if (!anotherCollectionAddress) {
throw new Error(
'TEST_COLLECTION_2 not provided, please provide address to run user vaults example',
);
}

console.log('Creating vault with nfts...');
const createTxn = await gondi.createUserVault({
nfts: [
// Assuming users[0] has same tokenId for collections
{ collection: anotherCollectionAddress, 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: [anotherCollectionAddress, 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 0cefb17

Please sign in to comment.