/
makeSendActions.js
48 lines (36 loc) · 1.28 KB
/
makeSendActions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { createActions } from 'spunky';
import Neon, { api, wallet } from '@cityofzion/neon-js';
import generateDAppActionId from './generateDAppActionId';
import { GAS, NEO } from '../../values/assets';
export const ID = 'send';
const send = async ({ net, asset, amount, receiver, address, wif }) => {
if (![GAS, NEO].includes(asset)) {
throw new Error(`Invalid asset: ${asset}`);
}
// TODO check if user has enough balance
if (!wallet.isAddress(receiver)) {
throw new Error(`Invalid script hash: "${receiver}"`);
}
if (amount <= 0) {
throw new Error(`Invalid amount: "${amount}"`);
}
const selectedAsset = asset === NEO ? 'NEO' : 'GAS';
const intents = await api.makeIntent({ [selectedAsset]: amount }, receiver);
const config = {
net,
address,
privateKey: new wallet.Account(wif).privateKey,
intents
};
const { response: { result, txid } } = await Neon.sendAsset(config);
if (!result) {
throw new Error('Invocation failed.');
}
return txid;
};
export default function makeSendActions(sessionId, requestId) {
const id = generateDAppActionId(sessionId, `${ID}-${requestId}`);
return createActions(id, ({ net, asset, amount, receiver, address, wif }) => () => {
return send({ net, asset, amount, receiver, address, wif });
});
}