Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

erc777 jsupdate #98

Merged
merged 2 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/hyperverse-evm-erc777/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,20 @@
},
"dependencies": {
"@decentology/hyperverse": "^1",
"@decentology/hyperverse-ethereum": "^1",
"@decentology/hyperverse-evm": "^1",
"@decentology/hyperverse-ethereum": "^1",
"@decentology/unstated-next": "^1",
"dotenv": "^16.0.0",
"ethers": "^5.5.3",
"hardhat-erc1820": "^0.1.0",
"react-query": "^3.34.7",
"react-use": "^17.3.2"
"react-use": "^17.3.2",
"real-cancellable-promise": "^1.1.1"
},
"devDependencies": {
"@babel/core": "^7.17.9",
"@decentology/config": "^1",
"@ethersproject/abstract-provider": "^5.6.0",
"@nomiclabs/hardhat-ethers": "^2.0.4",
"@nomiclabs/hardhat-waffle": "^2.0.2",
"@storybook/addon-actions": "^6.4.20",
Expand Down
12 changes: 3 additions & 9 deletions packages/hyperverse-evm-erc777/source/Provider.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import { HyperverseModuleInstance } from '@decentology/hyperverse';
import { ERC777 } from './useERC777';
import { FC } from 'react';
import { QueryClientProvider, QueryClient } from 'react-query';
const client = new QueryClient();
import { HyperverseModuleInstance } from '@decentology/hyperverse';

const Provider: FC<HyperverseModuleInstance> = ({ children, tenantId }) => {
if (tenantId == null) {
if (!tenantId) {
throw new Error('Tenant ID is required');
}
return (
<QueryClientProvider client={client}>
<ERC777.Provider initialState={{ tenantId: tenantId }}>{children}</ERC777.Provider>
</QueryClientProvider>
);
return <ERC777.Provider initialState={{ tenantId: tenantId }}>{children}</ERC777.Provider>;
};

export { Provider };
29 changes: 15 additions & 14 deletions packages/hyperverse-evm-erc777/source/environment.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
import {
Network,
Blockchain,
isEvm,
Blockchain,
BlockchainEvm,
EvmEnvironment,
useHyperverse,
NetworkConfig,
} from '@decentology/hyperverse';
import { ContractInterface } from 'ethers';
import Contracts from '../contracts.json';

import ERC777Factory from '../artifacts/contracts/ERC777Factory.sol/ERC777Factory.json';
import ERC777 from '../artifacts/contracts/ERC777.sol/ERC777.json';
export const ContractABI = ERC777.abi;
export const FactoryABI = ERC777Factory.abi;
import Contracts from '../contracts.json';

const environment = Contracts as EvmEnvironment;


function useEnvironment() {
const { blockchain, network } = useHyperverse();
if (blockchain == null) {
function getEnvironment(blockchainName: Blockchain, network: NetworkConfig) {
if (blockchainName == null) {
throw new Error('Blockchain is not set');
}
if (!isEvm(blockchain?.name)) {
if (!isEvm(blockchainName)) {
throw new Error('Blockchain is not EVM compatible');
}

const env = environment[blockchain.name as BlockchainEvm]
if (!env) {
throw new Error('Blockchain not found');
const chain = environment[blockchainName as BlockchainEvm];
if (!chain) {
throw new Error('Blockchain is not supported');
}

const env = chain[network.type];
return {
...env[network.type],
...env,
ContractABI,
FactoryABI,
};
}

export { environment, useEnvironment };
export { environment, getEnvironment };
294 changes: 294 additions & 0 deletions packages/hyperverse-evm-erc777/source/erc777Library.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
import { HyperverseConfig } from '@decentology/hyperverse';
import { EvmLibraryBase, getProvider } from '@decentology/hyperverse-evm';
import { ethers, BigNumber } from 'ethers';
import { TransactionReceipt } from '@ethersproject/abstract-provider';
import { CancellablePromise } from 'real-cancellable-promise';
import { getEnvironment } from './environment';
export type ERC777LibraryType = Awaited<ReturnType<typeof ERC777LibraryInternal>>;

export function ERC777Library(
...args: Parameters<typeof ERC777LibraryInternal>
): CancellablePromise<ERC777LibraryType> {
return new CancellablePromise(ERC777LibraryInternal(...args), () => {});
}

export async function ERC777LibraryInternal(
hyperverse: HyperverseConfig,
providerOrSigner?: ethers.providers.Provider | ethers.Signer
) {
const { FactoryABI, factoryAddress, ContractABI } = getEnvironment(
hyperverse.blockchain?.name!,
hyperverse.network
);

if (!providerOrSigner) {
providerOrSigner = getProvider(hyperverse.network);
}

const getTokenName = async () => {
try {
const name = await base.proxyContract?.name();
return name as string;
} catch (error) {
throw error;
}
};

const getTokenSymbol = async () => {
try {
const symbol = await base.proxyContract?.symbol();
return symbol as string;
} catch (error) {
throw error;
}
};

const getDecimal = async () => {
try {
const decimal = await base.proxyContract?.decimals();
return decimal as number;
} catch (error) {
throw error;
}
};

const getGranularity = async () => {
try {
const granularity = await base.proxyContract?.granularity();
return granularity as number;
} catch (error) {
throw error;
}
};

const getTotalSuply = async () => {
try {
const totalSupply = await base.proxyContract?.totalSupply();
return BigNumber.from(totalSupply) as BigNumber;
} catch (error) {
throw error;
}
};

const getBalanceOf = async (account: string) => {
try {
const balance = await base.proxyContract?.balanceOf(account);
return BigNumber.from(balance) as BigNumber;
} catch (error) {
throw error;
}
};

const getBalance = async () => {
try {
const balance = await base.proxyContract?.balance();
return BigNumber.from(balance) as BigNumber;
} catch (error) {
throw error;
}
};

const send = async ({ to, amount, data }: { to: string; amount: number; data: string }) => {
try {
const transferTxn = await base.proxyContract?.send(
to,
amount,
ethers.utils.formatBytes32String(data)
);
return transferTxn.wait() as TransactionReceipt;
} catch (error) {
throw error;
}
};

const transfer = async ({ to, amount }: { to: string; amount: number }) => {
try {
const transferTxn = await base.proxyContract?.transfer(to, amount);
return transferTxn.wait() as TransactionReceipt;
} catch (error) {
throw error;
}
};

const transferFrom = async ({
from,
to,
amount,
}: {
from: string;
to: string;
amount: number;
}) => {
try {
const transferTxn = await base.proxyContract?.transferFrom(from, to, amount);
return transferTxn.wait() as TransactionReceipt;
} catch (error) {
throw error;
}
};

const allowance = async (owner: string, spender: string) => {
try {
const allowance = await base.proxyContract?.allowance(owner, spender);
return BigNumber.from(allowance) as BigNumber;
} catch (error) {
throw error;
}
};

const approve = async ({ spender, amount }: { spender: string; amount: number }) => {
try {
const approveTxn = await base.proxyContract?.approve(spender, amount);
return approveTxn.wait() as TransactionReceipt;
} catch (error) {
throw error;
}
};

const getDefaultOperators = async () => {
try {
const operators = await base.proxyContract?.defaultOperators();
return operators;
} catch (error) {
throw error;
}
};

const checkOperator = async ({
operator,
tokenHolder,
}: {
operator: string;
tokenHolder: string;
}) => {
try {
const isOperator = await base.proxyContract?.isOperatorFor(operator, tokenHolder);
return isOperator;
} catch (error) {
throw error;
}
};

const authorizeOperator = async (operator: string) => {
try {
const authorizeTxn = await base.proxyContract?.authorizeOperator(operator);
return authorizeTxn.wait() as TransactionReceipt;
} catch (error) {
throw error;
}
};

const revokeOperator = async (operator: string) => {
try {
const revokeTxn = await base.proxyContract?.revokeOperator(operator);
return revokeTxn.wait() as TransactionReceipt;
} catch (error) {
throw error;
}
};

const operatorSend = async ({
sender,
recipient,
amount,
data,
operatorData,
}: {
sender: string;
recipient: string;
amount: number;
data: string;
operatorData: string;
}) => {
try {
const operatorSendTxn = await base.proxyContract?.operatorSend(
sender,
recipient,
amount,
ethers.utils.formatBytes32String(data),
ethers.utils.formatBytes32String(operatorData)
);
return operatorSendTxn.wait() as TransactionReceipt;
} catch (error) {
throw error;
}
};

const operatorBurn = async ({
account,
amount,
data,
operatorData,
}: {
account: string;
amount: number;
data: string;
operatorData: string;
}) => {
try {
const operatorBurnTxn = await base.proxyContract?.operatorBurn(
account,
amount,
ethers.utils.formatBytes32String(data),
ethers.utils.formatBytes32String(operatorData)
);
return operatorBurnTxn.wait() as TransactionReceipt;
} catch (error) {
throw error;
}
};

const burn = async ({ amount, data }: { amount: number; data: string }) => {
try {
const burnTxn = await base.proxyContract?.burn(
amount,
ethers.utils.formatBytes32String(data)
);
return burnTxn.wait() as TransactionReceipt;
} catch (error) {
throw error;
}
};

const mint = async (amount: number) => {
try {
const mintTxn = await base.proxyContract?.mint(amount);
return mintTxn.wait() as TransactionReceipt;
} catch (error) {
throw error;
}
};

const base = await EvmLibraryBase(
'ERC20',
hyperverse,
factoryAddress!,
FactoryABI,
ContractABI,
providerOrSigner
);

return {
...base,
getTokenName,
getTokenSymbol,
getDecimal,
getGranularity,
getTotalSuply,
getBalance,
getBalanceOf,
send,
transfer,
transferFrom,
allowance,
approve,
getDefaultOperators,
checkOperator,
authorizeOperator,
revokeOperator,
operatorSend,
operatorBurn,
burn,
mint,
};
}
Loading