Skip to content

Commit

Permalink
Merge branch 'feat/nestjs' into feat/evm-whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
gelicamarie committed Apr 19, 2022
2 parents 91ae247 + e817366 commit 7ed136d
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 51 deletions.
26 changes: 13 additions & 13 deletions apps/ethereum/nest/src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ const hyperverse = initialize({
blockchain: Ethereum,
network: Network.Testnet,
modules: [
{ bundle: Tribes, tenantId: '0x62a7aa79a52591Ccc62B71729329A80a666fA50f' }
]
{
bundle: Tribes,
tenantId: '0x62a7aa79a52591Ccc62B71729329A80a666fA50f',
},
],
});


const provider = new ethers.providers.InfuraProvider({
chainId: 4,
name: 'rinkeby',
}, 'fb9f66bab7574d70b281f62e19c27d49')


const tribes = new TribesLibrary(
hyperverse,
provider
const provider = new ethers.providers.InfuraProvider(
{
chainId: 4,
name: 'rinkeby',
},
'fb9f66bab7574d70b281f62e19c27d49',
);

Injectable()
Injectable();
export class AppService {
async getHello(): Promise<string> {
const tribes = await TribesLibrary(hyperverse, provider);
const totalTribes = await tribes.getTotalTenants();
return renderToStaticMarkup(
React.createElement('div', null, `Total Tenants: ${0}`),
Expand Down
1 change: 1 addition & 0 deletions packages/hyperverse-evm-tribes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"hardhat": "^2.9.1",
"react-query": "^3.34.7",
"react-use": "^17.3.2",
"real-cancellable-promise": "^1.1.1",
"skynet-js": "^4.0.23-beta"
},
"devDependencies": {
Expand Down
40 changes: 29 additions & 11 deletions packages/hyperverse-evm-tribes/source/library/TribesLibrary.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { HyperverseConfig } from '@decentology/hyperverse';
import { BaseLibrary, getProvider } from '@decentology/hyperverse-evm';
import { ethers } from 'ethers';
import { CancellablePromise } from 'real-cancellable-promise';
import { getEnvironment } from '../environment';
import { MetaData } from '../types';

export type TribesLibraryType = Awaited<ReturnType<typeof TribesLibrary>>;
export function TribesLibraryPromise(
hyperverse: HyperverseConfig,
providerOrSigner?: ethers.providers.Provider | ethers.Signer
): CancellablePromise<TribesLibraryType> {
return new CancellablePromise(TribesLibrary(hyperverse, providerOrSigner), () => {});
}
export async function TribesLibrary(

hyperverse: HyperverseConfig,
providerOrSigner?: ethers.providers.Provider | ethers.Signer
) {
Expand All @@ -17,7 +23,13 @@ export async function TribesLibrary(
if (!providerOrSigner) {
providerOrSigner = getProvider(hyperverse.network);
}
const base = await BaseLibrary(hyperverse, factoryAddress!, FactoryABI, ContractABI, providerOrSigner);
const base = await BaseLibrary(
hyperverse,
factoryAddress!,
FactoryABI,
ContractABI,
providerOrSigner
);

const factoryErrors = (err: any) => {
if (!base.factoryContract?.signer) {
Expand All @@ -42,7 +54,6 @@ export async function TribesLibrary(
return json;
};


return {
...base,
getTribeId: async (account: string) => {
Expand Down Expand Up @@ -100,24 +111,29 @@ export async function TribesLibrary(
0
);
const members = events
?.map((e) => {
?.map(e => {
if (e.args) {
return {
tribeId: e.args[0].toNumber(),
account: e.args[1],
account: e.args[1]
};
}
})
.filter((e) => e?.tribeId === tribeId);
.filter(e => e?.tribeId === tribeId);
return members;
} catch (err) {
throw err;
}
},

joinTribe: async (id: number) => {
debugger;
try {
// @ts-ignore
console.log(
'joinTribe',
base.proxyContract.signer,
window['tribesLibrary'] === this
);
const joinTxn = await base.proxyContract?.joinTribe(id);
return joinTxn.wait();
} catch (err) {
Expand All @@ -141,10 +157,12 @@ export async function TribesLibrary(
const { skylink: imageLink } = await hyperverse!.storage!.uploadFile(image);
const fullMetaData: MetaData = {
...metadata,
image: imageLink,
image: imageLink
};
const metadataFile = new File([JSON.stringify(fullMetaData)], 'metadata.json');
const { skylink: metadataFileLink } = await hyperverse!.storage!.uploadFile(metadataFile);
const { skylink: metadataFileLink } = await hyperverse!.storage!.uploadFile(
metadataFile
);

const addTxn = await base.proxyContract?.addNewTribe(metadataFileLink);
return addTxn.wait();
Expand All @@ -155,6 +173,6 @@ export async function TribesLibrary(

useTribeEvents: (eventName: string, callback: any) => {
// return useEvent(eventName, useCallback(callback, [proxyContract]), proxyContract);
},
}
}
};
}
23 changes: 17 additions & 6 deletions packages/hyperverse-evm-tribes/source/useTribes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,28 @@ function TribesState(initialState: { tenantId: string } = { tenantId: '' }) {
const hyperverse = useHyperverse();
const [tribesLibrary, setTribesLibrary] = useState<TribesLibraryType>();


useEffect(() => {
TribesLibrary(hyperverse, connectedProvider || readOnlyProvider).then(setTribesLibrary);
}, [readOnlyProvider, connectedProvider])

let cancel = false;
TribesLibrary(hyperverse, connectedProvider || readOnlyProvider).then((result) => {
if(!cancel) {
setTribesLibrary(result);
}
});
return () => {
cancel = true;
}
}, [ connectedProvider])

if(typeof window !=='undefined') {
// @ts-ignore
window['tribesLibrary'] = tribesLibrary;
}
const useTribeEvents = (eventName: string, callback: any) => {
return useEvent(eventName, useCallback(callback, [tribesLibrary?.proxyContract]), tribesLibrary?.proxyContract);
};



return {
tenantId,
factoryContract: tribesLibrary?.factoryContract,
Expand Down Expand Up @@ -69,10 +80,10 @@ function TribesState(initialState: { tenantId: string } = { tenantId: '' }) {
if (fn) fn(...args);
},
}),
TribeId: () => useQuery(['getTribeId', address,], () => tribesLibrary?.getTribeId(address!),{enabled: !!tribesLibrary}),
TribeId: () => useQuery(['getTribeId', address,], () => tribesLibrary?.getTribeId(address!), { enabled: !!tribesLibrary }),
Tribe: () => {
const { data: tribeId } = useQuery(['getTribeId', address,], () => tribesLibrary?.getTribeId(address!),
{ enabled: !!tribesLibrary}
{ enabled: !!tribesLibrary }
);
return useQuery(['getTribeData', tribeId], () => tribesLibrary?.getTribe(tribeId),);
},
Expand Down
47 changes: 26 additions & 21 deletions packages/hyperverse-evm/source/library/evmLibrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,36 @@ export async function BaseLibrary(
providerOrSigner: ethers.providers.Provider | ethers.Signer
) {

const factoryContract = new ethers.Contract(
let signer: ethers.Signer | undefined;
console.log('What is the provider?', providerOrSigner);
if (providerOrSigner instanceof Web3Provider) {
signer = providerOrSigner.getSigner();
}

let factoryContract = new ethers.Contract(
factoryAddress!,
factoryABI,
providerOrSigner
signer || providerOrSigner
) as Contract;
const tenantId = hyperverse.modules.find((x) => x.bundle.ModuleName === 'Tribes')?.tenantId;
if (!tenantId) {
throw new Error('Tenant ID is required');
}

// const setProvider = (provider: ethers.providers.Provider) => {
// let signer: ethers.Signer | undefined;
// if (provider instanceof Web3Provider) {
// signer = provider.getSigner();
// }
// this.factoryContract = new ethers.Contract(
// this.factoryAddress!,
// this.factoryABI,
// signer || provider
// ) as Contract;
// if (this.proxyContract) {
// this.proxyContract = new ethers.Contract(
// this.proxyContract.address,
// this.contractABI,
// signer || provider
// ) as Contract;
// }
// }
const setProvider = (provider: ethers.providers.Provider) => {
factoryContract = new ethers.Contract(
factoryAddress!,
factoryABI,
signer || provider
) as Contract;
if (proxyContract) {
proxyContract = new ethers.Contract(
proxyContract.address,
contractABI,
signer || provider
) as Contract;
}
}

let proxyAddress: string
let proxyContract: Contract | undefined;
Expand All @@ -60,9 +62,11 @@ export async function BaseLibrary(
proxyContract = new ethers.Contract(
proxyAddress,
contractABI,
providerOrSigner
signer || providerOrSigner
) as Contract;



const ready = true;

const checkInstance = async (account: any) => {
Expand Down Expand Up @@ -98,6 +102,7 @@ export async function BaseLibrary(

return {
ready,
setProvider,
checkInstance,
createInstance,
factoryContract,
Expand Down

0 comments on commit 7ed136d

Please sign in to comment.