Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nodejs send wrapper methods #1103

Merged
merged 8 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bindings/nodejs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Security -->

## 1.1.0 - 2023-MM-DD

### Added

- `Account::{burn(), consolidateOutputs(), createAliasOutput(), meltNativeToken(), mintNativeToken(), createNativeToken(), mintNfts(), transaction(), sendNativeTokens(), sendNft()}` methods;
Thoralf-M marked this conversation as resolved.
Show resolved Hide resolved

## 1.0.7 - 2023-08-29

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ async function run() {

// Consolidate unspent outputs and print the consolidation transaction ID
// Set `force` to true to force the consolidation even though the `output_threshold` isn't reached
const preparedTransaction = await account.prepareConsolidateOutputs({
const transaction = await account.consolidateOutputs({
force: true,
});
const transaction = await preparedTransaction.send();
console.log('Transaction sent: %s', transaction.transactionId);

// Wait for the consolidation transaction to get confirmed
Expand Down
4 changes: 1 addition & 3 deletions bindings/nodejs/examples/how_tos/alias/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ async function run() {
console.log('Sending the create-alias transaction...');

// Create an alias
const transaction = await account
.prepareCreateAliasOutput()
.then((prepared) => prepared.send());
const transaction = await account.createAliasOutput();

console.log(`Transaction sent: ${transaction.transactionId}`);

Expand Down
3 changes: 1 addition & 2 deletions bindings/nodejs/examples/how_tos/native_tokens/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ async function run() {
foundryMetadata: utf8ToHex('Hello, World!'),
};

const prepared = await account.prepareCreateNativeToken(params);
const transaction = await prepared.send();
const transaction = await account.createNativeToken(params);

console.log(`Transaction sent: ${transaction.transactionId}`);

Expand Down
7 changes: 4 additions & 3 deletions bindings/nodejs/examples/how_tos/native_tokens/melt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ async function run() {
console.log(`Balance before melting: ${token.available}`);

// Melt some of the circulating supply
const transaction = await account
.prepareMeltNativeToken(token.tokenId, MELT_AMOUNT)
.then((prepared) => prepared.send());
const transaction = await account.meltNativeToken(
token.tokenId,
MELT_AMOUNT,
);

console.log(`Transaction sent: ${transaction.transactionId}`);

Expand Down
4 changes: 1 addition & 3 deletions bindings/nodejs/examples/how_tos/native_tokens/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ async function run() {
}
console.log(`Balance before sending: ${token.available}`);

const transaction = await account
.prepareSendNativeTokens(outputs)
.then((prepared) => prepared.send());
const transaction = await account.sendNativeTokens(outputs);

console.log(`Transaction sent: ${transaction.transactionId}`);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ async function run() {
'This NFT will be the issuer from the awesome NFT collection',
),
};
const prepared = await account.prepareMintNfts([params]);

const transaction = await prepared.send();
const transaction = await account.mintNfts([params]);

// Wait for transaction to get included
const blockId = await account.retryTransactionUntilIncluded(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ async function run() {
i + chunk.length
}/${NFT_COLLECTION_SIZE})`,
);
const prepared = await account.prepareMintNfts(chunk);
const transaction = await prepared.send();
const transaction = await account.mintNfts(chunk);

// Wait for transaction to get included
const blockId = await account.retryTransactionUntilIncluded(
Expand Down
4 changes: 1 addition & 3 deletions bindings/nodejs/examples/how_tos/nfts/mint_nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ async function run() {
issuer: senderAddress,
immutableMetadata: NFT1_IMMUTABLE_METADATA,
};
const prepared = await account.prepareMintNfts([params]);

let transaction = await prepared.send();
let transaction = await account.mintNfts([params]);
console.log(`Transaction sent: ${transaction.transactionId}`);

// Wait for transaction to get included
Expand Down
4 changes: 1 addition & 3 deletions bindings/nodejs/examples/how_tos/nfts/send_nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ async function run() {
];

// Send the full NFT output to the specified address
const transaction = await account
.prepareSendNft(outputs)
.then((prepared) => prepared.send());
const transaction = await account.sendNft(outputs);

console.log(`Transaction sent: ${transaction.transactionId}`);

Expand Down
170 changes: 170 additions & 0 deletions bindings/nodejs/lib/wallet/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@ export class Account {
return Output.parse(JSON.parse(response).payload) as NftOutput;
}

/**
* A generic `burn()` function that can be used to prepare to burn native tokens, nfts, foundries and aliases.
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
* @param burn The outputs to burn
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
* @param transactionOptions The options to define a `RemainderValueStrategy`
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
* or custom inputs.
* @returns The transaction.
*/
async burn(
burn: Burn,
transactionOptions?: TransactionOptions,
): Promise<Transaction> {
return (await this.prepareBurn(burn, transactionOptions)).send();
}

/**
* A generic `burn()` function that can be used to prepare to burn native tokens, nfts, foundries and aliases.
* @param burn The outputs to burn
Expand Down Expand Up @@ -181,6 +195,7 @@ export class Account {
this,
);
}

/**
* Burn an nft output.
* @param nftId The NftId.
Expand Down Expand Up @@ -233,6 +248,20 @@ export class Account {
return plainToInstance(Transaction, parsed.payload);
}

/**
* Consolidate basic outputs with only an `AddressUnlockCondition` from an account
* by sending them to an own address again if the output amount is greater or
* equal to the output consolidation threshold.
* @param force Force consolidation on addresses where the threshold isn't met.
* @param outputConsolidationThreshold A default threshold is used if this is omitted.
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
* @returns The consolidation transaction.
*/
async consolidateOutputs(
params: ConsolidationParams,
): Promise<Transaction> {
return (await this.prepareConsolidateOutputs(params)).send();
}

/**
* Consolidate basic outputs with only an `AddressUnlockCondition` from an account
* by sending them to an own address again if the output amount is greater or
Expand Down Expand Up @@ -262,6 +291,22 @@ export class Account {
);
}

/**
* `createAliasOutput` creates an alias output
Thoralf-M marked this conversation as resolved.
Show resolved Hide resolved
* @param params The alias output options.
* @param transactionOptions The options to define a `RemainderValueStrategy`
* or custom inputs.
* @returns The transaction.
*/
async createAliasOutput(
params?: AliasOutputParams,
transactionOptions?: TransactionOptions,
): Promise<Transaction> {
return (
await this.prepareCreateAliasOutput(params, transactionOptions)
).send();
}

/**
* `createAliasOutput` creates an alias output
* @param params The alias output options.
Expand Down Expand Up @@ -292,6 +337,29 @@ export class Account {
);
}

/**
* Melt native tokens. This happens with the foundry output which minted them, by increasing its
* `melted_tokens` field.
* @param tokenId The native token id.
* @param meltAmount To be melted amount.
* @param transactionOptions The options to define a `RemainderValueStrategy`
* or custom inputs.
* @returns The transaction.
*/
async meltNativeToken(
tokenId: string,
meltAmount: bigint,
transactionOptions?: TransactionOptions,
): Promise<Transaction> {
return (
await this.prepareMeltNativeToken(
tokenId,
meltAmount,
transactionOptions,
)
).send();
}

/**
* Melt native tokens. This happens with the foundry output which minted them, by increasing its
* `melted_tokens` field.
Expand Down Expand Up @@ -790,6 +858,29 @@ export class Account {
};
}

/**
* Mint additional native tokens.
*
* @param tokenId The native token id.
* @param mintAmount To be minted amount.
* @param transactionOptions The options to define a `RemainderValueStrategy`
* or custom inputs.
* @returns The minting transaction.
*/
async mintNativeToken(
tokenId: string,
mintAmount: bigint,
transactionOptions?: TransactionOptions,
): Promise<Transaction> {
return (
await this.prepareMintNativeToken(
tokenId,
mintAmount,
transactionOptions,
)
).send();
}

/**
* Mint additional native tokens.
*
Expand Down Expand Up @@ -825,6 +916,23 @@ export class Account {
);
}

/**
* Create a native token.
*
* @param params The options for creating a native token.
* @param transactionOptions The options to define a `RemainderValueStrategy`
* or custom inputs.
* @returns The creating transaction and the token ID.
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
*/
async createNativeToken(
params: CreateNativeTokenParams,
transactionOptions?: TransactionOptions,
): Promise<Transaction> {
return (
await this.prepareCreateNativeToken(params, transactionOptions)
).send();
}

/**
* Create a native token.
*
Expand Down Expand Up @@ -866,6 +974,21 @@ export class Account {
);
}

/**
* Mint NFTs.
*
* @param params The options for minting nfts.
* @param transactionOptions The options to define a `RemainderValueStrategy`
* or custom inputs.
* @returns The minting transaction.
*/
async mintNfts(
params: MintNftParams[],
transactionOptions?: TransactionOptions,
): Promise<Transaction> {
return (await this.prepareMintNfts(params, transactionOptions)).send();
}

/**
* Mint NFTs.
*
Expand Down Expand Up @@ -970,6 +1093,21 @@ export class Account {
);
}

/**
* Send a transaction.
*
* @param outputs Outputs to use in the transaction.
* @param options The options to define a `RemainderValueStrategy`
* or custom inputs.
* @returns The transaction data.
*/
async transaction(
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
outputs: Output[],
options?: TransactionOptions,
): Promise<Transaction> {
return (await this.prepareTransaction(outputs, options)).send();
}

/**
* Prepare a transaction, useful for offline signing.
*
Expand Down Expand Up @@ -1108,6 +1246,23 @@ export class Account {
return plainToInstance(Transaction, parsed.payload);
}

/**
* Send native tokens.
*
* @param params Addresses amounts and native tokens.
* @param transactionOptions The options to define a `RemainderValueStrategy`
* or custom inputs.
* @returns The transaction.
*/
async sendNativeTokens(
params: SendNativeTokensParams[],
transactionOptions?: TransactionOptions,
): Promise<Transaction> {
return (
await this.prepareSendNativeTokens(params, transactionOptions)
).send();
}

/**
* Send native tokens.
*
Expand Down Expand Up @@ -1139,6 +1294,21 @@ export class Account {
);
}

/**
* Send NFT.
*
* @param params Addresses and nft ids.
* @param transactionOptions The options to define a `RemainderValueStrategy`
* or custom inputs.
* @returns The transaction.
*/
async sendNft(
params: SendNftParams[],
transactionOptions?: TransactionOptions,
): Promise<Transaction> {
return (await this.prepareSendNft(params, transactionOptions)).send();
}

/**
* Send NFT.
*
Expand Down
Loading