Skip to content

Instantly share code, notes, and snippets.

@ripper234
Created June 26, 2014 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ripper234/aafec73653deee9b8cd0 to your computer and use it in GitHub Desktop.
Save ripper234/aafec73653deee9b8cd0 to your computer and use it in GitHub Desktop.
First example of a Scripted Crowdsale on the Master Protocol
/**
This method is called by Master Protocol parser when parsing an incoming transaction into a crowdsale address. It is called once per incoming tx.
*/
handlePayment();
The method handlePayment() can call on auxilery methods to access some parameters:
/**
0 for BTC, 1 for MSC, etc... as per [the spec](https://github.com/mastercoin-MSC/spec#transaction-field-definitions)
*/
getIncomingCurrencyId();
/**
Returns the number of tokens that are incoming in the current payment (in base units e.g. satoshis or willets).
*/
getIncomingAmount();
/**
in milliseconds since the start of the crowdsale
*/
getTimeOfPayment();
/**
Only enabled for crowdsale that are time-limited, and is automatically calculated from getTimeOfPayment();
*/
getTimeUntilCrowdsaleEnds();
/**
the ordered ordinal of the token. The first ever payment into the crowdsale address gets this value as 0, the second value gets 1, etc...
*/
getPaymentOrdinal();
/**
Send @amount tokens to the source address of the current payment.
@currencyID - this allows for future support in crowdsales that simultaniously create more than one type of token.
*/
sendTokens(currencyID, amount);
Following are sample implementations of handlePayment for the above models:
/** Implements the original Mastercoin crowdsale model */
handlePaymentTimeBased() {
if (getIncomingCurrencyId() != CURRENCY_ID_BTC) {
// Only accept BTC
refundPayment(); // OPTIONAL - this refunds payments that are above some minimal anti-spam level ... assuming the tokens can be refunded for free. The tokens themselves can be used to buy the required BTC/MSC needed to send the refund transaction.
return;
}
if (getTimeUntilCrowdsaleEnds() <= 0) {
// The crowdsale has already expired
refundPayment();
return;
}
// Valid incoming tx ... let's send some MSC!
def amount = getIncomingAmount() * 100 * (1 + getTimeUntilCrowdsaleEnds / (MILLIS_IN_WEEK * 10))
sendPayment(getCreatedCurrencyId(), amount);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment