Skip to content

Commit 0fba4a5

Browse files
committed
Update test scripts (p-chain transaction examples)
1 parent c5ee3cb commit 0fba4a5

File tree

6 files changed

+195
-65
lines changed

6 files changed

+195
-65
lines changed

test-scripts/src/test-add-delegator.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,49 @@
1-
import { networkIDs, pvm, utils } from "@flarenetwork/flarejs";
1+
import { networkIDs, pvm, UnsignedTx, utils } from "@flarenetwork/flarejs";
22
import { issuePChainTx, localFlareContext } from "./utils";
33
import { runTest } from "./runner";
44

5-
async function addDelegator(nodeID: string, endTime: number, weight: number) {
5+
async function addDelegator(nodeId: string, endTime: number, weight: number) {
66
const ctx = await localFlareContext();
77

88
// Create the transaction to add a delegator
9-
console.log(`Creating add delegator transaction for node ${nodeID} with weight ${weight}...`);
9+
console.log(`Creating add delegator transaction for node ${nodeId} with weight ${weight}...`);
1010

1111
const { utxos } = await ctx.pvmapi.getUTXOs({
1212
addresses: [ctx.addressP]
1313
});
14-
const tx = pvm.newAddPermissionlessDelegatorTx(
14+
15+
let tx: UnsignedTx;
16+
if (ctx.isEtnaForkActive) {
17+
console.log("Etna fork is active, using new transaction format.");
18+
const feeState = await ctx.pvmapi.getFeeState();
19+
tx = pvm.e.newAddPermissionlessDelegatorTx(
20+
{
21+
feeState,
22+
utxos,
23+
nodeId,
24+
subnetId: networkIDs.PrimaryNetworkID.toString(),
25+
start: BigInt(Date.now()) / 1000n,
26+
end: BigInt(endTime),
27+
weight: BigInt(weight * 1e9),
28+
fromAddressesBytes: [utils.bech32ToBytes(ctx.addressP)],
29+
rewardAddresses: [utils.bech32ToBytes(ctx.addressP)],
30+
},
31+
ctx.context,
32+
);
33+
} else {
34+
console.log("Etna fork is not active, using legacy transaction format.");
35+
tx = pvm.newAddPermissionlessDelegatorTx(
1536
ctx.context,
1637
utxos,
1738
[utils.bech32ToBytes(ctx.addressP)],
18-
nodeID,
39+
nodeId,
1940
networkIDs.PrimaryNetworkID.toString(),
2041
BigInt(Date.now()) / 1000n,
2142
BigInt(endTime),
2243
BigInt(weight * 1e9),
2344
[utils.bech32ToBytes(ctx.addressP)],
24-
);
25-
45+
);
46+
}
2647
await issuePChainTx(ctx.pvmapi, tx, ctx.privateKey);
2748
}
2849

test-scripts/src/test-add-validator.ts

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,64 @@
1-
import { networkIDs, pvm, utils } from "@flarenetwork/flarejs";
1+
import { networkIDs, pvm, UnsignedTx, utils } from "@flarenetwork/flarejs";
22
import { issuePChainTx, localFlareContext } from "./utils";
33
import { runTest } from "./runner";
44

55
const nodeID = 'NodeID-MFrZFVCXPv5iCn6M9K6XduxGTYp891xXZ';
66
const blsPublicKey = '0xadb0203ebc76627d28fb9440272a2701b85f5c5d4266352686ea42666f5026f1fdabab59529932f1eddb317a6f7435f9';
77
const blsSignature = '0x926a1c21953babb12189ec88bfab5cb0060b385efa04c51abe4a3bc42266f80eebb84bcfaacfc8f075560ab444bda8de17bf9013ec20a80dfdfda4ae047a0b39669d153dcee94eb964fe194f5d55c5bba5939ff9ba07b728b6733d696c33ac5a'
88

9-
async function addValidator(nodeID: string, endTime: number, weight: number) {
9+
async function addValidator(nodeId: string, endTime: number, weight: number) {
1010
const ctx = await localFlareContext();
1111

1212
// Create the transaction to add a validator
13-
console.log(`Creating add validator transaction for node ${nodeID} with weight ${weight}...`);
13+
console.log(`Creating add validator transaction for node ${nodeId} with weight ${weight}...`);
1414

1515
const { utxos } = await ctx.pvmapi.getUTXOs({
1616
addresses: [ctx.addressP]
1717
});
18-
const tx = pvm.newAddPermissionlessValidatorTx(
19-
ctx.context,
20-
utxos,
21-
[utils.bech32ToBytes(ctx.addressP)],
22-
nodeID,
23-
networkIDs.PrimaryNetworkID.toString(),
24-
BigInt(Date.now()) / 1000n,
25-
BigInt(endTime),
26-
BigInt(weight * 1e9),
27-
[utils.bech32ToBytes(ctx.addressP)],
28-
[utils.bech32ToBytes(ctx.addressP)],
29-
10_0000,
30-
undefined,
31-
1,
32-
0n,
33-
utils.hexToBuffer(blsPublicKey),
34-
utils.hexToBuffer(blsSignature)
35-
);
18+
19+
let tx: UnsignedTx;
20+
if (ctx.isEtnaForkActive) {
21+
console.log("Etna fork is active, using new transaction format.");
22+
const feeState = await ctx.pvmapi.getFeeState();
23+
tx = pvm.e.newAddPermissionlessValidatorTx(
24+
{
25+
feeState,
26+
utxos,
27+
nodeId,
28+
subnetId: networkIDs.PrimaryNetworkID.toString(),
29+
start: BigInt(Date.now()) / 1000n,
30+
end: BigInt(endTime),
31+
weight: BigInt(weight * 1e9),
32+
shares: 10_0000,
33+
fromAddressesBytes: [utils.bech32ToBytes(ctx.addressP)],
34+
rewardAddresses: [utils.bech32ToBytes(ctx.addressP)],
35+
delegatorRewardsOwner: [utils.bech32ToBytes(ctx.addressP)],
36+
publicKey: utils.hexToBuffer(blsPublicKey),
37+
signature: utils.hexToBuffer(blsSignature),
38+
},
39+
ctx.context,
40+
);
41+
} else {
42+
console.log("Etna fork is not active, using legacy transaction format.");
43+
tx = pvm.newAddPermissionlessValidatorTx(
44+
ctx.context,
45+
utxos,
46+
[utils.bech32ToBytes(ctx.addressP)],
47+
nodeId,
48+
networkIDs.PrimaryNetworkID.toString(),
49+
BigInt(Date.now()) / 1000n,
50+
BigInt(endTime),
51+
BigInt(weight * 1e9),
52+
[utils.bech32ToBytes(ctx.addressP)],
53+
[utils.bech32ToBytes(ctx.addressP)],
54+
10_0000,
55+
undefined,
56+
1,
57+
0n,
58+
utils.hexToBuffer(blsPublicKey),
59+
utils.hexToBuffer(blsSignature)
60+
);
61+
}
3662

3763
await issuePChainTx(ctx.pvmapi, tx, ctx.privateKey);
3864
}

test-scripts/src/test-p-chain-export.ts

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { evm, pvm, utils, TransferableOutput } from '@flarenetwork/flarejs';
1+
import { evm, pvm, utils, TransferableOutput, UnsignedTx } from '@flarenetwork/flarejs';
22
import { issueCChainTx, issuePChainTx, localFlareContext } from './utils';
33
import { runTest } from './runner';
44

@@ -12,17 +12,43 @@ async function PtoCExport(amountFLR: number) {
1212
const { utxos: utxosp } = await ctx.pvmapi.getUTXOs({
1313
addresses: [ctx.addressP]
1414
});
15-
const exportTx = pvm.newExportTx(
16-
ctx.context,
17-
ctx.context.cBlockchainID,
18-
[utils.bech32ToBytes(ctx.addressP)],
19-
utxosp,
20-
[
21-
TransferableOutput.fromNative(ctx.context.avaxAssetID, BigInt(amountFLR * 1e9), [
22-
utils.bech32ToBytes(ctx.addressP)
23-
])
24-
]
25-
);
15+
16+
let exportTx: UnsignedTx;
17+
if (ctx.isEtnaForkActive) {
18+
console.log("Etna fork is active, using new transaction format for export.");
19+
const feeState = await ctx.pvmapi.getFeeState();
20+
exportTx = pvm.e.newExportTx(
21+
{
22+
feeState,
23+
utxos: utxosp,
24+
destinationChainId: ctx.context.cBlockchainID,
25+
fromAddressesBytes: [utils.bech32ToBytes(ctx.addressP)],
26+
outputs: [
27+
TransferableOutput.fromNative(
28+
ctx.context.avaxAssetID,
29+
BigInt(amountFLR * 1e9),
30+
[utils.bech32ToBytes(ctx.addressP)]
31+
)
32+
]
33+
},
34+
ctx.context,
35+
);
36+
} else {
37+
console.log("Etna fork is not active, using legacy transaction format for export.");
38+
exportTx = pvm.newExportTx(
39+
ctx.context,
40+
ctx.context.cBlockchainID,
41+
[utils.bech32ToBytes(ctx.addressP)],
42+
utxosp,
43+
[
44+
TransferableOutput.fromNative(
45+
ctx.context.avaxAssetID,
46+
BigInt(amountFLR * 1e9),
47+
[utils.bech32ToBytes(ctx.addressP)]
48+
)
49+
]
50+
);
51+
}
2652
await issuePChainTx(ctx.pvmapi, exportTx, ctx.privateKey);
2753

2854
// Create and issue a P to C chain import transaction

test-scripts/src/test-p-chain-import.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { evm, pvm, utils } from '@flarenetwork/flarejs';
1+
import { evm, pvm, UnsignedTx, utils } from '@flarenetwork/flarejs';
22
import { issueCChainTx, issuePChainTx, localFlareContext } from './utils';
33
import { runTest } from './runner';
44

@@ -29,14 +29,31 @@ async function CtoPExport(amountFLR: number) {
2929
sourceChain: 'C',
3030
addresses: [ctx.addressP]
3131
});
32-
const importTx = pvm.newImportTx(
33-
ctx.context,
34-
ctx.context.cBlockchainID,
35-
utxos,
36-
[utils.bech32ToBytes(ctx.addressP)],
37-
[utils.bech32ToBytes(ctx.addressP)]
38-
);
3932

33+
let importTx: UnsignedTx;
34+
if (ctx.isEtnaForkActive) {
35+
console.log("Etna fork is active, using new transaction format for import.");
36+
const feeState = await ctx.pvmapi.getFeeState();
37+
importTx = pvm.e.newImportTx(
38+
{
39+
feeState,
40+
utxos,
41+
sourceChainId: ctx.context.cBlockchainID,
42+
fromAddressesBytes: [utils.bech32ToBytes(ctx.addressP)],
43+
toAddressesBytes: [utils.bech32ToBytes(ctx.addressP)],
44+
},
45+
ctx.context,
46+
);
47+
} else {
48+
console.log("Etna fork is not active, using legacy transaction format for import.");
49+
importTx = pvm.newImportTx(
50+
ctx.context,
51+
ctx.context.cBlockchainID,
52+
utxos,
53+
[utils.bech32ToBytes(ctx.addressP)],
54+
[utils.bech32ToBytes(ctx.addressP)]
55+
);
56+
}
4057
await issuePChainTx(ctx.pvmapi, importTx, ctx.privateKey);
4158
}
4259

test-scripts/src/test-p-chain-transfer.ts

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { pvm, utils, TransferableOutput } from '@flarenetwork/flarejs';
1+
import { pvm, utils, TransferableOutput, UnsignedTx } from '@flarenetwork/flarejs';
22
import { issuePChainTx, localFlareContext } from './utils';
33
import { runTest } from './runner';
44

@@ -11,17 +11,42 @@ async function PtoPTransfer(amountFLR: number, toAddress: string) {
1111
const { utxos: utxosp } = await ctx.pvmapi.getUTXOs({
1212
addresses: [ctx.addressP]
1313
});
14-
const exportTx = pvm.newBaseTx(
15-
ctx.context,
16-
[utils.bech32ToBytes(ctx.addressP)],
17-
utxosp,
18-
[
19-
TransferableOutput.fromNative(ctx.context.avaxAssetID, BigInt(amountFLR * 1e9), [
20-
utils.bech32ToBytes(toAddress)
21-
])
22-
]
23-
);
24-
await issuePChainTx(ctx.pvmapi, exportTx, ctx.privateKey);
14+
15+
let tx: UnsignedTx;
16+
if (ctx.isEtnaForkActive) {
17+
console.log("Etna fork is active, using new transaction format for transfer.");
18+
const feeState = await ctx.pvmapi.getFeeState();
19+
tx = pvm.e.newBaseTx(
20+
{
21+
feeState,
22+
utxos: utxosp,
23+
fromAddressesBytes: [utils.bech32ToBytes(ctx.addressP)],
24+
outputs: [
25+
TransferableOutput.fromNative(
26+
ctx.context.avaxAssetID,
27+
BigInt(amountFLR * 1e9),
28+
[utils.bech32ToBytes(toAddress)]
29+
)
30+
]
31+
},
32+
ctx.context,
33+
);
34+
} else {
35+
console.log("Etna fork is not active, using legacy transaction format for transfer.");
36+
tx = pvm.newBaseTx(
37+
ctx.context,
38+
[utils.bech32ToBytes(ctx.addressP)],
39+
utxosp,
40+
[
41+
TransferableOutput.fromNative(
42+
ctx.context.avaxAssetID,
43+
BigInt(amountFLR * 1e9),
44+
[utils.bech32ToBytes(toAddress)]
45+
)
46+
]
47+
);
48+
}
49+
await issuePChainTx(ctx.pvmapi, tx, ctx.privateKey);
2550
}
2651

2752

test-scripts/src/utils.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
addTxSignatures,
77
UnsignedTx,
88
utils,
9+
info,
910
} from "@flarenetwork/flarejs";
1011
import { JsonRpcProvider } from "ethers";
1112

