Skip to content

Commit

Permalink
feat(BuildingTx): Solana add txFeeParams?: TxFeeParams
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKozAllB committed Mar 7, 2024
1 parent c900f5f commit aa76e85
Show file tree
Hide file tree
Showing 19 changed files with 226 additions and 279 deletions.
25 changes: 16 additions & 9 deletions examples/src/examples/bridge/solana/sol-build-send-tx.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { AllbridgeCoreSdk, ChainSymbol, Messenger, nodeUrlsDefault } from "@allbridge/bridge-core-sdk";
import {
AllbridgeCoreSdk,
ChainSymbol,
Messenger,
nodeUrlsDefault,
RawBridgeSolanaTransaction,
SolanaAutoTxFee,
} from "@allbridge/bridge-core-sdk";
import * as dotenv from "dotenv";
import { getEnvVar } from "../../../utils/env";
import { ensure } from "../../../utils/utils";
import solanaWeb3, { VersionedTransaction, Keypair } from "@solana/web3.js";
import solanaWeb3 from "@solana/web3.js";
import bs58 from "bs58";

dotenv.config({ path: ".env" });
Expand All @@ -23,20 +30,20 @@ const exampleViaWormhole = async () => {
const destinationTokenInfoPol = ensure(destinationChainPol.tokens.find((tokenInfo) => tokenInfo.symbol === "USDC"));

// initiate transfer using Messenger.WORMHOLE
const { transaction, wormholeMessageSigner } = (await sdk.bridge.rawTxBuilder.send({
const transaction = (await sdk.bridge.rawTxBuilder.send({
amount: "0.2",
fromAccountAddress: fromAddress,
toAccountAddress: toAddressPol,
sourceToken: sourceTokenInfo,
destinationToken: destinationTokenInfoPol,
messenger: Messenger.WORMHOLE,
})) as { transaction: VersionedTransaction; wormholeMessageSigner: Keypair };

await sdk.utils.sol.addPriorityFeeToTx(transaction, "100000");
// await sdk.utils.sol.addPriorityFeePerUnitToTx(transaction);
txFeeParams: {
solana: SolanaAutoTxFee,
},
})) as RawBridgeSolanaTransaction;

const keypair = solanaWeb3.Keypair.fromSecretKey(bs58.decode(privateKey));
transaction.sign([wormholeMessageSigner, keypair]);
transaction.sign([keypair]);

const connection = new solanaWeb3.Connection(nodeUrlsDefault.solanaRpcUrl, "confirmed");
const txid = await connection.sendTransaction(transaction);
Expand All @@ -62,7 +69,7 @@ const exampleViaAllbridge = async () => {
sourceToken: sourceTokenInfo,
destinationToken: destinationTokenInfoPol,
messenger: Messenger.ALLBRIDGE,
})) as VersionedTransaction;
})) as RawBridgeSolanaTransaction;

const keypair = solanaWeb3.Keypair.fromSecretKey(bs58.decode(privateKey));
transaction.sign([keypair]);
Expand Down
6 changes: 3 additions & 3 deletions examples/src/examples/bridge/solana/sol-build-swap-tx.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AllbridgeCoreSdk, ChainSymbol, nodeUrlsDefault } from "@allbridge/bridge-core-sdk";
import { AllbridgeCoreSdk, ChainSymbol, nodeUrlsDefault, RawSolanaTransaction } from "@allbridge/bridge-core-sdk";
import * as dotenv from "dotenv";
import { getEnvVar } from "../../../utils/env";
import { ensure } from "../../../utils/utils";
import solanaWeb3, { VersionedTransaction } from "@solana/web3.js";
import solanaWeb3 from "@solana/web3.js";
import bs58 from "bs58";

dotenv.config({ path: ".env" });
Expand Down Expand Up @@ -30,7 +30,7 @@ const example = async () => {
sourceToken: sourceTokenInfo,
destinationToken: destinationTokenInfo,
minimumReceiveAmount: await sdk.getAmountToBeReceived(amount, sourceTokenInfo, destinationTokenInfo),
})) as VersionedTransaction;
})) as RawSolanaTransaction;

