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

Updated nem2-sdk 0.16.3 dependency #323

Merged
merged 6 commits into from
Jan 11, 2020
Merged

Updated nem2-sdk 0.16.3 dependency #323

merged 6 commits into from
Jan 11, 2020

Conversation

dgarcia360
Copy link
Collaborator

No description provided.

@fboucquez
Copy link
Contributor

fboucquez commented Jan 11, 2020

Hi @dgarcia360

RepositoryFactory knows how to load and cache networkType and generationHash. What about doing something like this:

const sendintATransferTransaction = async () => {

    /* Start block 00*/
    const nodeUrl = 'http://api-harvest-20.us-west-1.nemtech.network:3000';
    const repositoryFactory = new RepositoryFactoryHttp(nodeUrl);
    /* end block 00 */

    /* start block 01 */
// replace with recipient address
    const rawAddress = 'TBONKW-COWBZY-ZB2I5J-D3LSDB-QVBYHB-757VN3-SKPP';
    const recipientAddress = Address.createFromRawAddress(rawAddress);
    const networkType = await repositoryFactory.getNetworkType().toPromise();
// replace with nem.xem id
    const networkCurrencyMosaicId = new MosaicId('75AF035421401EF0');
// replace with network currency divisibility
    const networkCurrencyDivisibility = 6;

    const transferTransaction = TransferTransaction.create(
        Deadline.create(),
        recipientAddress,
        [new Mosaic(networkCurrencyMosaicId,
            UInt64.fromUint(10 * Math.pow(10, networkCurrencyDivisibility)))],
        PlainMessage.create('This is a test message'),
        networkType,
        UInt64.fromUint(2000000));
    /* end block 01 */

    /* start block 02 */
// replace with sender private key
    const privateKey = '1111111111111111111111111111111111111111111111111111111111111111';
    const account = Account.createFromPrivateKey(privateKey, networkType);
    const networkGenerationHash = await repositoryFactory.getGenerationHash().toPromise();
    const signedTransaction = account.sign(transferTransaction, networkGenerationHash);
    /* end block 02 */

    /* start block 03 */
// replace with node endpoint
    const transactionHttp = repositoryFactory.createTransactionRepository();

    transactionHttp
        .announce(signedTransaction)
        .subscribe((x) => console.log(x), (err) => console.error(err));
    /* end block 03 */
}

Please note the:

  • async await function to simplify the examples. The alternative is to use nested observables, I reckon this is simpler when showing a how-to.
  • const networkType = await repositoryFactory.getNetworkType().toPromise();
  • const networkGenerationHash =await repositoryFactory.getGenerationHash().toPromise();

Both values are lazy and cached. They are loaded only and only the first time the get methods are called.

The same example will work for any network type and any generation hash without manually copying configuration. Two less replace with statements

@dgarcia360
Copy link
Collaborator Author

@fboucquez Thanks for the code example, have opened a new issue to add this improvement in the next release 👍

@dgarcia360 dgarcia360 merged commit fec4e21 into master Jan 11, 2020
@dgarcia360 dgarcia360 deleted the nem2-sdk-0.16.3 branch January 11, 2020 23:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants