Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
{
raw: accountTZnew,
test: (expect, account) => {
expect(account.operations).toEqual([]);
},
transactions: [
{
name: "send 10% to existing tz",
transaction: t => ({
...t,
recipient: addressTZregular,
useAllAmount: true
}),
expectedStatus: {
errors: {
amount: new NotEnoughBalance()
},
warnings: {}
}
}
]
}
// FIXME libcore bugs
/*
{
raw: accountTZwithKT,
transactions: [
{
name: "from KT 1, send max to new account",
transaction: (t, account) => (
invariant(account.subAccounts, "subAccounts"),
error => {
if (error.name === "NotEnoughBalance") {
errors.amount = error;
} else {
throw error;
}
}
);
}
const totalSpent = useAllAmount ? a.balance : t.amount.plus(estimatedFees);
const amount = useAllAmount ? a.balance.minus(estimatedFees) : t.amount;
// FIXME libcore have a bug that don't detect some cases like when doing send max!
if (!errors.amount && useAllAmount && !amount.gt(0)) {
errors.amount = new NotEnoughBalance();
}
if (amount.gt(0) && estimatedFees.times(10).gt(amount)) {
warnings.feeTooHigh = new FeeTooHigh();
}
if (!errors.amount && amount.eq(0)) {
errors.amount = new AmountRequired();
}
return Promise.resolve({
errors,
warnings,
estimatedFees,
amount,
totalSpent
? BigNumber(t.amount)
: BigNumber(t.amount).plus(estimatedFees);
const amount = useAllAmount
? tokenAccount
? BigNumber(t.amount)
: account.balance.minus(estimatedFees)
: BigNumber(t.amount);
if (amount.gt(0) && estimatedFees.times(10).gt(amount)) {
warnings.feeTooHigh = new FeeTooHigh();
}
// Fill up transaction errors...
if (totalSpent.gt(account.balance)) {
errors.amount = new NotEnoughBalance();
}
// Fill up recipient errors...
if (!t.recipient) {
errors.recipient = new RecipientRequired("");
} else if (isInvalidRecipient(t.recipient)) {
errors.recipient = new InvalidAddress("");
}
return Promise.resolve({
errors,
warnings,
estimatedFees,
amount,
totalSpent
});
const totalSpent = useAllAmount
? account.balance
: BigNumber(t.amount).plus(estimatedFees);
const amount = useAllAmount
? account.balance.minus(estimatedFees)
: BigNumber(t.amount);
if (amount.gt(0) && estimatedFees.times(10).gt(amount)) {
warnings.feeTooHigh = new FeeTooHigh();
}
// Fill up transaction errors...
if (totalSpent.gt(account.balance)) {
errors.amount = new NotEnoughBalance();
}
// Fill up recipient errors...
if (!t.recipient) {
errors.recipient = new RecipientRequired("");
} else if (isInvalidRecipient(t.recipient)) {
errors.recipient = new InvalidAddress("");
}
return Promise.resolve({
errors,
warnings,
estimatedFees,
amount,
totalSpent
});
test("insufficient balance have an error", async () => {
let t = {
...bridge.createTransaction(account),
recipient: "0x5df0C369641B8Af3c7e9ae076E5466eF678319Cd",
amount: BigNumber(
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
)
};
t = await bridge.prepareTransaction(account, t);
let status = await bridge.getTransactionStatus(account, t);
expect(status.errors.amount).toEqual(new NotEnoughBalance());
});
});
test("Amount to high should have a balanceError", async () => {
let t = {
...bridge.createTransaction(account),
recipient: "1FMpdbiC8dj7kHJ8tPWFcihvAcqEqramoN",
feePerByte: BigNumber(1),
amount: BigNumber(979079019)
};
let status = await bridge.getTransactionStatus(account, t);
expect(status.errors.recipient).toBeUndefined();
let transaction = await bridge.prepareTransaction(account, t);
status = await bridge.getTransactionStatus(account, transaction);
expect(status.errors.amount).toEqual(new NotEnoughBalance());
t = {
...t,
feePerByte: BigNumber(9999999),
amount: BigNumber(300)
};
status = await bridge.getTransactionStatus(account, t); //NB not used?
transaction = await bridge.prepareTransaction(account, t);
status = await bridge.getTransactionStatus(account, transaction);
expect(status.errors.amount).toEqual(new NotEnoughBalance());
});
const warnings = {};
const estimatedFees = (t.gasPrice || BigNumber(0)).times(getGasLimit(t));
const totalSpent = BigNumber(t.amount || 0).plus(estimatedFees);
const amount = BigNumber(t.amount || 0);
if (amount.gt(0) && estimatedFees.times(10).gt(amount)) {
warnings.feeTooHigh = new FeeTooHigh();
}
const gasLimit = getGasLimit(t);
if (!t.gasPrice) {
errors.gasPrice = new FeeNotLoaded();
} else if (gasLimit.eq(0)) {
errors.gasLimit = new FeeRequired();
} else if (totalSpent.gt(a.balance)) {
errors.amount = new NotEnoughBalance();
}
if (t.estimatedGasLimit && gasLimit.lt(t.estimatedGasLimit)) {
warnings.gasLimit = new GasLessThanEstimate();
}
let recipientWarning = getRecipientWarning(a.currency, t.recipient);
if (recipientWarning) {
warnings.recipient = recipientWarning;
}
if (!t.recipient) {
errors.recipient = new RecipientRequired("");
} else if (!isRecipientValid(a.currency, t.recipient)) {
errors.recipient = new InvalidAddress("", {
currencyName: a.currency.name
);
if (isCancelled()) return;
const transactionBuilder = await ethereumLikeAccount.buildTransaction();
if (isCancelled()) return;
if (subAccount && subAccount.type === "TokenAccount") {
const { balance, token } = subAccount;
let amount;
if (transaction.useAllAmount) {
amount = balance;
} else {
if (!transaction.amount) throw new Error("amount is missing");
amount = BigNumber(transaction.amount);
if (amount.gt(subAccount.balance)) {
throw new NotEnoughBalance();
}
}
const to256 = Buffer.concat([
Buffer.alloc(12),
Buffer.from(recipient.replace("0x", ""), "hex")
]);
invariant(to256.length === 32, "recipient is invalid");
const amountHex = amount.toString(16);
const amountBuf = Buffer.from(
amountHex.length % 2 === 0 ? amountHex : "0" + amountHex,
"hex"
);
const amount256 = Buffer.concat([
Buffer.alloc(32 - amountBuf.length),
amountBuf
]);
const checkValidTransaction = async (a, t) =>
!t.gasPrice
? Promise.reject(new FeeNotLoaded())
: t.useAllAmount ||
getMaxAmount(a, t).then(max => t.amount.isLessThanOrEqualTo(max))
? Promise.resolve(null)
: Promise.reject(new NotEnoughBalance());
checkValidTransaction: (a, t) =>
!t.gasPrice
? Promise.reject(new FeeNotLoaded())
: t.amount.isLessThanOrEqualTo(a.balance)
? Promise.resolve(null)
: Promise.reject(new NotEnoughBalance()),