Skip to content

Commit

Permalink
Merge pull request #1018 from statechannels/badtokens
Browse files Browse the repository at this point in the history
Allow Adjudicator to accepts "bad" ERC20 tokens (such as USDT)
  • Loading branch information
geoknee authored Dec 14, 2022
2 parents eafac67 + 0f5ed39 commit eb199af
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 37 deletions.
2 changes: 1 addition & 1 deletion client/engine/chainservice/adjudicator/NitroAdjudicator.go

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion nitro-protocol/.env
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ SINGLE_ASSET_PAYMENT_ADDRESS= 0x0000000000000000000000000000000000000000
TRIVIAL_APP_ADDRESS= 0x0000000000000000000000000000000000000000
TEST_FORCE_MOVE_ADDRESS= 0x0000000000000000000000000000000000000000
TEST_NITRO_ADJUDICATOR_ADDRESS= 0x0000000000000000000000000000000000000000
TEST_TOKEN_ADDRESS= 0x0000000000000000000000000000000000000000
TEST_TOKEN_ADDRESS= 0x0000000000000000000000000000000000000000
BAD_TOKEN_ADDRESS= 0x0000000000000000000000000000000000000000
1 change: 1 addition & 0 deletions nitro-protocol/.solhintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
./contracts/test/BadToken.sol
9 changes: 4 additions & 5 deletions nitro-protocol/contracts/MultiAssetHolder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ pragma experimental ABIEncoderV2;
import {ExitFormat as Outcome} from '@statechannels/exit-format/contracts/ExitFormat.sol';
import './ForceMove.sol';
import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';
import './interfaces/IMultiAssetHolder.sol';

