@river-build/sdk
Version:
For more details, visit the following resources:
151 lines • 7.42 kB
JavaScript
/**
* @group with-entitlements
*/
import { createTownWithRequirements, createUserStreamAndSyncClient, everyoneMembershipStruct, expectUserCannotJoinSpace, expectUserCanJoin, linkWallets, twoNftRuleData, } from '../../testUtils';
import { dlog } from '@river-build/dlog';
import { CheckOperationType, LogicalOperationType, OperationType, TestERC721, treeToRuleData, encodeThresholdParams, } from '@river-build/web3';
const log = dlog('csb:test:spaceWithComplexEntitlements');
describe('spaceWithComplexEntitlements', () => {
let testNft1Address, testNft2Address, testNft3Address;
beforeAll(async () => {
;
[testNft1Address, testNft2Address, testNft3Address] = await Promise.all([
TestERC721.getContractAddress('TestNFT1'),
TestERC721.getContractAddress('TestNFT2'),
TestERC721.getContractAddress('TestNFT3'),
]);
});
test('two nft gate join pass', async () => {
const { alice, bob, aliceSpaceDapp, aliceProvider, alicesWallet, spaceId, channelId } = await createTownWithRequirements({
everyone: false,
users: [],
ruleData: twoNftRuleData(testNft1Address, testNft2Address),
});
const aliceMintTx1 = TestERC721.publicMint('TestNFT1', alicesWallet.address);
const aliceMintTx2 = TestERC721.publicMint('TestNFT2', alicesWallet.address);
log('Minting nfts for alice');
await Promise.all([aliceMintTx1, aliceMintTx2]);
log('Alice should be able to join space');
await expectUserCanJoin(spaceId, channelId, 'alice', alice, aliceSpaceDapp, alicesWallet.address, aliceProvider.wallet);
// kill the clients
const doneStart = Date.now();
await bob.stopSync();
await alice.stopSync();
log('Done', Date.now() - doneStart);
});
test('two nft gate join pass - across linked wallets', async () => {
const { alice, bob, aliceSpaceDapp, aliceProvider, carolProvider, alicesWallet, carolsWallet, spaceId, channelId, } = await createTownWithRequirements({
everyone: false,
users: [],
ruleData: twoNftRuleData(testNft1Address, testNft2Address),
});
const aliceMintTx1 = TestERC721.publicMint('TestNFT1', alicesWallet.address);
const carolMintTx2 = TestERC721.publicMint('TestNFT2', carolsWallet.address);
log('Minting nfts for alice and carol');
await Promise.all([aliceMintTx1, carolMintTx2]);
log("linking carols wallet to alice's wallet");
await linkWallets(aliceSpaceDapp, aliceProvider.wallet, carolProvider.wallet);
log('Alice should be able to join space with one asset in carol wallet');
await expectUserCanJoin(spaceId, channelId, 'alice', alice, aliceSpaceDapp, alicesWallet.address, aliceProvider.wallet);
// kill the clients
const doneStart = Date.now();
await bob.stopSync();
await alice.stopSync();
log('Done', Date.now() - doneStart);
});
test('two nft gate join fail', async () => {
const { alice, bob, aliceSpaceDapp, aliceProvider, alicesWallet, spaceId } = await createTownWithRequirements({
everyone: false,
users: [],
ruleData: twoNftRuleData(testNft1Address, testNft2Address),
});
// join alice
log('Minting an NFT for alice');
await TestERC721.publicMint('TestNFT1', alicesWallet.address);
// first join the space on chain
const aliceJoinStart = Date.now();
log('transaction start Alice joining space');
const { issued } = await aliceSpaceDapp.joinSpace(spaceId, alicesWallet.address, aliceProvider.wallet);
expect(issued).toBe(false);
log('Alice failed to join space and has a MembershipNFT', Date.now() - aliceJoinStart);
// Have alice create her own space so she can initialize her user stream.
// Then she will attempt to join the space from the client, which should fail.
await createUserStreamAndSyncClient(alice, aliceSpaceDapp, 'alice', await everyoneMembershipStruct(aliceSpaceDapp, alice), aliceProvider.wallet);
// Alice cannot join the space on the stream node.
await expectUserCannotJoinSpace(spaceId, alice, aliceSpaceDapp, alicesWallet.address);
// kill the clients
await bob.stopSync();
await alice.stopSync();
});
test('or of two nft gate join pass', async () => {
const { alice, bob, aliceSpaceDapp, aliceProvider, alicesWallet, spaceId, channelId } = await createTownWithRequirements({
everyone: false,
users: [],
ruleData: twoNftRuleData(testNft1Address, testNft2Address, LogicalOperationType.OR),
});
// join alice
log('Minting an NFT for alice');
await TestERC721.publicMint('TestNFT1', alicesWallet.address);
// first join the space on chain
log('Expect alice can join space');
await expectUserCanJoin(spaceId, channelId, 'alice', alice, aliceSpaceDapp, alicesWallet.address, aliceProvider.wallet);
// kill the clients
const doneStart = Date.now();
await bob.stopSync();
await alice.stopSync();
log('Done', Date.now() - doneStart);
});
test('or of two nft or one nft gate join pass', async () => {
const params = encodeThresholdParams({ threshold: 1n });
const leftOperation = {
opType: OperationType.CHECK,
checkType: CheckOperationType.ERC721,
chainId: 31337n,
contractAddress: testNft1Address,
params,
};
const rightOperation = {
opType: OperationType.CHECK,
checkType: CheckOperationType.ERC721,
chainId: 31337n,
contractAddress: testNft2Address,
params,
};
const two = {
opType: OperationType.LOGICAL,
logicalType: LogicalOperationType.AND,
leftOperation,
rightOperation,
};
const root = {
opType: OperationType.LOGICAL,
logicalType: LogicalOperationType.OR,
leftOperation: two,
rightOperation: {
opType: OperationType.CHECK,
checkType: CheckOperationType.ERC721,
chainId: 31337n,
contractAddress: testNft3Address,
params,
},
};
const ruleData = treeToRuleData(root);
const { alice, bob, aliceSpaceDapp, aliceProvider, alicesWallet, spaceId, channelId } = await createTownWithRequirements({
everyone: false,
users: [],
ruleData,
});
log("Mint Alice's NFTs");
const aliceMintTx1 = TestERC721.publicMint('TestNFT1', alicesWallet.address);
const aliceMintTx2 = TestERC721.publicMint('TestNFT2', alicesWallet.address);
await Promise.all([aliceMintTx1, aliceMintTx2]);
log('expect alice can join space');
await expectUserCanJoin(spaceId, channelId, 'alice', alice, aliceSpaceDapp, alicesWallet.address, aliceProvider.wallet);
// kill the clients
const doneStart = Date.now();
await bob.stopSync();
await alice.stopSync();
log('Done', Date.now() - doneStart);
});
});
//# sourceMappingURL=spaceWithComplexEntitlements.test.js.map