Skip to content

Commit 210d1b2

Browse files
committed
Consolidates duplicate functions within a common library for token transfers (#464)
Use a TokenUtils for sending, pulling and burning tokens
1 parent 9d67083 commit 210d1b2

File tree

7 files changed

+262
-268
lines changed

7 files changed

+262
-268
lines changed

contracts/disputes/DisputeManager.sol

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import "@openzeppelin/contracts/cryptography/ECDSA.sol";
88

99
import "../governance/Managed.sol";
1010
import "../upgrades/GraphUpgradeable.sol";
11+
import "../utils/TokenUtils.sol";
1112

1213
import "./DisputeManagerStorage.sol";
1314
import "./IDisputeManager.sol";
@@ -562,7 +563,7 @@ contract DisputeManager is DisputeManagerV1Storage, GraphUpgradeable, IDisputeMa
562563
_slashIndexer(dispute.indexer, dispute.fisherman, dispute.disputeType);
563564

564565
// Give the fisherman their deposit back
565-
_pushTokens(dispute.fisherman, dispute.deposit);
566+
TokenUtils.pushTokens(graphToken(), dispute.fisherman, dispute.deposit);
566567

567568
// Resolve the conflicting dispute if any
568569
_resolveDisputeInConflict(dispute);
@@ -590,9 +591,7 @@ contract DisputeManager is DisputeManagerV1Storage, GraphUpgradeable, IDisputeMa
590591
);
591592

592593
// Burn the fisherman's deposit
593-
if (dispute.deposit > 0) {
594-
graphToken().burn(dispute.deposit);
595-
}
594+
TokenUtils.burnTokens(graphToken(), dispute.deposit);
596595

597596
emit DisputeRejected(_disputeID, dispute.indexer, dispute.fisherman, dispute.deposit);
598597
}
@@ -606,7 +605,7 @@ contract DisputeManager is DisputeManagerV1Storage, GraphUpgradeable, IDisputeMa
606605
Dispute memory dispute = _resolveDispute(_disputeID);
607606

608607
// Return deposit to the fisherman
609-
_pushTokens(dispute.fisherman, dispute.deposit);
608+
TokenUtils.pushTokens(graphToken(), dispute.fisherman, dispute.deposit);
610609

611610
// Resolve the conflicting dispute if any
612611
_resolveDisputeInConflict(dispute);
@@ -662,33 +661,7 @@ contract DisputeManager is DisputeManagerV1Storage, GraphUpgradeable, IDisputeMa
662661
require(_deposit >= minimumDeposit, "Dispute deposit is under minimum required");
663662

664663
// Transfer tokens to deposit from fisherman to this contract
665-
_pullTokens(msg.sender, _deposit);
666-
}
667-
668-
/**
669-
* @dev Pull tokens from an address to this contract.
670-
* @param _from Address sending the tokens
671-
* @param _amount Amount of tokens to transfer
672-
*/
673-
function _pullTokens(address _from, uint256 _amount) private {
674-
if (_amount > 0) {
675-
// Transfer tokens to deposit from fisherman to this contract
676-
require(
677-
graphToken().transferFrom(_from, address(this), _amount),
678-
"Cannot transfer tokens"
679-
);
680-
}
681-
}
682-
683-
/**
684-
* @dev Push tokens from this contract to a receiving address.
685-
* @param _to Address receiving the tokens
686-
* @param _amount Amount of tokens to transfer
687-
*/
688-
function _pushTokens(address _to, uint256 _amount) private {
689-
if (_amount > 0) {
690-
require(graphToken().transfer(_to, _amount), "Cannot transfer tokens");
691-
}
664+
TokenUtils.pullTokens(graphToken(), msg.sender, _deposit);
692665
}
693666

