-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (30 loc) · 1.69 KB
/
index.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
import { ErgoAddress, OutputBuilder, SColl, SByte, SConstant, SLong, SSigmaProp, TransactionBuilder } from "@fleet-sdk/core";
const DEFAULT_EXPLORER_URL = "https://api-testnet.ergoplatform.com";
const ERGONAMES_PROXY_ADDRESS = "34sQ9WYJ67tfGss9Za6jpiM6s1F9QAV2GR2Cvc3N5zJFVTuqTo68R9tBktFuwTZ7C67QX8xutEsV7Rsn8wUZno67EcRAf9hJ8wKZr6NDseFK6J3aCk76i3VWD3uqrnwqkQX1hEK8MVu1uXhoizUuKkuNQtKwYeMUK6yG3B3id4SoeQzgx24pqJEgw6cTjrNirjib7ossgbk3WDMCGpQt6htYDZWcmBPALxaxjrZWBRBsdNeVWFZckZ";
export async function sendTransaction(name, ergonamePrice, receiverAddress, explorerUrl = DEFAULT_EXPLORER_URL) {
let currentHeight = await getCurrentHeight(explorerUrl);
let amountToSend = ergonamePrice + (1000000 * 2);
let inputs = await ergo.get_utxos(amountToSend);
let receiverErgoAddress = ErgoAddress.fromBase58(String(receiverAddress));
let receiverErgoTree = receiverErgoAddress.ergoTree;
const unsignedTransaction = new TransactionBuilder(currentHeight)
.from(inputs)
.to(new OutputBuilder(amountToSend, ERGONAMES_PROXY_ADDRESS)
.setAdditionalRegisters({
R4: SConstant(SColl(SByte, Buffer.from(name, "utf-8"))).toString("hex"),
R5: receiverErgoTree,
})
)
.sendChangeTo(receiverAddress)
.payMinFee()
.build("EIP-12");
let signedTransaction = await ergo.sign_tx(unsignedTransaction);
let txInfo = await ergo.submit_tx(signedTransaction);
return { txId: txInfo };
}
async function getCurrentHeight(explorerUrl = DEFAULT_EXPLORER_URL) {
let url = `${explorerUrl}/api/v1/blocks?limit=1`;
let response = await fetch(url);
let json = await response.json();
return json.total;
}