/**
@dev An implementation of the IMultiAssetHolder interface. The AssetHolder contract escrows ETH or tokens against state channels. It allows assets to be internally accounted for, and ultimately prepared for transfer from one channel to other channels and/or external destinations, as well as for guarantees to be reclaimed.
*/
contract MultiAssetHolder is IMultiAssetHolder, StatusManager {
using SafeERC20 for IERC20;

// *******
// Storage
// *******
Expand Down Expand Up @@ -56,11 +59,7 @@ contract MultiAssetHolder is IMultiAssetHolder, StatusManager {
if (asset == address(0)) {
require(msg.value == amount, 'Incorrect msg.value for deposit');
} else {
// require successful deposit before updating holdings (protect against reentrancy)
require(
IERC20(asset).transferFrom(msg.sender, address(this), amountDeposited),
'Could not deposit ERC20s'
);
IERC20(asset).safeTransferFrom(msg.sender, address(this), amountDeposited);
}

uint256 nowHeld = held + amountDeposited;
Expand Down
123 changes: 123 additions & 0 deletions nitro-protocol/contracts/test/BadToken.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (token/ERC20/ERC20.sol)

pragma solidity 0.8.17;

/**
* @dev Copy-pasted from Openzeppelin ERC20 contract, but with the inheritance from IERC20 interface removed and no return value for transferFrom.
*/
contract BadToken {
function transferFrom(
address from,
address to,
uint256 amount
) public virtual {
address spender = msg.sender;
_spendAllowance(from, spender, amount);
_transfer(from, to, amount);
// return true; // purposefully ommitted because bad token
}

constructor(address owner) {
_mint(owner, 10_000_000_000);
}

// The rest of the file is as it is in the Openzeppelin ERC20 contract.
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;
uint256 private _totalSupply;
string private _name;
string private _symbol;

function balanceOf(address account) public view virtual returns (uint256) {
return _balances[account];
}

function transfer(address to, uint256 amount) public virtual returns (bool) {
address owner = msg.sender;
_transfer(owner, to, amount);
return true;
}

function allowance(address owner, address spender) public view virtual returns (uint256) {
return _allowances[owner][spender];
}

function approve(address spender, uint256 amount) public virtual returns (bool) {
address owner = msg.sender;
_approve(owner, spender, amount);
return true;
}

function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
address owner = msg.sender;
_approve(owner, spender, allowance(owner, spender) + addedValue);
return true;
}

function _transfer(
address from,
address to,
uint256 amount
) internal virtual {
require(from != address(0), 'ERC20: transfer from the zero address');
require(to != address(0), 'ERC20: transfer to the zero address');
_beforeTokenTransfer(from, to, amount);
uint256 fromBalance = _balances[from];
require(fromBalance >= amount, 'ERC20: transfer amount exceeds balance');
unchecked {
_balances[from] = fromBalance - amount;
}
_balances[to] += amount;
emit Transfer(from, to, amount);
_afterTokenTransfer(from, to, amount);
}

function _mint(address account, uint256 amount) internal virtual {
require(account != address(0), 'ERC20: mint to the zero address');
_beforeTokenTransfer(address(0), account, amount);
_totalSupply += amount;
_balances[account] += amount;
emit Transfer(address(0), account, amount);
_afterTokenTransfer(address(0), account, amount);
}

function _approve(
address owner,
address spender,
uint256 amount
) internal virtual {
require(owner != address(0), 'ERC20: approve from the zero address');
require(spender != address(0), 'ERC20: approve to the zero address');
_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}

function _spendAllowance(
address owner,
address spender,
uint256 amount
) internal virtual {
uint256 currentAllowance = allowance(owner, spender);
if (currentAllowance != type(uint256).max) {
require(currentAllowance >= amount, 'ERC20: insufficient allowance');
unchecked {
_approve(owner, spender, currentAllowance - amount);
}
}
}

function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual {}

function _afterTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual {}
}
9 changes: 9 additions & 0 deletions nitro-protocol/deployment/deploy-test-contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import testStrictTurnTakingArtifact from '../artifacts/contracts/test/TESTStrict
import testConsensusArtifact from '../artifacts/contracts/test/TESTConsensus.sol/TESTConsensus.json';
import testNitroAdjudicatorArtifact from '../artifacts/contracts/test/TESTNitroAdjudicator.sol/TESTNitroAdjudicator.json';
import tokenArtifact from '../artifacts/contracts/Token.sol/Token.json';
import badTokenArtifact from '../artifacts/contracts/test/BadToken.sol/BadToken.json';
import trivialAppArtifact from '../artifacts/contracts/TrivialApp.sol/TrivialApp.json';
import consensusAppArtifact from '../artifacts/contracts/ConsensusApp.sol/ConsensusApp.json';
import virtualPaymentAppArtifact from '../artifacts/contracts/VirtualPaymentApp.sol/VirtualPaymentApp.json';
Expand All @@ -32,6 +33,7 @@ const [
testConsensusFactory,
testNitroAdjudicatorFactory,
tokenFactory,
badTokenFactory,
trivialAppFactory,
consensusAppFactory,
virtualPaymentAppFactory,
Expand All @@ -46,6 +48,7 @@ const [
testConsensusArtifact,
testNitroAdjudicatorArtifact,
tokenArtifact,
badTokenArtifact,
trivialAppArtifact,
consensusAppArtifact,
virtualPaymentAppArtifact,
Expand All @@ -72,6 +75,11 @@ export async function deploy(): Promise<Record<string, string>> {
const TEST_TOKEN_ADDRESS = (
await tokenFactory.deploy(new Wallet(TEST_ACCOUNTS[0].privateKey).address)
).address;

const BAD_TOKEN_ADDRESS = (
await badTokenFactory.deploy(new Wallet(TEST_ACCOUNTS[0].privateKey).address)
).address;

return {
NITRO_ADJUDICATOR_ADDRESS,
COUNTING_APP_ADDRESS,
Expand All @@ -86,5 +94,6 @@ export async function deploy(): Promise<Record<string, string>> {
TEST_CONSENSUS_ADDRESS,
TEST_NITRO_ADJUDICATOR_ADDRESS,
TEST_TOKEN_ADDRESS,
BAD_TOKEN_ADDRESS,
};
}
28 changes: 14 additions & 14 deletions nitro-protocol/gas-benchmarks/gasResults.json
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
{
"deployInfrastructureContracts": {
"satp": {
"NitroAdjudicator": 3316515
"NitroAdjudicator": 3363358
}
},
"directlyFundAChannelWithETHFirst": {
"satp": 47318
"satp": 47279
},
"directlyFundAChannelWithETHSecond": {
"satp": 30230
"satp": 30191
},
"directlyFundAChannelWithERC20First": {
"satp": {
"approve": 46218,
"deposit": 70026
"deposit": 70595
}
},
"directlyFundAChannelWithERC20Second": {
"satp": {
"approve": 46218,
"deposit": 52938
"deposit": 53507
}
},
"ETHexit": {
"satp": 110949
},
"ERC20exit": {
"satp": 112231
"satp": 112233
},
"ETHexitSad": {
"satp": {
"challenge": 113232,
"challenge": 113241,
"transferAllAssets": 80967,
"total": 194199
"total": 194208
}
},
"ETHexitSadLedgerFunded": {
"satp": {
"challengeX": 113232,
"challengeL": 106643,
"challengeX": 113241,
"challengeL": 106652,
"transferAllAssetsL": 61079,
"transferAllAssetsX": 80967,
"total": 361921
"total": 361939
}
},
"ETHexitSadVirtualFunded": {
"satp": {
"challengeL": 122187,
"challengeV": 172745,
"challengeL": 122196,
"challengeV": 172754,
"reclaimL": 60323,
"transferAllAssetsL": 112667,
"total": 467922
"total": 467940
}
}
}
Loading

0 comments on commit eb199af

Please sign in to comment.