Skip to content

Commit

Permalink
upgrade to erdjs 10.2.5
Browse files Browse the repository at this point in the history
  • Loading branch information
razvanstatescu committed Jun 5, 2022
1 parent 8a32203 commit 975a0f5
Show file tree
Hide file tree
Showing 10 changed files with 483 additions and 4,926 deletions.
Empty file added CHANGELOG.md
Empty file.
4,785 changes: 157 additions & 4,628 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
"typescript": "^4.7.3"
},
"dependencies": {
"@elrondnetwork/erdjs": "^9.0.3",
"@elrondnetwork/erdjs": "^10.2.5",
"@elrondnetwork/erdjs-network-providers": "^0.1.5",
"@elrondnetwork/erdjs-walletcore": "^1.0.0",
"axios": "^0.27.2",
"chalk": "4.1.2",
"figlet": "^1.5.2",
Expand Down
96 changes: 96 additions & 0 deletions src/commands/issue-token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import {
Address,
BytesValue,
ContractFunction,
TokenPayment,
Transaction,
TransactionPayload,
TransactionWatcher,
} from '@elrondnetwork/erdjs/out';
import prompts, { PromptObject } from 'prompts';

import { chain, chainId, elrondExplorer, issueTokenScAddress, pemKeyFileName } from '../config';
import { publicEndpointSetup, saveConfigToFile } from '../utils';

export const issueToken = async () => {
const promptQuestions: PromptObject[] = [
{
type: "text",
name: "tokenName",
message: "Token name",
validate: (value) => {
if (!value) return "Required!";
if (value.length > 20 || value.length < 3) {
return "Length between 3 and 20 characters!";
}
if (!new RegExp(/^[a-zA-Z0-9]+$/).test(value)) {
return "Alphanumeric characters only!";
}
return true;
},
},
{
type: "text",
name: "tokenTicker",
message: "Token ticker",
validate: (value) => {
if (!value) return "Required!";
if (value.length > 10 || value.length < 3) {
return "Length between 3 and 10 characters!";
}
if (!new RegExp(/^[A-Z0-9]+$/).test(value)) {
return "Alphanumeric UPPERCASE only!";
}
return true;
},
},
];

try {
const { tokenName, tokenTicker } = await prompts(promptQuestions);

let payload = TransactionPayload.contractCall()
.setFunction(new ContractFunction("issueSemiFungible"))
.addArg(new BytesValue(Buffer.from(tokenName, "utf-8")))
.addArg(new BytesValue(Buffer.from(tokenTicker, "utf-8")));

let transaction = new Transaction({
data: payload.build(),
receiver: new Address(issueTokenScAddress),
value: TokenPayment.egldFromAmount(0.05),
gasLimit: 60000000,
chainID: chainId[chain],
});

const { signer, provider, userAccount } = await publicEndpointSetup(pemKeyFileName);

console.log("Sending transaction...");

transaction.setNonce(userAccount.nonce);
userAccount.incrementNonce();

await signer.sign(transaction);

await provider.sendTransaction(transaction);

const watcher = new TransactionWatcher(provider);
const transactionOnNetwork = await watcher.awaitCompleted(transaction);

const txHash = transactionOnNetwork.hash;
const txStatus = transactionOnNetwork.status;

console.log(`Issue token transaction (${txStatus}) ${elrondExplorer[chain]}/transactions/${txHash}`);

if (transactionOnNetwork.status.isExecuted()) {
const payload = transactionOnNetwork.contractResults?.items?.[0]?.data;
const payloadParts = payload?.split("@");
const tokenIdentifier = Buffer.from(payloadParts[payloadParts.length - 1], "hex").toString("utf-8");
saveConfigToFile({
tokenIdentifier,
});
console.log(`Token identifier ${tokenIdentifier}`);
}
} catch (e) {
console.log((e as Error)?.message);
}
};
154 changes: 154 additions & 0 deletions src/commands/mint-sft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import {
BytesValue,
ContractFunction,
TokenPayment,
Transaction,
TransactionPayload,
TransactionWatcher,
U32Value,
} from '@elrondnetwork/erdjs/out';
import prompts, { PromptObject } from 'prompts';

import { chain, chainId, elrondExplorer, pemKeyFileName } from '../config';
import { loadConfigFromFile, publicEndpointSetup } from '../utils';

export const mintSft = async () => {
const config = loadConfigFromFile();
if (!config.tokenIdentifier) {
console.log('Token identifier not found. Please run "issue-token" command first.');
return;
}

const tokenIdentifier = config.tokenIdentifier;

const promptQuestions: PromptObject[] = [
{
type: "number",
name: "qunatity",
message: "SFT Quantity",
min: 1,
validate: (value) => {
if (!value) return "Required!";
if (value < 1) {
return "Quantity must be at least 1";
}
return true;
},
},
{
type: "text",
name: "sftName",
message: "SFT Name",
validate: (value) => {
if (!value) return "Required!";
if (value.length > 20 || value.length < 1) {
return "Length between 1 and 20 characters!";
}
if (!new RegExp(/^[a-zA-Z0-9 ]+$/).test(value)) {
return "Alphanumeric characters & spaces only!";
}
return true;
},
},
{
type: "number",
name: "royalties",
message: "SFT Royalties",
min: 0,
max: 100,
validate: (value) => {
if (!value) return "Required!";
if (value < 0 || value > 100) {
return "Royalties must be between 0 and 100";
}
return true;
},
},
{
type: "text",
name: "metadataCid",
message: "Metadata CID",
validate: (value) => {
if (!value) return "Required!";
if (value.length !== 59) {
return "The length of a CID is 59 characters";
}
if (!new RegExp(/^[a-z0-9,]+$/).test(value)) {
return "Invalid CID!";
}
return true;
},
},
{
type: "text",
name: "tags",
message: "SFT Tags (comma separated)",
validate: (value) => {
if (!value) return "Required!";
if (!new RegExp(/^[a-z0-9,]+$/).test(value)) {
return "Alphanumeric characters & commas only!";
}
return true;
},
},
{
type: "text",
name: "imageCid",
message: "Image CID",
validate: (value) => {
if (!value) return "Required!";
if (value.length !== 59) {
return "The length of a CID is 59 characters";
}
if (!new RegExp(/^[a-z0-9]+$/).test(value)) {
return "Invalid CID!";
}
return true;
},
},
];

try {
const { qunatity, sftName, royalties, metadataCid, tags, imageCid } = await prompts(promptQuestions);

const { signer, provider, userAccount } = await publicEndpointSetup(pemKeyFileName);

console.log("Sending transaction...");

let payload = TransactionPayload.contractCall()
.setFunction(new ContractFunction("ESDTNFTCreate"))
.addArg(new BytesValue(Buffer.from(tokenIdentifier, "utf-8")))
.addArg(new U32Value(qunatity))
.addArg(new BytesValue(Buffer.from(sftName, "utf-8")))
.addArg(new U32Value(royalties * 100))
.addArg(new U32Value(0))
.addArg(new BytesValue(Buffer.from(`metadata:${metadataCid};tags:${tags}`, "utf-8")))
.addArg(new BytesValue(Buffer.from(`https://ipfs.io/ipfs/${imageCid}`, "utf-8")))
.addArg(new BytesValue(Buffer.from(`https://ipfs.io/ipfs/${metadataCid}`, "utf-8")));

let transaction = new Transaction({
data: payload.build(),
receiver: userAccount.address,
value: TokenPayment.egldFromAmount(0),
gasLimit: 60000000,
chainID: chainId[chain],
});

transaction.setNonce(userAccount.nonce);
userAccount.incrementNonce();

await signer.sign(transaction);

await provider.sendTransaction(transaction);

const watcher = new TransactionWatcher(provider);
const transactionOnNetwork = await watcher.awaitCompleted(transaction);

const txHash = transactionOnNetwork.hash;
const txStatus = transactionOnNetwork.status;

console.log(`SFT Mint Transaction (${txStatus}) ${elrondExplorer[chain]}/transactions/${txHash}`);
} catch (e) {
console.log((e as Error)?.message);
}
};
62 changes: 62 additions & 0 deletions src/commands/set-roles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {
Address,
AddressValue,
BytesValue,
ContractFunction,
TokenPayment,
Transaction,
TransactionPayload,
TransactionWatcher,
} from '@elrondnetwork/erdjs/out';

import { chain, chainId, elrondExplorer, issueTokenScAddress, pemKeyFileName } from '../config';
import { loadConfigFromFile, publicEndpointSetup } from '../utils';

export const setRoles = async () => {
const config = loadConfigFromFile();
if (!config.tokenIdentifier) {
console.log('Token identifier not found. Please run "issue-token" command first.');
return;
}

const tokenIdentifier = config.tokenIdentifier;

try {
const { signer, provider, userAccount } = await publicEndpointSetup(pemKeyFileName);

console.log("Sending transaction...");

let payload = TransactionPayload.contractCall()
.setFunction(new ContractFunction("setSpecialRole"))
.addArg(new BytesValue(Buffer.from(tokenIdentifier, "utf-8")))
.addArg(new AddressValue(userAccount.address))
.addArg(new BytesValue(Buffer.from("ESDTRoleNFTCreate", "utf-8")))
.addArg(new BytesValue(Buffer.from("ESDTRoleNFTBurn", "utf-8")))
.addArg(new BytesValue(Buffer.from("ESDTRoleNFTAddQuantity", "utf-8")));

let transaction = new Transaction({
data: payload.build(),
receiver: new Address(issueTokenScAddress),
value: TokenPayment.egldFromAmount(0),
gasLimit: 60000000,
chainID: chainId[chain],
});

transaction.setNonce(userAccount.nonce);
userAccount.incrementNonce();

await signer.sign(transaction);

await provider.sendTransaction(transaction);

const watcher = new TransactionWatcher(provider);
const transactionOnNetwork = await watcher.awaitCompleted(transaction);

const txHash = transactionOnNetwork.hash;
const txStatus = transactionOnNetwork.status;

console.log(`Set Roles Transaction (${txStatus}) ${elrondExplorer[chain]}/transactions/${txHash}`);
} catch (e) {
console.log((e as Error)?.message);
}
};
8 changes: 0 additions & 8 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,3 @@ export const chainId: { [key: string]: string } = {
};

export const issueTokenScAddress = "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u";

export const getConfig = () => {
return {
chain,
proxyGateway: proxyGateways[chain],
explorer: elrondExplorer[chain],
};
};
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import chalk from 'chalk';
import figlet from 'figlet';
import { argv, exit } from 'process';

import { issueToken } from './commands/issue-token';
import { mintSft } from './commands/mint-sft';
import { setRoles } from './commands/set-roles';
import { chain } from './config';
import { issueToken, mintSft, setRoles } from './issue-sft';
import packageJson from './packageCopy.json';

const COMMANDS = {
Expand Down
Loading

0 comments on commit 975a0f5

Please sign in to comment.