Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update run-predicate.ts #512

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 40 additions & 35 deletions contracts/predicate/scripts/run-predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,60 @@ import { BaseAssetId, Predicate, Provider, Wallet, bn, hexlify } from 'fuels';

const { NEXT_PUBLIC_FUEL_CHAIN_NAME, PRIVATE_KEY } = process.env;
const BIN_PATH = resolve(__dirname, '../out/debug/predicate-app.bin');
const ABI_PATH = resolve(__dirname, '../out/debug/predicate-app-abi.json');
const AMOUNT = 300_000;
const GAS_DEDUCTION = 150_000;

if (!NEXT_PUBLIC_FUEL_CHAIN_NAME || !PRIVATE_KEY) {
throw new Error(
'Missing some config in .env file. Should have NEXT_PUBLIC_FUEL_CHAIN_NAME and PRIVATE_KEY',
'Missing necessary config in .env file. Ensure NEXT_PUBLIC_FUEL_CHAIN_NAME and PRIVATE_KEY are set.',
);
}

const providerUrl = FUEL_CHAIN.providerUrl;

async function main() {
const binHex = hexlify(await fs.readFile(BIN_PATH));
const provider = await Provider.create(providerUrl);
const wallet = Wallet.fromPrivateKey(PRIVATE_KEY!, provider);
const { minGasPrice: gasPrice } = wallet.provider.getGasConfig();
const walletAddress = wallet.address.toB256();
const abiPath = resolve(__dirname, '../out/debug/predicate-app-abi.json');
const abi = await fs.readFile(abiPath, 'utf-8');
const abiJson = JSON.parse(abi);
const predicate = new Predicate(binHex, provider, abiJson);

console.log('💰 Funding predicate...');
const tx1 = await wallet.transfer(predicate.address, AMOUNT, BaseAssetId, {
gasPrice,
});

const res1 = await tx1.waitForResult();
const predicateBalance = bn(await predicate.getBalance());
console.log(`→ Transaction Id: ${res1.id}`);
console.log(`→ Predicate balance: ${predicateBalance.format()}`);
console.log(`→ Predicate Id: ${predicate.address.toB256()}`);
console.log(`📝 Wallet address: ${walletAddress}\n`);

console.log('⌛️ Running predicate...');
predicate.setData(walletAddress);

try {
const tx2 = await predicate.transfer(
wallet.address,
AMOUNT - 150_000,
BaseAssetId,
{
gasPrice,
},
);
const binHex = hexlify(await fs.readFile(BIN_PATH));
const provider = await Provider.create(providerUrl);
const wallet = Wallet.fromPrivateKey(PRIVATE_KEY!, provider);

// Fetch gas price configuration
const { minGasPrice: gasPrice } = wallet.provider.getGasConfig();
const walletAddress = wallet.address.toB256();

// Load ABI for predicate
const abi = await fs.readFile(ABI_PATH, 'utf-8');
const abiJson = JSON.parse(abi);
const predicate = new Predicate(binHex, provider, abiJson);

console.log('💰 Funding predicate...');

// Transfer funds to predicate address
const tx1 = await wallet.transfer(predicate.address, AMOUNT, BaseAssetId, { gasPrice });
const res1 = await tx1.waitForResult();

const predicateBalance = bn(await predicate.getBalance());
console.log(`→ Transaction Id: ${res1.id}`);
console.log(`→ Predicate balance: ${predicateBalance.format()}`);
console.log(`→ Predicate Id: ${predicate.address.toB256()}`);
console.log(`📝 Wallet address: ${walletAddress}\n`);

console.log('⌛️ Running predicate...');
predicate.setData(walletAddress);

// Transfer funds back from predicate
const transferAmount = AMOUNT - GAS_DEDUCTION;
if (transferAmount <= 0) {
throw new Error('Insufficient amount after gas deduction');
}

const tx2 = await predicate.transfer(wallet.address, transferAmount, BaseAssetId, { gasPrice });
const res2 = await tx2.waitForResult();

console.log(`→ Transaction Id: ${res2.id}`);
} catch (error: any) {
console.error(error?.response?.errors?.[0].message);
console.error('An error occurred:', error?.response?.errors?.[0]?.message || error.message);
}
}

Expand Down