-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathrun-predicate.ts
54 lines (44 loc) · 2.08 KB
/
run-predicate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { promises as fs } from 'node:fs';
import { resolve } from 'node:path';
import { FUEL_CHAIN } from 'app-commons';
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 AMOUNT = 300_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',
);
}
const providerUrl = FUEL_CHAIN.providerUrl;
async function loadFile(filePath: string): Promise<string> {
return fs.readFile(filePath, 'utf-8');
}
async function main() {
try {
const binHex = hexlify(await fs.readFile(BIN_PATH));
const provider = await Provider.create(providerUrl);
const wallet = Wallet.fromPrivateKey(PRIVATE_KEY!, provider);
const { minGasPrice: gasPrice } = await wallet.provider.getGasConfig();
const walletAddress = wallet.address.toB256();
const abiPath = resolve(__dirname, '../out/debug/predicate-app-abi.json');
const abiJson = JSON.parse(await loadFile(abiPath));
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);
const tx2 = await predicate.transfer(wallet.address, AMOUNT - 150_000, BaseAssetId, { gasPrice });
const res2 = await tx2.waitForResult();
console.log(`→ Transaction Id: ${res2.id}`);
} catch (error: any) {
console.error(error?.response?.errors?.[0]?.message || error.message);
}
}
main();