Skip to content

Commit

Permalink
Merge pull request #1049 from PrimeDAO/refac/add-logging-to-for-fundi…
Browse files Browse the repository at this point in the history
…ng-flow

Add logging to for funding flow
  • Loading branch information
hiaux0 authored Jun 2, 2022
2 parents 0d8b190 + 5a3fe98 commit ba22f7e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/entities/DealTokenSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,8 @@ export class DealTokenSwap implements IDeal {
}

this.daoTokenTransactions = daoTokenTransactions;

this.consoleLogService.logObject("5. [DTS#hydrateDaoTransactions] daoTokenTransactions: ", this.daoTokenTransactions);
}

private async hydrateDaoClaims(): Promise<void> {
Expand Down Expand Up @@ -737,11 +739,14 @@ export class DealTokenSwap implements IDeal {
metadata,
deadline,
];
this.consoleLogService.logMessage(`[DTS#createSwap] dealParameters: ${JSON.stringify(dealParameters)}`);

return this.transactionsService.send(
() => this.moduleContract.createSwap(...dealParameters))
.then(async receipt => {
if (receipt) {
this.consoleLogService.logMessage(`[DTS#createSwap] receipt: ${JSON.stringify(receipt)}`);

//need to set the fundingStartedAt here because it will be undefined until the page refreshes and will cause an infinite loop of errors on the UI
this.fundingStartedAt = new Date();
this.hydrate();
Expand Down Expand Up @@ -789,10 +794,19 @@ export class DealTokenSwap implements IDeal {

public unlockTokens(dao: IDAO, token: Address, amount: BigNumber): Promise<TransactionReceipt> {
const tokenContract = this.tokenService.getTokenContract(token);

this.consoleLogService.logMessage(`10.1 [DTS#unlockTokens] tokenContract address: ${tokenContract.address}`);
this.consoleLogService.logMessage(`10.2 [DTS#unlockTokens] Spender: ${this.daoDepositContracts.get(dao).address}`);

return this.transactionsService.send(() => tokenContract.approve(this.daoDepositContracts.get(dao).address, amount));
}

public depositTokens(dao: IDAO, tokenAddress: Address, amount: BigNumber): Promise<TransactionReceipt> {
this.consoleLogService.logMessage(`20.1 [DTS#depositTokens] Spender: ${this.daoDepositContracts.get(dao).address}`);
this.consoleLogService.logMessage(`20.2 [DTS#depositTokens] moduleContract address: ${this.moduleContract.address}`);
this.consoleLogService.logMessage(`20.3 [DTS#depositTokens] contractDealId: ${this.contractDealId}`);
this.consoleLogService.logMessage(`20.4 [DTS#depositTokens] tokenAddress: ${tokenAddress}`);

return this.transactionsService.send(
() => this.daoDepositContracts.get(dao).deposit(
this.moduleContract.address,
Expand All @@ -808,6 +822,11 @@ export class DealTokenSwap implements IDeal {
}

public withdrawTokens(dao: IDAO, depositId: number): Promise<TransactionReceipt> {
this.consoleLogService.logMessage(`30.1 [DTS#depositTokens] Spender: ${this.daoDepositContracts.get(dao).address}`);
this.consoleLogService.logMessage(`30.2 [DTS#depositTokens] moduleContract address: ${this.moduleContract.address}`);
this.consoleLogService.logMessage(`30.3 [DTS#depositTokens] contractDealId: ${this.contractDealId}`);
this.consoleLogService.logMessage(`30.4 [DTS#depositTokens] depositId: ${depositId}`);

return this.transactionsService.send(
() => this.daoDepositContracts.get(dao).withdraw(
this.moduleContract.address,
Expand Down Expand Up @@ -839,10 +858,15 @@ export class DealTokenSwap implements IDeal {

public execute(): Promise<TransactionReceipt> {
if (!this.isFailed) {
this.consoleLogService.logMessage(`40.1 [DTS#execute] moduleContract address: ${this.moduleContract.address}`);
this.consoleLogService.logMessage(`40.2 [DTS#execute] contractDealId: ${this.contractDealId}`);

return this.transactionsService.send(
() => this.moduleContract.executeSwap(this.contractDealId))
.then(async (receipt) => {
if (receipt) {
this.consoleLogService.logMessage(`40.3 [DTS#execute] receipt: ${receipt}`);

this.isExecuted = true;
this.executedAt = new Date((await this.ethereumService.getBlock(receipt.blockNumber)).timestamp * 1000);
return receipt;
Expand Down Expand Up @@ -970,6 +994,8 @@ export class DealTokenSwap implements IDeal {
// We're using bignumberjs because BigNumber can't handle division
token.fundingPercentCompleted = toBigNumberJs(token.fundingDeposited).dividedBy(token.amount).toNumber() * 100;
}

this.consoleLogService.logObject("4. [DTS#setFundingContractInfo] token: ", token);
}

public async setClaimingContractInfo(token: ITokenCalculated, dao: IDAO): Promise<void> {
Expand All @@ -991,6 +1017,8 @@ export class DealTokenSwap implements IDeal {
token.claimingLocked = totalAmount.sub(token.claimingInstantTransferAmount.add(token.claimingClaimable).add(token.claimingClaimed).add(token.claimingFee));
token.claimingPercentCompleted = toBigNumberJs(token.claimingClaimed.add(token.claimingFee).add(token.claimingInstantTransferAmount)).dividedBy(token.amount).toNumber() * 100;
}

this.consoleLogService.logObject("3.4. [DTS#setClaimingContractInfo] token: ", token);
}

private daoVotesSemaphore = 0;
Expand Down
20 changes: 20 additions & 0 deletions src/funding/funding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { IAlertModel } from "services/AlertService";
import { IGridColumn } from "resources/elements/primeDesignSystem/pgrid/pgrid";
import { depositColumns, claimTokenGridColumns } from "./funding-grid-columns";
import { AureliaHelperService } from "services/AureliaHelperService";
import { formatBytes32String } from "ethers/lib/utils";
import { ConsoleLogService } from "services/ConsoleLogService";

@autoinject
export class Funding {
Expand Down Expand Up @@ -57,6 +59,7 @@ export class Funding {
private alertService: AlertService,
private tokenService: TokenService,
private aureliaHelperService: AureliaHelperService,
private consoleLogService: ConsoleLogService,
) {
this.eventAggregator.subscribe("Network.Changed.Account", async (): Promise<void> => {
//This is for the page to redirect to the home page if the user changes their account address while on the funding page and their new account address isn't part of this deal
Expand All @@ -76,6 +79,12 @@ export class Funding {
if (!this.deal.fundingWasInitiated) this.goToDealPage();
//Make sure the connected account is part of this deal. Otherwise redirect to home page.
this.verifySecurity();

const contractDealId = await this.deal.moduleContract.metadataToDealId(formatBytes32String(this.dealId));
this.consoleLogService.logMessage(">>>>> Start Funding logs <<<<<");
this.consoleLogService.logMessage(`2.1 [Funding#activate] dealId: ${this.dealId}`);
this.consoleLogService.logMessage(`2.2 [Funding#activate] contractDealId: ${contractDealId}`);

//wait until the dao transactions from the contract are there
await Utils.waitUntilTrue(() => this.deal.daoTokenTransactions !== undefined);
//wait until the dao token claims from the contract are there
Expand All @@ -91,6 +100,9 @@ export class Funding {
await this.deal.hydrateDaoTransactions();
//get contract token information from the other DAO
if (this.deal.isFunding || this.deal.isFailed){
this.consoleLogService.logMessage(`3.1 [#initializeData] isFunding: ${this.deal.isFunding}`);
this.consoleLogService.logMessage(`3.2 [#initializeData] isFailed: ${this.deal.isFailed}`);

await this.setTokenFundingData();
if (!this.deal.isFailed && this.firstDao.tokens.length === 1) {
//if there is only one token, auto select it in the deposit form
Expand All @@ -100,6 +112,8 @@ export class Funding {
await this.setFundingTokenAllowance();
}
} else if (this.deal.isClaiming){
this.consoleLogService.logMessage(`3.3 [Funding#initializeData] isClaiming: ${this.deal.isClaiming}`);

await this.setTokenClaimingData();
}
}
Expand Down Expand Up @@ -325,12 +339,16 @@ export class Funding {
if (this.selectedToken !== null && this.selectedToken >= 0){
const contract = this.tokenService.getTokenContract(this.firstDao.tokens[this.selectedToken].address);
this.accountBalance = await contract.balanceOf(this.ethereumService.defaultAccountAddress);

this.consoleLogService.logMessage(`6. [Funding#setAccountBalance] accountBalance: ${this.accountBalance}`);
}
}

public async setFundingTokenAllowance(): Promise<void> {
const contract = this.tokenService.getTokenContract(this.firstDao.tokens[this.selectedToken].address);
this.userFundingTokenAllowance = await contract.allowance(this.ethereumService.defaultAccountAddress, this.deal.daoDepositContracts.get(this.firstDao).address);

this.consoleLogService.logMessage(`7. [Funding#setFundingTokenAllowance] userFundingTokenAllowance: ${this.userFundingTokenAllowance}`);
}

@computedFrom("deal.isRepresentativeUser", "deal.daoRepresentedByCurrentAccount", "deal.primaryDao")
Expand Down Expand Up @@ -374,6 +392,8 @@ export class Funding {
this.processing = true;
const transaction = await this.deal.claim(this.firstDao);
if (transaction){
this.consoleLogService.logObject("9. [Funding#claimTokens] transaction: ", transaction);

const congratulatePopupModel: IAlertModel = {
header: "Congratulations!",
message: "<p class='excitement'>You have successfully claimed your tokens!</p>",
Expand Down
19 changes: 18 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ import { ValidationService } from "./services/ValidationService";
import { DealService } from "services/DealService";
import { initialize as initializeMarkdown} from "resources/elements/markdown/markdown";

const FORCE_DEBUG = "[Prime] FORCE_DEBUG";
// @ts-ignore
window.primeDebug = {
on: () => {
localStorage.setItem(FORCE_DEBUG, "true");
},
off: () => {
localStorage.removeItem(FORCE_DEBUG);
},
check: () => {
return localStorage.getItem(FORCE_DEBUG);
},
};

export function configure(aurelia: Aurelia): void {
// Note, this Cypress hack has to be at the very start.
// Reason: Imports in eg. /resources/index, where EthereumService is imported to
Expand Down Expand Up @@ -50,8 +64,11 @@ export function configure(aurelia: Aurelia): void {

const network = process.env.NETWORK as AllowedNetworks;
const inDev = process.env.NODE_ENV === "development";
const isForceDebug = window.localStorage.getItem(FORCE_DEBUG);

if (inDev) {
if (isForceDebug === "true") {
aurelia.use.developmentLogging(); // everything
} else if (inDev) {
aurelia.use.developmentLogging(); // everything
} else {
aurelia.use.developmentLogging("warn"); // only errors and warnings
Expand Down
19 changes: 19 additions & 0 deletions src/services/ConsoleLogService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,23 @@ export class ConsoleLogService {
break;
}
}

public logObject(msg: string, obj: any, level: ConsoleLogMessageTypes = "info"): void {
switch (level) {
case "info":
default:
this.logger.info(msg, obj);
break;
case "warn":
case "warning":
this.logger.warn(msg, obj);
break;
case "error":
this.logger.error(msg, obj);
break;
case "debug":
this.logger.debug(msg, obj);
break;
}
}
}

1 comment on commit ba22f7e

@vercel
Copy link

@vercel vercel bot commented on ba22f7e Jun 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.