-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcasper_wrapper.js
69 lines (53 loc) · 2.17 KB
/
casper_wrapper.js
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
const { Keys, CasperClient, DeployUtil, CLPublicKey, RuntimeArgs, CLValueBuilder } = require('casper-js-sdk');
const axios = require('axios');
function generateNewAccount() {
const keyPair = Keys.Ed25519.new();
const publicKey = keyPair.publicKey.toHex();
const privateKey = Buffer.from(keyPair.privateKey).toString('hex');
return {
publicKey: publicKey,
privateKey: privateKey
};
}
async function transferFunds(nodeAddress, privateKey, targetPublicKeyHex, amount, fee = 10000, chainName = "casper") {
const client = new CasperClient(nodeAddress);
const senderKeyPair = DeployUtil.privateToPublicKey(privateKey);
const recipientPublicKey = CLPublicKey.fromHex(targetPublicKeyHex);
const transferDeploy = DeployUtil.makeTransferDeploy(
new DeployUtil.DeployParams(senderKeyPair, chainName),
recipientPublicKey,
amount,
fee
);
transferDeploy.sign(senderKeyPair);
const deployHash = await client.putDeploy(transferDeploy);
return deployHash;
}
async function callContractMethod(nodeAddress, privateKey, contractHash, entryPoint, args, chainName = "casper") {
const client = new CasperClient(nodeAddress);
const senderKeyPair = DeployUtil.privateToPublicKey(privateKey);
const deploy = DeployUtil.makeDeploy(
new DeployUtil.DeployParams(senderKeyPair, chainName),
DeployUtil.ExecutableDeployItem.newStoredContractByHash(
Uint8Array.from(Buffer.from(contractHash, 'hex')),
entryPoint,
args
),
DeployUtil.standardPayment(10000)
);
deploy.sign(senderKeyPair);
const deployHash = await client.putDeploy(deploy);
return deployHash;
}
async function requestTestnetTokens(publicKey) {
const faucetUrl = 'https://testnet.cspr.live/tools/faucet';
try {
const response = await axios.post(faucetUrl, {
publicKey: publicKey
});
console.log('Tokens requested successfully:', response.data);
} catch (error) {
console.error('Error requesting tokens:', error);
}
}
module.exports = { generateNewAccount, transferFunds, callContractMethod, requestTestnetTokens };