WA-238 get Daily Limit by token from the safe smart contract (useful for spentToday)

This commit is contained in:
apanizo 2018-05-11 18:07:02 +02:00
parent 1e42b3d67e
commit 86fad267ca
2 changed files with 43 additions and 2 deletions

View File

@ -12,8 +12,9 @@ import SafeView from '~/routes/safe/component/Safe'
import AppRoutes from '~/routes'
import { WITHDRAWN_BUTTON_TEXT } from '~/routes/safe/component/Safe/DailyLimit'
import WithdrawnComponent, { SEE_TXS_BUTTON_TEXT } from '~/routes/safe/component/Withdrawn'
import { getBalanceInEtherOf } from '~/wallets/getWeb3'
import { getBalanceInEtherOf, getProviderInfo } from '~/wallets/getWeb3'
import { sleep } from '~/utils/timer'
import withdrawn, { DESTINATION_PARAM, VALUE_PARAM, getDailyLimitFrom } from '~/routes/safe/component/Withdrawn/withdrawn'
describe('React DOM TESTS > Withdrawn funds from safe', () => {
let SafeDom
@ -70,4 +71,22 @@ describe('React DOM TESTS > Withdrawn funds from safe', () => {
const visitTxsButton = withdrawnButtons[0]
expect(visitTxsButton.props.children).toEqual(SEE_TXS_BUTTON_TEXT)
})
it('spentToday dailyLimitModule property is updated correctly', async () => {
const providerInfo = await getProviderInfo()
const userAddress = providerInfo.account
const values = {
[DESTINATION_PARAM]: userAddress,
[VALUE_PARAM]: '0.01',
}
await withdrawn(values, address, userAddress)
await withdrawn(values, address, userAddress)
const ethAddress = 0
const dailyLimit: DailyLimitProps = await getDailyLimitFrom(address, ethAddress)
expect(dailyLimit.value).toBe(0.5)
expect(dailyLimit.spentToday).toBe(0.02)
})
})

View File

@ -1,11 +1,14 @@
// @flow
import { getWeb3 } from '~/wallets/getWeb3'
import { getGnosisSafeContract, getCreateDailyLimitExtensionContract } from '~/wallets/safeContracts'
import { type DailyLimitProps } from '~/routes/safe/store/model/safe'
export const LIMIT_POSITION = 0
export const SPENT_TODAY_POS = 1
export const DESTINATION_PARAM = 'destination'
export const VALUE_PARAM = 'ether'
const withdrawn = async (values: Object, safeAddress: string, userAccount: string): Promise<void> => {
const getDailyLimitModuleFrom = async (safeAddress) => {
const web3 = getWeb3()
const gnosisSafe = getGnosisSafeContract(web3).at(safeAddress)
@ -17,6 +20,25 @@ const withdrawn = async (values: Object, safeAddress: string, userAccount: strin
throw new Error('Using an extension of different safe')
}
return dailyLimitModule
}
export const getDailyLimitFrom = async (safeAddress, tokenAddress): DailyLimitProps => {
const web3 = getWeb3()
const dailyLimitModule = await getDailyLimitModuleFrom(safeAddress)
const dailyLimitEth = await dailyLimitModule.dailyLimits(tokenAddress)
const limit = web3.fromWei(dailyLimitEth[LIMIT_POSITION].valueOf(), 'ether').toString()
const spentToday = web3.fromWei(dailyLimitEth[SPENT_TODAY_POS].valueOf(), 'ether').toString()
return { value: Number(limit), spentToday: Number(spentToday) }
}
const withdrawn = async (values: Object, safeAddress: string, userAccount: string): Promise<void> => {
const web3 = getWeb3()
const dailyLimitModule = await getDailyLimitModuleFrom(safeAddress)
const destination = values[DESTINATION_PARAM]
const value = web3.toWei(values[VALUE_PARAM], 'ether')