Skip to content

Commit

Permalink
[ts-sdk] Fix default for coin client transfer and register on default (
Browse files Browse the repository at this point in the history
…#9396)

* [ts-sdk] Fix default for coin client transfer and register on default

* [ts-sdk] Update coin client tests

* [ts-sdk] Fix broken test
  • Loading branch information
gregnazario authored Aug 7, 2023
1 parent a4e81b8 commit 2070c26
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions ecosystem/typescript/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
All notable changes to the Aptos Node SDK will be captured in this file. This changelog is written by hand for now. It adheres to the format set out by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## Unreleased
- Fix default behavior for coin client to transfer and create account by default

## 1.17.0 (2023-08-04)

Expand Down
11 changes: 9 additions & 2 deletions ecosystem/typescript/sdk/src/plugins/coin_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { FungibleAssetClient } from "./fungible_asset_client";
import { Provider } from "../providers";
import { AccountAddress } from "../aptos_types";

export const TRANSFER_COINS = "0x1::aptos_account::transfer_coins";
export const COIN_TRANSFER = "0x1::coin::transfer";
/**
* Class for working with the coin module, such as transferring coins and
* checking balances.
Expand Down Expand Up @@ -86,8 +88,13 @@ export class CoinClient {
const coinTypeToTransfer = extraArgs?.coinType ?? APTOS_COIN;

// If we should create the receiver account if it doesn't exist on-chain,
// use the `0x1::aptos_account::transfer` function.
const func = extraArgs?.createReceiverIfMissing ? "0x1::aptos_account::transfer_coins" : "0x1::coin::transfer";
// use the `0x1::aptos_account::transfer_coins` function.
let func: string;
if (extraArgs?.createReceiverIfMissing === undefined) {
func = TRANSFER_COINS;
} else {
func = extraArgs?.createReceiverIfMissing ? TRANSFER_COINS : COIN_TRANSFER;
}

// Get the receiver address from the AptosAccount or MaybeHexString.
const toAddress = getAddressFromAccountOrAddress(to);
Expand Down
23 changes: 20 additions & 3 deletions ecosystem/typescript/sdk/src/tests/e2e/coin_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import { AptosClient } from "../../providers/aptos_client";
import { getFaucetClient, longTestTimeout, NODE_URL } from "../unit/test_helper.test";
import { AptosAccount } from "../../account/aptos_account";
import { CoinClient } from "../../plugins/coin_client";
import { COIN_TRANSFER, CoinClient, TRANSFER_COINS } from "../../plugins/coin_client";
import { EntryFunctionPayload, Transaction_UserTransaction } from "../../generated";

test(
"transfer and checkBalance works",
Expand All @@ -18,18 +19,34 @@ test(
await faucetClient.fundAccount(alice.address(), 100_000_000);
await faucetClient.fundAccount(bob.address(), 0);

await client.waitForTransaction(await coinClient.transfer(alice, bob, 42), { checkSuccess: true });
const txnHash1 = await coinClient.transfer(alice, bob, 42);
await client.waitForTransaction(txnHash1, { checkSuccess: true });

expect(await coinClient.checkBalance(bob)).toBe(BigInt(42));
let txn1 = (await client.getTransactionByHash(txnHash1)) as Transaction_UserTransaction;
expect((txn1.payload as EntryFunctionPayload).function).toBe(TRANSFER_COINS);

// Test that `createReceiverIfMissing` works.
const jemima = new AptosAccount();
await client.waitForTransaction(await coinClient.transfer(alice, jemima, 717, { createReceiverIfMissing: true }), {
const txnHash2 = await coinClient.transfer(alice, jemima, 717, { createReceiverIfMissing: true });
await client.waitForTransaction(txnHash2, {
checkSuccess: true,
});

// Check that using a string address instead of an account works with `checkBalance`.
expect(await coinClient.checkBalance(jemima.address().hex())).toBe(BigInt(717));
let txn2 = (await client.getTransactionByHash(txnHash2)) as Transaction_UserTransaction;
expect((txn2.payload as EntryFunctionPayload).function).toBe(TRANSFER_COINS);

// Test that `createReceiverIfMissing` works off (has to already be registered
const txnHash3 = await coinClient.transfer(alice, jemima, 1234, { createReceiverIfMissing: false });
await client.waitForTransaction(txnHash3, {
checkSuccess: true,
});

expect(await coinClient.checkBalance(jemima.address().hex())).toBe(BigInt(1951));
let txn3 = (await client.getTransactionByHash(txnHash3)) as Transaction_UserTransaction;
expect((txn3.payload as EntryFunctionPayload).function).toBe(COIN_TRANSFER);
},
longTestTimeout,
);

0 comments on commit 2070c26

Please sign in to comment.