This repository was archived by the owner on Sep 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (87 loc) · 2.2 KB
/
index.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
*
* Decentralized Record of Value
* DRV100: Record
*
*/
const isValid = require('./validations');
const SUCCESS_CODE = 200;
module.exports = ({ drv, peers, serviceEvents }) => {
/*
* Define a contract that either transfers DRV from
* a sender to a recipient, or records collateral (like USD)
* from a recipient, then recursively transfers DRV
* against it.
*/
const Contract = async ({
senderAddress,
recipientAddress,
contract = 'DRV100',
usdValue,
drvValue,
isDrv
}) => {
if (!isValid({
senderAddress,
recipientAddress,
contract,
usdValue,
drvValue
})) return false;
/*
* If DRV is being transferred, send a normal POST request
* to `drv-core` and return the result.
*/
if (isDrv) {
const transactionResult = await serviceEvents.onServicePost({
service: drv,
serviceName: '/',
method: 'transaction',
body: {
senderAddress,
recipientAddress,
contract,
usdValue,
drvValue,
peers: Object.values(peers)
}
});
if (!transactionResult || transactionResult.status !== SUCCESS_CODE) {
return false;
}
return transactionResult;
}
/*
* If non-DRV is being transferred, send a GET request
* to `drv-core` to make sure the DRV is tradable.
* (hit the /price endpoint)
*/
const priceResult = await serviceEvents.onServiceGet({
service: drv,
serviceName: '/',
method: 'price'
});
if (!priceResult || priceResult.status !== SUCCESS_CODE) {
return false;
}
/*
* If it has a valid price, recursively call this contract
* transferring the originally requested DRV amount.
*/
const transferResult = await Contract({
senderAddress: recipientAddress,
recipientAddress: senderAddress,
contract: 'DRV100',
usdValue,
drvValue,
isDrv: true
});
if (!transferResult) {
console.log(
'<DRV> Transfer Error: There was a problem transferring DRV between accounts.', senderAddress, recipientAddress
);
}
return transferResult;
};
return Contract;
};