This repository has been archived by the owner on May 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(storage): staking support (#286)
- Loading branch information
Showing
23 changed files
with
599 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { EventData } from 'web3-eth-contract' | ||
import BigNumber from 'bignumber.js' | ||
import config from 'config' | ||
|
||
import { loggingFactory } from '../../../logger' | ||
import { Handler, SupportedTokens } from '../../../definitions' | ||
import { StorageServices } from '../index' | ||
import StakeModel from '../models/stake.model' | ||
|
||
const logger = loggingFactory('storage:handler:stake') | ||
|
||
/** | ||
* Make a call to ERC20 token SC and return token symbol | ||
* Return `rbtc` for ZERO_ADDRESS | ||
* @param tokenContractAddress | ||
* @returns {SupportedTokens} token symbol | ||
*/ | ||
function getTokenSymbol (tokenContractAddress: string): SupportedTokens { | ||
if (!config.has(`storage.tokens.${tokenContractAddress}`)) { | ||
throw new Error(`Token at ${tokenContractAddress} not supported`) | ||
} | ||
|
||
return config.get<SupportedTokens>(`storage.tokens.${tokenContractAddress}`) | ||
} | ||
|
||
/** | ||
* Find or create stake | ||
* @param account | ||
* @param token | ||
* @returns {Promise<StakeModel>} stake | ||
*/ | ||
async function findOrCreateStake (account: string, token: string): Promise<StakeModel> { | ||
const stake = await StakeModel.findOne({ where: { account, token } }) | ||
|
||
if (stake) { | ||
return stake | ||
} | ||
const symbol = getTokenSymbol(token) | ||
return StakeModel.create({ account, token, symbol, total: 0 }) | ||
} | ||
|
||
const handlers = { | ||
async Staked (event: EventData, { stakeService }: StorageServices): Promise<void> { | ||
const { user: account, total, token, amount } = event.returnValues | ||
|
||
const stake = await findOrCreateStake(account, token) | ||
|
||
stake.total = new BigNumber(stake.total).plus(amount) | ||
await stake.save() | ||
logger.info(`Account ${account} stake amount ${amount}, final balance ${total}`) | ||
|
||
if (stakeService.emit) { | ||
stakeService.emit('updated', stake.toJSON()) | ||
} | ||
}, | ||
|
||
async Unstaked (event: EventData, { stakeService }: StorageServices): Promise<void> { | ||
const { user: account, total, token, amount } = event.returnValues | ||
|
||
const stake = await StakeModel.findOne({ where: { token, account } }) | ||
|
||
if (!stake) { | ||
throw new Error(`Stake for account ${account}, token ${token} not exist`) | ||
} | ||
|
||
stake.total = new BigNumber(stake.total).minus(amount) | ||
await stake.save() | ||
logger.info(`Account ${account} un-stake amount ${amount}, final balance ${total}`) | ||
|
||
if (stakeService.emit) { | ||
stakeService.emit('updated', stake.toJSON()) | ||
} | ||
} | ||
} | ||
|
||
function isValidEvent (value: string): value is keyof typeof handlers { | ||
return value in handlers | ||
} | ||
|
||
const handler: Handler<StorageServices> = { | ||
events: ['Staked', 'Unstaked'], | ||
process (event: EventData, services: StorageServices): Promise<void> { | ||
if (!isValidEvent(event.event)) { | ||
return Promise.reject(new Error(`Unknown event ${event.event}`)) | ||
} | ||
|
||
return handlers[event.event](event, services) | ||
} | ||
} | ||
|
||
export default handler |
Oops, something went wrong.