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

Commit

Permalink
Deploy EOA if it doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementWalter committed Jul 29, 2023
1 parent 37bdf87 commit a9236a1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
.env
43 changes: 39 additions & 4 deletions src/components/Faucet.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,29 @@ async function getStarknetAddress(ethAddress) {
return callResponse.result[0];
}

async function addressAlreadyDeployed(address) {
try {
await provider.getClassHashAt(address, "latest");
return true;
} catch (error) {
console.log("error", error);
return false;
}
}

async function makeTransfer(toAddress) {
try {
const starknetAddress = await getStarknetAddress(toAddress);
const response = await fetch("/faucet", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ to: starknetAddress }),
body: JSON.stringify({ to: toAddress }),
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const data = await response.json();

return data;
Expand Down Expand Up @@ -70,6 +78,28 @@ async function getBalanceOf(ofAddress) {
}
}

async function deployKakarotAccount(ofAddress) {
try {
const response = await fetch("/deploy-kakarot-account", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ of: ofAddress }),
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const data = await response.json();
toast.info("EOA ready 🚀");
return data.balance;
} catch (error) {
toast.error(`Error in deployKakarotAccount: ${error.message}`);
}
}

function Faucet() {
const [addressInput, setAddressInput] = useState("");

Expand All @@ -89,8 +119,13 @@ function Faucet() {

async function sendToken() {
assertAddressInputFormat();
const data = await makeTransfer(addressInput);
const starknetAddress = await getStarknetAddress(addressInput);
const data = await makeTransfer(starknetAddress);
toast.info("Transaction hash: " + data.hash.transaction_hash);
const accountExists = await addressAlreadyDeployed(starknetAddress);
if (!accountExists) {
await deployKakarotAccount(addressInput);
}
}

async function getBalance() {
Expand Down

0 comments on commit a9236a1

Please sign in to comment.