Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
this.getBalance(this.neon.address, assetid).subscribe((balances: any) => {
if (balances.length === 0) {
reject('no balance');
}
let assetId = balances[0].asset_id;
if (assetId.startsWith('0x') && assetId.length === 66) {
assetId = assetId.substring(2);
}
newTx.addOutput({ assetId, value: new Fixed8(amount), scriptHash: toScript });
let curr = 0.0;
for (const item of balances) {
curr += parseFloat(item.value) || 0;
newTx.inputs.push(new TransactionInput({
prevIndex: item.n, prevHash: item.txid.startsWith('0x') && item.txid.length == 66 ? item.txid.substring(2) : item.txid
}));
if (curr >= amount + fee) {
break;
}
}
const payback = (assetId === GAS || assetId === GAS.substring(2)) ?
this.global.mathSub(this.global.mathSub(curr, amount), fee) : this.global.mathSub(curr, amount);
if (payback < 0) {
reject('no enough balance to pay');
}
if (payback > 0) {
const fromScript = wallet.getScriptHashFromAddress(from);
const toScript = wallet.getScriptHashFromAddress(to);
if (fromScript.length !== 40 || toScript.length !== 40) {
throw new Error('target address error');
}
if (balances.length === 0) {
throw new Error('no balance');
}
let assetId = balances[0].asset_id;
if (assetId.startsWith('0x') && assetId.length === 66) {
assetId = assetId.substring(2);
}
const newTx = new tx.ContractTransaction();
newTx.addOutput({ assetId, value: new Fixed8(amount), scriptHash: toScript });
let curr = 0.0;
for (const item of balances) {
curr += parseFloat(item.value) || 0;
newTx.inputs.push(new TransactionInput({
prevIndex: item.n, prevHash: item.txid.startsWith('0x') &&
item.txid.length === 66 ? item.txid.substring(2) : item.txid
}));
if (curr >= amount + fee) {
break;
}
}
const payback = (assetId === GAS || assetId === GAS.substring(2)) ?
this.global.mathSub(this.global.mathSub(curr, amount), fee) : this.global.mathSub(curr, amount);
if (payback < 0) {
throw new Error('no enough balance to pay');
}
for (const item of balances) {
curr += parseFloat(item.value) || 0;
newTx.inputs.push(new TransactionInput({
prevIndex: item.n, prevHash: item.txid.startsWith('0x') && item.txid.length == 66 ? item.txid.substring(2) : item.txid
}));
if (curr >= amount + fee) {
break;
}
}
const payback = (assetId === GAS || assetId === GAS.substring(2)) ?
this.global.mathSub(this.global.mathSub(curr, amount), fee) : this.global.mathSub(curr, amount);
if (payback < 0) {
reject('no enough balance to pay');
}
if (payback > 0) {
newTx.addOutput({ assetId, value: new Fixed8(payback), scriptHash: fromScript });
}
resolve(newTx);
});
});
curr += parseFloat(item.value) || 0;
newTx.inputs.push(new TransactionInput({
prevIndex: item.n, prevHash: item.txid.startsWith('0x') &&
item.txid.length === 66 ? item.txid.substring(2) : item.txid
}));
if (curr >= amount + fee) {
break;
}
}
const payback = (assetId === GAS || assetId === GAS.substring(2)) ?
this.global.mathSub(this.global.mathSub(curr, amount), fee) : this.global.mathSub(curr, amount);
if (payback < 0) {
throw new Error('no enough balance to pay');
}
if (payback > 0) {
newTx.addOutput({ assetId, value: new Fixed8(payback), scriptHash: fromScript });
}
return newTx;
}
public createTxForNEP5(from: string, to: string, scriptHash: string, amount: number, decimals: number): Transaction {
} catch (error) {
reject(error);
}
try {
newTx = await this.addFee(fromAddress, newTx, amount + parseFloat(this.pramsData.networkFee));
} catch (error) {
this.chrome.windowCallback({
error: ERRORS.INSUFFICIENT_FUNDS,
return: requestTarget.Deploy,
ID: this.messageID
});
window.close();
}
const uniqTag = `from NEOLine at ${new Date().getTime()}`;
newTx.addAttribute(tx.TxAttrUsage.Remark1, u.reverseHex(u.str2hexstring(uniqTag)));
newTx.gas = new Fixed8(amount);
resolve(newTx);
});
}