@@ -24,6 +25,8 @@ export interface TestContext {
2425
evmapi: evm.EVMApi;
2526
pvmapi: pvm.PVMApi;
2627
avmapi: avm.AVMApi;
28+
infoapi: info.InfoApi;
29+
isEtnaForkActive: boolean;
2730
provider: JsonRpcProvider;
2831
addressC: string;
2932
addressP: string;
@@ -53,16 +56,28 @@ export async function PChainBalance(): Promise<BigInt> {
5356
}
5457

5558
export async function localFlareContext(): Promise<TestContext> {
56-
const evmapi = new evm.EVMApi(LocalURL);
57-
const pvmapi = new pvm.PVMApi(LocalURL);
58-
const avmapi = new avm.AVMApi(LocalURL);
59-
const context = await Context.getContextFromURI(LocalURL);
60-
const provider = new JsonRpcProvider(LocalURL + "/ext/bc/C/rpc");
59+
return getContext(LocalURL);
60+
}
61+
62+
export async function getContext(url: string): Promise<TestContext> {
63+
const evmapi = new evm.EVMApi(url);
64+
const pvmapi = new pvm.PVMApi(url);
65+
const avmapi = new avm.AVMApi(url);
66+
const infoapi = new info.InfoApi(url);
67+
const context = await Context.getContextFromURI(url);
68+
const provider = new JsonRpcProvider(url + "/ext/bc/C/rpc");
69+
70+
const { etnaTime } = await infoapi.getUpgradesInfo();
71+
const etnaDateTime = new Date(etnaTime);
72+
const now = new Date();
73+
6174
return {
6275
context,
6376
evmapi,
6477
pvmapi,
6578
avmapi,
79+
infoapi,
80+
isEtnaForkActive: now > etnaDateTime,
6681
provider,
6782
addressC: TestCAddress,
6883
addressP: TestPAddress,

0 commit comments

Comments
 (0)