Skip to content

Commit

Permalink
Merge pull request #88 from FuelLabs/ps/chore/refactored-paymaster
Browse files Browse the repository at this point in the history
feat: refactored paymaster
  • Loading branch information
petertonysmith94 authored Dec 17, 2024
2 parents a31c288 + 345aba0 commit 9fa84a2
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 276 deletions.
70 changes: 8 additions & 62 deletions frontend/src/components/NewPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import type { FarmContract } from "../sway-api";

import Loading from "./Loading";
import { PlayerOutput } from "../sway-api/contracts/FarmContract";
import { Address, BN, type Coin, Provider, bn } from "fuels";
import axios from "axios";
import { Address, BN, Provider } from "fuels";
import { usePaymaster } from "../hooks/usePaymaster";

interface NewPlayerProps {
contract: FarmContract | null;
Expand Down Expand Up @@ -83,43 +83,10 @@ export default function NewPlayer({
try {
// Try with gas station first
const provider = await Provider.create(FUEL_PROVIDER_URL);
const { data: MetaDataResponse } = await axios.get<{
maxValuePerCoin: string;
}>(`http://167.71.42.88:3000/metadata`);
const { maxValuePerCoin } = MetaDataResponse;
console.log("maxValuePerCoin", maxValuePerCoin);
if (!maxValuePerCoin) {
throw new Error("No maxValuePerCoin found");
}
const { data } = await axios.post<{
coin: {
id: string;
amount: string;
assetId: string;
owner: string;
blockCreated: string;
txCreatedIdx: string;
};
jobId: string;
utxoId: string;
}>(`http://167.71.42.88:3000/allocate-coin`);
if (!data?.coin) {
throw new Error("No coin found");
}

if (!data.jobId) {
throw new Error("No jobId found");
}
const gasCoin: Coin = {
id: data.coin.id,
amount: bn(data.coin.amount),
assetId: data.coin.assetId,
owner: Address.fromAddressOrString(data.coin.owner),
blockCreated: bn(data.coin.blockCreated),
txCreatedIdx: bn(data.coin.txCreatedIdx),
};
console.log("gasCoin", gasCoin);
// const address = Address.fromRandom();

const paymaster = usePaymaster();

Check failure on line 87 in frontend/src/components/NewPlayer.tsx

View workflow job for this annotation

GitHub Actions / Lint

React Hook "usePaymaster" is called in function "handleNewPlayer" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hook names must start with the word "use"
const { maxValuePerCoin } = await paymaster.metadata();
const { coin: gasCoin, jobId } = await paymaster.allocate();

const addressIdentityInput = {
Address: { bits: Address.fromAddressOrString(wallet.address.toString()).toB256() },
Expand All @@ -146,29 +113,8 @@ export default function NewPlayer({
request.gasLimit = gasUsed;
request.maxFee = maxFee;

// return;
const response = await axios.post(`http://167.71.42.88:3000/sign`, {
request: request.toJSON(),
jobId: data.jobId,
});
if (response.status !== 200) {
throw new Error("Failed to sign transaction");
}

if (!response.data.signature) {
throw new Error("No signature found");
}
console.log("response.data", response.data);
const gasInput = request.inputs.find((coin) => {
return coin.type === 0;
});
if (!gasInput) {
throw new Error("Gas coin not found");
}

const wi = request.getCoinInputWitnessIndexByOwner(gasCoin.owner);
console.log("wi", wi);
request.witnesses[wi as number] = response.data.signature;
const { signature } = await paymaster.fetchSignature(request, jobId);
request.updateWitnessByOwner(gasCoin.owner, signature);

// await wallet.fund(request, txCost);

Expand Down
62 changes: 7 additions & 55 deletions frontend/src/components/modals/BuySeeds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
useNetwork,

Check failure on line 19 in frontend/src/components/modals/BuySeeds.tsx

View workflow job for this annotation

GitHub Actions / Lint

'useNetwork' is defined but never used
useBalance,

Check failure on line 20 in frontend/src/components/modals/BuySeeds.tsx

View workflow job for this annotation

GitHub Actions / Lint

'useBalance' is defined but never used
} from "@fuels/react";
import { usePaymaster } from "../../hooks/usePaymaster";

interface BuySeedsProps {
contract: FarmContract | null;
Expand All @@ -43,42 +44,11 @@ export default function BuySeeds({
throw new Error("No wallet found");
}
const provider = await Provider.create(FUEL_PROVIDER_URL);
const { data: MetaDataResponse } = await axios.get<{
maxValuePerCoin: string;
}>(`http://167.71.42.88:3000/metadata`);
const { maxValuePerCoin } = MetaDataResponse;
console.log("maxValuePerCoin", maxValuePerCoin);
if (!maxValuePerCoin) {
throw new Error("No maxValuePerCoin found");
}
const { data } = await axios.post<{
coin: {
id: string;
amount: string;
assetId: string;
owner: string;
blockCreated: string;
txCreatedIdx: string;
};
jobId: string;
utxoId: string;
}>(`http://167.71.42.88:3000/allocate-coin`);
if (!data?.coin) {
throw new Error("No coin found");
}

if (!data.jobId) {
throw new Error("No jobId found");
}
const gasCoin: Coin = {
id: data.coin.id,
amount: bn(data.coin.amount),
assetId: data.coin.assetId,
owner: Address.fromAddressOrString(data.coin.owner),
blockCreated: bn(data.coin.blockCreated),
txCreatedIdx: bn(data.coin.txCreatedIdx),
};
console.log("gasCoin", gasCoin);
const paymaster = usePaymaster();
const { maxValuePerCoin } = await paymaster.metadata();
const { coin: gasCoin, jobId } = await paymaster.allocate();

const amount = 10;
const realAmount = amount / 1_000_000_000;
const inputAmount = bn.parseUnits(realAmount.toFixed(9).toString());
Expand Down Expand Up @@ -148,26 +118,8 @@ export default function BuySeeds({
);
request.addChangeOutput(gasCoin.owner, provider.getBaseAssetId());

const response = await axios.post(`http://167.71.42.88:3000/sign`, {
request: request.toJSON(),
jobId: data.jobId,
});
if (response.status !== 200) {
throw new Error("Failed to sign transaction");
}
if (!response.data.signature) {
throw new Error("No signature found");
}
const gasInput = request.inputs.find((coin) => {
return coin.type === 0;
});
if (!gasInput) {
throw new Error("Gas coin not found");
}
console.log("gasInput", gasInput);
const wi = request.getCoinInputWitnessIndexByOwner(gasCoin.owner);
request.witnesses[wi as number] = response.data.signature;
console.log("request manually after coin", request.toJSON());
const {signature} = await paymaster.fetchSignature(request, jobId);
request.updateWitnessByOwner(gasCoin.owner, signature);

const tx = await wallet.sendTransaction(request, {
estimateTxDependencies: false,
Expand Down
64 changes: 10 additions & 54 deletions frontend/src/components/modals/HarvestModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { buttonStyle, FUEL_PROVIDER_URL } from "../../constants";
import type { FarmContract } from "../../sway-api";
import type { Modals } from "../../constants";
import { useWallet } from "@fuels/react";
import { Address, type Coin, Provider, bn } from "fuels";
import axios from "axios";
import { Address, Provider } from "fuels";
import { usePaymaster } from "../../hooks/usePaymaster";
interface HarvestProps {
contract: FarmContract | null;
tileArray: number[];
Expand Down Expand Up @@ -59,42 +59,11 @@ export default function HarvestModal({
try {
// Try with gas station first
const provider = await Provider.create(FUEL_PROVIDER_URL);
const { data: MetaDataResponse } = await axios.get<{
maxValuePerCoin: string;
}>(`http://167.71.42.88:3000/metadata`);
const { maxValuePerCoin } = MetaDataResponse;
console.log("maxValuePerCoin", maxValuePerCoin);
if (!maxValuePerCoin) {
throw new Error("No maxValuePerCoin found");
}
const { data } = await axios.post<{
coin: {
id: string;
amount: string;
assetId: string;
owner: string;
blockCreated: string;
txCreatedIdx: string;
};
jobId: string;
utxoId: string;
}>(`http://167.71.42.88:3000/allocate-coin`);
if (!data?.coin) {
throw new Error("No coin found");
}

if (!data.jobId) {
throw new Error("No jobId found");
}
const gasCoin: Coin = {
id: data.coin.id,
amount: bn(data.coin.amount),
assetId: data.coin.assetId,
owner: Address.fromAddressOrString(data.coin.owner),
blockCreated: bn(data.coin.blockCreated),
txCreatedIdx: bn(data.coin.txCreatedIdx),
};
console.log("gasCoin", gasCoin);
const paymaster = usePaymaster();
const { maxValuePerCoin } = await paymaster.metadata();
const { coin: gasCoin, jobId } = await paymaster.allocate();

const addressIdentityInput = {
Address: {
bits: Address.fromAddressOrString(
Expand Down Expand Up @@ -122,23 +91,10 @@ export default function HarvestModal({
request.gasLimit = gasUsed;
request.maxFee = maxFee;
console.log(`Harvest Cost gasLimit: ${gasUsed}, Maxfee: ${maxFee}`);
const response = await axios.post(`http://167.71.42.88:3000/sign`, {
request: request.toJSON(),
jobId: data.jobId,
});
if (response.status !== 200) {
throw new Error("Failed to sign transaction");
}
if (!response.data.signature) {
throw new Error("No signature found");
}
const gasInput = request.inputs.find((coin) => {
return coin.type === 0;
});
if (!gasInput) {
throw new Error("Gas coin not found");
}
request.witnesses[gasInput.witnessIndex] = response.data.signature;

const { signature } = await paymaster.fetchSignature(request, jobId);
request.updateWitnessByOwner(gasCoin.owner, signature);

console.log("harvest request manually", request.toJSON());
const tx = await wallet.sendTransaction(request);
if (tx) {
Expand Down
60 changes: 7 additions & 53 deletions frontend/src/components/modals/PlantModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Loading from "../Loading";
import { Address, type Coin, Provider, bn } from "fuels";
import { useWallet } from "@fuels/react";
import axios from "axios";
import { usePaymaster } from "../../hooks/usePaymaster";

interface PlantModalProps {
contract: FarmContract | null;
Expand Down Expand Up @@ -65,42 +66,11 @@ export default function PlantModal({
try {
// Try with gas station first
const provider = await Provider.create(FUEL_PROVIDER_URL);
const { data: MetaDataResponse } = await axios.get<{
maxValuePerCoin: string;
}>(`http://167.71.42.88:3000/metadata`);
const { maxValuePerCoin } = MetaDataResponse;
console.log("maxValuePerCoin", maxValuePerCoin);
if (!maxValuePerCoin) {
throw new Error("No maxValuePerCoin found");
}
const { data } = await axios.post<{
coin: {
id: string;
amount: string;
assetId: string;
owner: string;
blockCreated: string;
txCreatedIdx: string;
};
jobId: string;
utxoId: string;
}>(`http://167.71.42.88:3000/allocate-coin`);
if (!data?.coin) {
throw new Error("No coin found");
}

if (!data.jobId) {
throw new Error("No jobId found");
}
const gasCoin: Coin = {
id: data.coin.id,
amount: bn(data.coin.amount),
assetId: data.coin.assetId,
owner: Address.fromAddressOrString(data.coin.owner),
blockCreated: bn(data.coin.blockCreated),
txCreatedIdx: bn(data.coin.txCreatedIdx),
};
console.log("gasCoin", gasCoin);
const paymaster = usePaymaster();
const { maxValuePerCoin } = await paymaster.metadata();
const { coin: gasCoin, jobId } = await paymaster.allocate();

const addressIdentityInput = {
Address: {
bits: Address.fromAddressOrString(
Expand Down Expand Up @@ -129,26 +99,10 @@ export default function PlantModal({
request.gasLimit = gasUsed;
request.maxFee = maxFee;
console.log(`Plant Cost gasLimit: ${gasUsed}, Maxfee: ${maxFee}`);
const response = await axios.post(`http://167.71.42.88:3000/sign`, {
request: request.toJSON(),
jobId: data.jobId,
});
if (response.status !== 200) {
throw new Error("Failed to sign transaction");
}

if (!response.data.signature) {
throw new Error("No signature found");
}
console.log("response.data", response.data);
const gasInput = request.inputs.find((coin) => {
return coin.type === 0;
});
if (!gasInput) {
throw new Error("Gas coin not found");
}
const { signature } = await paymaster.fetchSignature(request, jobId);
request.updateWitnessByOwner(gasCoin.owner, signature);

request.witnesses[gasInput.witnessIndex] = response.data.signature;
const tx = await wallet.sendTransaction(request);
if (tx) {
console.log("tx", tx);
Expand Down
Loading

0 comments on commit 9fa84a2

Please sign in to comment.