Skip to content

Commit

Permalink
Merge pull request #26 from FinBook/Staking4
Browse files Browse the repository at this point in the history
Staking4
  • Loading branch information
yizhoucao committed Jun 3, 2019
2 parents f826826 + 48c2377 commit 95adaa5
Show file tree
Hide file tree
Showing 18 changed files with 364 additions and 72 deletions.
28 changes: 28 additions & 0 deletions src/ts/actions/__snapshots__/stakeActions.test.ts.snap
Expand Up @@ -14,6 +14,13 @@ Object {
}
`;

exports[`actions allowanceUpdate 1`] = `
Object {
"type": "stakeAllowance",
"value": 123,
}
`;

exports[`actions balancesUpdate 1`] = `
Object {
"type": "stakeBalance",
Expand Down Expand Up @@ -53,6 +60,15 @@ Array [
]
`;

exports[`actions getAllowance 1`] = `
Array [
Object {
"type": "stakeAllowance",
"value": 100,
},
]
`;

exports[`actions getBalances 1`] = `
Array [
Object {
Expand Down Expand Up @@ -129,6 +145,10 @@ Array [
"type": "stakeBalance",
"value": 100,
},
Object {
"type": "stakeAllowance",
"value": 100,
},
Object {
"type": "stakeAddresses",
"value": Object {
Expand Down Expand Up @@ -227,6 +247,10 @@ Array [
"type": "stakeBalance",
"value": 100,
},
Object {
"type": "stakeAllowance",
"value": 100,
},
Object {
"type": "stakeAddresses",
"value": Object {
Expand Down Expand Up @@ -300,6 +324,10 @@ Array [
"type": "stakeBalance",
"value": 100,
},
Object {
"type": "stakeAllowance",
"value": 100,
},
Object {
"type": "stakeAddresses",
"value": Object {
Expand Down
17 changes: 17 additions & 0 deletions src/ts/actions/stakeActions.test.ts
Expand Up @@ -52,6 +52,7 @@ describe('actions', () => {
Promise.resolve(500)
);
web3Wrapper.getErc20Balance = jest.fn(() => Promise.resolve(100));
web3Wrapper.getErc20Allowance = jest.fn(() => Promise.resolve(100));

test('balancesUpdate', () => {
expect(stakeActions.balancesUpdate(123)).toMatchSnapshot();
Expand All @@ -69,6 +70,22 @@ describe('actions', () => {
}, 0)
);
});
test('allowanceUpdate', () => {
expect(stakeActions.allowanceUpdate(123)).toMatchSnapshot();
});

test('getAllowance', () => {
const store: any = stakeStore({
web3: { account: '0x415DE7Edfe2c9bBF8449e33Ff88c9be698483CC0' }
});
store.dispatch(stakeActions.getAllowance());
return new Promise(resolve =>
setTimeout(() => {
expect(store.getActions()).toMatchSnapshot();
resolve();
}, 0)
);
});
test('addressesUpdate', () => {
expect(
stakeActions.addressesUpdate({
Expand Down
20 changes: 20 additions & 0 deletions src/ts/actions/stakeActions.ts
Expand Up @@ -37,6 +37,25 @@ export function getBalances(): VoidThunkAction {
};
}

export function allowanceUpdate(duo: number) {
return {
type: CST.AC_STK_ALLOWANCE,
value: duo
};
}

export function getAllowance(): VoidThunkAction {
return async (dispatch, getState) => {
const account = getState().web3.account;
const duoAllowance = await web3Wrapper.getErc20Allowance(
web3Wrapper.contractAddresses.DUO.address,
account,
web3Wrapper.contractAddresses.Stake.address
);
dispatch(allowanceUpdate(duoAllowance));
};
}

export function addressesUpdate(addr: IStakeAddress) {
return {
type: CST.AC_STK_ADDRESSES,
Expand Down Expand Up @@ -124,6 +143,7 @@ export function refresh(): VoidThunkAction {
return async dispatch => {
await dispatch(getStates());
dispatch(getBalances());
dispatch(getAllowance());
dispatch(getAddresses());
dispatch(getUserStake());
dispatch(getOracleStake());
Expand Down
1 change: 1 addition & 0 deletions src/ts/common/constants.ts
Expand Up @@ -25,6 +25,7 @@ export const AC_ESP_OTHER_CONTRACT_ADDR = 'esplanadeOtherContractAddress';
export const AC_ESP_VOTING_STAGE = 'esplanadeVotingStage';
export const AC_ESP_VOTING_DATA = 'esplanadeVotingData';
export const AC_STK_BALANCE = 'stakeBalance';
export const AC_STK_ALLOWANCE = 'stakeAllowance';
export const AC_STK_STATES = 'stakeStates';
export const AC_STK_ADDRESSES = 'stakeAddresses';
export const AC_STK_SUB = 'stakeSubscription';
Expand Down
117 changes: 117 additions & 0 deletions src/ts/common/stakingCST.ts
@@ -0,0 +1,117 @@
export interface ILocaleText {
EN: string;
CN: string;
[key: string]: string;
}
export const LOCALE_CN = 'CN';
export const LOCALE_EN = 'EN';

export const STK_TITLE: ILocaleText = {
[LOCALE_CN]: 'DUO 质押活动',
[LOCALE_EN]: 'DUO Staking Campaign'
};

export const STK_STAKE: ILocaleText = {
[LOCALE_CN]: '质押',
[LOCALE_EN]: 'Stake'
};

export const STK_UNSTAKE: ILocaleText = {
[LOCALE_CN]: '解除质押',
[LOCALE_EN]: 'Unstake'
};

export const STK_ENABLED: ILocaleText = {
[LOCALE_CN]: '开放质押',
[LOCALE_EN]: 'Enabled'
};

export const STK_DISABLED: ILocaleText = {
[LOCALE_CN]: '质押暂停',
[LOCALE_EN]: 'Disabled'
};

export const STK_ADDRESS: ILocaleText = {
[LOCALE_CN]: '账户地址',
[LOCALE_EN]: 'My Address'
};

export const STK_BALANCE: ILocaleText = {
[LOCALE_CN]: '钱包余额',
[LOCALE_EN]: 'Wallet Balance'
};

export const STK_AWARD: ILocaleText = {
[LOCALE_CN]: '质押奖励',
[LOCALE_EN]: 'Staking Award'
};

export const STK_APPROVE: ILocaleText = {
[LOCALE_CN]: '授权 DUO',
[LOCALE_EN]: 'Approve DUO'
};

export const STK_CLAIM: ILocaleText = {
[LOCALE_CN]: '领取奖励',
[LOCALE_EN]: 'Claim Award'
};

export const STK_ORACKE: ILocaleText = {
[LOCALE_CN]: '报价节点',
[LOCALE_EN]: 'Pricing Node'
};

export const STK_POOLSIZE: ILocaleText = {
[LOCALE_CN]: '节点总额',
[LOCALE_EN]: 'Node Balance'
};

export const STK_ESTREUTRN: ILocaleText = {
[LOCALE_CN]: '预期收益率',
[LOCALE_EN]: 'Est Return'
};

export const STK_MYSTAKE: ILocaleText = {
[LOCALE_CN]: '已质押',
[LOCALE_EN]: 'My Balance'
};

export const STK_ESTAWARD: ILocaleText = {
[LOCALE_CN]: '预计每周回报',
[LOCALE_EN]: 'Est Weekly Award'
};

export const STK_PLACEHODLER: ILocaleText = {
[LOCALE_CN]: '输入质押数量',
[LOCALE_EN]: 'Input Stake Amount'
};

export const STK_JOIN: ILocaleText = {
[LOCALE_CN]: '参与质押',
[LOCALE_EN]: 'Stake'
};

export const STK_WARING: ILocaleText = {
[LOCALE_CN]: 'DUO余额不足。',
[LOCALE_EN]: 'Not enough DUO balance.'
};

export const STK_REMIUNDER: ILocaleText = {
[LOCALE_CN]: '提醒',
[LOCALE_EN]: 'Reminder'
};

export const STK_REMIUNDERTEST: ILocaleText = {
[LOCALE_CN]: '首次质押前请先授权DUO。',
[LOCALE_EN]: 'Please apporve DUO before staking.'
};

export const STK_OK: ILocaleText = {
[LOCALE_CN]: '授权DUO',
[LOCALE_EN]: 'Approve Now'
};

export const STK_LOADINGNODES: ILocaleText = {
[LOCALE_CN]: '载入节点中。。。',
[LOCALE_EN]: 'Loading Nodes...'
};
1 change: 1 addition & 0 deletions src/ts/common/types.ts
Expand Up @@ -55,6 +55,7 @@ export interface IDualClassState {
export interface IStakeState {
readonly states: IStakeStates;
readonly duo: number;
readonly allowance: number;
readonly subscription: number;
readonly addresses: IStakeAddress;
readonly userStake: { [key: string]: IStakeLot[] };
Expand Down
32 changes: 18 additions & 14 deletions src/ts/components/Cards/StakingNodesCard.tsx
Expand Up @@ -7,6 +7,7 @@ import { IStakeLot, Web3Wrapper } from '@finbook/duo-contract-wrapper';
//import { Table } from 'antd';
import * as d3 from 'd3';
import * as React from 'react';
import * as StakingCST from 'ts/common/stakingCST';
import { stakeWrapper } from 'ts/common/wrappers';
//import * as CST from 'ts/common/constants';
import {
Expand All @@ -19,6 +20,7 @@ import {
} from './_styled';

interface IProps {
locale: string;
enabled: boolean;
title: string;
myDUO: number;
Expand Down Expand Up @@ -47,7 +49,7 @@ export default class AdminCard extends React.Component<IProps, IState> {
this.setState({ inputText: newText, inputValue: parseInt(value, 0) });
};
private handleStake = async () => {
const { myAddr, oracleAddr, myDUO } = this.props;
const { myAddr, oracleAddr, myDUO, locale } = this.props;
const { inputValue } = this.state;
if (inputValue <= myDUO) {
const txHash = await stakeWrapper.stake(myAddr, oracleAddr, inputValue, {
Expand All @@ -56,7 +58,7 @@ export default class AdminCard extends React.Component<IProps, IState> {
this.setState({ inputText: '', inputValue: 0 });
console.log('Transaction submit: ' + txHash);
} else {
window.alert('Not enough DUO balance');
window.alert(StakingCST.STK_WARING[locale]);
this.setState({ inputText: '', inputValue: 0 });
}
};
Expand All @@ -68,7 +70,7 @@ export default class AdminCard extends React.Component<IProps, IState> {
console.log('Transaction submit: ' + txHash);
};
public render() {
const { enabled, title, myStake, oracleAddr, oracleStakes } = this.props;
const { enabled, title, myStake, oracleAddr, oracleStakes, locale } = this.props;
const { inputText } = this.state;
const myStakeList = myStake[oracleAddr];
let myAccStake = 0;
Expand All @@ -79,7 +81,7 @@ export default class AdminCard extends React.Component<IProps, IState> {
const estReturn =
(3036 * Math.pow(2, Math.log(oracleStakes[oracleAddr]) / 2.3)) /
oracleStakes[oracleAddr] || 0;
const myReward = myAccStake * estReturn;
const myReward = myAccStake * estReturn / 52;
return (
<SCard
title={<SCardTitle>{title.toUpperCase()}</SCardTitle>}
Expand All @@ -90,7 +92,7 @@ export default class AdminCard extends React.Component<IProps, IState> {
<SCardTag3 style={{ pointerEvents: 'none', marginRight: 15 }}>
<div className="tag-content">
<div className={'tag-price USD'} style={{ fontSize: 12 }}>
Pool Size
{StakingCST.STK_POOLSIZE[locale]}
</div>
</div>
<div className="tag-subtext">
Expand All @@ -112,7 +114,7 @@ export default class AdminCard extends React.Component<IProps, IState> {
<SCardTag3 style={{ pointerEvents: 'none', marginRight: 15 }}>
<div className="tag-content">
<div className={'tag-price USD'} style={{ fontSize: 12 }}>
Est Return
{StakingCST.STK_ESTREUTRN[locale]}
</div>
</div>
<div className="tag-subtext">
Expand All @@ -134,7 +136,7 @@ export default class AdminCard extends React.Component<IProps, IState> {
<SCardTag3 style={{ pointerEvents: 'none', marginRight: 15 }}>
<div className="tag-content">
<div className={'tag-price USD'} style={{ fontSize: 12 }}>
My Stake
{StakingCST.STK_MYSTAKE[locale]}
</div>
</div>
<div className="tag-subtext">
Expand All @@ -156,7 +158,7 @@ export default class AdminCard extends React.Component<IProps, IState> {
<SCardTag3 style={{ pointerEvents: 'none', marginRight: 15 }}>
<div className="tag-content">
<div className={'tag-price USD'} style={{ fontSize: 12 }}>
Est Annual Reward
{StakingCST.STK_ESTAWARD[locale]}
</div>
</div>
<div className="tag-subtext">
Expand Down Expand Up @@ -197,26 +199,28 @@ export default class AdminCard extends React.Component<IProps, IState> {
}}
>
<SStakingInput
placeholder="Input staking amount"
placeholder={StakingCST.STK_PLACEHODLER[locale]}
value={inputText}
onChange={e => this.handleInputChange(e.target.value)}
/>
<SStakingButtonM
style={{ cursor: !enabled ? 'not-allowed' : 'default' }}
onClick={() => enabled && this.handleStake()}
>
Join Node
{StakingCST.STK_STAKE[locale]}
</SStakingButtonM>
</div>
<SStakingButtonF
style={{ cursor: !enabled ? 'not-allowed' : 'default' }}
onClick={() => enabled && this.handleUnstake()}
>
Unstake (
{StakingCST.STK_UNSTAKE[locale]} (
{myStakeList
? (myStakeList[0] as any)['amtInWei'] === '0'
? 0
: myStakeList.length
? myStakeList[0]
? (myStakeList[0] as any)['amtInWei'] === '0'
? 0
: myStakeList.length
: 0
: 0}
)
</SStakingButtonF>
Expand Down

0 comments on commit 95adaa5

Please sign in to comment.