Skip to content

Commit

Permalink
Resume deployment and other improvements (#576)
Browse files Browse the repository at this point in the history
* making it possible to resume deployment. adding ERC20 function too

* fix test

* fix test for erc20 and fix slither warning

* fix logging & speed up verify
  • Loading branch information
adridadou committed Oct 21, 2022
1 parent a2cc438 commit 5f20833
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 117 deletions.
57 changes: 33 additions & 24 deletions contracts/extensions/token/erc20/ERC20TokenExtension.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,6 @@ contract ERC20Extension is AdapterGuard, IExtension, IERC20 {
dao = _dao;
}

function bytes32ToString(bytes32 _bytes32)
internal
pure
returns (string memory)
{
uint8 i = 0;
while (i < 32 && _bytes32[i] != 0) {
i++;
}
bytes memory bytesArray = new bytes(i);
for (i = 0; i < 32 && _bytes32[i] != 0; i++) {
bytesArray[i] = _bytes32[i];
}
return string(bytesArray);
}

/**
* @dev Returns the token address managed by the DAO that tracks the
* internal transfers.
Expand Down Expand Up @@ -156,6 +140,18 @@ contract ERC20Extension is AdapterGuard, IExtension, IERC20 {
return bank.balanceOf(DaoHelper.TOTAL, tokenAddress);
}

/**
* @dev Returns the amount of tokens assigned to all the members.
*/
function totalAssignedTokens() external view returns (uint256) {
BankExtension bank = BankExtension(
dao.getExtensionAddress(DaoHelper.BANK)
);
return
bank.balanceOf(DaoHelper.TOTAL, tokenAddress) -
bank.balanceOf(DaoHelper.GUILD, tokenAddress);
}

/**
* @dev Returns the amount of tokens owned by `account`.
*/
Expand Down Expand Up @@ -214,16 +210,13 @@ contract ERC20Extension is AdapterGuard, IExtension, IERC20 {
address senderAddr = dao.getAddressIfDelegated(msg.sender);
require(
DaoHelper.isNotZeroAddress(senderAddr),
"ERC20: approve from the zero address"
);
require(
DaoHelper.isNotZeroAddress(spender),
"ERC20: approve to the zero address"
"ERC20: approve from 0x0"
);
require(DaoHelper.isNotZeroAddress(spender), "ERC20: approve to 0x0");
require(dao.isMember(senderAddr), "sender is not a member");
require(
DaoHelper.isNotReservedAddress(spender),
"spender can not be a reserved address"
"spender is reserved address"
);

_allowances[senderAddr][spender] = amount;
Expand Down Expand Up @@ -285,7 +278,7 @@ contract ERC20Extension is AdapterGuard, IExtension, IERC20 {
) public override returns (bool) {
require(
DaoHelper.isNotZeroAddress(recipient),
"ERC20: transfer to the zero address"
"ERC20: transfer to 0x0"
);

IERC20TransferStrategy strategy = IERC20TransferStrategy(
Expand Down Expand Up @@ -323,7 +316,7 @@ contract ERC20Extension is AdapterGuard, IExtension, IERC20 {
//check if sender has approved msg.sender to spend amount
require(
currentAllowance >= amount,
"ERC20: transfer amount exceeds allowance"
"ERC20: amount exceeds allowance"
);

if (allowedAmount >= amount) {
Expand All @@ -340,4 +333,20 @@ contract ERC20Extension is AdapterGuard, IExtension, IERC20 {

return false;
}

function bytes32ToString(bytes32 _bytes32)
internal
pure
returns (string memory)
{
uint8 i = 0;
while (i < 32 && _bytes32[i] != 0) {
i++;
}
bytes memory bytesArray = new bytes(i);
for (i = 0; i < 32 && _bytes32[i] != 0; i++) {
bytesArray[i] = _bytes32[i];
}
return string(bytesArray);
}
}
2 changes: 0 additions & 2 deletions hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ module.exports = {
network_id: 5,
chainId: 5,
skipDryRun: true,
gas: 2100000,
gasPrice: 4000000000,
accounts: {
mnemonic: process.env.WALLET_MNEMONIC || "",
count: 10,
Expand Down
5 changes: 4 additions & 1 deletion tasks/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ const main = async () => {
p.then(async () => {
const r = await verify(c);
log(`[${count++}/${verifyContracts.length}]`);
await sleep(1500); // avoid rate-limit errors

if(!r || !r.stderr) {
await sleep(1500); // avoid rate-limit errors
}
return r;
}),
Promise.resolve(0)
Expand Down
11 changes: 11 additions & 0 deletions test/extensions/erc20.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const {
ZERO_ADDRESS,
numberOfUnits,
DAI_TOKEN,
GUILD,
} = require("../../utils/contract-util");

const {
Expand Down Expand Up @@ -447,6 +448,7 @@ describe("Extension - ERC20", () => {
let externalAddressAUnits = await erc20Ext.balanceOf(
externalAddressA.address
);

expect(externalAddressAUnits.toString()).equal(
numberOfUnits.mul(toBN("0")).toString()
);
Expand All @@ -463,6 +465,15 @@ describe("Extension - ERC20", () => {
expect(externalAddressAUnits.toString()).equal(
numberOfUnits.mul(toBN("0")).toString()
);

const guildBalance = await erc20Ext.balanceOf(GUILD);
const totalSupply = await erc20Ext.totalSupply();

const assignedBalance = await erc20Ext.totalAssignedTokens();

expect(totalSupply.sub(guildBalance).toString()).equal(
assignedBalance.toString()
);
});

it("should not be possible to approve a transferFrom units from a member to an external account when the transfer type is equals 0 (member transfer only)", async () => {
Expand Down
5 changes: 4 additions & 1 deletion utils/contract-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const toBNWeb3 = Web3Utils.toBN;
// Ethers.js utils
const { ethers } = require("ethers");
const { error } = require("./log-util");
const { info } = require("console");
const toUtf8 = ethers.utils.toUtf8String;
const toBytes32 = ethers.utils.formatBytes32String;
const toHex = ethers.utils.hexValue;
Expand Down Expand Up @@ -72,11 +73,13 @@ const maxUnits = toBN("10000000000000000000");

const waitTx = async (p) => {
try {
info("waiting for transaction to be submitted / mined");
let res = await p;
if (res && res.wait) {
info(`submitted! tx hash:${res.hash} - nonce:${res.nonce}`);
res = await res.wait();
info("transaction mined");
}

return res;
} catch (err) {
error(err);
Expand Down
Loading

0 comments on commit 5f20833

Please sign in to comment.