Skip to content

Commit

Permalink
Merge pull request #1001 from thesandboxgame/feat/signedMultiGiveaway…
Browse files Browse the repository at this point in the history
…_package

Feat/signed multi giveaway package
  • Loading branch information
adjisb authored Jun 9, 2023
2 parents 862d962 + d2b5afd commit 0419d7c
Show file tree
Hide file tree
Showing 22 changed files with 3,790 additions and 29 deletions.
16 changes: 16 additions & 0 deletions packages/giveaway/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
node_modules
.env
coverage
coverage.json
typechain
typechain-types

# Hardhat files
cache
artifacts

# generated docs
generated-markups

# editors
.idea
41 changes: 41 additions & 0 deletions packages/giveaway/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const path = require('path');
const tsconfigPath = path.join(__dirname, 'tsconfig.json');
module.exports = {
root: true,
extends: [
'eslint:recommended',
'plugin:mocha/recommended',
'plugin:prettier/recommended',
],
parserOptions: {
ecmaVersion: 2020,
},
plugins: ['mocha'],
env: {
commonjs: true,
node: true,
mocha: true,
},
overrides: [
{
files: ['*.ts'],
parser: '@typescript-eslint/parser',
parserOptions: {
project: [tsconfigPath],
ecmaVersion: 2020,
sourceType: 'module',
},
plugins: ['mocha', '@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:mocha/recommended',
'plugin:prettier/recommended',
],
rules: {
'@typescript-eslint/no-misused-promises': 'error',
'@typescript-eslint/no-floating-promises': 'error',
},
},
],
};
16 changes: 16 additions & 0 deletions packages/giveaway/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
node_modules
.env
coverage
coverage.json
typechain
typechain-types

# Hardhat files
cache
artifacts

# generated docs
generated-markups

# editors
.idea
16 changes: 16 additions & 0 deletions packages/giveaway/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
node_modules
.env
coverage
coverage.json
typechain
typechain-types

# Hardhat files
cache
artifacts

# generated docs
generated-markups

# editors
.idea
15 changes: 15 additions & 0 deletions packages/giveaway/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
singleQuote: true,
bracketSpacing: false,
plugins: ['prettier-plugin-solidity'],
overrides: [
{
files: '*.sol',
options: {
printWidth: 120,
tabWidth: 4,
singleQuote: false,
},
},
],
};
6 changes: 6 additions & 0 deletions packages/giveaway/.solcover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
mocha: {
grep: '@skip-on-coverage', // Find everything with this tag
invert: true, // Run the grep's inverse set.
},
};
21 changes: 21 additions & 0 deletions packages/giveaway/.solhint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"extends": "solhint:recommended",
"plugins": ["prettier"],
"rules": {
"prettier/prettier": [
"error",
{
"endOfLine": "auto"
}
],
"code-complexity": ["error", 7],
"compiler-version": ["error", "^0.8.0"],
"const-name-snakecase": "off",
"func-name-mixedcase": "off",
"constructor-syntax": "error",
"func-visibility": ["error", {"ignoreConstructors": true}],
"not-rely-on-time": "off",
"no-inline-assembly": "off",
"reason-string": ["warn", {"maxLength": 64}]
}
}
33 changes: 33 additions & 0 deletions packages/giveaway/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Signed Giveaways

This is a hardhat project, see [https://hardhat.org](https://hardhat.org)

The main contract gives rewards in any ERC20, ERC721 or ERC1155 when the backend authorize it via message signing.

The message is composed of:

- A list of signatures. If the contract holt too much value more than one signature is needed. Ideally the systems that
sign must be independent.
- A list of claim ids used by the backend to avoid double spending.
- Expiration the expiration time of the message in unix timestamp. After the expiration the message cannot be used
anymore.
- From: usually the rewards must be transferred to the contract and this value is address(this), if a strategy with
approve and transfer is used this is the address of the source.
- To: the destination address that will get the rewards.
- Claims: A list of union structs (ClaimEntry) that include: token type (ERC20, ERC721, ERC1155, etc), token address and
a data field that depending on the token type may have: amount, tokenId, etc.

# Usage

```shell
yarn hardhat help
yarn hardhat test
yarn hardhat coverage
REPORT_GAS=true yarn hardhat test
yarn hardhat markup
```

# Deployment

This package exports the contract source code, for deployments see: [@sandbox-smart-contract/deploy](../deploy) package.

39 changes: 39 additions & 0 deletions packages/giveaway/contracts/ERC2771Handler.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: MIT
// solhint-disable-next-line compiler-version
pragma solidity 0.8.18;

/// @dev minimal ERC2771 handler to keep bytecode-size down
/// based on: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.6.0/contracts/metatx/ERC2771Context.sol
/// with an initializer for proxies and a mutable forwarder

abstract contract ERC2771Handler {
address internal _trustedForwarder;

function __ERC2771Handler_initialize(address forwarder) internal {
_trustedForwarder = forwarder;
}

function isTrustedForwarder(address forwarder) public view returns (bool) {
return forwarder == _trustedForwarder;
}

function getTrustedForwarder() external view returns (address) {
return _trustedForwarder;
}

function trustedForwarder() external view returns (address) {
return _trustedForwarder;
}

function _msgSender() internal view virtual returns (address sender) {
if (isTrustedForwarder(msg.sender)) {
// The assembly code is more direct than the Solidity version using `abi.decode`.
// solhint-disable-next-line no-inline-assembly
assembly {
sender := shr(96, calldataload(sub(calldatasize(), 20)))
}
} else {
return msg.sender;
}
}
}
Loading

1 comment on commit 0419d7c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage for this commit

100.00%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
packages/giveaway/contracts
   ERC2771Handler.sol100%100%100%100%
   SignedMultiGiveaway.sol100%100%100%100%
   SignedMultiGiveawayBase.sol100%100%100%100%
packages/giveaway/contracts/test
   FakeMintableERC1155.sol100%100%100%100%
   FakeMintableERC20.sol100%100%100%100%
   FakeMintableERC721.sol100%100%100%100%

Please sign in to comment.