Skip to content

Commit

Permalink
Merge pull request #3 from oscarpascualbakker/feature/overall-improve…
Browse files Browse the repository at this point in the history
…ments

Feature/overall-improvements
Merge because the only problem is the Web3 component (it gives problems in my local environment as well).
  • Loading branch information
oscarpascualbakker authored Oct 8, 2023
2 parents e4274c7 + defaaa5 commit 445bf53
Show file tree
Hide file tree
Showing 18 changed files with 17,497 additions and 176 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Tests and Slither

on: pull_request

jobs:
build:
runs-on: ubuntu-latest

steps:
# Checkout the code from the repository
- name: Checkout code
uses: actions/checkout@v2

# Install project dependencies
- name: Install dependencies
run: npm install

# Build and start the Docker environment
# Wait 10 seconds for ganache to be ready
- name: Build environment
run: |
docker compose up -d
sleep 10
# Run unit tests (forcing compilation)
- name: Run tests
run: |
docker compose run --rm --user root txoco mkdir -p /app/build
docker compose run --rm txoco chown -R node:node /app/build
docker compose run --rm txoco truffle test --compile-all
# Execute Slither for smart contract analysis
- name: Run Slither
run: |
docker compose run --rm slither slither . --exclude-dependencies --exclude-informational --exclude-low --exclude-optimization || echo "SLITHER_EXIT_CODE=$?" >> $GITHUB_ENV
# Check the exit code of the Slither command
- name: Check Slither output
run: |
SLITHER_EXIT_CODE=${SLITHER_EXIT_CODE:-0}
if [ -z "$SLITHER_EXIT_CODE" ]; then
echo "SLITHER_EXIT_CODE is not set or is empty"
else
if [ "$SLITHER_EXIT_CODE" -ne 0 ]; then
exit 1
fi
fi
# Archive Slither results if the job fails
- name: Archive Slither results on failure
if: failure()
uses: actions/upload-artifact@v2
with:
name: slither-results
path: slither-results.txt
28 changes: 15 additions & 13 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
# Dependency directory
node_modules

# Build directory
build/

# Environment variables
.env

# Metadata
metadata.json

# System files
.DS_Store
Thumbs.db
metadata/

# IDE files
.idea/
*.iml
# Images
images/

# Log files
npm-debug.log
Expand All @@ -25,6 +17,16 @@ yarn-error.log
# Directory for deployed contracts, not needed in version control
deployed/

# Cache & Artifacts
# Build, Cache & Artifacts
cache/
artifacts/
artifacts/
build/

# System files
.DS_Store
Thumbs.db

# IDE files
.idea/
*.iml

2 changes: 2 additions & 0 deletions .slither.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
contracts_to_exclude:
- "node_modules/@openzeppelin/contracts/*.sol"
8 changes: 3 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ COPY package.json ./
# Install Truffle globally
RUN npm install -g truffle

# Install Truffle tools
RUN npm install @truffle/hdwallet-provider truffle-plugin-verify

# Install Truffle tools, OpenZeppelin contracts and other needed stuff
RUN npm install @truffle/hdwallet-provider truffle-plugin-verify truffle-assertions
RUN npm install @openzeppelin/contracts
RUN npm install chai truffle-assertions
RUN npm install dotenv
RUN npm install chai dotenv web3

# Install dependencies
ARG CACHE_INVALIDATE=1
Expand Down
24 changes: 24 additions & 0 deletions Dockerfile.slither
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM python:3.8

# Download slither code
RUN git clone https://github.com/crytic/slither.git

# Install slither
WORKDIR /slither
RUN python3 setup.py install

# Install node, npm and npx
RUN apt-get update
RUN apt-get install -y curl dirmngr apt-transport-https lsb-release ca-certificates
RUN curl -sSL https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add -
RUN sh -c 'echo "deb https://deb.nodesource.com/node_18.x $(lsb_release -sc) main" > /etc/apt/sources.list.d/nodesource.list'
RUN apt-get update
RUN apt-get install nodejs

# Install Truffle globally
RUN npm install -g truffle

# Install Truffle tools and other stuff
RUN npm install @truffle/hdwallet-provider truffle-plugin-verify truffle-assertions
RUN npm install @openzeppelin/contracts
RUN npm install chai dotenv
62 changes: 35 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,38 @@

## Description

TxocoCalPadri is a smart contract written in Solidity for the Ethereum Virtual Machine (EVM). It allows our gastronomic association to manage proposals and voting through the use of NFT tokens. Members of the association own an NFT which grants them the ability to vote on proposals. NFTs are managed by administrators and are not transferable by the token holders.
TxocoCalPadri is a smart contract written in Solidity for the Ethereum Virtual Machine (EVM). It allows our gastronomic association to manage proposals and voting through the use of NFT tokens. Members of the association own an NFT which grants them the ability to vote on proposals. NFTs are managed by administrators and are non-transferable by token holders.

