- Contract Overview
- ERC20 Functions
- Crowdsale Functions
- Administrative Functions
- Utility Functions
- View Functions
- Events
- Error Codes
- Gas Estimates
- Usage Examples
Contract Name: ReferenceSystemDeFi
Symbol: RSD
Decimals: 18
Standard: ERC20 + Custom Extensions
Solidity Version: >=0.6.0
Transfers tokens from the caller's account to recipient.
Parameters:
recipient(address): The address to receive tokensamount(uint256): The amount of tokens to transfer
Returns:
bool:trueif the transfer succeeded
Effects:
- Updates balances
- Triggers supply adjustment mechanism
- Distributes rewards to miners and pools
- May trigger PoBet mechanism
Events Emitted:
Transfer(address from, address to, uint256 value)SupplyAdjustment(bool reduction, uint256 amount)Reward(address miner, uint256 amount)Pool(uint256 amount)
Gas Cost: ~150,000 - 300,000 gas (depending on supply adjustments)
Example:
// Transfer 100 RSD tokens
await rsdContract.transfer("0x742d35Cc6084F81170839f78E5EbE9dB1eD4e3c8", web3.utils.toWei("100", "ether"));Allows spender to withdraw from the caller's account multiple times, up to the amount.
Parameters:
spender(address): The address authorized to spend tokensamount(uint256): The maximum amount authorized
Returns:
bool:trueif the approval succeeded
Events Emitted:
Approval(address owner, address spender, uint256 value)
Gas Cost: ~45,000 gas
Transfers tokens from sender to recipient using the allowance mechanism.
Parameters:
sender(address): The address to send tokens fromrecipient(address): The address to receive tokensamount(uint256): The amount of tokens to transfer
Returns:
bool:trueif the transfer succeeded
Requirements:
- Caller must have allowance for
sender's tokens of at leastamount
Gas Cost: ~200,000 - 350,000 gas
Atomically increases the allowance granted to spender by the caller.
Parameters:
spender(address): The address to increase allowance foraddedValue(uint256): The amount to increase the allowance by
Gas Cost: ~50,000 gas
Atomically decreases the allowance granted to spender by the caller.
Parameters:
spender(address): The address to decrease allowance forsubtractedValue(uint256): The amount to decrease the allowance by
Requirements:
- Current allowance must be >=
subtractedValue
Gas Cost: ~50,000 gas
Allows users to purchase RSD tokens by sending ETH.
Parameters:
beneficiary(address): The address to receive the purchased tokens
Payment:
- Must send ETH with the transaction
- Conversion rate:
1 ETH = SALE_RATE * 1.5 RSD - Maximum purchase: 50,000 RSD per transaction
Requirements:
- Crowdsale must be active (within duration)
- Purchase amount must not exceed maximum
Events Emitted:
Transfer(address(0), beneficiary, amount)
Gas Cost: ~200,000 gas
Example:
// Purchase RSD tokens with 1 ETH
await rsdContract.crowdsale(userAddress, {
value: web3.utils.toWei("1", "ether"),
gas: 200000
});Fallback function that automatically calls crowdsale(msg.sender) when ETH is sent to the contract.
Example:
// Send ETH directly to contract
await web3.eth.sendTransaction({
from: userAddress,
to: rsdContractAddress,
value: web3.utils.toWei("1", "ether"),
gas: 200000
});All administrative functions require onlyOwner modifier.
Updates the crowdsale conversion rate.
Parameters:
rate(uint16): New sale rate (tokens per ETH)
Example:
// Set rate to 3000 RSD per ETH
await rsdContract.updateSaleRate(3000, {from: ownerAddress});Updates the crowdsale duration.
Parameters:
timestampDuration(uint128): Duration in seconds from contract deployment
Updates the market cap expansion rate.
Parameters:
expansionRate(uint16): New expansion rate (affects reward pool growth)
Updates the maximum transaction interval for target supply adjustments.
Parameters:
maxTxInterval(uint16): Maximum number of transactions before adjustment
Updates the minimum transaction interval.
Parameters:
minTxInterval(uint16): Minimum number of transactions before adjustment
Updates the seed number for random number generation.
Parameters:
newSeedNumber(uint16): New seed for randomization
Enables or disables owner rewards.
Parameters:
should(bool): Whether to reward the owner
Withdraws specific amount of crowdsale proceeds.
Parameters:
account(address payable): Address to receive the ETHamount(uint256): Amount of ETH to withdraw (in wei)
Withdraws all crowdsale proceeds.
Parameters:
account(address payable): Address to receive the ETH
Withdraws accidentally sent ERC20/BEP20 tokens.
Parameters:
tokenAddress(address): Address of the token contract to withdraw
Burns tokens from the caller's balance.
Parameters:
amount(uint256): Amount of tokens to burn
Effects:
- Reduces total supply
- Updates market cap total supply
Example:
// Burn 50 RSD tokens
await rsdContract.burn(web3.utils.toWei("50", "ether"), {from: userAddress});Generates a new random number and updates the internal randomness state.
Gas Cost: ~50,000 gas
Generates and emits a random number for testing purposes.
Parameters:
modulus(uint256): The upper bound for the random number
Events Emitted:
RandomNumber(uint256 modulus, uint256 randomNumber)
Mints tokens for stakeholders (only callable by stake helper contract).
Parameters:
stakeholder(address): Address to receive minted tokensamount(uint256): Amount of tokens to mint
Requirements:
- Caller must be the designated stake helper contract
Returns the token name: "Reference System for DeFi"
Returns the token symbol: "RSD"
Returns the number of decimals: 18
Returns the current total supply of tokens
Returns the token balance of account
Returns remaining tokens that spender can spend on behalf of owner
Returns the market cap total supply (for price calculations)
Returns the target total supply used in adjustment mechanisms
Returns the current "more than once" random number
Returns the current Q value used in optimal policy calculations
Returns the current seed number for randomization
Returns the crowdsale duration in seconds
Returns the current expansion rate
Returns the current crowdsale rate
Returns the ceiling of log base 2 of x (utility function)
Emitted when tokens are transferred, including minting and burning.
Emitted when allowance is set by approve or increaseAllowance/decreaseAllowance.
Emitted when token supply is adjusted (minted or burned).
Emitted when the adjustment policy is updated.
Emitted when someone wins the Proof of Bet mechanism.
Emitted when rewards are distributed to miners or owner.
Emitted when tokens are added to the reward pool.
Emitted by obtainRandomNumber for testing purposes.
"ERC20: transfer from the zero address""ERC20: transfer to the zero address""ERC20: transfer amount exceeds balance""ERC20: transfer amount exceeds allowance""ERC20: approve from the zero address""ERC20: approve to the zero address""ERC20: mint to the zero address""ERC20: burn from the zero address""ERC20: burn amount exceeds balance""ERC20: decreased allowance below zero"
"RSD: crowdsale is over""RSD: required amount exceeds the maximum allowed""RSD: required amount exceeds the balance""RSD: does not have any balance""RSD: only stake helper can call this function"
"Ownable: caller is not the owner"
| Function | Estimated Gas | Notes |
|---|---|---|
transfer |
150,000 - 300,000 | Varies with supply adjustments |
approve |
45,000 | Standard approval |
transferFrom |
200,000 - 350,000 | Includes transfer mechanics |
crowdsale (receive/fallback) |
200,000 | Token purchase |
burn |
100,000 | Token burning |
| Administrative functions | 50,000 - 100,000 | Parameter updates |
| View functions | < 30,000 | Read-only operations |
const Web3 = require('web3');
const web3 = new Web3('http://localhost:8545');
// Purchase tokens
const purchaseAmount = web3.utils.toWei('1', 'ether'); // 1 ETH
const tx = await rsdContract.crowdsale(buyerAddress, {
from: buyerAddress,
value: purchaseAmount,
gas: 200000
});
console.log('Tokens purchased:', tx.logs[0].args.value.toString());// Transfer tokens (triggers supply adjustment)
const transferAmount = web3.utils.toWei('100', 'ether');
const tx = await rsdContract.transfer(recipientAddress, transferAmount, {
from: senderAddress,
gas: 300000
});
// Check events for supply adjustments
tx.logs.forEach(log => {
if (log.event === 'SupplyAdjustment') {
console.log('Supply adjusted:', log.args);
}
});// Update sale rate (owner only)
await rsdContract.updateSaleRate(2500, {from: ownerAddress});
// Withdraw crowdsale proceeds
await rsdContract.withdrawSales(ownerAddress, {from: ownerAddress});
// Update system parameters
await rsdContract.updateExpansionRate(1200, {from: ownerAddress});
await rsdContract.shouldRewardOwner(false, {from: ownerAddress});// Listen for all RSD events
rsdContract.events.allEvents({
fromBlock: 'latest'
}, (error, event) => {
if (error) {
console.error('Error:', error);
} else {
console.log('Event:', event.event, event.returnValues);
}
});
// Listen for specific events
rsdContract.events.PoBet({
fromBlock: 'latest'
}, (error, event) => {
if (!error) {
console.log('PoBet winner:', event.returnValues.winner);
console.log('Amount won:', event.returnValues.amount);
}
});Note: All gas estimates are approximate and may vary based on network conditions and actual transaction complexity. Always test transactions on testnets before mainnet deployment.