Skip to content

Commit

Permalink
optimize transaction hash handling
Browse files Browse the repository at this point in the history
  • Loading branch information
H34D committed Dec 9, 2022
1 parent 9ec5406 commit 16d1f4f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 155 deletions.
157 changes: 19 additions & 138 deletions src/credit-score/create.ts
@@ -1,134 +1,13 @@
import Masa from "../masa";
import { signMessage, Templates } from "../utils";
import { CreateCreditScoreResult } from "../interface";

const mockRecipe = {
to: "0xc51cC18238426108f9eC51203d6a590ce8516B68",
from: "0xe7D2AB3F051c385A025C177348f17463231BEB5e",
contractAddress: null,
transactionIndex: 9,
gasUsed: {
type: "BigNumber",
hex: "0x02bbe7",
},
logsBloom:
"0x04000000000000000000000000000000000001200000000000010000000000000000000000000000000000000000000000000000040000000000000000000000000000000020000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000400040000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000800100000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000008000000000000000000000",
blockHash:
"0x273fa489b733fbc590605078e578862bd22b6df475e1624912b91010a0d9bdea",
transactionHash:
"0x6ba64f962b103b867c7a5cec2224aae31ebe5e472f77cda56fe5db05554930c6",
logs: [
{
transactionIndex: 9,
blockNumber: 8101010,
transactionHash:
"0x6ba64f962b103b867c7a5cec2224aae31ebe5e472f77cda56fe5db05554930c6",
address: "0xc51cC18238426108f9eC51203d6a590ce8516B68",
topics: [
"0x0f6798a560793a54c3bcfe86a93cde1e73087d944c0ea20544137d4121396885",
"0x000000000000000000000000e7d2ab3f051c385a025c177348f17463231beb5e",
"0x0000000000000000000000000000000000000000000000000000000000000002",
],
data: "0x",
logIndex: 17,
blockHash:
"0x273fa489b733fbc590605078e578862bd22b6df475e1624912b91010a0d9bdea",
},
{
transactionIndex: 9,
blockNumber: 8101010,
transactionHash:
"0x6ba64f962b103b867c7a5cec2224aae31ebe5e472f77cda56fe5db05554930c6",
address: "0xc51cC18238426108f9eC51203d6a590ce8516B68",
topics: [
"0xba587fc996588eaf73691ecc5a1c4c42228fab71b44f01544686211cb1e68177",
],
data: "0x000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040000000000000000000000003c8d9f130970358b7e8cbc1dbd0a1eba6ebe368f00000000000000000000000000000000000000000000000000000184f44c589800000000000000000000000057f1887a8bf19b14fc0df6fd9b2acc9af147ea850000000000000000000000000000000000000000000000000000000000000000",
logIndex: 18,
blockHash:
"0x273fa489b733fbc590605078e578862bd22b6df475e1624912b91010a0d9bdea",
},
],
blockNumber: 8101010,
confirmations: 1,
cumulativeGasUsed: {
type: "BigNumber",
hex: "0x15c527",
},
effectiveGasPrice: {
type: "BigNumber",
hex: "0x59682f0a",
},
status: 1,
type: 2,
byzantium: true,
events: [
{
transactionIndex: 9,
blockNumber: 8101010,
transactionHash:
"0x6ba64f962b103b867c7a5cec2224aae31ebe5e472f77cda56fe5db05554930c6",
address: "0xc51cC18238426108f9eC51203d6a590ce8516B68",
topics: [
"0x0f6798a560793a54c3bcfe86a93cde1e73087d944c0ea20544137d4121396885",
"0x000000000000000000000000e7d2ab3f051c385a025c177348f17463231beb5e",
"0x0000000000000000000000000000000000000000000000000000000000000002",
],
data: "0x",
logIndex: 17,
blockHash:
"0x273fa489b733fbc590605078e578862bd22b6df475e1624912b91010a0d9bdea",
args: [
"0xe7D2AB3F051c385A025C177348f17463231BEB5e",
{
type: "BigNumber",
hex: "0x02",
},
],
event: "Mint",
eventSignature: "Mint(address,uint256)",
},
{
transactionIndex: 9,
blockNumber: 8101010,
transactionHash:
"0x6ba64f962b103b867c7a5cec2224aae31ebe5e472f77cda56fe5db05554930c6",
address: "0xc51cC18238426108f9eC51203d6a590ce8516B68",
topics: [
"0xba587fc996588eaf73691ecc5a1c4c42228fab71b44f01544686211cb1e68177",
],
data: "0x000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040000000000000000000000003c8d9f130970358b7e8cbc1dbd0a1eba6ebe368f00000000000000000000000000000000000000000000000000000184f44c589800000000000000000000000057f1887a8bf19b14fc0df6fd9b2acc9af147ea850000000000000000000000000000000000000000000000000000000000000000",
logIndex: 18,
blockHash:
"0x273fa489b733fbc590605078e578862bd22b6df475e1624912b91010a0d9bdea",
args: [
{
type: "BigNumber",
hex: "0x02",
},
{
type: "BigNumber",
hex: "0x04",
},
"0x3c8D9f130970358b7E8cbc1DbD0a1EbA6EBE368F",
{
type: "BigNumber",
hex: "0x0184f44c5898",
},
"0x57f1887a8BF19b14fC0dF6Fd9B2acc9Af147eA85",
{
type: "BigNumber",
hex: "0x00",
},
],
event: "SoulboundCreditScoreMinted",
eventSignature:
"SoulboundCreditScoreMinted(uint256,uint256,address,uint256,address,uint256)",
},
],
};
const mockTransactionHash =
"0x6ba64f962b103b867c7a5cec2224aae31ebe5e472f77cda56fe5db05554930c6";

