Skip to content

Commit

Permalink
docs: update examples readme for ma example
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Cruz committed Jan 26, 2021
1 parent c37ed17 commit a103108
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
19 changes: 19 additions & 0 deletions examples/README.md
Expand Up @@ -76,4 +76,23 @@ In order to execute the example, run:

```javascript
$ yarn withdrawal-example
```

## Sending transactions with single multi assets

Sends a transaction that spends a single MultiAsset token. So:

1. A predefined address `PAYMENT_ADDRESS` expects a specific token
2. Sends `total token amount` to another predefined address `SEND_FUNDS_ADDRESS`
3. All the ADA is also sent to that address

Important notes:
- `EXPECTED_TOKEN` will be used to define the token expected. It requires policy and symbol both defined as hex string.
- Running this example requieres a sender that owns tokens (for example by minting) and can send them to the rosetta-cli generated address. It's strongly recommended to use [this helper scripts](https://github.com/james-iohk/scripts)
- Funds will be sent to `SEND_FUNDS_ADDRESS` by default as it's the address where testnet funds should be returned.

In order to execute the example, run:

```javascript
$ yarn ma-transfer-example
```
26 changes: 22 additions & 4 deletions examples/ma-transfer-example.ts
Expand Up @@ -18,6 +18,11 @@ const logger = console;
const PAYMENT_ADDRESS =
"addr_test1vpcv26kdu8hr9x939zktp275xhwz4478c8hcdt7l8wrl0ecjftnfa";

const EXPECTED_TOKEN = {
policy: "3e6fc736d30770b830db70994f25111c18987f1407585c0f55ca470f",
symbol: "6a78546f6b656e31",
};

const PAYMENT_KEYS = {
secretKey: Buffer.from(
"67b638cef68135c4005cb71782b070c4805c9e1077c7ab6145b152206073272974dabdc594506574a9b58f719787d36ea1af291d141d3e5e5ccfe076909ae106",
Expand All @@ -35,11 +40,21 @@ const SEND_FUNDS_ADDRESS =
const doRun = async (): Promise<void> => {
const keyAddressMapper = {};
keyAddressMapper[PAYMENT_ADDRESS] = PAYMENT_KEYS;
const unspents = await waitForBalanceToBe(

const responseCondition = (response) =>
response.coins.length !== 0 &&
response.balances.some(
(balance) =>
balance.currency.symbol === EXPECTED_TOKEN.symbol &&
balance.currency?.metadata?.policyId === EXPECTED_TOKEN.policy
);

const unspents = await waitForBalanceToBe(PAYMENT_ADDRESS, responseCondition);
const builtOperations = buildOperation(
unspents,
PAYMENT_ADDRESS,
(response) => response.coins.length !== 0
SEND_FUNDS_ADDRESS
);
const builtOperations = buildOperation(unspents, PAYMENT_ADDRESS, SEND_FUNDS_ADDRESS);
const preprocess = await constructionPreprocess(
builtOperations.operations,
1000
Expand All @@ -59,7 +74,10 @@ const doRun = async (): Promise<void> => {
logger.info(
`[doRun] transaction with hash ${hashResponse.transaction_identifier.hash} sent`
);
await waitForBalanceToBe(PAYMENT_ADDRESS, (response) => response.coins.length === 0);
await waitForBalanceToBe(
PAYMENT_ADDRESS,
(response) => response.coins.length === 0
);
};

doRun()
Expand Down

0 comments on commit a103108

Please sign in to comment.