Skip to content

Commit

Permalink
alert about update to onchain
Browse files Browse the repository at this point in the history
  • Loading branch information
0xjaqbek committed Jul 26, 2024
1 parent c086f06 commit fd7e19d
Showing 1 changed file with 21 additions and 34 deletions.
55 changes: 21 additions & 34 deletions src/race/scripts/getData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,43 @@ export async function getDataFromOnChainRace(client: TonClient, onChainRaceAddre
try {
// Open OnChainRace instance by address
const onChainRace = OnChainRace.createFromAddress(onChainRaceAddress);
console.log(`OnChainRace contract address: ${onChainRaceAddress.toString()}`);
const contractAddress = onChainRaceAddress.toString();
console.log(`OnChainRace contract address: ${contractAddress}`);

// Create a ContractProvider for OnChainRace contract
const onChainRaceProvider = client.provider(onChainRaceAddress);

// Retrieve and log run time and user address from the smart contract
const runTimeMilliseconds = await onChainRace.getRunTime(onChainRaceProvider);
const runTimeSeconds = runTimeMilliseconds / 1000;
console.log("Run Time:", runTimeSeconds.toFixed(3)); // Show the run time with millisecond precision
const runTimeMessage = `Run Time: ${runTimeSeconds.toFixed(3)}`; // Show the run time with millisecond precision
console.log(runTimeMessage);

const userAddressObj = await onChainRace.getUserAddress(onChainRaceProvider);
console.log("User Address:", userAddressObj.toString());
const userAddressMessage = `User Address: ${userAddressObj.toString()}`;
console.log(userAddressMessage);

} catch (error) {
if (axios.isAxiosError(error)) {
// Handle AxiosError
console.error('Axios error:', error.message);
console.error('Response data:', error.response?.data);
} else {
// Handle other errors
console.error('Unexpected error:', error);
}
}
}

export async function run() {
try {
console.log("Starting interaction with OnChainRace contract...");

// Initialize TON RPC client on testnet
const endpoint = await getHttpEndpoint({ network: "testnet" });
console.log(`Using endpoint: ${endpoint}`);
const client = new TonClient({ endpoint });

// Define the OnChainRace contract address
const onChainRaceAddress = Address.parse("kQDW1VLFvS3FJW5rl2tyNfQ-mOfN5nPYGPAHh1vueJsRywwm"); // Replace with your contract address

// Call the function to get data from OnChainRace
await getDataFromOnChainRace(client, onChainRaceAddress);
// Combine all messages into a single alert
const combinedMessage = `
OnChainRace contract address: ${contractAddress}
${runTimeMessage}
${userAddressMessage}
`;
alert(combinedMessage.trim());

} catch (error) {
if (axios.isAxiosError(error)) {
// Handle AxiosError
console.error('Axios error:', error.message);
console.error('Response data:', error.response?.data);
const axiosErrorMessage = `Axios error: ${error.message}`;
const responseData = `Response data: ${error.response?.data}`;
console.error(axiosErrorMessage);
console.error(responseData);
alert(`${axiosErrorMessage}\n${responseData}`);
} else {
// Handle other errors
console.error('Unexpected error:', error);
const unexpectedErrorMessage = `Unexpected error: ${error}`;
console.error(unexpectedErrorMessage);
alert(unexpectedErrorMessage);
}
}
}

run().catch(console.error);

0 comments on commit fd7e19d

Please sign in to comment.