forked from ar-io/testnet-contract
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buy-arns-name.ts
74 lines (63 loc) · 2.14 KB
/
buy-arns-name.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import * as fs from 'fs';
import { keyfile } from './constants';
import { getContractManifest, initialize, warp } from './utilities';
/* eslint-disable no-console */
(async () => {
// simple setup script
initialize();
//~~~~~~~~~~~~~~~~~~~~~~~~~~UPDATE THE BELOW~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// This is the name that will be purchased in the Arweave Name System Registry
const nameToBuy = process.env.ARNS_NAME ?? 'a-test-name';
// This is the ANT Smartweave Contract TX ID that will be added to the registry. It must follow the ArNS ANT Specification
const contractTxId =
process.env.ANT_CONTRACT_TX_ID ??
'gh673M0Koh941OIITVXl9hKabRaYWABQUedZxW-swIA';
// The lease time for purchasing the name
const years = 1;
// load local wallet
const wallet = JSON.parse(
process.env.JWK ? process.env.JWK : fs.readFileSync(keyfile).toString(),
);
// load state of contract
const arnsContractTxId =
process.env.ARNS_CONTRACT_TX_ID ??
'E-pRI1bokGWQBqHnbut9rsHSt9Ypbldos3bAtwg4JMc';
// get contract manifest
const { evaluationOptions = {} } = await getContractManifest({
contractTxId: arnsContractTxId,
});
// Read the ANT Registry Contract
const contract = warp
.pst(arnsContractTxId)
.setEvaluationOptions(evaluationOptions);
contract.connect(wallet);
// check if this name exists in the registry, if not exit the script.
const currentState = await contract.readState();
const currentStateString = JSON.stringify(currentState);
const currentStateJSON = JSON.parse(currentStateString);
if (currentStateJSON.records[nameToBuy] !== undefined) {
console.log(
'This name %s is already taken and is not available for purchase. Exiting.',
nameToBuy,
);
return;
}
// Buy the available record in ArNS Registry
console.log(
'Buying the record, %s using the ANT %s',
nameToBuy,
contractTxId,
);
const recordTxId = await contract.writeInteraction(
{
function: 'buyRecord',
name: nameToBuy,
contractTxId,
years,
},
{
disableBundling: true,
},
);
console.log('Finished purchasing the record: %s', recordTxId);
})();