Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ async function run() {
// Creating a export transaction request from the C-chain to the P-chain
const cChainExportTxnRequest = await walletClient.cChain.prepareExportTxn({
destinationChain: "P",
fromAddress: "0x76Dd3d7b2f635c2547B861e55aE8A374E587742D",
fromAddress: account.getEVMAddress(), // 0x76Dd3d7b2f635c2547B861e55aE8A374E587742D
exportedOutput: {
addresses: ["P-fuji19fc97zn3mzmwr827j4d3n45refkksgms4y2yzz"],
addresses: [account.getXPAddress("P", "fuji")], // P-fuji19fc97zn3mzmwr827j4d3n45refkksgms4y2yzz
amount: 0.0001,
},
});
Expand All @@ -46,7 +46,7 @@ async function run() {
const pChainImportTxnRequest = await walletClient.pChain.prepareImportTxn({
sourceChain: "C",
importedOutput: {
addresses: ["P-fuji19fc97zn3mzmwr827j4d3n45refkksgms4y2yzz"],
addresses: [account.getXPAddress("P", "fuji")], // P-fuji19fc97zn3mzmwr827j4d3n45refkksgms4y2yzz
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ async function run() {
// Creating a export transaction request from the C-chain to the X-chain
const cChainExportTxnRequest = await walletClient.cChain.prepareExportTxn({
destinationChain: "X",
fromAddress: "0x76Dd3d7b2f635c2547B861e55aE8A374E587742D",
fromAddress: account.getEVMAddress(), // 0x76Dd3d7b2f635c2547B861e55aE8A374E587742D
exportedOutput: {
addresses: ["X-fuji19fc97zn3mzmwr827j4d3n45refkksgms4y2yzz"],
addresses: [account.getXPAddress("X", "fuji")], // X-fuji19fc97zn3mzmwr827j4d3n45refkksgms4y2yzz
amount: 0.0011,
},
});
Expand All @@ -46,7 +46,7 @@ async function run() {
const xChainImportTxnRequest = await walletClient.xChain.prepareImportTxn({
sourceChain: "C",
importedOutput: {
addresses: ["X-fuji19fc97zn3mzmwr827j4d3n45refkksgms4y2yzz"],
addresses: [account.getXPAddress("X", "fuji")], // X-fuji19fc97zn3mzmwr827j4d3n45refkksgms4y2yzz
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function run() {
const pChainExportTxnRequest = await walletClient.pChain.prepareExportTxn({
exportedOutputs: [
{
addresses: ["P-fuji19fc97zn3mzmwr827j4d3n45refkksgms4y2yzz"],
addresses: [account.getXPAddress("P", "fuji")], // P-fuji19fc97zn3mzmwr827j4d3n45refkksgms4y2yzz
amount: 0.0001,
},
],
Expand All @@ -48,7 +48,7 @@ async function run() {
// Creating a import transaction request in C-chain
const cChainImportTxnRequest = await walletClient.cChain.prepareImportTxn({
sourceChain: "P",
toAddress: "0x76Dd3d7b2f635c2547B861e55aE8A374E587742D",
toAddress: account.getEVMAddress(), // 0x76Dd3d7b2f635c2547B861e55aE8A374E587742D
});

// Signing and sending the import transaction request to the C-chain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function run() {
const pChainExportTxnRequest = await walletClient.pChain.prepareExportTxn({
exportedOutputs: [
{
addresses: ["P-fuji19fc97zn3mzmwr827j4d3n45refkksgms4y2yzz"],
addresses: [account.getXPAddress("P", "fuji")], // P-fuji19fc97zn3mzmwr827j4d3n45refkksgms4y2yzz
amount: 0.0111,
},
],
Expand All @@ -46,7 +46,7 @@ async function run() {
const xChainImportTxnRequest = await walletClient.xChain.prepareImportTxn({
sourceChain: "P",
importedOutput: {
addresses: ["X-fuji19fc97zn3mzmwr827j4d3n45refkksgms4y2yzz"],
addresses: [account.getXPAddress("X", "fuji")], // X-fuji19fc97zn3mzmwr827j4d3n45refkksgms4y2yzz
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function run() {
const xChainExportTxnRequest = await walletClient.xChain.prepareExportTxn({
exportedOutputs: [
{
addresses: ["X-fuji19fc97zn3mzmwr827j4d3n45refkksgms4y2yzz"],
addresses: [account.getXPAddress("X", "fuji")], // X-fuji19fc97zn3mzmwr827j4d3n45refkksgms4y2yzz
amount: 0.0001,
},
],
Expand All @@ -45,7 +45,7 @@ async function run() {
// Creating a import transaction request in C-chain
const cChainImportTxnRequest = await walletClient.cChain.prepareImportTxn({
sourceChain: "X",
toAddress: "0x76Dd3d7b2f635c2547B861e55aE8A374E587742D",
toAddress: account.getEVMAddress(), // 0x76Dd3d7b2f635c2547B861e55aE8A374E587742D
});

// Signing and sending the import transaction request to the C-chain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function run() {
const xChainExportTxnRequest = await walletClient.xChain.prepareExportTxn({
exportedOutputs: [
{
addresses: ["X-fuji19fc97zn3mzmwr827j4d3n45refkksgms4y2yzz"],
addresses: [account.getXPAddress("X", "fuji")], // X-fuji19fc97zn3mzmwr827j4d3n45refkksgms4y2yzz
amount: 0.001,
},
],
Expand All @@ -46,7 +46,7 @@ async function run() {
const pChainImportTxnRequest = await walletClient.pChain.prepareImportTxn({
sourceChain: "X",
importedOutput: {
addresses: ["P-fuji19fc97zn3mzmwr827j4d3n45refkksgms4y2yzz"],
addresses: [account.getXPAddress("P", "fuji")], // P-fuji19fc97zn3mzmwr827j4d3n45refkksgms4y2yzz
},
});

Expand Down
60 changes: 60 additions & 0 deletions client/examples/sendAvax.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { createAvalancheWalletClient } from "@avalanche-sdk/client";
import { privateKeyToAvalancheAccount } from "@avalanche-sdk/client/accounts";
import { avalancheFuji } from "@avalanche-sdk/client/chains";

async function run() {
// This is the private key of the account that will be used to export the avax from the X-chain to the P-chain
const account = privateKeyToAvalancheAccount(
"0x67d127b32d4c3dccba8a4493c9d6506e6e1c7e0f08fd45aace29c9973c7fc2ce"
);

// This is the wallet client that will be used to export the avax from the X-chain to the P-chain
const walletClient = createAvalancheWalletClient({
chain: avalancheFuji,
transport: {
type: "http",
},
account,
});

// 1. Send avax to another address on C-chain from C-chain
// Creates a transaction, signs, sends and waits for the transaction to
// be confirmed on C-chain
const sendC2CResponse = await walletClient.send({
to: "0x909d71Ed4090ac6e57E3645dcF2042f8c6548664",
amount: 0.001,
});
console.log("sendC2CResponse", sendC2CResponse);

// 2. Send avax to another address on P-chain from C-chain
// Creates a export and import transaction, signs, sends and waits for
// the transaction to be confirmed on C-chain and P-chain
const sendC2PResponse = await walletClient.send({
to: "P-fuji1apmh7wxg3js48fhacfv5y9md9065jxuf8rtns7",
amount: 0.001,
destinationChain: "P",
});
console.log("sendC2PResponse", sendC2PResponse);

// 3. Send avax to another address on C-chain from P-chain
// Creates a export and import transaction, signs, sends and waits for
// the transaction to be confirmed on C-chain and P-chain
const sendP2CResponse = await walletClient.send({
to: "0x909d71Ed4090ac6e57E3645dcF2042f8c6548664",
amount: 0.001,
sourceChain: "P",
destinationChain: "C",
});
console.log("sendP2CResponse", sendP2CResponse);

// 4. Send avax to another address on P-chain from P-chain
const sendP2PResponse = await walletClient.send({
to: "P-fuji1apmh7wxg3js48fhacfv5y9md9065jxuf8rtns7",
amount: 0.001,
sourceChain: "P",
destinationChain: "P",
});
console.log("sendP2PResponse", sendP2PResponse);
}

run();
29 changes: 29 additions & 0 deletions client/src/clients/decorators/avalancheWallet.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Chain, Transport } from "viem";
import { getAccountPubKey } from "../../methods/wallet/getAccountPubKey.js";
import { send } from "../../methods/wallet/send.js";
import { sendXPTransaction } from "../../methods/wallet/sendXPTransaction.js";
import { signXPMessage } from "../../methods/wallet/signXPMessage.js";
import { signXPTransaction } from "../../methods/wallet/signXPTransaction.js";
import { GetAccountPubKeyReturnType } from "../../methods/wallet/types/getAccountPubKey.js";
import {
SendParameters,
SendReturnType,
} from "../../methods/wallet/types/send.js";
import {
SendXPTransactionParameters,
SendXPTransactionReturnType,
Expand Down Expand Up @@ -207,6 +212,29 @@ export type AvalancheWalletActions = {
* ```
*/
waitForTxn: (args: WaitForTxnParameters) => Promise<void>;

/**
* Sends tokens from the source chain to the destination chain.
*
* @param args - The parameters for the transaction. {@link SendParameters}
* @returns The hashes of the transactions. {@link SendReturnType}
*
* @example
* ```ts
* import { createAvalancheWalletClient } from '@avalanche-sdk/client'
* import { avalanche } from '@avalanche-sdk/client/chains'
*
* const walletClient = createAvalancheWalletClient({
* chain: avalanche,
* transport: { type: "http" },
* })
*
* const result = await walletClient.send({
* amount: 1,
* to: "0x0000000000000000000000000000000000000000",
* });
*/
send: (args: SendParameters) => Promise<SendReturnType>;
};

export function avalancheWalletActions<
Expand All @@ -218,5 +246,6 @@ export function avalancheWalletActions<
signXPTransaction: (args) => signXPTransaction(client, args),
getAccountPubKey: () => getAccountPubKey(client),
waitForTxn: (args) => waitForTxn(client, args),
send: (args) => send(client, args),
};
}
6 changes: 4 additions & 2 deletions client/src/methods/wallet/cChain/prepareImportTxn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { C_CHAIN_ALIAS } from "../../consts.js";
import { baseFee as getBaseFee } from "../../public/index.js";
import { getContextFromURI } from "../getContextFromURI.js";
import {
addOrModifyXPAddressesAlias,
bech32AddressToBytes,
getBech32AddressFromAccountOrClient,
getChainIdFromAlias,
Expand Down Expand Up @@ -53,8 +54,9 @@ export async function prepareImportTxn(
const context = params.context || (await getContextFromURI(client));
const baseFee = await getBaseFee(client);

const fromAddresses = params.fromAddresses || [];

const fromAddresses =
addOrModifyXPAddressesAlias(params.fromAddresses, C_CHAIN_ALIAS) || [];
console.log("fromAddresses", fromAddresses);
if (fromAddresses.length === 0) {
const paramAc = parseAvalancheAccount(account);
const address = await getBech32AddressFromAccountOrClient(
Expand Down
7 changes: 7 additions & 0 deletions client/src/methods/wallet/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export type { AvalancheWalletRpcSchema } from "./avalancheWalletRPCSchema.js";
export { getAccountPubKey } from "./getAccountPubKey.js";
export { send } from "./send.js";
export { sendXPTransaction } from "./sendXPTransaction.js";
export { signXPMessage } from "./signXPMessage.js";
export { signXPTransaction } from "./signXPTransaction.js";
Expand All @@ -14,6 +15,11 @@ export type {
GetAccountPubKeyErrorType,
GetAccountPubKeyReturnType,
} from "./types/getAccountPubKey.js";
export type {
SendErrorType,
SendParameters,
SendReturnType,
} from "./types/send.js";
export type {
SendXPTransactionErrorType,
SendXPTransactionParameters,
Expand All @@ -33,4 +39,5 @@ export type {
WaitForTxnErrorType,
WaitForTxnParameters,
} from "./types/waitForTxn.js";
export { addOrModifyXPAddressesAlias as addOrModifyXPAddressAlias } from "./utils.js";
export { waitForTxn } from "./waitForTxn.js";
64 changes: 64 additions & 0 deletions client/src/methods/wallet/send.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { AvalancheWalletCoreClient } from "../../clients/createAvalancheWalletCoreClient.js";
import { transferCtoCChain } from "./transferUtils/transferCtoCChain.js";
import { transferCtoPChain } from "./transferUtils/transferCtoPChain.js";
import { transferPtoCChain } from "./transferUtils/transferPtoCChain.js";
import { transferPtoPChain } from "./transferUtils/transferPtoPChain.js";
import { SendParameters, SendReturnType } from "./types/send.js";

/**
* @description Sends tokens from the source chain to the destination chain.
* @param client - The client to use for the transaction. {@link AvalancheWalletCoreClient}
* @param params - The parameters for the transaction. {@link SendParameters}
* @returns The hashes of the transactions. {@link SendReturnType}
*
* @example
* ```ts
* import { AvalancheWalletCoreClient } from "@avalabs/wallet-core-client";
* import { send } from "@avalabs/wallet-core-client/methods/wallet/send";
*
* const client = new AvalancheWalletCoreClient({
* network: "mainnet",
* transport: {
* type: "custom",
* provider: window.avalanche,
* },
* });
*
* const txHashes = await send(client, {
* amount: 1,
* to: "0x0000000000000000000000000000000000000000",
* });
*/
export async function send(
client: AvalancheWalletCoreClient,
params: SendParameters
): Promise<SendReturnType> {
const { sourceChain = "C", destinationChain = "C", token = "AVAX" } = params;

if (token !== "AVAX") {
throw new Error(`Invalid token: ${token}, only AVAX is supported.`);
}

switch (sourceChain) {
case "C":
switch (destinationChain) {
case "C":
return transferCtoCChain(client, params);
case "P":
return transferCtoPChain(client, params);
default:
throw new Error(`Invalid destination chain: ${destinationChain}`);
}
case "P":
switch (destinationChain) {
case "P":
return transferPtoPChain(client, params);
case "C":
return transferPtoCChain(client, params);
default:
throw new Error(`Invalid destination chain: ${destinationChain}`);
}
default:
throw new Error(`Invalid source chain: ${sourceChain}`);
}
}
Loading