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

Commit

Permalink
Merge pull request #1 from kkrt-labs/backend
Browse files Browse the repository at this point in the history
Backend
  • Loading branch information
ClementWalter authored Jul 13, 2023
2 parents 8cdba53 + 0576fc7 commit deaf142
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
9 changes: 5 additions & 4 deletions backend/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transfer, balanceOf } from './onchain.js';
import { balanceOf, getStarknetEoaAddress, transfer } from './onchain.js';

export function initRoutes(app) {
app.get('/health', async (req, res) => {
Expand All @@ -12,7 +12,8 @@ export function initRoutes(app) {
return res.status(400).json({ message: 'Missing "to" address in request body.' });
}

const txHash = await transfer(toAddress);
const starknetEoaAddress = await getStarknetEoaAddress(toAddress)
const txHash = await transfer(starknetEoaAddress);

return res.json({ message: `Transfer successful. Transaction hash: ${txHash}`, hash: txHash });
} catch (error) {
Expand All @@ -27,8 +28,8 @@ export function initRoutes(app) {
if (!ofAddress) {
return res.status(400).json({ message: 'Missing "of" address in request body.' });
}

const balance = await balanceOf(ofAddress);
const starknetEoaAddress = await getStarknetEoaAddress(ofAddress)
const balance = await balanceOf(starknetEoaAddress);

return res.json({ message: `Query successful.`, balance: balance });
} catch (error) {
Expand Down
37 changes: 31 additions & 6 deletions backend/onchain.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
import * as starknet from 'starknet';
import erc20Json from './erc20_abi.json' assert { type: 'json' };

const eth_address = process.env.TOKEN_ADDRESS;
const address = process.env.STARKNET_ACCOUNT_ADDRESS;
function getEnvVariable(name, defaultValue) {
const value = process.env[name];
if (value !== undefined) {
return value;
} else if (defaultValue !== undefined) {
return defaultValue;
} else {
throw new Error(`${name} environment variable is not defined`);
}
}

const ethTokenAddress = getEnvVariable('TOKEN_ADDRESS');
const address = getEnvVariable('STARKNET_ACCOUNT_ADDRESS');
const kakarotAddress = getEnvVariable('KAKAROT_ADDRESS');
const rpcUrl = getEnvVariable('RPC_URL');
const privateKey = getEnvVariable('PRIVATE_KEY');

const provider = new starknet.RpcProvider({
nodeUrl: process.env.RPC_URL,
nodeUrl: rpcUrl,
});

const account = new starknet.Account(provider, address, process.env.PRIVATE_KEY);
const account = new starknet.Account(provider, address, privateKey);

const contract = new starknet.Contract(erc20Json, ethTokenAddress, provider);

const contract = new starknet.Contract(erc20Json, eth_address, provider);
export async function getStarknetEoaAddress(ethAddress) {
console.log('call kakarot.compute_starknet_address - ', ethAddress);
const callResponse = await provider.callContract({
contractAddress: kakarotAddress,
entrypoint: 'compute_starknet_address',
calldata: [ethAddress],
});

return callResponse.result[0];
}

export async function transfer(to) {
console.log('Transfer to - ', to);
Expand Down Expand Up @@ -43,4 +68,4 @@ export async function balanceOf(of) {
const balance = await contract.balanceOf(of);
console.log("Balance -" + starknet.uint256.uint256ToBN(balance.balance).toString())
return starknet.uint256.uint256ToBN(balance.balance).toString()
}
}
9 changes: 5 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ services:
context: ./backend
dockerfile: Dockerfile
environment:
- PRIVATE_KEY=0
- STARKNET_ACCOUNT_ADDRESS=0
- TOKEN_ADDRESS=0
- RPC_URL=0
- PRIVATE_KEY=0x0300001800000000300000180000000000030000000000003006001800006600
- STARKNET_ACCOUNT_ADDRESS=0x03ee9e18edc71a6df30ac3aca2e0b02a198fbce19b7480a63a0d71cbd76652e0
- TOKEN_ADDRESS=0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7
- KAKAROT_ADDRESS=0x66a5454898f533792a1b3563d808c680a1cf6dd485797fc7044d71d88c49205
- RPC_URL=http://host.docker.internal:5050
- AMOUNT_TRANSFERED=1000000000000000
networks:
- faucet
Expand Down

0 comments on commit deaf142

Please sign in to comment.