add approval to create subscription
This commit is contained in:
parent
da6e808e70
commit
906229de14
|
@ -1,4 +1,4 @@
|
||||||
import React from 'react';
|
import React, { useState, useEffect } from 'react';
|
||||||
import Button from '@material-ui/core/Button';
|
import Button from '@material-ui/core/Button';
|
||||||
import { Formik } from 'formik'
|
import { Formik } from 'formik'
|
||||||
import Subscription from 'Embark/contracts/Subscription';
|
import Subscription from 'Embark/contracts/Subscription';
|
||||||
|
@ -7,6 +7,7 @@ import TextField from '@material-ui/core/TextField'
|
||||||
import { withStyles } from '@material-ui/core/styles';
|
import { withStyles } from '@material-ui/core/styles';
|
||||||
import Divider from '@material-ui/core/Divider';
|
import Divider from '@material-ui/core/Divider';
|
||||||
|
|
||||||
|
const { createAgreement } = Subscription.methods;
|
||||||
|
|
||||||
const styles = theme => ({
|
const styles = theme => ({
|
||||||
root: {
|
root: {
|
||||||
|
@ -66,7 +67,30 @@ const Title = ({ className }) => (
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async function checkAndSetAllowance(setAllowance) {
|
||||||
|
const payor = await web3.eth.getCoinbase()
|
||||||
|
const approved = await DAI.methods.allowance(payor, Subscription.address).call()
|
||||||
|
setAllowance(approved)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function approveTransfers(setAllowance) {
|
||||||
|
const payor = await web3.eth.getCoinbase()
|
||||||
|
const balance = await DAI.methods.balanceOf(payor).call()
|
||||||
|
DAI
|
||||||
|
.methods
|
||||||
|
.approve(Subscription.address, balance).send()
|
||||||
|
.then(res => { setAllowance(balance)})
|
||||||
|
.catch(console.log)
|
||||||
|
}
|
||||||
|
|
||||||
function CreateSubscription({ classes, history }) {
|
function CreateSubscription({ classes, history }) {
|
||||||
|
const [allowance, setAllowance] = useState(0)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
checkAndSetAllowance(setAllowance)
|
||||||
|
}, [])
|
||||||
|
console.log({allowance}, !!Number(allowance))
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Formik
|
<Formik
|
||||||
initialValues={{
|
initialValues={{
|
||||||
|
@ -75,18 +99,31 @@ function CreateSubscription({ classes, history }) {
|
||||||
description: '',
|
description: '',
|
||||||
}}
|
}}
|
||||||
onSubmit={async (values, { resetForm }) => {
|
onSubmit={async (values, { resetForm }) => {
|
||||||
console.log({values,Subscription, DAI})
|
console.log({values,Subscription, DAI}, DAI.address)
|
||||||
{/* const args = [
|
const { receiver, amount, description } = values
|
||||||
receiver,
|
// Start immediately
|
||||||
payor,
|
const startDate = "0"
|
||||||
TestToken.address,
|
const payor = await web3.eth.getCoinbase()
|
||||||
annualSalary,
|
const approved = await DAI.methods.allowance(payor, Subscription.address).call()
|
||||||
"0",
|
const args = [
|
||||||
"ipfs/hash"
|
receiver,
|
||||||
] */}
|
payor,
|
||||||
|
DAI.address,
|
||||||
const payor = web3.eth.getCoinbase();
|
web3.utils.toWei(amount),
|
||||||
//Subscription.methods.createAgreement
|
startDate,
|
||||||
|
description
|
||||||
|
]
|
||||||
|
console.log({args, approved})
|
||||||
|
createAgreement(...args)
|
||||||
|
.send()
|
||||||
|
.then(res => {
|
||||||
|
console.log({res})
|
||||||
|
resetForm()
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
console.log({err})
|
||||||
|
resetForm()
|
||||||
|
})
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{({
|
{({
|
||||||
|
@ -155,7 +192,9 @@ function CreateSubscription({ classes, history }) {
|
||||||
onBlur={handleBlur}
|
onBlur={handleBlur}
|
||||||
value={values.description || ''}
|
value={values.description || ''}
|
||||||
/>
|
/>
|
||||||
<Button type="submit" color="primary" variant="contained" className={classes.formButton}>{isSubmitting ? 'Ethereum Submission In Progress' : 'Create Subscription'}</Button>
|
{!!Number(allowance)
|
||||||
|
? <Button type="submit" color="primary" variant="contained" className={classes.formButton}>{isSubmitting ? 'Ethereum Submission In Progress' : 'Create Subscription'}</Button>
|
||||||
|
: <Button color="secondary" variant="contained" className={classes.formButton} onClick={()=> { approveTransfers(setAllowance)}}>Grant DAI Transfer Permissions</Button>}
|
||||||
</form>
|
</form>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,9 +57,9 @@ module.exports = {
|
||||||
// assumed to be the intended environment by `embark run`
|
// assumed to be the intended environment by `embark run`
|
||||||
development: {
|
development: {
|
||||||
dappConnection: [
|
dappConnection: [
|
||||||
|
"$WEB3", // uses pre existing web3 object if available (e.g in Mist)
|
||||||
"ws://localhost:8546",
|
"ws://localhost:8546",
|
||||||
"http://localhost:8545",
|
"http://localhost:8545",
|
||||||
"$WEB3" // uses pre existing web3 object if available (e.g in Mist)
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -144,7 +144,8 @@ contract Subscription {
|
||||||
|
|
||||||
function supply(uint256 amount) public returns (uint256) {
|
function supply(uint256 amount) public returns (uint256) {
|
||||||
uint256 balance = payorBalances[msg.sender];
|
uint256 balance = payorBalances[msg.sender];
|
||||||
dai.transferFrom(msg.sender, address(this), amount);
|
require(dai.transferFrom(msg.sender, address(this), amount), "Error transfering DAI");
|
||||||
|
//dai.transferFrom(msg.sender, address(this), amount);
|
||||||
compound.supply(daiAddress, amount);
|
compound.supply(daiAddress, amount);
|
||||||
uint256 newBalance = balance.add(amount);
|
uint256 newBalance = balance.add(amount);
|
||||||
payorBalances[msg.sender] = newBalance;
|
payorBalances[msg.sender] = newBalance;
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
{
|
{
|
||||||
"dappConnection": [
|
"dappConnection": [
|
||||||
|
"$WEB3",
|
||||||
"ws://localhost:8546",
|
"ws://localhost:8546",
|
||||||
"http://localhost:8545",
|
"http://localhost:8545"
|
||||||
"$WEB3"
|
|
||||||
],
|
],
|
||||||
"dappAutoEnable": true,
|
"dappAutoEnable": true,
|
||||||
"warnIfMetamask": true,
|
"warnIfMetamask": true,
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -66,6 +66,7 @@ contract("subscription", function () {
|
||||||
"0",
|
"0",
|
||||||
"ipfs/hash"
|
"ipfs/hash"
|
||||||
]
|
]
|
||||||
|
const approval = await TestToken.methods.approve(Subscription.address, toWei("1000000")).send({from: payor})
|
||||||
const agreementCreation = await Subscription.methods.createAgreement(
|
const agreementCreation = await Subscription.methods.createAgreement(
|
||||||
...args
|
...args
|
||||||
).send({ from: payor })
|
).send({ from: payor })
|
||||||
|
|
Loading…
Reference in New Issue