Skip to content

Commit

Permalink
fix(SRB): add srb
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKozAllB committed Jan 10, 2024
1 parent 4e41981 commit 72ad8e4
Show file tree
Hide file tree
Showing 20 changed files with 1,253 additions and 1,038 deletions.
4 changes: 3 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
"tsconfig.json",
"node_modules/**",
"src/configs/",
"src/__tests__/services/liquidity-pool/sol/data/"
"src/__tests__/services/liquidity-pool/sol/data/",
"src/services/models/srb/token-contract.ts",
"src/services/utils/srb/assembled-tx.ts"
]
}
61 changes: 36 additions & 25 deletions examples/src/examples/bridge/srb/srb-send-full-example.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,89 @@
import Big from "big.js";
import {
AllbridgeCoreSdk,
AmountFormat,
ChainSymbol,
mainnet,
FeePaymentMethod,
Messenger,
nodeUrlsDefault,
nodeRpcUrlsDefault,
SendParams,
testnet,
} from "@allbridge/bridge-core-sdk";
import * as SorobanClient from "soroban-client";
import { SorobanRpc, TransactionBuilder } from "soroban-client";
import { Keypair as StellarKeypair, TransactionBuilder as StellarTransactionBuilder } from "stellar-sdk";
import { ensure } from "../../../utils/utils";
import { getEnvVar } from "../../../utils/env";
import {
Keypair,
Keypair as StellarKeypair,
SorobanRpc,
TransactionBuilder as StellarTransactionBuilder,
TransactionBuilder,
} from "stellar-sdk";
import {ensure} from "../../../utils/utils";
import {getEnvVar} from "../../../utils/env";
import * as dotenv from "dotenv";

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

const fromAddress = getEnvVar("SRB_ACCOUNT_ADDRESS");
const privateKey = getEnvVar("SRB_PRIVATE_KEY");
const toAddress = getEnvVar("ETH_ACCOUNT_ADDRESS");

const main = async () => {
const sdk = new AllbridgeCoreSdk(nodeUrlsDefault);
const sdk = new AllbridgeCoreSdk(nodeRpcUrlsDefault);

const chainDetailsMap = await sdk.chainDetailsMap();
const sourceToken = ensure(chainDetailsMap[ChainSymbol.SRB].tokens.find((t) => t.symbol == "USDC"));
const destinationToken = ensure(chainDetailsMap[ChainSymbol.ETH].tokens.find((t) => t.symbol == "USDC"));

const amount = "1";
const sourceToken = ensure(chainDetailsMap[ChainSymbol.SRB].tokens.find((t) => t.symbol == "USDT"));
const destinationToken = ensure(chainDetailsMap[ChainSymbol.ETH].tokens.find((t) => t.symbol == "USDT"));
const amount = "2";
const sendParams: SendParams = {
amount,
fromAccountAddress: fromAddress,
toAccountAddress: toAddress,
sourceToken,
destinationToken,
messenger: Messenger.ALLBRIDGE,
extraGas: "1.15",
extraGasFormat: AmountFormat.FLOAT,
gasFeePaymentMethod: FeePaymentMethod.WITH_STABLECOIN,
};
const xdrTx: string = (await sdk.bridge.rawTxBuilder.send(sendParams)) as string;

//SignTx
const srbKeypair = SorobanClient.Keypair.fromSecret(privateKey);
const transaction = TransactionBuilder.fromXDR(xdrTx, mainnet.sorobanNetworkPassphrase);
const srbKeypair = Keypair.fromSecret(privateKey);
const transaction = TransactionBuilder.fromXDR(xdrTx, testnet.sorobanNetworkPassphrase);
transaction.sign(srbKeypair);
const signedTx = transaction.toXDR();
//

const sent = await sdk.utils.srb.sendTransactionSoroban(signedTx);
const confirm = await sdk.utils.srb.confirmTx(sent.hash);
if (confirm.status === SorobanRpc.GetTransactionStatus.NOT_FOUND) {
if (confirm.status === SorobanRpc.Api.GetTransactionStatus.NOT_FOUND) {
console.log(
`Waited for transaction to complete, but it did not. ` +
`Check the transaction status manually. ` +
`Hash: ${sent.hash}`
`Check the transaction status manually. ` +
`Hash: ${sent.hash}`
);
} else if (confirm.status === SorobanRpc.GetTransactionStatus.FAILED) {
} else if (confirm.status === SorobanRpc.Api.GetTransactionStatus.FAILED) {
console.log(`Transaction failed. Check the transaction manually.` + `Hash: ${sent.hash}`);
} else {
console.log(`Transaction Confirmed. Hash: ${sent.hash}`);
}

//TrustLine check and Set up for destinationToken if it is SRB
const destinationTokenSBR = sourceToken; // simulate destination is srb
const toAddressSBR = fromAddress; // simulate destination is srb
const balanceLine = await sdk.utils.srb.getBalanceLine(toAddressSBR, destinationTokenSBR.tokenAddress);

const balanceLine = await sdk.utils.srb.getBalanceLine(fromAddress, destinationTokenSBR.tokenAddress);
console.log(`BalanceLine:`, balanceLine);
if (!balanceLine || Big(balanceLine.balance).add(amount).gt(Big(balanceLine.limit))) {
const notEnoughBalanceLine = !balanceLine || Big(balanceLine.balance).add(amount).gt(Big(balanceLine.limit));
if (notEnoughBalanceLine) {
const xdrTx = await sdk.utils.srb.buildChangeTrustLineXdrTx({
sender: toAddressSBR,
sender: fromAddress,
tokenAddress: destinationTokenSBR.tokenAddress,
// limit: "1000000",
});

//SignTx
const transaction = StellarTransactionBuilder.fromXDR(xdrTx, mainnet.sorobanNetworkPassphrase);
const keypair = StellarKeypair.fromSecret(privateKey);
const transaction = StellarTransactionBuilder.fromXDR(xdrTx, testnet.sorobanNetworkPassphrase);
transaction.sign(keypair);
const signedTrustLineTx = transaction.toXDR();
//

const submit = await sdk.utils.srb.submitTransactionStellar(signedTrustLineTx);
console.log("Submitted change trust tx. Hash:", submit.hash);
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@
"erc-20-abi": "^1.0.0",
"querystring": "^0.2.1",
"randombytes": "^2.1.0",
"soroban-client": "1.0.0-beta.4",
"stellar-sdk": "^10.4.1",
"stellar-sdk": "11.1.0",
"timed-cache": "^2.0.0",
"tronweb": "^4.4.0",
"web3": "1.9.0",
Expand Down
Loading

0 comments on commit 72ad8e4

Please sign in to comment.