/
TokenExchange.js
75 lines (55 loc) · 1.93 KB
/
TokenExchange.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
var Web3 = require('web3')
var solc = require('solc')
var fs = require('fs')
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var code = fs.readFileSync('TitanToken.sol').toString()
var compiledCode = solc.compile(code)
var data;
var exchangevalue;
var abiDefinition = JSON.parse(compiledCode.contracts[':TitanToken'].interface)
var Contract = web3.eth.contract(abiDefinition)
var byteCode = compiledCode.contracts[':TitanToken'].bytecode
var address;
const factor = 10; //factor that token exchange
//input _value
//this function helps to switch between two tokens
function switchContractForFrom(symbol){
switch (symbol){
case 'R':
address = '0xff';
contractFrom(address);
case 'A':
address = '0xaf';
contractFrom(address);
}
}
function switchContractForTo(symbol){
switch (symbol){
case 'R':
address = '0xff';
contractTo(address);
case 'A':
address = '0xaf';
contractTo(address);
}
}
function contractFrom(address) {
const DeployedContractRef = Contract.at(address);
// function transfer(address _to, uint256 _value)
data = DeployedContractRef.exchangeFrom(_value,{ from: web3.eth.accounts[0], gas: 400000 });
console.log(data);
DeployedContractRef.Eventexchangefrom().watch(function(error, result) {
console.log(result.args.result);
})
exchangevalue = DeployedContractRef.exchange(data,factor, { from: web3.eth.accounts[0], gas: 400000 });
console.log(exchangevalue);
}
function contractTo(address) {
const DeployedContractRef = Contract.at(address);
// function transfer(address _to, uint256 _value)
DeployedContractRef.exchangeTo(data,{ from: web3.eth.accounts[0], gas: 400000 });
console.log(data);
DeployedContractRef.Eventexchangeto().watch(function(error, result) {
console.log(result.args.result);
})
}