![Solidity 0.8.18](https://img.shields.io/badge/Solidity-0.8.18-blue) ![Truffle 5.11.4](https://img.shields.io/badge/Truffle-5.11.4-blue) ![Ganache 7.9.1](https://img.shields.io/badge/Ganache-7.9.1-blue) ![Node 16.20.2](https://img.shields.io/badge/Node-16.20.2-blue) ![Slither 0.9.6](https://img.shields.io/badge/Slither-0.9.6-blue)

## Features

- Issuance and revocation of NFTs for members.
- NFTs are non-transferable by members, only by administrators, as membership is not transferable.
- Creation and management of proposals with voting options.
- Members can vote on active proposals.
- Issuance and revocation of NFTs for members
- NFTs are non-transferable by members, only administrators can transfer them as membership is non-transferable
- Creation and management of proposals with voting options
- Members can vote on active proposals

## Functions

##### `setBaseURI(string memory baseURI)`
##### `setBaseURI(string memory _baseURI)`

Allows the contract owner to set the URI for the token metadata.
Allows the contract owner to set the base URI for token metadata.

##### `setAdministrator(address _admin, bool _status)`

Allows the contract owner to add or remove administrators. Set `_status` to true to add, and false to remove.
Allows the contract owner to add or remove administrators. Set `_status` to true to add, false to remove.

##### `mintNFT(address _to)`
##### `mintNFT(address _to, uint256 _tokenId)`

Allows administrators to mint an NFT to a specific address, granting them membership.
Allows administrators to mint an NFT to a specific address, granting membership.

##### `revokeNFT(address _from)`
##### `revokeNFT(address _from, uint256 _tokenId)`

Allows administrators to revoke an NFT from a specific address, revoking their membership.
Allows administrators to revoke an NFT from a specific address, revoking membership.

##### `createProposal(string memory _title, string memory _description, string[] memory _options, uint256 _startTime, uint256 _endTime)`

Allows administrators to create a new proposal with a title, description, array of voting options, and a time window for voting.
Allows administrators to create a new proposal with title, description, array of voting options, and a time window for voting.

##### `closeProposal(uint256 _proposalId)`

Expand All @@ -48,39 +49,46 @@ Returns an array of vote counts for each option of a specific proposal.

##### `getWinningOption(uint256 _proposalId)`

Returns the index, name, and vote count of the winning option for a specific proposal.
Returns index, name, and vote count of winning option for a specific proposal.

## Events

- `ProposalCreated`
- `Voted`
- `NFTMinted`

- `ProposalCreated`: Emitted when a new proposal is created.
- `Voted`: Emitted when a vote is cast on a proposal.
- `NFTMinted`: Emitted when a new NFT is minted.

## Overridden Functions

This contract overrides the following functions from its inherited contracts to adapt their behavior for the specific requirements of this contract:

##### `uri(uint256 tokenId) -> string`

This function overrides the uri function from the ERC1155 contract. It is used to get the URI for a token's metadata. However, in this contract, it ignores the tokenId parameter and returns the base URI for all tokens.
Overrides the uri function from the ERC1155 contract. Used to get a token's metadata URI. However, this ignores the tokenId and returns the base URI for all tokens.

##### `safeTransferFrom(address _from, address _to, uint256 _id, uint256 _value, bytes memory _data)`

This function overrides the safeTransferFrom function from the ERC1155 contract. It is used to safely transfer tokens from one address to another. In this contract, the function has been modified to only allow administrators to transfer tokens.
Overrides safeTransferFrom function from ERC1155 contract. Used to safely transfer tokens between addresses. Modified to only allow administrators to transfer tokens.

##### `safeBatchTransferFrom(address _from, address _to, uint256[] memory _ids, uint256[] memory _values, bytes memory _data)`

This function overrides the safeBatchTransferFrom function from the ERC1155 contract. It is used to perform a batch transfer of multiple tokens from one address to another. In this contract, the function has been modified to only allow administrators to transfer tokens.
Overrides safeBatchTransferFrom from ERC1155 contract. Used to perform batch transfer of multiple tokens between addresses. Modified to only allow administrators to transfer tokens.

## Testing

1. Clone this repository.
2. Navigate to the project folder and run `docker-compose build --no-cache`.
3. Run the containers using `docker-compose up -d`.
4. Compile and run tests using the following command: `docker-compose run --rm txoco truffle test`.
1. Clone this repository
2. Navigate to project folder and run `docker-compose build --no-cache`
3. Start containers with `docker-compose up -d`
4. Compile and run tests with `docker-compose run --rm txoco truffle test`
5. Run static code analysis with `docker-compose run --rm slither slither . --exclude-dependencies`

#### Expected test result:

![Test results image](https://oscarpascual.com/txococalpadri/results-tests.jpg)

#### Expected code analysis result

![Code analysis result](https://oscarpascual.com/txococalpadri/results-slither.jpg)

#### Expected result:
![Resultado de los tests](https://oscarpascual.com/test-results-txococalpadri.jpg)

## License

Expand Down
38 changes: 38 additions & 0 deletions alchemy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
require 'vendor/autoload.php';
use Web3\Web3;

$apiKey = 'MkCoC6RmzJXTJryxG7iFXvRvX9zIr4aA';
$endpoint = 'https://polygon-mainnet.g.alchemy.com/v2/' . $apiKey;
$contractAddress = '0xf6fe664d0D61297fBfBb829A1FF77286b887a9Fd';

// Inicializar Web3
$web3 = new Web3($endpoint);

// La ABI del contrato (simplificada para este ejemplo)
$contractAbi = [
[
'constant' => true,
'inputs' => [],
'name' => 'baseURI',
'outputs' => [
['name' => '', 'type' => 'string']
],
'payable' => false,
'stateMutability' => 'view',
'type' => 'function',
]
];

$contract = new \Web3\Contract($contractAbi, $contractAddress, $web3->provider);

// Obtener baseURI
$contract->call('baseURI', [], function ($err, $result) use (&$baseURI) {
if ($err !== null) {
echo 'Error: ' . $err->getMessage();
return;
}
$baseURI = $result[0];
echo 'El valor de baseURI es: ' . $baseURI;
});
?>
Loading

0 comments on commit 445bf53

Please sign in to comment.