Skip to content

Commit

Permalink
Update # rep to redeem from tokens when switching account
Browse files Browse the repository at this point in the history
Also attempt to set to 0 after redeem succeeds, but hard to do this with current client library, see daostack/arc.js#412
  • Loading branch information
tibetsprague committed Feb 7, 2020
1 parent 0e6eead commit c6e5bc7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/actions/arcActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export function redeemProposal(daoAvatarAddress: string, proposalId: string, acc
};
}

export function redeemReputationFromToken(scheme: Scheme, addressToRedeem: string, privateKey: string|undefined, redeemerAddress: Address|undefined) {
export function redeemReputationFromToken(scheme: Scheme, addressToRedeem: string, privateKey: string|undefined, redeemerAddress: Address|undefined, redemptionSucceededCallback: () => void) {
return async (dispatch: Redux.Dispatch<any, any>) => {
const arc = getArc();

Expand Down Expand Up @@ -181,6 +181,7 @@ export function redeemReputationFromToken(scheme: Scheme, addressToRedeem: strin
try {
await arc.web3.eth.sendSignedTransaction(signedTransaction.rawTransaction);
dispatch(showNotification(NotificationStatus.Success, "Transaction was succesful!"));
redemptionSucceededCallback();
} catch(err) {
dispatch(showNotification(NotificationStatus.Failure, `Transaction failed: ${err.message}`));
}
Expand All @@ -191,7 +192,7 @@ export function redeemReputationFromToken(scheme: Scheme, addressToRedeem: strin
// send the transaction and get notifications
if (reputationFromTokenScheme) {
const agreementHash = await reputationFromTokenScheme.getAgreementHash();
reputationFromTokenScheme.redeem(addressToRedeem, agreementHash).subscribe(...observer);
reputationFromTokenScheme.redeem(addressToRedeem, agreementHash).subscribe(observer[0], observer[1], redemptionSucceededCallback);
}
}
};
Expand Down
20 changes: 12 additions & 8 deletions src/components/Scheme/ReputationFromToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ class ReputationFromToken extends React.Component<IProps, IState> {
};
}

private async _loadReputationBalance() {
const redeemerAddress = this.state.redeemerAddress;
private async _loadReputationBalance(redeemerAddress: string) {
if (redeemerAddress) {
const schemeState = this.props.schemeState;
const schemeAddress = schemeState.address;
Expand All @@ -111,25 +110,24 @@ class ReputationFromToken extends React.Component<IProps, IState> {
this.setState({
redemptionAmount,
alreadyRedeemed,
redeemerAddress,
});
} else {
this.setState({
redemptionAmount: new BN(0),
alreadyRedeemed: false,
redeemerAddress: null,
});
}
}

public async componentDidMount() {
await this._loadReputationBalance();
await this._loadReputationBalance(this.state.redeemerAddress);
}

public async componentDidUpdate(prevProps: IProps) {
if (!this.state.privateKey && this.props.currentAccountAddress !== prevProps.currentAccountAddress) {
this.setState({
redeemerAddress: this.props.currentAccountAddress,
});
await this._loadReputationBalance();
await this._loadReputationBalance(this.props.currentAccountAddress);
}
}

Expand All @@ -148,6 +146,7 @@ class ReputationFromToken extends React.Component<IProps, IState> {
const alreadyRedeemed = await schemeContract.methods.redeems(this.state.redeemerAddress).call();
if (alreadyRedeemed) {
this.props.showNotification(NotificationStatus.Failure, `Reputation for the account ${this.state.redeemerAddress} was already redeemed`);
this.redemptionSucceeded();
} else if (values.useTxSenderService === true) {
// construct the message to sign
// const signatureType = 1
Expand Down Expand Up @@ -241,6 +240,7 @@ class ReputationFromToken extends React.Component<IProps, IState> {
this.props.showNotification(NotificationStatus.Failure, `An error occurred on the transaction service: ${response.data.status}: ${response.data.message}`);
} else {
this.props.showNotification(NotificationStatus.Success, `You've successfully redeemed rep to ${values.accountAddress}`);
this.redemptionSucceeded();
}
} catch(err) {
this.props.showNotification(NotificationStatus.Failure, `${err.message}}`);
Expand All @@ -255,11 +255,15 @@ class ReputationFromToken extends React.Component<IProps, IState> {
// ,{from:_fromAccount}));
} else {
const scheme = arc.scheme(schemeState.id);
this.props.redeemReputationFromToken(scheme, values.accountAddress, this.state.privateKey, this.state.redeemerAddress);
await this.props.redeemReputationFromToken(scheme, values.accountAddress, this.state.privateKey, this.state.redeemerAddress, this.redemptionSucceeded);
}
setSubmitting(false);
}

public redemptionSucceeded = () => {
this.setState( { redemptionAmount: new BN(0) });
}

private onSubmitClick = (setFieldValue: any) => ()=>{ setFieldValue("useTxSenderService",false); }

public render(): RenderOutput {
Expand Down

0 comments on commit c6e5bc7

Please sign in to comment.