Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

fix(chain-network): fixed chain network #1576

Merged
merged 5 commits into from
Apr 10, 2024
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
26 changes: 13 additions & 13 deletions apps/frontend/src/components/modal/stepperModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ function StepperModal({ isOpen, onClose }: Props) {
return;
}
if (!applicationData) return;
const balanceResponse = await fetch(
`/api/get-balance?stxAddress=${stxAddress}`
);
const { message: balance } = await balanceResponse.json();

if (
parseFloat(balance) * Math.pow(10, 6) <
parseFloat(applicationData.paymentTerms.downpayment) * Math.pow(10, 6)
) {
toast.error("Insufficient sUSDT balance");
onClose();
return;
}
// const balanceResponse = await fetch(
// `/api/get-balance?stxAddress=${stxAddress}`
// );
// const { message: balance } = await balanceResponse.json();

// if (
// parseFloat(balance) * Math.pow(10, 6) <
// parseFloat(applicationData.paymentTerms.downpayment) * Math.pow(10, 6)
// ) {
// toast.error("Insufficient sUSDT balance");
// onClose();
// return;
// }
dorucioclea marked this conversation as resolved.
Show resolved Hide resolved

const transactionData = await createTaralPurchaseOrder(
applicationId,
Expand Down
9 changes: 5 additions & 4 deletions apps/frontend/src/pages/api/get-balance.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { SUSDT_CONTRACT, stacksNetwork } from "@utils/lib/constants";
import { useNetworks } from "@hooks/useNetwork";
import { tokenToNumber } from "@utils/helper";
import { SUSDT_CONTRACT } from "@utils/lib/constants";
import { fetchReadOnlyFunction } from "micro-stacks/api";
import { standardPrincipalCV } from "micro-stacks/clarity";
import { NextRequest, NextResponse } from "next/server";
import type { NextApiRequest, NextApiResponse } from "next";

type ResponseData = {
message: string;
Expand All @@ -13,8 +13,9 @@ export default async function handler(
req: NextApiRequest,
res: NextApiResponse<ResponseData>
) {
const { currentStacksNetwork } = useNetworks();
const balance: any = await fetchReadOnlyFunction({
network: new stacksNetwork(),
network: currentStacksNetwork,
contractAddress: SUSDT_CONTRACT.split(".")[0],
contractName: SUSDT_CONTRACT.split(".")[1],
senderAddress: req.query.stxAddress as string,
Expand Down
40 changes: 24 additions & 16 deletions apps/frontend/src/utils/hooks/useTaralContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
createAssetInfo,
PostConditionMode,
makeContractFungiblePostCondition,
makeStandardFungiblePostCondition,
} from "micro-stacks/transactions";

import { fetchReadOnlyFunction } from "micro-stacks/api";
Expand All @@ -21,13 +22,14 @@ import {
SUSDT_CONTRACT,
TARAL_BANK_CONTRACT,
TARAL_IMPORTER_CONTRACT,
stacksNetwork,
} from "@utils/lib/constants";
import { useNetworks } from "@hooks/useNetwork";

function useTaralContracts() {
const { isSignedIn } = useAuth();
const { stxAddress } = useAccount();
const { openContractCall } = useOpenContractCall();
const { currentStacksNetwork } = useNetworks();

async function registerTaralImporterOnChain(
importerPrincipal: string,
Expand Down Expand Up @@ -87,9 +89,8 @@ function useTaralContracts() {
const postConditionCode = FungibleConditionCode.LessEqual;
const postConditionAmount = downPayment;

const contractFungiblePostCondition = makeContractFungiblePostCondition(
contractAddress,
contractName,
const contractFungiblePostCondition = makeStandardFungiblePostCondition(
stxAddress!,
postConditionCode,
postConditionAmount,
fungibleAssetInfo
Expand All @@ -100,7 +101,14 @@ function useTaralContracts() {
contractAddress,
contractName,
functionName: "create-purchase-order",
postConditions: [contractFungiblePostCondition],
postConditions:
currentStacksNetwork.chainId === 1
? [contractFungiblePostCondition]
: [],
postConditionMode:
currentStacksNetwork.chainId === 1
? PostConditionMode.Deny
: PostConditionMode.Allow,
functionArgs: functionArgs,

onFinish: async (data: any) => {
Expand All @@ -127,7 +135,7 @@ function useTaralContracts() {
contractAddress,
contractName,
functionName: "accept-financing",
// postConditionMode: PostConditionMode.Allow,
postConditionMode: PostConditionMode.Allow,
functionArgs: [],

onFinish: async (data: any) => {
Expand Down Expand Up @@ -171,9 +179,7 @@ function useTaralContracts() {

async function checkPurchaseOrderHasActiveFinancing(id: string) {
try {
const network = new stacksNetwork();
const result: any = await fetchReadOnlyFunction({
network,
contractAddress: TARAL_BANK_CONTRACT.split(".")[0],
contractName: TARAL_BANK_CONTRACT.split(".")[1],
senderAddress: TARAL_BANK_CONTRACT.split(".")[0],
Expand All @@ -188,9 +194,7 @@ function useTaralContracts() {

async function getPurchaseOrderById(id: string) {
try {
const network = new stacksNetwork();
const result: any = await fetchReadOnlyFunction({
network,
contractAddress: TARAL_BANK_CONTRACT.split(".")[0],
contractName: TARAL_BANK_CONTRACT.split(".")[1],
senderAddress: TARAL_BANK_CONTRACT.split(".")[0],
Expand All @@ -205,9 +209,7 @@ function useTaralContracts() {

async function getActivePurchaseOrder() {
try {
const network = new stacksNetwork();
const result: any = await fetchReadOnlyFunction({
network,
contractAddress: TARAL_BANK_CONTRACT.split(".")[0],
contractName: TARAL_BANK_CONTRACT.split(".")[1],
senderAddress: stxAddress!,
Expand All @@ -234,9 +236,8 @@ function useTaralContracts() {
const postConditionCode = FungibleConditionCode.LessEqual;
const postConditionAmount = amount;

const contractFungiblePostCondition = makeContractFungiblePostCondition(
contractAddress,
contractName,
const contractFungiblePostCondition = makeStandardFungiblePostCondition(
stxAddress!,
postConditionCode,
postConditionAmount,
fungibleAssetInfo
Expand All @@ -247,7 +248,14 @@ function useTaralContracts() {
contractAddress,
contractName,
functionName: "make-payment",
postConditions: [contractFungiblePostCondition],
postConditions:
currentStacksNetwork.chainId === 1
? [contractFungiblePostCondition]
: [],
postConditionMode:
currentStacksNetwork.chainId === 1
? PostConditionMode.Deny
: PostConditionMode.Allow,
functionArgs: [],

onFinish: async (data: any) => {
Expand Down
10 changes: 5 additions & 5 deletions apps/frontend/src/utils/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,23 @@ export const DEFAULT_NETWORK_INDEX = parseFloat(

export const devnet = process.env.NODE_ENV === "development";

export const stacksNetwork =
process.env.NODE_ENV === "production" ? StacksMainnet : StacksTestnet;
// export const stacksNetwork =
// process.env.NODE_ENV === "production" ? StacksMainnet : StacksTestnet;

export const TARAL_IMPORTER_CONTRACT = devnet
? "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.taral-importer"
: "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.taral-importer";

export const TARAL_BANK_CONTRACT = devnet
? "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.taral-bank"
? "ST3PF13W7Z0RRM42A8VZRVFQ75SV1K26RXEP8YGKJ.taral-bank-v1"
: process.env.NEXT_PUBLIC_TARAL_BANK_CONTRACT || "";

export const SUSDT_CONTRACT = devnet
? "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.token-susdt"
? "STRP7MYBHSMFH5EGN3HGX6KNQ7QBHVTBPF1669DW.susdt-token-v2"
dorucioclea marked this conversation as resolved.
Show resolved Hide resolved
: process.env.NEXT_PUBLIC_SUSDT_CONTRACT || "";

export const LENDER_ADDRESS = devnet
? "ST2CY5V39NHDPWSXMW9QDT3HC3GD6Q6XX4CFRK9AG"
? "STNHKEPYEPJ8ET55ZZ0M5A34J0R3N5FM2CMMMAZ6"
: process.env.NEXT_PUBLIC_TARAL_LENDER_ADDRESS || "";

export const CURRENCIES = [
Expand Down
59 changes: 59 additions & 0 deletions apps/frontend/src/utils/lib/mint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {
makeContractCall,
uintCV,
principalCV,
broadcastTransaction,
bufferCV,
} from "@stacks/transactions";
import { StacksTestnet } from "@stacks/network";

async function mintTokens() {
const network = new StacksTestnet();
const secretKey = ""; // Replace with your secret key
const contractAddress =
"STRP7MYBHSMFH5EGN3HGX6KNQ7QBHVTBPF1669DW.susdt-token-v2"; // Replace with the contract address

const transaction = await makeContractCall({
//feeEstimateApiUrl: "https://api.testnet.hiro.so/v2/fees/transfer",
contractAddress: contractAddress.split(".")[0],
contractName: contractAddress.split(".")[1], // Replace with your contract name
functionName: "transfer", // Replace with your minting function name
functionArgs: [
uintCV(1000000000),
principalCV("ST3PF13W7Z0RRM42A8VZRVFQ75SV1K26RXEP8YGKJ"),
principalCV("ST3NBRSFKX28FQ2ZJ1MAKX58HKHSDGNV5N7R21XCP"),
bufferCV(Buffer.from("memo tx")),
], // Add arguments if required
senderKey: secretKey,
anchorMode: 1,
postConditionMode: 1,
network,
fee: 1,
});
// const transaction = await makeContractCall({
// contractAddress: contractAddress.split(".")[0],
// contractName: contractAddress.split(".")[1], // Replace with your contract name
// functionName: "mint", // Replace with your minting function name
// functionArgs: [
// uintCV(1000000000),
// principalCV("ST3PF13W7Z0RRM42A8VZRVFQ75SV1K26RXEP8YGKJ"),
// ], // Add arguments if required
// senderKey: secretKey,
// anchorMode: 1,
// postConditionMode: 1,
// network,
// fee: 1000000,
// });

return transaction;
}

mintTokens()
.then(async (transaction) => {
const broadcastResponse = await broadcastTransaction(transaction);
const txId = broadcastResponse.txid;
console.log("Transaction ID:", txId);
})
.catch((error) => {
console.error("Error minting tokens:", error);
});