694667
/**

contracts/staking/Staking.sol

Lines changed: 12 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pragma experimental ABIEncoderV2;
66
import "@openzeppelin/contracts/cryptography/ECDSA.sol";
77

88
import "../upgrades/GraphUpgradeable.sol";
9+
import "../utils/TokenUtils.sol";
910

1011
import "./IStaking.sol";
1112
import "./StakingStorage.sol";
@@ -695,7 +696,7 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
695696
);
696697

697698
// Transfer tokens to stake from caller to this contract
698-
_pullTokens(graphToken(), msg.sender, _tokens);
699+
TokenUtils.pullTokens(graphToken(), msg.sender, _tokens);
699700

700701
// Stake the transferred tokens
701702
_stake(_indexer, _tokens);
@@ -789,10 +790,10 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
789790
IGraphToken graphToken = graphToken();
790791

791792
// Set apart the reward for the beneficiary and burn remaining slashed stake
792-
_burnTokens(graphToken, _tokens.sub(_reward));
793+
TokenUtils.burnTokens(graphToken, _tokens.sub(_reward));
793794

794795
// Give the beneficiary a reward for slashing
795-
_pushTokens(graphToken, _beneficiary, _reward);
796+
TokenUtils.pushTokens(graphToken, _beneficiary, _reward);
796797

797798
emit StakeSlashed(_indexer, _tokens, _reward, _beneficiary);
798799
}
@@ -812,7 +813,7 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
812813
address delegator = msg.sender;
813814

814815
// Transfer tokens to delegate to this contract
815-
_pullTokens(graphToken(), delegator, _tokens);
816+
TokenUtils.pullTokens(graphToken(), delegator, _tokens);
816817

817818
// Update state
818819
return _delegate(delegator, _indexer, _tokens);
@@ -971,7 +972,7 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
971972
if (queryFees > 0) {
972973
// Pull tokens to collect from the authorized sender
973974
IGraphToken graphToken = graphToken();
974-
_pullTokens(graphToken, msg.sender, _tokens);
975+
TokenUtils.pullTokens(graphToken, msg.sender, _tokens);
975976

976977
// -- Collect protocol tax --
977978
// If the Allocation is not active or closed we are going to charge a 100% protocol tax
@@ -1070,7 +1071,7 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
10701071
require(tokensToWithdraw > 0, "!tokens");
10711072

10721073
// Return tokens to the indexer
1073-
_pushTokens(graphToken(), _indexer, tokensToWithdraw);
1074+
TokenUtils.pushTokens(graphToken(), _indexer, tokensToWithdraw);
10741075

10751076
emit StakeWithdrawn(_indexer, tokensToWithdraw);
10761077
}
@@ -1262,7 +1263,7 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
12621263

12631264
// When all allocations processed then burn unclaimed fees and prune rebate pool
12641265
if (rebatePool.unclaimedAllocationsCount == 0) {
1265-
_burnTokens(graphToken, rebatePool.unclaimedFees());
1266+
TokenUtils.burnTokens(graphToken, rebatePool.unclaimedFees());
12661267
delete rebates[alloc.closedAtEpoch];
12671268
}
12681269

@@ -1408,7 +1409,7 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
14081409
_delegate(_delegator, _delegateToIndexer, tokensToWithdraw);
14091410
} else {
14101411
// Return tokens to the delegator
1411-
_pushTokens(graphToken(), _delegator, tokensToWithdraw);
1412+
TokenUtils.pushTokens(graphToken(), _delegator, tokensToWithdraw);
14121413
}
14131414

14141415
return tokensToWithdraw;
@@ -1484,7 +1485,7 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
14841485
// Transfer and call collect()
14851486
// This function transfer tokens to a trusted protocol contracts
14861487
// Then we call collect() to do the transfer bookeeping
1487-
_pushTokens(_graphToken, address(curation), curationFees);
1488+
TokenUtils.pushTokens(_graphToken, address(curation), curationFees);
14881489
curation.collect(_subgraphDeploymentID, curationFees);
14891490
}
14901491
return curationFees;
@@ -1505,7 +1506,7 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
15051506
uint256 _percentage
15061507
) private returns (uint256) {
15071508
uint256 tax = uint256(_percentage).mul(_tokens).div(MAX_PPM);
1508-
_burnTokens(_graphToken, tax); // Burn tax if any
1509+
TokenUtils.burnTokens(_graphToken, tax); // Burn tax if any
15091510
return tax;
15101511
}
15111512

@@ -1616,54 +1617,11 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
16161617
} else {
16171618
// Transfer funds to the beneficiary's designated rewards destination if set
16181619
address destination = rewardsDestination[_beneficiary];
1619-
_pushTokens(
1620+
TokenUtils.pushTokens(
16201621
_graphToken,
16211622
destination == address(0) ? _beneficiary : destination,
16221623
_amount
16231624
);
16241625
}
16251626
}
1626-
1627-
/**
1628-
* @dev Burn tokens held by this contract.
1629-
* @param _graphToken Token to burn
1630-
* @param _amount Amount of tokens to burn
1631-
*/
1632-
function _burnTokens(IGraphToken _graphToken, uint256 _amount) private {
1633-
if (_amount > 0) {
1634-
_graphToken.burn(_amount);
1635-
}
1636-
}
1637-
1638-
/**
1639-
* @dev Pull tokens from an address to this contract.
1640-
* @param _graphToken Token to transfer
1641-
* @param _from Address sending the tokens
1642-
* @param _amount Amount of tokens to transfer
1643-
*/
1644-
function _pullTokens(
1645-
IGraphToken _graphToken,
1646-
address _from,
1647-
uint256 _amount
1648-
) private {
1649-
if (_amount > 0) {
1650-
require(_graphToken.transferFrom(_from, address(this), _amount), "!transfer");
1651-
}
1652-
}
1653-
1654-
/**
1655-
* @dev Push tokens from this contract to a receiving address.
1656-
* @param _graphToken Token to transfer
1657-
* @param _to Address receiving the tokens
1658-
* @param _amount Amount of tokens to transfer
1659-
*/
1660-
function _pushTokens(
1661-
IGraphToken _graphToken,
1662-
address _to,
1663-
uint256 _amount
1664-
) private {
1665-
if (_amount > 0) {
1666-
require(_graphToken.transfer(_to, _amount), "!transfer");
1667-
}
1668-
}
16691627
}

