Skip to content

Commit

Permalink
Merge pull request #47 from Supercolony-net/feature/psp22-extensions
Browse files Browse the repository at this point in the history
Feature/psp22 extensions
  • Loading branch information
xgreenx authored Dec 3, 2021
2 parents 21bf50f + a834660 commit dba1323
Show file tree
Hide file tree
Showing 114 changed files with 2,456 additions and 87 deletions.
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ exclude = [

[profile.release]
panic = "abort"
lto = true
lto = true
overflow-checks = false

[profile.dev]
panic = "abort"
lto = true
overflow-checks = false
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Why use this library?
- Useful features which can simplify development

Which Standard tokens & useful contracts does it provide?
- **PSP22** - Fungible Token(*ERC20 equivalent*)
- **PSP22** - Fungible Token (*ERC20 equivalent*) with extensions
- **Non-Fungible Token** *ERC721 equivalent*
- **Multi-Token** *ERC1155 equivalent*
- **Ownable** Restrict access to action for non-owners
Expand Down Expand Up @@ -57,13 +57,13 @@ pub trait Trait1 {
}

#[brush::wrapper]
type Wrapper1 = dyn Trait1;
type Trait1Ref = dyn Trait1;

{
// It should be `AccountId` of some contract in the network
let callee: brush::traits::AccountId = [1; 32].into();
// This code will execute a cross contract call to `callee` contract
let result_of_foo: bool = Wrapper1::foo(&callee);
let result_of_foo: bool = Trait1Ref::foo(&callee);
}
```

Expand Down
2 changes: 1 addition & 1 deletion contracts/access/access-control/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ where
}

#[brush::wrapper]
pub type AccessControlWrapper = dyn AccessControl;
pub type AccessControlRef = dyn AccessControl;

/// Contract module that allows children to implement role-based access
/// control mechanisms. This is a lightweight version that doesn't allow enumerating role
Expand Down
2 changes: 1 addition & 1 deletion contracts/access/ownable/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ where
}

#[brush::wrapper]
pub type OwnableWrapper = dyn Ownable;
pub type OwnableRef = dyn Ownable;

/// Contract module which provides a basic access control mechanism, where
/// there is an account (an owner) that can be granted exclusive access to
Expand Down
138 changes: 138 additions & 0 deletions contracts/common/errors/flashloan.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
use super::{
AccessControlError,
OwnableError,
PSP22Error,
PSP22ReceiverError,
PausableError,
ReentrancyGuardError,
};
use ink_prelude::string::String;

#[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum FlashBorrowerError {
FlashloanRejected(String),
}

impl From<OwnableError> for FlashBorrowerError {
fn from(ownable: OwnableError) -> Self {
match ownable {
OwnableError::CallerIsNotOwner => {
FlashBorrowerError::FlashloanRejected(String::from("O::CallerIsNotOwner"))
}
OwnableError::NewOwnerIsZero => FlashBorrowerError::FlashloanRejected(String::from("O::NewOwnerIsZero")),
}
}
}

impl From<AccessControlError> for FlashBorrowerError {
fn from(access: AccessControlError) -> Self {
match access {
AccessControlError::MissingRole => FlashBorrowerError::FlashloanRejected(String::from("AC::MissingRole")),
AccessControlError::RoleRedundant => {
FlashBorrowerError::FlashloanRejected(String::from("AC::RoleRedundant"))
}
AccessControlError::InvalidCaller => {
FlashBorrowerError::FlashloanRejected(String::from("AC::InvalidCaller"))
}
}
}
}

impl From<PausableError> for FlashBorrowerError {
fn from(pausable: PausableError) -> Self {
match pausable {
PausableError::Paused => FlashBorrowerError::FlashloanRejected(String::from("P::Paused")),
PausableError::NotPaused => FlashBorrowerError::FlashloanRejected(String::from("P::NotPaused")),
}
}
}

impl From<ReentrancyGuardError> for FlashBorrowerError {
fn from(guard: ReentrancyGuardError) -> Self {
match guard {
ReentrancyGuardError::ReentrantCall => {
FlashBorrowerError::FlashloanRejected(String::from("RG::ReentrantCall"))
}
}
}
}

impl From<PSP22ReceiverError> for FlashBorrowerError {
fn from(error: PSP22ReceiverError) -> Self {
match error {
PSP22ReceiverError::TransferRejected(message) => FlashBorrowerError::FlashloanRejected(message),
}
}
}

#[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum FlashLenderError {
Custom(String),
/// Returned if we our flashlendner does not support lending of this token
WrongTokenAddress,
/// Returned if the contract does not have enough allowance to transfer borrowed amount and fees
AllowanceDoesNotAllowRefund,
/// Callee contract rejected the flashloan
BorrowerRejected(String),
}

impl From<PSP22Error> for FlashLenderError {
fn from(error: PSP22Error) -> Self {
match error {
PSP22Error::Custom(message) => FlashLenderError::Custom(message),
PSP22Error::InsufficientBalance => FlashLenderError::Custom(String::from("PSP22: Insufficient Balance")),
PSP22Error::InsufficientAllowance => {
FlashLenderError::Custom(String::from("PSP22: Insufficient Allowance"))
}
PSP22Error::ZeroRecipientAddress => FlashLenderError::Custom(String::from("PSP22: Zero Recipient Address")),
PSP22Error::ZeroSenderAddress => FlashLenderError::Custom(String::from("PSP22: Zero Sender Address")),
PSP22Error::SafeTransferCheckFailed(message) => FlashLenderError::Custom(message),
}
}
}

impl From<FlashBorrowerError> for FlashLenderError {
fn from(error: FlashBorrowerError) -> Self {
match error {
FlashBorrowerError::FlashloanRejected(message) => FlashLenderError::BorrowerRejected(message),
}
}
}

impl From<OwnableError> for FlashLenderError {
fn from(ownable: OwnableError) -> Self {
match ownable {
OwnableError::CallerIsNotOwner => FlashLenderError::Custom(String::from("O::CallerIsNotOwner")),
OwnableError::NewOwnerIsZero => FlashLenderError::Custom(String::from("O::NewOwnerIsZero")),
}
}
}

impl From<AccessControlError> for FlashLenderError {
fn from(access: AccessControlError) -> Self {
match access {
AccessControlError::MissingRole => FlashLenderError::Custom(String::from("AC::MissingRole")),
AccessControlError::RoleRedundant => FlashLenderError::Custom(String::from("AC::RoleRedundant")),
AccessControlError::InvalidCaller => FlashLenderError::Custom(String::from("AC::InvalidCaller")),
}
}
}

impl From<PausableError> for FlashLenderError {
fn from(pausable: PausableError) -> Self {
match pausable {
PausableError::Paused => FlashLenderError::Custom(String::from("P::Paused")),
PausableError::NotPaused => FlashLenderError::Custom(String::from("P::NotPaused")),
}
}
}

impl From<ReentrancyGuardError> for FlashLenderError {
fn from(guard: ReentrancyGuardError) -> Self {
match guard {
ReentrancyGuardError::ReentrantCall => FlashLenderError::Custom(String::from("RG::ReentrantCall")),
}
}
}
6 changes: 6 additions & 0 deletions contracts/common/errors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod access_control;
mod flashloan;
mod ownable;
mod pausable;
mod payment_splitter;
Expand All @@ -9,6 +10,10 @@ mod reentrancy_guard;
mod timelock_controller;

pub use access_control::AccessControlError;
pub use flashloan::{
FlashBorrowerError,
FlashLenderError,
};
pub use ownable::OwnableError;
pub use pausable::PausableError;
pub use payment_splitter::PaymentSplitterError;
Expand All @@ -19,6 +24,7 @@ pub use psp1155::{
pub use psp22::{
PSP22Error,
PSP22ReceiverError,
PSP22TokenTimelockError,
};
pub use psp721::{
PSP721Error,
Expand Down
51 changes: 51 additions & 0 deletions contracts/common/errors/psp22.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,54 @@ impl From<PSP22ReceiverError> for PSP22Error {
}
}
}

#[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum PSP22TokenTimelockError {
PSP22Error(PSP22Error),
/// Returned if the owner wants to withdraw the tokens before the release time
CurrentTimeIsBeforeReleaseTime,
/// Returned if there are no tokens to be released
NoTokensToRelease,
/// Returned if the timestamp provided is before the current time
ReleaseTimeIsBeforeCurrentTime,
}

impl From<PSP22Error> for PSP22TokenTimelockError {
fn from(error: PSP22Error) -> Self {
match error {
PSP22Error::Custom(message) => PSP22TokenTimelockError::PSP22Error(PSP22Error::Custom(message)),
PSP22Error::InsufficientBalance => PSP22TokenTimelockError::PSP22Error(PSP22Error::InsufficientBalance),
PSP22Error::InsufficientAllowance => PSP22TokenTimelockError::PSP22Error(PSP22Error::InsufficientAllowance),
PSP22Error::ZeroRecipientAddress => PSP22TokenTimelockError::PSP22Error(PSP22Error::ZeroRecipientAddress),
PSP22Error::ZeroSenderAddress => PSP22TokenTimelockError::PSP22Error(PSP22Error::ZeroSenderAddress),
PSP22Error::SafeTransferCheckFailed(message) => {
PSP22TokenTimelockError::PSP22Error(PSP22Error::SafeTransferCheckFailed(message))
}
}
}
}

impl From<OwnableError> for PSP22TokenTimelockError {
fn from(ownable: OwnableError) -> Self {
PSP22TokenTimelockError::PSP22Error(ownable.into())
}
}

impl From<AccessControlError> for PSP22TokenTimelockError {
fn from(access: AccessControlError) -> Self {
PSP22TokenTimelockError::PSP22Error(access.into())
}
}

impl From<PausableError> for PSP22TokenTimelockError {
fn from(pausable: PausableError) -> Self {
PSP22TokenTimelockError::PSP22Error(pausable.into())
}
}

impl From<ReentrancyGuardError> for PSP22TokenTimelockError {
fn from(guard: ReentrancyGuardError) -> Self {
PSP22TokenTimelockError::PSP22Error(guard.into())
}
}
2 changes: 1 addition & 1 deletion contracts/finance/payment-splitter/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct PaymentSplitterData {
declare_storage_trait!(PaymentSplitterStorage, PaymentSplitterData);

#[brush::wrapper]
pub type PaymentSplitterWrapper = dyn PaymentSplitter;
pub type PaymentSplitterRef = dyn PaymentSplitter;

/// This contract allows splitting native token payments among a group of accounts. The sender does not need to be aware
/// that the native token will be split in this way, since it is handled transparently by the contract.
Expand Down
7 changes: 4 additions & 3 deletions contracts/governance/timelock-controller/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ where
}

#[brush::wrapper]
pub type TimelockControllerWrapper = dyn TimelockController + AccessControl;
pub type TimelockControllerRef = dyn TimelockController + AccessControl;

/// Contract module which acts as a time-locked controller. When set as the
/// owner of an `Ownable` smart contract, it enforces a timelock on all
Expand Down Expand Up @@ -432,18 +432,19 @@ pub trait TimelockController: AccessControl + TimelockControllerStorage + Flush
// Flush the state into storage before the cross call.
// Because during cross call we cann call this contract(for example for `update_delay` method).
self.flush();
build_call::<DefaultEnvironment>()
let result = build_call::<DefaultEnvironment>()
.callee(transaction.callee)
.gas_limit(transaction.gas_limit)
.transferred_value(transaction.transferred_value)
.exec_input(ExecutionInput::new(transaction.selector.into()).push_arg(CallInput(&transaction.input)))
.returns::<()>()
.fire()
.map_err(|_| TimelockControllerError::UnderlyingTransactionReverted)?;
.map_err(|_| TimelockControllerError::UnderlyingTransactionReverted);

// Load the sate of the contract after the cross call.
self.load();

result?;
self._emit_call_executed_event(id, i, transaction);
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/security/pausable/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ where
}

#[brush::wrapper]
pub type PausableWrapper = dyn Pausable;
pub type PausableRef = dyn Pausable;

/// Contract module, which allows children to implement an emergency stop
/// mechanism that an authorized account can trigger.
Expand Down
2 changes: 1 addition & 1 deletion contracts/token/psp1155/src/extensions/burnable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use brush::traits::{
use ink_prelude::vec::Vec;

#[brush::wrapper]
pub type PSP1155BurnableWrapper = dyn PSP1155Burnable + PSP1155;
pub type PSP1155BurnableRef = dyn PSP1155Burnable + PSP1155;

#[brush::trait_definition]
pub trait PSP1155Burnable: PSP1155 {
Expand Down
2 changes: 1 addition & 1 deletion contracts/token/psp1155/src/extensions/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct PSP1155MetadataData {
declare_storage_trait!(PSP1155MetadataStorage, PSP1155MetadataData);

#[brush::wrapper]
pub type PSP1155MetadataWrapper = dyn PSP1155Metadata;
pub type PSP1155MetadataRef = dyn PSP1155Metadata;

#[brush::trait_definition]
pub trait PSP1155Metadata: PSP1155MetadataStorage {
Expand Down
2 changes: 1 addition & 1 deletion contracts/token/psp1155/src/extensions/mintable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use brush::traits::{
use ink_prelude::vec::Vec;

#[brush::wrapper]
pub type PSP1155MintableWrapper = dyn PSP1155Mintable + PSP1155;
pub type PSP1155MintableRef = dyn PSP1155Mintable + PSP1155;

#[brush::trait_definition]
pub trait PSP1155Mintable: PSP1155 {
Expand Down
Loading

0 comments on commit dba1323

Please sign in to comment.