-
Notifications
You must be signed in to change notification settings - Fork 10
/
create_ibc_transfer.ts
96 lines (84 loc) · 3.62 KB
/
create_ibc_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
85
86
87
88
89
90
91
92
93
94
95
96
import { DirectSecp256k1Wallet, DirectSignResponse, OfflineDirectSigner } from '@cosmjs/proto-signing';
import { SigningStargateClient } from "@cosmjs/stargate";
import { BigNumber } from "bignumber.js";
import * as BIP39 from "bip39";
import { AddressUtils, CarbonSDK, CarbonTx, CarbonWallet, IBCUtils } from "./_sdk";
import { SignDoc } from '../lib/codec/cosmos/tx/v1beta1/tx';
import { registry } from "../lib/codec/index";
import { ChainIds, IBCAddress, swthChannels } from "../lib/constant";
import "./_setup";
import { Models } from '../lib';
const network = ChainIds.Osmosis;
const networkObj = IBCUtils.EmbedChainInfos[network];
const swthDenom = "ibc/8FEFAE6AECF6E2A255585617F781F35A8D5709A545A804482A261C0C9548A9D3";
(async () => {
const mnemonics = process.env.MNEMONICS ?? BIP39.generateMnemonic();
console.log("mnemonics", mnemonics);
const sdk = await CarbonSDK.instance({
network: CarbonSDK.Network.DevNet,
});
const connectedSDK = await sdk.connectWithMnemonic(mnemonics);
console.log("connected sdk");
const swthToken = await sdk.token.tokenForId(swthDenom); // swth on osmo blockchain
if (!swthToken) return;
const channelObj = swthChannels[network];
if (!channelObj) return;
const tokenDecimals = swthToken.decimals.toNumber() ?? 0;
const counterAddressBytes = IBCAddress.getAddressBytes(sdk?.wallet?.bech32Address ?? "");
const counterAddr = IBCAddress.deriveAddressFromBytes(counterAddressBytes, networkObj.bech32Config.bech32PrefixAccAddr)
// Withdrawal
const withdrawResponse = await connectedSDK.ibc.sendIBCTransfer({
sourcePort: "transfer",
sourceChannel: channelObj.sourceChannel ?? "channel-0", // channel of receiving blockchain
denom: "swth",
amount: new BigNumber(50).shiftedBy(tokenDecimals),
sender: sdk?.wallet?.bech32Address ?? "", // address to send from
receiver: counterAddr, // address to send to
});
console.log("withdrawal response", withdrawResponse);
const walletPrivateKey = AddressUtils.SWTHAddress.mnemonicToPrivateKey(mnemonics);
const counterWallet = await DirectSecp256k1Wallet.fromKey(walletPrivateKey, networkObj.bech32Config.bech32PrefixAccAddr);
const counterSigner: OfflineDirectSigner = {
getAccounts() {
return counterWallet.getAccounts();
},
async signDirect(signerAddress: string, signDoc: SignDoc): Promise<DirectSignResponse> {
return await counterWallet.signDirect(signerAddress, signDoc);
},
}
if (!networkObj.tmRpc) {
throw new Error('No rpc url provided in network obj')
}
const signingClient = await SigningStargateClient.connectWithSigner(
networkObj.tmRpc,
counterSigner,
{ registry },
);
const lastHeight: number = await sdk.query.chain.getHeight();
// Deposit
const txBody = await Models.IBC.TranferV1.MsgTransfer.fromPartial({
sourcePort: "transfer",
sourceChannel: channelObj.dstChannel ?? "channel-0",
token: {
denom: swthToken.denom,
amount: new BigNumber(50).shiftedBy(tokenDecimals).toString(10),
},
sender: counterAddr, // address to send from
receiver: sdk?.wallet?.bech32Address ?? "", // address to send to
timeoutHeight: {
revisionHeight: (lastHeight ?? 0) + 150,
revisionNumber: 1,
},
});
const txMsgs = [{
typeUrl: CarbonTx.Types.MsgTransfer,
value: txBody,
}];
const txRaw = await signingClient.sign(counterAddr, txMsgs, {
amount: [],
gas: "350000",
}, "");
const tx = CarbonWallet.TxRaw.encode(txRaw).finish();
const depositResponse = await signingClient.broadcastTx(tx, 60_000, 3_000)
console.log("deposit response", depositResponse);
})().catch(console.error).finally(() => process.exit(0));