-
Notifications
You must be signed in to change notification settings - Fork 21
/
transfer.ts
84 lines (72 loc) · 2.88 KB
/
transfer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import type { SubstrateAssetTransferRequest } from "@buildwithsygma/substrate";
import { createSubstrateFungibleAssetTransfer } from "@buildwithsygma/substrate";
import { ApiPromise, WsProvider } from "@polkadot/api";
import { Keyring } from "@polkadot/keyring";
import { cryptoWaitReady } from "@polkadot/util-crypto";
import dotenv from "dotenv";
dotenv.config();
const MNEMONIC = process.env.PRIVATE_MNEMONIC;
if (!MNEMONIC) {
throw new Error("Missing environment variable: PRIVATE_MNEMONIC");
}
const TANGLE_CHAIN_ID = 3799;
const SEPOLIA_CHAIN_ID = 11155111;
const RECIPIENT_ADDRESS =
process.env.RECIPIENT_ADDRESS || "0xE39bb23F17a2cf7C9a8C4918376A32036A8867db";
const RESOURCE_ID =
"0x0000000000000000000000000000000000000000000000000000000000002000";
const SYGMA_EXPLORER_URL = "https://scan.test.buildwithsygma.com";
const TANGLE_RPC_URL =
process.env.SOURCE_SUBSTRATE_RPC_URL ?? "wss://rpc.tangle.tools";
const getSygmaExplorerTransferUrl = (params: {
blockNumber: number;
extrinsicIndex: number;
}): string =>
`${SYGMA_EXPLORER_URL}/transfer/${params.blockNumber}-${params.extrinsicIndex}`;
const substrateTransfer = async (): Promise<void> => {
// Make sure to account with native tokens
const keyring = new Keyring({ type: "sr25519" });
await cryptoWaitReady();
const account = keyring.addFromUri(MNEMONIC);
const wsProvider = new WsProvider(TANGLE_RPC_URL);
const api = await ApiPromise.create({ provider: wsProvider });
const transferParams: SubstrateAssetTransferRequest = {
source: TANGLE_CHAIN_ID,
destination: SEPOLIA_CHAIN_ID,
sourceNetworkProvider: api,
sourceAddress: account.address,
resource: RESOURCE_ID,
amount: BigInt(1) * BigInt(1e18),
recipientAddress: RECIPIENT_ADDRESS,
environment: process.env.SYGMA_ENV,
};
const transfer = await createSubstrateFungibleAssetTransfer(transferParams);
const transferTx = await transfer.getTransferTransaction();
const unsub = await transferTx.signAndSend(account, (results) => {
const { status } = results;
console.log(`Current status is ${status.toString()}`);
if (status.isInBlock) {
console.log(
`Transaction included at blockHash ${status.asInBlock.toString()}`
);
} else if (status.isFinalized) {
const blockNumber = results.blockNumber?.toNumber();
const extrinsicIndex = results.txIndex;
if (blockNumber && extrinsicIndex) {
console.log(
`Transaction finalized at blockHash ${status.asFinalized.toString()}`
);
console.log(
`Explorer URL: ${getSygmaExplorerTransferUrl({ blockNumber, extrinsicIndex })}`
);
}
unsub();
process.exit(0);
} else if (status.isDropped || status.isInvalid || status.isUsurped) {
console.error("Transaction failed. Status:", status.toString());
}
});
};
substrateTransfer()
.catch((e) => console.log(e))
.finally(() => {});