-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal.js
More file actions
47 lines (38 loc) · 1.47 KB
/
local.js
File metadata and controls
47 lines (38 loc) · 1.47 KB
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
// To test in local, run these 2 script from the library (directory packages/requestNetwork.js in repository RequestNetwork/requestNetwork):
// npm run ganache
// npm run testdeploy
const RequestNetwork = require('@requestnetwork/request-network.js');
const Web3 = require('web3');
(async () => {
// Initialize web3 with local provider
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
// Initialize the Request Network library
const requestNetwork = new RequestNetwork.default({
provider: web3.currentProvider,
ethNetworkId: 10000000000,
useIpfsPublic: false,
});
// Create a request as payer:
const [payeeAddress, payerAddress] = await web3.eth.getAccounts();
const payerInfo = {
idAddress: payerAddress,
refundAddress: payerAddress,
};
const payeesInfo = [{
idAddress: payeeAddress,
paymentAddress: payeeAddress,
expectedAmount: web3.utils.toWei('1.5', 'ether'),
}]
const { request } = await requestNetwork.createRequest(
RequestNetwork.Types.Role.Payee,
RequestNetwork.Types.Currency.ETH,
payeesInfo,
payerInfo,
);
// Pay a request
await request.pay([web3.utils.toWei('1.5', 'ether')], [0], { from: payerAddress });
// The balance is the same amount as the the expected amount, the request is paid
const data = await request.getData();
console.log(web3.utils.fromWei(data.payee.expectedAmount.toString()));
console.log(web3.utils.fromWei(data.payee.balance.toString()));
})();