Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
getTxFeeInWei = (txSpeed?: string, gasLimit?: number): BigNumber => {
const { gasInfo, activeAccount } = this.props;
if (activeAccount && checkIfSmartWalletAccount(activeAccount)) {
return this.getSmartWalletTxFeeInWei();
}
txSpeed = txSpeed || this.getTxSpeed();
// calculate either with gasLimit in state or provided as param
if (!gasLimit) {
({ gasLimit } = this.state);
}
const gasPrice = gasInfo.gasPrice[txSpeed] || 0;
const gasPriceWei = utils.parseUnits(gasPrice.toString(), 'gwei');
return gasPriceWei.mul(gasLimit);
};
export const calculateMaxAmount = (token: string, balance: number | string, txFeeInWei: BigNumber): number => {
if (typeof balance !== 'string') {
balance = balance.toString();
}
if (token !== ETH) {
return +balance;
}
// we need to convert txFeeInWei to BigNumber as ethers.js utils use different library for Big Numbers
const maxAmount = utils.parseUnits(balance, 'ether').sub(utils.bigNumberify(txFeeInWei.toString()));
if (maxAmount.lt(0)) return 0;
return new BigNumber(utils.formatEther(maxAmount)).toNumber();
};
it('should start challenge successfully without correcting cumulative transfer amount', async () => {
await ethersDriipSettlementChallengeByPayment.startChallengeFromPaymentByProxy(
payment.sender.wallet, payment, payment.sender.balances.current, {gasLimit: 3e6}
);
const logs = await provider.getLogs(filter);
logs[logs.length - 1].topics[0].should.equal(filter.topics[0]);
const proposal = await ethersDriipSettlementChallengeState._proposals(0);
proposal.wallet.should.equal(utils.getAddress(payment.sender.wallet));
proposal.amounts.cumulativeTransfer._bn.should.eq.BN(
payment.sender.balances.current.sub(utils.parseUnits('10000', 18))._bn
);
proposal.amounts.stage._bn.should.eq.BN(payment.sender.balances.current._bn);
proposal.amounts.targetBalance._bn.should.eq.BN(0);
proposal.currency.ct.should.equal(payment.currency.ct);
proposal.currency.id._bn.should.eq.BN(payment.currency.id._bn);
proposal.referenceBlockNumber._bn.should.eq.BN(payment.blockNumber._bn);
proposal.nonce._bn.should.eq.BN(payment.sender.nonce._bn);
proposal.walletInitiated.should.be.false;
proposal.challenged.hash.should.equal(payment.seals.operator.hash);
proposal.challenged.kind.should.equal('payment');
});
});
ct: exports.address2,
id: utils.bigNumberify(0)
}
}
}
]
}
},
transfers: {
intended: {
single: utils.parseUnits('100', 18),
total: utils.parseUnits('200', 18)
},
conjugate: {
single: utils.parseUnits('0.1', 18),
total: utils.parseUnits('0.2', 18)
}
},
blockNumber: utils.bigNumberify(0),
operatorId: utils.bigNumberify(0)
}, params);
const operatorSigner = exports.createWeb3Signer(operator);
return await exports.augmentTradeSeal(trade, operatorSigner);
};
previous: utils.parseUnits('700', 18)
}
},
balances: {
intended: {
current: utils.parseUnits('19500', 18),
previous: utils.parseUnits('19600', 18)
},
conjugate: {
current: utils.parseUnits('19.6996', 18),
previous: utils.parseUnits('19.5998', 18)
}
},
fees: {
single: {
amount: utils.parseUnits('0.0002', 18),
currency: {
ct: exports.address2,
id: utils.bigNumberify(0)
}
},
total: [
{
originId: utils.bigNumberify(0),
figure: {
amount: utils.parseUnits('0.0004', 18),
currency: {
ct: exports.address2,
id: utils.bigNumberify(0)
}
}
}
await ethersDriipSettlementState.initSettlement(
'trade', trade.seal.hash,
trade.seller.wallet, trade.seller.nonce,
trade.buyer.wallet, trade.buyer.nonce,
{gasLimit: 1e6}
);
await ethersDriipSettlementState._setSettlementPartyDoneBlockNumber(
mocks.settlementRoles.indexOf('Target'), trade.blockNumber.add(1)
);
await ethersBalanceTracker._set(
await ethersBalanceTracker.depositedBalanceType(), utils.parseUnits('10000', 18),
{gasLimit: 1e6}
);
await ethersBalanceTracker._setFungibleRecord(
await ethersBalanceTracker.depositedBalanceType(), utils.parseUnits('10000', 18),
trade.blockNumber, {gasLimit: 1e6}
);
filter = {
fromBlock: await provider.getBlockNumber(),
topics: ethersDriipSettlementChallengeByTrade.interface.events['StartChallengeFromTradeByProxyEvent'].topics
};
});
exports.mockPayment = async (operator, params) => {
const senderWallet = Wallet.createRandom();
const recipientWallet = Wallet.createRandom();
const payment = exports.mergeDeep({
amount: utils.parseUnits('100', 18),
currency: {
ct: exports.address1,
id: utils.bigNumberify(0)
},
sender: {
wallet: senderWallet.address,
nonce: utils.bigNumberify(1),
balances: {
current: utils.parseUnits('9399.8', 18),
previous: utils.parseUnits('9500', 18)
},
fees: {
single: {
amount: utils.parseUnits('0.2', 18),
currency: {
ct: exports.address1,
getTxFeeInWei = () => {
const { gasInfo } = this.props;
const { gasLimit } = this.state;
const gasPrice = gasInfo.gasPrice[NORMAL] || 0;
const gasPriceWei = utils.parseUnits(gasPrice.toString(), 'gwei');
return gasPriceWei.mul(gasLimit);
};
return async (dispatch: Dispatch, getState: GetState) => {
if (!smartWalletService || !smartWalletService.sdkInitialized) return;
const accountAssets = accountAssetsSelector(getState());
const { decimals = 18 } = accountAssets[PPN_TOKEN] || {};
const value = utils.parseUnits(amount, decimals);
const tokenAddress = getPPNTokenAddress(PPN_TOKEN, accountAssets);
const response = await smartWalletService
.estimateTopUpAccountVirtualBalance(value, tokenAddress)
.catch((e) => {
Toast.show({
message: e.toString(),
type: 'warning',
autoClose: false,
});
return {};
});
if (isEmpty(response)) return;
const { gasAmount, gasPrice, totalCost } = parseEstimatePayload(response);
onButtonPress = async () => {
try {
this.props.components.setLoadingVisible(true);
const address = await this.props.services.ethereumService.ethereumAddressFromPublicKey(this.props.context.friend.ethereum_pub_Key);
const etherAmount = this.state.from.currency === 'ETH' ? this.state.from.amount : this.state.to.amount;
const xpatAmount = this.state.from.currency === 'XPAT' ? this.state.from.amount : this.state.to.amount;
const tokenAddress = this.props.services.getXPATTokenAddress();
const result = await this.props.services.deployContract(
ContractInfo.bytecode,
ContractInfo.abi,
etherAmount,
tokenAddress,
utils.parseEther(etherAmount),
utils.parseUnits(xpatAmount, 18),
address,
);
const data: MessageData = {
deployTxHash: result.hash,
etherAmount,
tokenAmount: xpatAmount,
tokensFromAddress: address,
tokensFromName: this.props.context.friend.name,
tokenContractAddress: tokenAddress,
};
this.props.services.sendMessage('EXCHANGE', '', data, () => {
this.props.navigation.dismiss();
});
} catch (error) {
if (error.isCancelled === true) {