const keypair = solanaWeb3.Keypair.fromSecretKey(bs58.decode(privateKey));
transaction.sign([keypair]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as dotenv from "dotenv";
import { getEnvVar } from "../../../utils/env";
import { AllbridgeCoreSdk, nodeUrlsDefault, RawTransaction } from "@allbridge/bridge-core-sdk";
import { AllbridgeCoreSdk, nodeUrlsDefault, RawPoolSolanaTransaction } from "@allbridge/bridge-core-sdk";
import { ensure } from "../../../utils/utils";
import solanaWeb3, { sendAndConfirmTransaction, Transaction } from "@solana/web3.js";
import bs58 from "bs58";

dotenv.config({ path: ".env" });

const main = async () => {
Expand All @@ -16,20 +17,20 @@ const main = async () => {
const tokenInfo = ensure((await sdk.tokens()).find((tokenInfo) => tokenInfo.tokenAddress === tokenAddress));

// create claim rewards raw transaction
const transaction = await sdk.pool.rawTxBuilder.claimRewards({
const transaction = (await sdk.pool.rawTxBuilder.claimRewards({
accountAddress: accountAddress,
token: tokenInfo,
});
})) as RawPoolSolanaTransaction;

const tx = await sendRawTransaction(transaction, privateKey, nodeUrlsDefault.solanaRpcUrl);

console.log("Token claim rewards:", tx);
};

async function sendRawTransaction(transaction: RawTransaction, privateKey: string, solanaRpcUrl: string) {
async function sendRawTransaction(transaction: Transaction, privateKey: string, solanaRpcUrl: string) {
const keypair = solanaWeb3.Keypair.fromSecretKey(bs58.decode(privateKey));
const connection = new solanaWeb3.Connection(solanaRpcUrl, "confirmed");
return await sendAndConfirmTransaction(connection, transaction as Transaction, [keypair]);
return await sendAndConfirmTransaction(connection, transaction, [keypair]);
}

main()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as dotenv from "dotenv";
import { getEnvVar } from "../../../utils/env";
import { AllbridgeCoreSdk, nodeUrlsDefault, RawTransaction } from "@allbridge/bridge-core-sdk";
import { AllbridgeCoreSdk, nodeUrlsDefault, RawPoolSolanaTransaction } from "@allbridge/bridge-core-sdk";
import { ensure } from "../../../utils/utils";
import solanaWeb3, { sendAndConfirmTransaction, Transaction } from "@solana/web3.js";
import bs58 from "bs58";

dotenv.config({ path: ".env" });

const main = async () => {
Expand All @@ -17,21 +18,21 @@ const main = async () => {

const oneToken = "1";
// create deposit raw transaction
const transaction = await sdk.pool.rawTxBuilder.deposit({
const transaction = (await sdk.pool.rawTxBuilder.deposit({
amount: oneToken,
accountAddress: accountAddress,
token: tokenInfo,
});
})) as RawPoolSolanaTransaction;

const tx = await sendRawTransaction(transaction, privateKey, nodeUrlsDefault.solanaRpcUrl);

console.log("Token deposit:", tx);
};

async function sendRawTransaction(transaction: RawTransaction, privateKey: string, solanaRpcUrl: string) {
async function sendRawTransaction(transaction: Transaction, privateKey: string, solanaRpcUrl: string) {
const keypair = solanaWeb3.Keypair.fromSecretKey(bs58.decode(privateKey));
const connection = new solanaWeb3.Connection(solanaRpcUrl, "confirmed");
return await sendAndConfirmTransaction(connection, transaction as Transaction, [keypair]);
return await sendAndConfirmTransaction(connection, transaction, [keypair]);
}

main()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as dotenv from "dotenv";
import { getEnvVar } from "../../../utils/env";
import { AllbridgeCoreSdk, nodeUrlsDefault, RawTransaction } from "@allbridge/bridge-core-sdk";
import { AllbridgeCoreSdk, nodeUrlsDefault, RawPoolSolanaTransaction } from "@allbridge/bridge-core-sdk";
import { ensure } from "../../../utils/utils";
import solanaWeb3, { sendAndConfirmTransaction, Transaction } from "@solana/web3.js";
import bs58 from "bs58";
Expand All @@ -17,21 +17,21 @@ const main = async () => {

const halfToken = "0.5";
// create withdraw raw transaction
const transaction = await sdk.pool.rawTxBuilder.withdraw({
const transaction = (await sdk.pool.rawTxBuilder.withdraw({
amount: halfToken,
accountAddress: accountAddress,
token: tokenInfo,
});
})) as RawPoolSolanaTransaction;

const tx = await sendRawTransaction(transaction, privateKey, nodeUrlsDefault.solanaRpcUrl);

console.log("Token withdraw:", tx);
};

async function sendRawTransaction(transaction: RawTransaction, privateKey: string, solanaRpcUrl: string) {
async function sendRawTransaction(transaction: Transaction, privateKey: string, solanaRpcUrl: string) {
const keypair = solanaWeb3.Keypair.fromSecretKey(bs58.decode(privateKey));
const connection = new solanaWeb3.Connection(solanaRpcUrl, "confirmed");
return await sendAndConfirmTransaction(connection, transaction as Transaction, [keypair]);
return await sendAndConfirmTransaction(connection, transaction, [keypair]);
}

main()
Expand Down

This file was deleted.

19 changes: 1 addition & 18 deletions src/__tests__/services/liquidity-pool/evm/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as fs from "fs";
import nock, { abortPendingRequests, cleanAll, disableNetConnect, load } from "nock";
import { abortPendingRequests, cleanAll, disableNetConnect } from "nock";
import Web3 from "web3";
import { ChainSymbol } from "../../../../chains";
import { AllbridgeCoreClient } from "../../../../client/core-api";
Expand Down Expand Up @@ -88,19 +87,3 @@ describe("EvmPool", () => {
});
});
});

function nockRequests(recName: string) {
const nocks = load(`./src/__tests__/services/liquidity-pool/evm/data/nock/${recName}-rec.json`);
nocks.forEach(function (nock) {
nock.filteringRequestBody((b) => {
try {
const body = JSON.parse(b);
body[0].id = 8551125359729656;
body[1].id = 8551125359729657;
return JSON.stringify(body);
} catch (e) {
return b;
}
});
});
}
3 changes: 1 addition & 2 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export {
} from "../services/bridge/models/bridge.model";
export { BridgeService } from "../services/bridge/index";
export { LiquidityPoolService } from "../services/liquidity-pool/index";
export { TransactionResponse } from "../services/models/index";
export {
Messenger,
TransferStatusResponse,
Expand Down Expand Up @@ -38,7 +37,7 @@ export {
CheckAllowanceParams as LiquidityPoolsCheckAllowanceParams,
GetAllowanceParams as LiquidityPoolsGetAllowanceParams,
} from "../services/liquidity-pool/models/pool.model";
export { Provider, RawTransaction } from "../services/models/index";
export * from "../services/models/index";
export {
SwapAndBridgeCalculationData,
SwapFromVUsdCalcResult,
Expand Down
2 changes: 1 addition & 1 deletion src/services/bridge/evm/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Big from "big.js";
import { Big } from "big.js";
import BN from "bn.js";
import Web3 from "web3";
import { TransactionConfig } from "web3-core";
Expand Down
4 changes: 3 additions & 1 deletion src/services/bridge/models/bridge.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Big } from "big.js";
import { ChainSymbol } from "../../../chains";
import { Messenger } from "../../../client/core-api/core-api.model";
import { AmountFormat, FeePaymentMethod } from "../../../models";
import { AmountFormat, FeePaymentMethod, TxFeeParams } from "../../../models";
import { TokenWithChainDetails } from "../../../tokens-info";

export interface ApproveParams {
Expand Down Expand Up @@ -72,6 +72,8 @@ export interface BaseSendParams {
* {@link TokenWithChainDetails |The token info object} on the destination chain.
*/
destinationToken: TokenWithChainDetails;

txFeeParams?: TxFeeParams;
}

/**
Expand Down
Loading

0 comments on commit aa76e85

Please sign in to comment.