Skip to content
This repository has been archived by the owner on Feb 6, 2019. It is now read-only.

Airdrop Contract #49

Merged
merged 13 commits into from
Feb 17, 2018
167 changes: 167 additions & 0 deletions contracts/Airdrop.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/* solhint-disable-next-line compiler-fixed */
pragma solidity ^0.4.17;

// Copyright 2018 OpenST Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ----------------------------------------------------------------------------
// Utility chain: Airdrop
//
// http://www.simpletoken.org/
//
// ----------------------------------------------------------------------------

import "./Workers.sol";
import "./Pricer.sol";


contract Airdrop is Pricer {

/*
* Events
*/
/// Emit AirdropPayment Event
event AirdropPayment(
Copy link
Contributor

Choose a reason for hiding this comment

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

up to three arguments can be indexed and it makes them (expanded and) searchable in the logs; also for formatting on events we've previously filled the line (different from function signatures)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indexing is cool. Does indexing cost extra gas or it's neglegible?
Can we do indexing on uint256 also?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

also for formatting on events we've previously filled the line (different from function signatures)
Didn't understand this point. Please explain with an example?

Copy link
Contributor

@jasonklein jasonklein Feb 15, 2018

Choose a reason for hiding this comment

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

@benjaminbollen: for consistency/ease, we started declaring events similar to functions,

event EventName(
arg,
arg,
arg)

This way it not subject to personal preference in terms of how many is too many arguments to list on the line. We can go back to the other way, if preferred, but that will mean changes in other rule for max number of arguments per line (so that how it looks in an individual dev's editor is not driving the decision).

Copy link
Contributor

Choose a reason for hiding this comment

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

the event AirdropPayment is missing the quote currency; worth adding if we have the price point?

address indexed _beneficiary,
uint256 _transferAmount,
Copy link
Contributor

Choose a reason for hiding this comment

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

Note: for alignment with Payment event from tokenAmount to transferAmount; we can take input from Saas what the best set of return values;

address indexed _commissionBeneficiary,
uint256 _commissionAmount,
uint256 _actualPricePoint,
address indexed _spender,
uint256 _airdropAmount
);

/*
* Storage
*/
Workers public workers;
address public airdropBudgetHolder;

/*
* Constructor
*/
/// @dev Takes _brandedToken, _baseCurrency, _workers, _airdropBudgetHolder;
/// constructor;
/// public method;
/// @param _brandedToken Branded Token
/// @param _baseCurrency Base Currency
/// @param _workers Workers contract address
/// @param _airdropBudgetHolder Airdrop Budget Holder Address
function Airdrop(
address _brandedToken,
bytes3 _baseCurrency,
Workers _workers,
address _airdropBudgetHolder)
public
Pricer(_brandedToken, _baseCurrency)
OpsManaged()
{
require(_workers != address(0));
require(airdropBudgetHolder != address(0));

workers = _workers;
airdropBudgetHolder = _airdropBudgetHolder;
}

/*
* External functions
*/
/// payAirdrop matches the behaviour of Pricer:pay with extra functionality of airdrop evaluation
/// @param _beneficiary beneficiary
/// @param _transferAmount transferAmount
/// @param _commissionBeneficiary commissionBeneficiary
/// @param _commissionAmount commissionAmount
/// @param _currency currency
/// @param _intendedPricePoint intendedPricePoint
/// @param _spender spender
/// @param _airdropAmount airdropAmount
/// @return uint256 totalPaid
function payAirdrop(
address _beneficiary,
uint256 _transferAmount,
address _commissionBeneficiary,
uint256 _commissionAmount,
bytes3 _currency,
uint256 _intendedPricePoint,
address _spender,
uint256 _airdropAmount)
public
returns (
uint256 /* totalPaid */)
{
require(workers.isWorker(msg.sender));
require(_spender != address(0));

require(isValidBeneficiaryData(_beneficiary, _transferAmount,
_commissionBeneficiary, _commissionAmount));

uint256 tokenAmount = _transferAmount;
uint256 commissionTokenAmount = _commissionAmount;
uint256 pricePoint = _intendedPricePoint;

// check Margin And Calculate BTAmount
if (_currency != "") {
(pricePoint, tokenAmount, commissionTokenAmount) = validateMarginAndCalculateBTAmount(_currency,
_intendedPricePoint, _transferAmount, _commissionAmount);
}

require(performAirdropTransferToSpender(_spender, _airdropAmount,
tokenAmount, commissionTokenAmount));
require(performTransfers(_spender, _beneficiary, tokenAmount,
_commissionBeneficiary, commissionTokenAmount));

/// Emit AirdropPayment Event
AirdropPayment(_beneficiary, _transferAmount, _commissionBeneficiary,
_commissionAmount, pricePoint, _spender, _airdropAmount);

return ((tokenAmount + commissionTokenAmount));
}

/*
* Private functions
*/
/// @dev Takes _spender, _airdropAmount, _tokenAmount, _commissionTokenAmount;
/// Calculate airdropUsed to transfer
/// Perform perform Airdrop Transfer To Spender
/// internal method;
/// @param _spender spenderUser
/// @param _airdropAmount airdropAmount
/// @param _tokenAmount tokenAmount
/// @param _commissionTokenAmount commissionTokenAmount
/// @return uint256 airdropUsed
function performAirdropTransferToSpender(
address _spender,
uint256 _airdropAmount,
uint256 _tokenAmount,
uint256 _commissionTokenAmount)
private
returns (
bool /* boolean value */)
{
uint256 totalPaid = (_tokenAmount + _commissionTokenAmount);
// Find out minimum of totalPaid and _airdropAmount
uint256 airdropUsed = _airdropAmount;
if (totalPaid < airdropUsed) {
airdropUsed = totalPaid;
}

// Prefund the user from the airdrop budget holder
if (airdropUsed > 0) {
require(EIP20Interface(brandedToken()).transferFrom(airdropBudgetHolder, _spender, airdropUsed));
}

return true;
}

}
Loading