forked from OpenZeppelin/openzeppelin-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding ERC23 and approveWithCall() implementations - OpenZeppelin#346
- Loading branch information
Peter Murray
committed
Aug 3, 2017
1 parent
287b873
commit 7928c64
Showing
12 changed files
with
145 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pragma solidity ^0.4.11; | ||
|
||
contract ApprovalReceiver { | ||
function receiveApproval(address from, uint value, address tokenContract, bytes extraData) returns (bool); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
pragma solidity ^0.4.11; | ||
|
||
import "./ERC20Basic.sol"; | ||
|
||
contract ERC23 is ERC20Basic { | ||
function transfer(address to, uint value, string fallback, bytes data) returns (bool ok); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pragma solidity ^0.4.11; | ||
|
||
contract ERC23DefaultReceiver { | ||
function onTokenReceived(address sender, uint value, bytes data) returns (bool ok); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
pragma solidity ^0.4.11; | ||
|
||
import './BasicToken.sol'; | ||
import './ERC23.sol'; | ||
|
||
/** | ||
* @title Standard ERC23 token | ||
* | ||
* @dev Implementation of the ERC23 standard token. | ||
* @dev https://github.com/ethereum/EIPs/issues/223 | ||
* @dev Based on code by Dexaran: https://github.com/Dexaran/ERC23-tokens/tree/Custom-Fallback/token/ERC223 | ||
*/ | ||
contract StandardERC23Token is ERC23, BasicToken { | ||
|
||
// | ||
// overridden to call transfer(to, value, fallback, data) | ||
// | ||
function transfer(address _to, uint256 _value) returns (bool) { | ||
transfer(_to, _value, "onTokenReceived(address,uint256,bytes)", new bytes(0)); | ||
} | ||
|
||
function transfer(address to, uint value, string fallback, bytes data) returns (bool ok) { | ||
uint destinationCodeLength; | ||
|
||
assembly { | ||
destinationCodeLength:= extcodesize(to) | ||
} | ||
|
||
balances[msg.sender] = balances[msg.sender].sub(value); | ||
balances[to] = balances[to].add(value); | ||
if(destinationCodeLength > 0) { | ||
assert(to.call.value(0)(bytes4(sha3(fallback)), msg.sender, value, data)); | ||
} | ||
Transfer(msg.sender, to, value); | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
pragma solidity ^0.4.11; | ||
|
||
import './StandardToken.sol'; | ||
import './ApprovalReceiver.sol'; | ||
|
||
contract StandardTokenWithCall is StandardToken { | ||
|
||
function approveAndCall(address _spender, uint256 _value, bytes _extraData) returns (bool success) { | ||
assert(approve(_spender, _value)); | ||
return ApprovalReceiver(_spender).receiveApproval(msg.sender, _value, this, _extraData); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict'; | ||
|
||
const assertJump = require('./helpers/assertJump'); | ||
var StandardTokenWithCallMock = artifacts.require('./helpers/StandardTokenWithCallMock.sol'); | ||
var ApprovalReceiverMock = artifacts.require('./helpers/ApprovalReceiverMock.sol'); | ||
|
||
function awaitEvent(event, handler) { | ||
return new Promise((resolve, reject) => { | ||
function wrappedHandler(...args) { | ||
Promise.resolve(handler(...args)).then(resolve).catch(reject); | ||
} | ||
|
||
event.watch(wrappedHandler); | ||
}); | ||
} | ||
|
||
contract('StandardTokenWithCall', function(accounts) { | ||
let token; | ||
let receiver; | ||
|
||
beforeEach(async function() { | ||
token = await StandardTokenWithCallMock.new(accounts[0], 100); | ||
receiver = await ApprovalReceiverMock.new(); | ||
}); | ||
|
||
it('should approve and call', async function () { | ||
var event = null; | ||
var filter = receiver.ApprovalReceived({}, {}); | ||
|
||
await token.approveAndCall(receiver.address, 50, '0x123456'); | ||
await awaitEvent(filter, (err, e) => { | ||
event = e; | ||
}) | ||
assert.isOk(event); | ||
assert.equal(event.args.from, accounts[0]); | ||
assert.equal(event.args.value, 50); | ||
assert.equal(event.args.tokenContract, token.address); | ||
assert.equal(event.args.extra, '0x123456'); | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
pragma solidity ^0.4.11; | ||
|
||
|
||
import '../../contracts/token/ApprovalReceiver.sol'; | ||
import '../../contracts/token/ERC20.sol'; | ||
|
||
// mock class using StandardTokenWithCall | ||
contract ApprovalReceiverMock is ApprovalReceiver { | ||
|
||
event ApprovalReceived(address from, uint value, address tokenContract, bytes extra); | ||
|
||
function receiveApproval(address from, uint value, address tokenContract, bytes extraData) returns (bool) { | ||
assert(ERC20(tokenContract).transferFrom(from, this, value)); | ||
ApprovalReceived(from, value, tokenContract, extraData); | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
pragma solidity ^0.4.11; | ||
|
||
|
||
import '../../contracts/token/StandardTokenWithCall.sol'; | ||
|
||
|
||
// mock class using StandardTokenWithCall | ||
contract StandardTokenWithCallMock is StandardTokenWithCall { | ||
|
||
function StandardTokenWithCallMock(address initialAccount, uint256 initialBalance) { | ||
balances[initialAccount] = initialBalance; | ||
totalSupply = initialBalance; | ||
} | ||
|
||
} |