-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
170 lines (146 loc) · 6.64 KB
/
index.ts
File metadata and controls
170 lines (146 loc) · 6.64 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { BigNumber } from '@ethersproject/bignumber';
import * as fs from 'fs';
import pkg from 'casper-js-sdk';
const { HttpHandler, RpcClient, InitiatorAddr, Timestamp, Duration, PricingMode, TransactionTarget, TransactionEntryPoint, TransactionScheduling, Args, PublicKey, PaymentLimitedMode, Transaction, TransactionV1Payload, TransactionV1, CLValue, TransactionEntryPointEnum, PrivateKey, KeyAlgorithm, InfoGetTransactionResult, TransactionHash, ContractCallBuilder, Key, KeyTypeID } = pkg;
// Load the private key.
const pemFilePath = '/Users/ziel/workspace/odra/casper-keys/testnet-account/secret_key.pem';
const fileContent = fs.readFileSync(pemFilePath, 'utf8');
const privateKey = PrivateKey.fromPem(fileContent, KeyAlgorithm.ED25519);
// Create RPC client.
const rpcHandler = new HttpHandler('https://node.testnet.casper.network/rpc');
const rpcClient = new RpcClient(rpcHandler);
// Create a new, example SR_ID argument value.
// You'll have to put keccak256 hash here.
const EXAMPLE_SR_ID = new_sr_id("8a2eb90b352c2e55340d04f792ee1d0907141214208bab69a5569b83ee87da55");
function new_sr_id(value: string): CLValue {
let arr: Uint8Array = Buffer.from(value, 'hex');
return CLValue.newCLByteArray(Uint8Array.from(arr));
}
// Extract the sr_id argument value from a transaction.
function extract_sr_id(transaction: InfoGetTransactionResult): string {
let arg = transaction.transaction.args.getByName('sr_id');
if (arg == undefined) {
throw new Error("sr_id argument not found in transaction");
}
return arg.byteArray!.toString();
}
// --------- Native Transfer Example ---------
// Create a native transfer transaction.
function build_native_transfer(
publicKey: PublicKey,
recipient: PublicKey,
amount: BigNumber | string,
sr_id: CLValue
): Transaction {
// Define pricing mode.
const pricingMode = new PricingMode();
const paymentLimited = new PaymentLimitedMode();
paymentLimited.standardPayment = true;
paymentLimited.paymentAmount = 100_000_000;
paymentLimited.gasPriceTolerance = 1;
pricingMode.paymentLimited = paymentLimited;
// Args
const runtimeArgs = Args.fromMap({});
runtimeArgs.insert('target', CLValue.newCLPublicKey(recipient));
runtimeArgs.insert('amount', CLValue.newCLUInt512(amount));
runtimeArgs.insert('sr_id', sr_id);
// Build transaction payload.
const transactionPayload = TransactionV1Payload.build({
initiatorAddr: new InitiatorAddr(publicKey),
timestamp: new Timestamp(new Date()),
ttl: new Duration(1800000),
chainName: 'casper-test',
pricingMode: pricingMode,
args: runtimeArgs,
transactionTarget: new TransactionTarget({}),
entryPoint: new TransactionEntryPoint(
TransactionEntryPointEnum.Transfer
),
scheduling: new TransactionScheduling({})
});
const transactionV1 = TransactionV1.makeTransactionV1(transactionPayload);
return Transaction.fromTransactionV1(transactionV1);
}
// Sends example native transfer with a given SR_ID.
async function send_native_transfer(sr_id: CLValue): Promise<TransactionHash> {
const transaction = build_native_transfer(
privateKey.publicKey,
PublicKey.fromHex('0202f5a92ab6da536e7b1a351406f3744224bec85d7acbab1497b65de48a1a707b64'),
'4200000000',
sr_id
)
transaction.sign(privateKey);
console.log('[x] Transaction Hash:', transaction.hash.toHex());
await rpcClient.putTransaction(transaction);
const transactionResult = await rpcClient.waitForTransaction(transaction, 100000);
// Additioanally check for success.
let is_success = transactionResult.executionInfo?.executionResult.errorMessage == undefined;
if (!is_success) {
throw new Error("Transaction was not successful");
}
return transaction.hash;
}
// Run full native transfer example.
// 1. Create new native transfer.
// 2. Extract SR_ID argument from the successful transaction.
async function run_native_transfer_example() {
console.log('[x] Running native transfer example');
console.log('[x] Public key:', privateKey.publicKey.toHex());
let native_tx_hash = (await send_native_transfer(EXAMPLE_SR_ID)).toHex();
const native_tx = await rpcClient.getTransactionByTransactionHash(native_tx_hash);
let extracted_sr_id = extract_sr_id(native_tx);
console.log("[x] Extracted sr_id argument value:", extracted_sr_id);
console.log('[x] Native transfer example finished\n');
}
// --------- CEP-18 Transfer Example ---------
// Sends example CEP-18 transfer with a given SR_ID.
async function send_cep18_transfer(sr_id: CLValue): Promise<TransactionHash> {
let txBuilder = new ContractCallBuilder()
.byPackageHash("b72183e301022030195350876ce3226d0067aee1eb3695a5252ee2b85eabe741")
.entryPoint('transfer')
.from(privateKey.publicKey)
.chainName('casper-test')
.payment(2500000000)
.ttl(1800000)
.runtimeArgs(
Args.fromMap({
recipient: CLValue.newCLKey(
Key.createByType(
"account-hash-040fe59024a78372bc1225cfd5ed258eacdbbc7c5d09d35767777dc10c5d14ca",
KeyTypeID.Account
)
),
amount: CLValue.newCLUInt256(1000),
sr_id: sr_id
})
);
let transaction = txBuilder.build();
transaction.sign(privateKey);
console.log('[x] Transaction Hash:', transaction.hash.toHex());
await rpcClient.putTransaction(transaction);
const transactionResult = await rpcClient.waitForTransaction(transaction, 100000);
// Additioanally check for success.
let is_success = transactionResult.executionInfo?.executionResult.errorMessage == undefined;
if (!is_success) {
throw new Error("Transaction was not successful");
}
return transaction.hash;
}
// Run full CEP-18 transfer example.
// 1. Create new CEP-18 transfer.
// 2. Extract SR_ID argument from the successful transaction.
async function run_cep18_transfer_example() {
console.log('[x] Running CEP-18 transfer example');
console.log('[x] Public key:', privateKey.publicKey.toHex());
let cep18_tx_hash = (await send_cep18_transfer(EXAMPLE_SR_ID)).toHex();
const cep18_tx = await rpcClient.getTransactionByTransactionHash(cep18_tx_hash);
let extracted_sr_id = extract_sr_id(cep18_tx);
console.log("[x] Extracted sr_id argument value:", extracted_sr_id);
console.log('[x] CEP-18 transfer example finished\n');
}
// --------- Main ---------
async function main() {
await run_native_transfer_example().catch(console.error);
await run_cep18_transfer_example().catch(console.error);
}
main();