export const createCreditScore = async (masa: Masa, mock: boolean = false) => {
export const createCreditScore = async (
masa: Masa,
mock = false
): Promise<CreateCreditScoreResult | undefined> => {
const result: CreateCreditScoreResult = {
success: false,
message: "Unknown Error",
Expand All @@ -148,7 +27,6 @@ export const createCreditScore = async (masa: Masa, mock: boolean = false) => {
);

if (creditScoreResponse?.signature) {
let recipe;
if (!mock) {
const wallet = masa.config.wallet;
const date = new Date(
Expand All @@ -165,18 +43,21 @@ export const createCreditScore = async (masa: Masa, mock: boolean = false) => {
);

console.log("Waiting for transaction to finalize");
recipe = await tx.wait();
} catch (e) {
console.error(e);
return { success: false, message: "Unexpected error" };
}
}

const creditScoreUpdateResponse = await masa.client.updateCreditScore(
mock ? mockRecipe : recipe
);
const transactionReceipt = await tx.wait();
const creditScoreUpdateResponse = await masa.client.updateCreditScore(
mock ? mockTransactionHash : transactionReceipt.transactionHash
);

return creditScoreUpdateResponse;
return { ...result, ...creditScoreUpdateResponse };
} catch (e: any) {
result.success = false;
result.message = `Unexpected error: ${e.message}`;
console.error(result.message);
}
}
}
}

return result;
};
45 changes: 28 additions & 17 deletions src/utils/clients/middleware.ts
Expand Up @@ -318,17 +318,21 @@ export class MasaClient {
createCreditScore = async (
address: string
): Promise<
| {
creditScore: any;
signature: string;
}
| {
success: boolean;
status: string;
message: string;
creditScore?: any;
signature?: string;
}
| undefined
> => {
const result = {
success: false,
status: "failed",
message: "Credit Score failed",
};

const creditScoreResponse = await this._middlewareClient
.post(
`/contracts/credit-score/generate`,
Expand All @@ -346,7 +350,10 @@ export class MasaClient {
});

if (creditScoreResponse?.status === 200 && creditScoreResponse?.data) {
return creditScoreResponse.data;
result.success = true;
result.message = "";
result.status = "success";
return { ...result, ...creditScoreResponse.data };
} else {
console.error("Generation of credit score failed!");
return {
Expand All @@ -358,24 +365,28 @@ export class MasaClient {
};

updateCreditScore = async (
tx: any
transactionHash: string
): Promise<
| {
creditScore: any;
signature: string;
}
| {
success: boolean;
status: string;
message: string;
creditScore?: any;
signature?: string;
}
| undefined
> => {
const result = {
success: false,
status: "failed",
message: "Credit Score failed",
};

const creditScoreResponse = await this._middlewareClient
.post(
`/contracts/credit-score/update`,
{
tx,
transactionHash,
},
{
headers: {
Expand All @@ -388,15 +399,15 @@ export class MasaClient {
});

if (creditScoreResponse?.status === 200 && creditScoreResponse?.data) {
return creditScoreResponse.data;
result.success = true;
result.message = "";
result.status = "success";
return { ...result, ...creditScoreResponse.data };
} else {
console.error("Generation of credit score failed!");
return {
success: false,
status: "failed",
message: "Credit Score failed",
};
}

return result;
};

sessionLogout = async (): Promise<
Expand Down

0 comments on commit 16d1f4f

Please sign in to comment.