Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web3 and cosmwasm services added with transaction cron job module #6

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import dotenv from "dotenv";
import app from "./app";
import awsSecretsManager from "./utils/awsSecretsManager";
import transactionsJob from "./utils/crons/transactionsJob";

dotenv.config();

(async () => {
await awsSecretsManager();
transactionsJob();
})().catch((e) => {
console.log(e);
});
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./web3.interface";
export * from "./job.interface";
16 changes: 16 additions & 0 deletions src/interfaces/job.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Transaction, TransactionReceipt } from '../interfaces';

export interface JobRequestBody {
name: string;
sourceRpcURL: string;
isSourceNonEVM: boolean;
destinationRpcURL: string;
isDestinationNonEVM: boolean;
bridgeAmount: string;
txId: string;
}

export interface UpdateJobRequestBody {
transaction: Transaction;
transactionReceipt: TransactionReceipt;
}
57 changes: 57 additions & 0 deletions src/interfaces/web3.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
export interface Transaction {
hash: string;
nonce: number;
blockHash: string | null;
blockNumber: number | null;
transactionIndex: number | null;
from: string;
to: string | null;
value: string;
gasPrice: string;
maxPriorityFeePerGas?: number | string | any;
maxFeePerGas?: number | string | any;
gas: number;
input: string;
}

export interface TransactionReceipt {
status: boolean;
transactionHash: string;
transactionIndex: number;
blockHash: string;
blockNumber: number;
from: string;
to: string;
contractAddress?: string;
cumulativeGasUsed: number;
gasUsed: number;
effectiveGasPrice: number;
logs: Log[];
logsBloom: string;
events?: {
[eventName: string]: EventLog;
};
}

export interface EventLog {
event: string;
address: string;
returnValues: any;
logIndex: number;
transactionIndex: number;
transactionHash: string;
blockHash: string;
blockNumber: number;
raw?: { data: string; topics: any[] };
}
export interface Log {
address: string;
data: string;
topics: string[];
logIndex: number;
transactionIndex: number;
transactionHash: string;
blockHash: string;
blockNumber: number;
removed: boolean;
}
154 changes: 154 additions & 0 deletions src/services/cosmWasm.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import Web3 from "web3";
import { TransactionReceipt, Transaction } from "../interfaces";
const { SigningCosmWasmClient } = require("@cosmjs/cosmwasm-stargate");
import { Wallet, ethers } from "ethers";
import { CUDOS_CHAIN_ID, THRESHOLD } from "../constants/constants";

export const getTransactionReceipt = async (
txId: string,
rpcURL: string,
tries = 0
): Promise<TransactionReceipt> => {
let client = await SigningCosmWasmClient.connectWithSigner(rpcURL);
const transaction = await client.getTx(txId);
console.log("transaction status", transaction?.code);
if (tries < THRESHOLD) {
tries += 1;
if (!transaction || transaction === null) {
await getTransactionReceipt(txId, rpcURL, tries);
}
}
if (transaction && transaction.code == 0) {
transaction.status = true;
} else {
transaction.status = false;
}
return transaction;
};

export const signedTransaction = async (
job: any,
decodedData: any,
transaction: any
): Promise<any> => {
try {
const destinationAmountToMachine = await getDestinationAmount(job.data);
const txData = {
transactionHash: job.returnvalue.transactionHash,
from: transaction.from,
token: decodedData.sourceToken,
amount: decodedData.sourceAmount,
chainId: decodedData.sourceChainId,
targetChainId: decodedData.targetChainId,
targetToken: decodedData.targetToken,
targetAddress: decodedData.targetAddress,
signatures: [],
salt: "",
};

txData.salt = Web3.utils.keccak256(
txData.transactionHash.toLocaleLowerCase()
);
const payBySig = await createSignedPayment(
txData.targetChainId,
txData.targetAddress,
destinationAmountToMachine,
txData.targetToken,
txData.salt,
job
);

return {
...txData,
signatures: [payBySig.signatures, payBySig.signatures],
hash: "payBySig.hash",
};
} catch (error) {
console.error("Error occured while decoding transaction", error);
}
};

const createSignedPayment = async (
chainId: string,
address: string,
amount: string,
token: string,
salt: string,
job: any
) => {
const payBySig = produceSignatureWithdrawHash(
chainId,
token,
address,
amount,
salt
);
console.log("hash", payBySig.hash);
const privateKey = process.env.PRIVATE_KEY as string;
let provider = ethers.getDefaultProvider(job.data.sourceRpcURL);
const wallet = new Wallet(privateKey, provider);
let signature = await wallet.signMessage(payBySig.hash);
signature = signature.replace(/^0x/, "");
payBySig.signatures = [signature];
return payBySig;
};

const produceSignatureWithdrawHash = (
chainId: string,
token: string,
payee: string,
amount: string,
salt: string
): any => {
const hash = `{"chain_id":"${chainId}","payee":"${payee}","token":"${token}","amount":"${amount}","salt":"${salt}"}`;
return {
signatures: [],
hash,
};
};

export const getLogsFromTransactionReceipt = (job: any) => {
try {
let decodedData: any = {};
job.returnvalue.transactionHash = job?.returnvalue?.hash;
job.returnvalue.status = false;
if (job?.returnvalue?.code == 0) {
job.returnvalue.status = true;
}
let rawLogs = job?.returnvalue?.rawLog;
var logs = JSON.parse(rawLogs);
decodedData.sourceToken = filterLogsAndGetValue(logs, "token");
decodedData.sourceAmount = filterLogsAndGetValue(logs, "amount");
if (decodedData.sourceAmount) {
decodedData.sourceAmount = decodedData.sourceAmount.replace("acudos", "");
}
decodedData.sourceChainId = CUDOS_CHAIN_ID;
decodedData.targetChainId = filterLogsAndGetValue(logs, "target_chain_id");
decodedData.targetToken = filterLogsAndGetValue(logs, "target_token");
decodedData.targetAddress = filterLogsAndGetValue(logs, "target_address");
decodedData.from = filterLogsAndGetValue(logs, "from");
return decodedData;
} catch (error) {
console.error("Error occured while getting logs from transaction", error);
}
};

export const filterLogsAndGetValue = (logs: any, key: string) => {
if (logs?.length > 0) {
let events = logs[0].events;
for (const event of events) {
if (event?.attributes.length > 0) {
for (const attribute of event?.attributes) {
if (attribute.key === key) {
return attribute.value;
}
}
}
}
}
};

const getDestinationAmount = async (data: any) => {
console.log("data.bridgeAmount", data.bridgeAmount);
return data.bridgeAmount;
};
3 changes: 3 additions & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export * as userService from "./user.service";
export * as securityKeyService from "./securityKey.service";
export * as web3Service from "./web3.service";
export * as cosmWasmService from "./cosmWasm.service";
export * as axiosService from "./axios.service";
Loading