Skip to content

Commit

Permalink
add depositTo
Browse files Browse the repository at this point in the history
  • Loading branch information
OneRootNetwork committed Oct 8, 2018
1 parent 9b4152e commit c317745
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
19 changes: 17 additions & 2 deletions contracts/R1Exchange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ contract R1Exchange is SafeMath, Ownable {
bool public stop = false;

event Deposit(address indexed token, address indexed user, uint256 amount, uint256 balance);
event DepositTo(address indexed token, address indexed from, address indexed user, uint256 amount, uint256 balance);
event Withdraw(address indexed token, address indexed user, uint256 amount, uint256 balance);
event ApplyWithdraw(address indexed token, address indexed user, uint256 amount, uint256 time);
event Trade(address indexed maker, address indexed taker, uint256 amount, uint256 makerFee, uint256 takerFee, uint256 makerNonce, uint256 takerNonce);
Expand Down Expand Up @@ -101,6 +102,20 @@ contract R1Exchange is SafeMath, Ownable {
Deposit(token, msg.sender, amount, tokenList[token][msg.sender]);
}

function depositTo(address token, address to, uint256 amount) public {
require(token != 0 && to != 0);
tokenList[token][to] = safeAdd(tokenList[token][to], amount);
require(Token(token).transferFrom(msg.sender, this, amount));
DepositTo(token, msg.sender, to, amount, tokenList[token][to]);
}

function batchDepositTo(address token, address[] to, uint256[] amount) public {
require(to.length == amount.length && to.length <= 200);
for (uint i = 0; i < to.length; i++) {
depositTo(token, to[i], amount[i]);
}
}

function applyWithdraw(address token, uint256 amount) public {
uint256 apply = safeAdd(applyList[token][msg.sender], amount);
require(safeAdd(apply, withdrawAllowance[token][msg.sender]) <= tokenList[token][msg.sender]);
Expand Down Expand Up @@ -187,7 +202,7 @@ contract R1Exchange is SafeMath, Ownable {
require(amount <= tokenList[token][user]);
fee = checkFee(amount, fee);

bytes32 hash = keccak256(this,user, token, amount, nonce);
bytes32 hash = keccak256(this, user, token, amount, nonce);
require(!withdrawn[hash]);
withdrawn[hash] = true;
require(ecrecover(keccak256("\x19Ethereum Signed Message:\n32", hash), v, r, s) == user);
Expand All @@ -213,7 +228,7 @@ contract R1Exchange is SafeMath, Ownable {
}

function getOrderHash(address tokenBuy, uint256 amountBuy, address tokenSell, uint256 amountSell, address base, uint256 expires, uint256 nonce, address feeToken) public view returns (bytes32) {
return keccak256(this,tokenBuy, amountBuy, tokenSell, amountSell, base, expires, nonce, feeToken);
return keccak256(this, tokenBuy, amountBuy, tokenSell, amountSell, base, expires, nonce, feeToken);
}


Expand Down
10 changes: 5 additions & 5 deletions test/deploy.js

Large diffs are not rendered by default.

15 changes: 13 additions & 2 deletions test/test_r1.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,22 @@ contract("Exchange", function (accounts) {


})
// it("depositTo", async () => {
// let tokenInstance = await RNTToken.deployed()
// let exchangeInstance = await Exchange.deployed()
// await tokenInstance.transfer(taker, web3.toWei(takerRNTBalance, "ether"), {from: owner})
// await tokenInstance.approve(exchangeInstance.address, web3.toWei(1000000000000, "ether"), {from: taker})
// let result = await exchangeInstance.batchDepositTo(tokenInstance.address, [maker], [web3.toWei(1, "ether")], {from: taker})
// assert.equal(result.receipt.status, 1, "taker deposit rnt to maker failed")
// balance = await exchangeInstance.balanceOf(tokenInstance.address, maker)
// assert.equal(web3.fromWei(balance.valueOf()), 1, "taker deposit " + initRNT + " rnt to maker failed")
// })

it("adminWithdraw", async () => {
let tokenInstance = await RNTToken.deployed()
let exchangeInstance = await Exchange.deployed()
hash = "0x" + abi.soliditySHA3(["address","address", "address", "uint256", "uint256"],
[exchangeInstance.address,taker, tokenInstance.address, web3.toWei("1", "ether"), 11]
hash = "0x" + abi.soliditySHA3(["address", "address", "address", "uint256", "uint256"],
[exchangeInstance.address, taker, tokenInstance.address, web3.toWei("1", "ether"), 11]
).toString("hex")
console.log("=========", hash)
var signed = web3.eth.sign(taker, hash);
Expand Down

0 comments on commit c317745

Please sign in to comment.