Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export async function requirePayment(toAddress: string, amount: number, currency: string): Promise {
try {
let fromAddress = await getUserAccount()
if (!fromAddress) {
throw new Error(`Not a web3 game session`)
}
let result: Promise
if (currency === 'ETH') {
result = requestManager.eth_sendTransaction({
to: toAddress,
from: fromAddress,
// TODO: fix this in eth-connect
value: toWei(amount as any, 'ether') as any,
data: null as any
})
} else {
const supportedTokens: Record = {} // a TODO: getNetworkConfigurations(network).paymentTokens
if (currency in supportedTokens === false) {
// tslint:disable:no-console
console.warn(`WARNING! Using a non-supported coin. Skipping operation! Please use one of the next coins 'ETH'`)
return false
}
const contractInstance = await getERC20(requestManager, supportedTokens[currency])
// Check this account has enough tokens for the transaction
const balance = await getERC20Balance(fromAddress, contractInstance.address)
if (balance.lt(amount)) {
import { ethereumConfigurations, ETHEREUM_NETWORK } from 'config'
import { defaultLogger } from 'shared/logger'
import { Account } from 'web3x/account'
import { getUserProfile } from 'shared/comms/peers'
import { getTLD } from '../../config/index'
import { removeUserProfile } from '../comms/peers'
import { Eth } from 'web3x/eth'
declare var window: Window & {
ethereum: any
web3: any
}
export const providerFuture = future()
export const requestManager = new RequestManager(null)
let providerRequested = false
function processLoginAttempt(response: IFuture<{ successful: boolean; provider: any; localIdentity?: Account }>) {
return async () => {
// TODO - look for user id matching account - moliva - 18/02/2020
let userData = getUserProfile()
// Modern dapp browsers...
if (window['ethereum']) {
if (!isSessionExpired(userData)) {
response.resolve({ successful: true, provider: window.ethereum })
} else {
showEthConnectAdvice(false)
let result
try {
export async function getERC20Balance(
address: string,
tokenAddress: string,
inWei: boolean = false
): Promise {
const contract = await getERC20(requestManager, tokenAddress)
const balance = await contract.balanceOf(address)
if (inWei) {
return balance
}
return fromWei(balance as any, 'ether') as any
}
function createProvider() {
const network = getTLD() === 'zone' ? ETHEREUM_NETWORK.ROPSTEN : ETHEREUM_NETWORK.MAINNET
return new WebSocketProvider(ethereumConfigurations[network].wss)
}
export async function getERC20(requestManager: RequestManager, address: Address): Promise {
return (await new ContractFactory(requestManager, ERC20Abi).at(address)) as IERC20
}
export async function getERC721(requestManager: RequestManager, address: Address): Promise {
return (await new ContractFactory(requestManager, ERC721Abi).at(address)) as IERC721
}