contracts/utils/TokenUtils.sol

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.7.3;
4+
5+
import "../token/IGraphToken.sol";
6+
7+
library TokenUtils {
8+
/**
9+
* @dev Pull tokens from an address to this contract.
10+
* @param _graphToken Token to transfer
11+
* @param _from Address sending the tokens
12+
* @param _amount Amount of tokens to transfer
13+
*/
14+
function pullTokens(
15+
IGraphToken _graphToken,
16+
address _from,
17+
uint256 _amount
18+
) internal {
19+
if (_amount > 0) {
20+
require(_graphToken.transferFrom(_from, address(this), _amount), "!transfer");
21+
}
22+
}
23+
24+
/**
25+
* @dev Push tokens from this contract to a receiving address.
26+
* @param _graphToken Token to transfer
27+
* @param _to Address receiving the tokens
28+
* @param _amount Amount of tokens to transfer
29+
*/
30+
function pushTokens(
31+
IGraphToken _graphToken,
32+
address _to,
33+
uint256 _amount
34+
) internal {
35+
if (_amount > 0) {
36+
require(_graphToken.transfer(_to, _amount), "!transfer");
37+
}
38+
}
39+
40+
/**
41+
* @dev Burn tokens held by this contract.
42+
* @param _graphToken Token to burn
43+
* @param _amount Amount of tokens to burn
44+
*/
45+
function burnTokens(IGraphToken _graphToken, uint256 _amount) internal {
46+
if (_amount > 0) {
47+
_graphToken.burn(_amount);
48+
}
49+
}
50+
}

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,25 @@
3030
"@types/inquirer": "^7.3.1",
3131
"@types/minimist": "^1.2.1",
3232
"@types/mocha": "^8.2.2",
33-
"@types/node": "^14.14.41",
34-
"@types/yargs": "^15.0.13",
35-
"@typescript-eslint/eslint-plugin": "^3.10.1",
36-
"@typescript-eslint/parser": "^3.10.1",
33+
"@types/node": "^15.0.1",
34+
"@types/yargs": "^16.0.0",
35+
"@typescript-eslint/eslint-plugin": "^4.0.0",
36+
"@typescript-eslint/parser": "^4.0.0",
37+
"@typescript-eslint/typescript-estree": "^4.0.0",
3738
"axios": "^0.21.1",
3839
"bignumber.js": "^9.0.0",
3940
"chai": "^4.3.4",
4041
"cli-table": "^0.3.6",
4142
"consola": "^2.15.3",
4243
"dotenv": "^8.2.0",
4344
"eslint": "^7.24.0",
44-
"eslint-config-prettier": "^6.11.0",
45-
"eslint-config-standard": "^14.1.1",
45+
"eslint-config-prettier": "^8.3.0",
46+
"eslint-config-standard": "^16.0.0",
4647
"eslint-plugin-import": "^2.22.0",
4748
"eslint-plugin-mocha-no-only": "^1.1.1",
4849
"eslint-plugin-node": "^11.1.0",
4950
"eslint-plugin-prettier": "^3.4.0",
50-
"eslint-plugin-promise": "^4.3.1",
51+
"eslint-plugin-promise": "^5.0.0",
5152
"eslint-plugin-standard": "^4.1.0",
5253
"ethereum-waffle": "^3.3.0",
5354
"ethlint": "^1.2.5",

test/disputes/query.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const { AddressZero, HashZero } = constants
2626

2727
const NON_EXISTING_DISPUTE_ID = randomHexBytes()
2828

29-
describe.only('DisputeManager:Query', async () => {
29+
describe('DisputeManager:Query', async () => {
3030
let me: Account
3131
let other: Account
3232
let governor: Account

test/lib/deployment.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,10 @@ export async function deployGNS(
195195
}
196196

197197
export async function deployEthereumDIDRegistry(deployer: Signer): Promise<EthereumDIDRegistry> {
198-
return (deployContract('EthereumDIDRegistry', deployer) as unknown) as Promise<
199-
EthereumDIDRegistry
200-
>
198+
return (deployContract(
199+
'EthereumDIDRegistry',
200+
deployer,
201+
) as unknown) as Promise<EthereumDIDRegistry>
201202
}
202203

203204
export async function deployServiceRegistry(

0 commit comments

Comments
 (0)