This commit is contained in:
Sewmina 2024-12-23 17:23:58 +05:30
parent 2d8d540263
commit 4d911aee74
5 changed files with 358 additions and 178 deletions

View File

@ -0,0 +1,149 @@
export const erc20ABI = [
{
"inputs": [
{
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"internalType": "address",
"name": "spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "account",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "decimals",
"outputs": [
{
"internalType": "uint8",
"name": "",
"type": "uint8"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "recipient",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"internalType": "address",
"name": "recipient",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
}
];

View File

@ -0,0 +1,169 @@
export const contractABI =[
{
"inputs": [
{
"internalType": "string",
"name": "receiver",
"type": "string"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "transferUSDT",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "contractBalance",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getAllTransactions",
"outputs": [
{
"components": [
{
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "string",
"name": "receiver",
"type": "string"
},
{
"internalType": "bytes32",
"name": "txHash",
"type": "bytes32"
}
],
"internalType": "struct USDTTransfer.Transaction[]",
"name": "",
"type": "tuple[]"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "_receiver",
"type": "string"
}
],
"name": "getTransactionsByReceiver",
"outputs": [
{
"components": [
{
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "string",
"name": "receiver",
"type": "string"
},
{
"internalType": "bytes32",
"name": "txHash",
"type": "bytes32"
}
],
"internalType": "struct USDTTransfer.Transaction[]",
"name": "",
"type": "tuple[]"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "recipient",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "transactions",
"outputs": [
{
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "string",
"name": "receiver",
"type": "string"
},
{
"internalType": "bytes32",
"name": "txHash",
"type": "bytes32"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "usdtToken",
"outputs": [
{
"internalType": "contract IERC20",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
}
];

View File

@ -8,7 +8,8 @@ import ParallaxScrollView from '@/components/ParallaxScrollView';
import { ThemedText } from '@/components/ThemedText';
import { ThemedView } from '@/components/ThemedView';
import { WalletConnectModal, useWalletConnectModal } from '@walletconnect/modal-react-native';
import { CHAIN_ID, CONTRACT_ADDRESS, contractABI, projectId, providerMetadata } from './shared';
import { CHAIN_ID, CONTRACT_ADDRESS, projectId, providerMetadata } from './shared';
import { contractABI } from './ABI/transfer_contract';

View File

@ -4,8 +4,10 @@ import { CameraView } from "expo-camera";
import { Stack, useNavigation } from "expo-router"; // Updated import for navigation
import { Overlay } from "./Overlay";
import { ethers } from "ethers";
import { CHAIN_ID, CONTRACT_ADDRESS, contractABI } from "./shared";
import { CHAIN_ID, CONTRACT_ADDRESS, TOKEN_CONTRACT_ADDRESS } from "./shared";
import { useWalletConnectModal } from "@walletconnect/modal-react-native";
import { contractABI } from "./ABI/transfer_contract";
import { erc20ABI } from "./ABI/erc20_contract";
export default function Home() {
const qrLock = useRef(false);
@ -13,8 +15,8 @@ export default function Home() {
const navigation = useNavigation(); // Hook for navigation
const [modalVisible, setModalVisible] = useState(false);
const [scannedAddress, setScannedAddress] = useState("");
const [dollarValue, setDollarValue] = useState("");
const [scannedAddress, setScannedAddress] = useState("0xE09865aaCd816729C19E4BeA4caFf247704De9fE");
const [dollarValue, setDollarValue] = useState("400");
useEffect(() => {
const subscription = AppState.addEventListener("change", (nextAppState) => {
@ -24,6 +26,8 @@ export default function Home() {
appState.current = nextAppState;
});
transferEth();
return () => {
subscription.remove();
};
@ -42,29 +46,38 @@ export default function Home() {
try {
const ethersProvider = new ethers.providers.Web3Provider(provider);
const network = await ethersProvider.getNetwork();
if (network.chainId !== CHAIN_ID) {
console.log(`Switching from ${network.chainId} to ${CHAIN_ID} (Arbitrum Sepolia)...`);
await provider.request({
method: "wallet_switchEthereumChain",
params: [{ chainId: `0x${CHAIN_ID.toString(16)}` }],
params: [{ chainId: `0x${CHAIN_ID.toString(16)}`, name:"Arbitrum Sepolia" }],
});
console.log(`Switched to chain ID: ${CHAIN_ID}`);
}
const signer = ethersProvider.getSigner();
const contract = new ethers.Contract(CONTRACT_ADDRESS, contractABI, signer);
const priceResponse = await fetch("http://vps.playpoolstudios.com:28328/getMarkPriceETH");
const priceResponse = await fetch("http://vps.playpoolstudios.com:28328/getMarkPriceUSDT");
const priceJson = await priceResponse.json();
const price = priceJson['price'];
const ethValue = parseFloat(dollarValue) / price; // Convert dollars to ETH assuming 1 ETH = $3600
console.log(`Converted ${dollarValue} THB to ${ethValue} ETH`);
// const ethValue = parseFloat(dollarValue) / price; // Convert dollars to ETH assuming 1 ETH = $3600
const usdtValue = parseFloat(dollarValue)/price; // ex: 10 USDT
console.log(`Converted ${dollarValue} THB to ${usdtValue} USDT`);
console.log("prepping the contract");
const signer = ethersProvider.getSigner();
const contract = new ethers.Contract(CONTRACT_ADDRESS, contractABI, signer);
const usdtTokenContract = new ethers.Contract(TOKEN_CONTRACT_ADDRESS, erc20ABI, signer);
console.log("Requesting contract call");
const tx = await contract.transferETH(scannedAddress, {
value: ethers.utils.parseEther(ethValue.toFixed(18).toString()),
});
const tx = await usdtTokenContract.approve(
CONTRACT_ADDRESS,
ethers.utils.parseUnits((usdtValue).toFixed(6), 18) // USDT has 6 decimals
);
await tx.wait();
console.log("USDT approved for transfer by the contract");
const tx2 = await contract.transferUSDT(scannedAddress, ethers.utils.parseUnits(usdtValue.toFixed(6), 6));
await tx2.wait();
console.log("Transaction sent:", tx.hash);
await tx.wait();
@ -73,10 +86,17 @@ export default function Home() {
// Navigate back after the transaction is confirmed
navigation.goBack();
} catch (error) {
console.error("Transfer failed:", error);
if (error instanceof Error) {
console.error("Transfer failed:", error.message); // Logs the error message
// console.error("Stack trace:", error.stack); // Logs the full stack trace
} else {
console.error("Unexpected error:", error);
}
}
};
return (
<SafeAreaView style={StyleSheet.absoluteFillObject}>
<Stack.Screen

View File

@ -1,5 +1,8 @@
export const CHAIN_ID = 421614; // Arbitrum Sepolia
export const CONTRACT_ADDRESS = '0x6b6F4a57Ca3ADfCF5526455c47bc91fF4b7C86A0';
export const CONTRACT_ADDRESS = '0xD66858fB654b69a1F454F084700dC403B1636184';
export const TOKEN_CONTRACT_ADDRESS = '0x976E7ED682f781F39Fd79BF2966bB54A9CBbDF5F';
export const RECEIPENT_ADDRESS = "0xE09865aaCd816729C19E4BeA4caFf247704De9fE";
//Old contract: 0xa9B7B85cFB7eD8C57b92086C52c63E3c01eac0C0
/*
inputs: [
@ -33,165 +36,3 @@ export const providerMetadata = {
},
};
export const contractABI =[
{
"inputs": [
{
"internalType": "string",
"name": "receiver",
"type": "string"
}
],
"name": "transferETH",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_recipient",
"type": "address"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "contractBalance",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getAllTransactions",
"outputs": [
{
"components": [
{
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "string",
"name": "receiver",
"type": "string"
},
{
"internalType": "bytes32",
"name": "txHash",
"type": "bytes32"
}
],
"internalType": "struct ETHTransfer.Transaction[]",
"name": "",
"type": "tuple[]"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "_receiver",
"type": "string"
}
],
"name": "getTransactionsByReceiver",
"outputs": [
{
"components": [
{
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "string",
"name": "receiver",
"type": "string"
},
{
"internalType": "bytes32",
"name": "txHash",
"type": "bytes32"
}
],
"internalType": "struct ETHTransfer.Transaction[]",
"name": "",
"type": "tuple[]"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "recipient",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "transactions",
"outputs": [
{
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "string",
"name": "receiver",
"type": "string"
},
{
"internalType": "bytes32",
"name": "txHash",
"type": "bytes32"
}
],
"stateMutability": "view",
"type": "function"
}
]