Skip to content

Commit

Permalink
cosmetic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianscatularo committed Aug 11, 2023
1 parent aa44c68 commit 60fcd91
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 102 deletions.
243 changes: 147 additions & 96 deletions src/assets/providers/tbtc/solana/WormholeGateway.v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import {
SOL_BRIDGE_ADDRESS as CORE_BRIDGE_ADDRESS,
SOL_TOKEN_BRIDGE_ADDRESS as TOKEN_BRIDGE_ADDRESS,
THRESHOLD_GATEWAYS,
THRESHOLD_TBTC_SOLANA_PROGRAM_TESTNET,
THRESHOLD_TBTC_SOLANA_PROGRAM,
} from "../../../../utils/consts";
import {
CHAIN_ID_ETH,
CHAIN_ID_ETH,
CHAIN_ID_SOLANA,
ChainId,
SignedVaa,
Expand All @@ -33,7 +33,7 @@ import {
const WORMHOLE_GATEWAY_PROGRAM_ID = new PublicKey(
THRESHOLD_GATEWAYS[CHAIN_ID_SOLANA]
);
const TBTC_PROGRAM_ID = new PublicKey(THRESHOLD_TBTC_SOLANA_PROGRAM_TESTNET);
const TBTC_PROGRAM_ID = new PublicKey(THRESHOLD_TBTC_SOLANA_PROGRAM);
const CORE_BRIDGE_PROGRAM_ID = new PublicKey(CORE_BRIDGE_ADDRESS);
const TOKEN_BRIDGE_PROGRAM_ID = new PublicKey(TOKEN_BRIDGE_ADDRESS);

Expand Down Expand Up @@ -99,47 +99,96 @@ function getCoreMessagePDA(sequence: bigint): PublicKey {
}

function getGatewayInfoPDA(targetChain: number): PublicKey {
const encodedChain = Buffer.alloc(2);
encodedChain.writeUInt16LE(targetChain);
return PublicKey.findProgramAddressSync(
[Buffer.from("gateway-info"), encodedChain],
WORMHOLE_GATEWAY_PROGRAM_ID
)[0];
const encodedChain = Buffer.alloc(2);
encodedChain.writeUInt16LE(targetChain);
return PublicKey.findProgramAddressSync(
[Buffer.from("gateway-info"), encodedChain],
WORMHOLE_GATEWAY_PROGRAM_ID
)[0];
}

type SendTbtcArgs = {
amount: BN;
recipientChain: number;
recipient: Uint8Array;
nonce: number;
};

}

type SendTbtcWrappedAccounts = {}
type SendTbtcWrappedAccounts = {
custodian: PublicKey;
wrappedTbtcToken: PublicKey;
wrappedTbtcMint: PublicKey;
tbtcMint: PublicKey;
senderToken: PublicKey;
sender: PublicKey;
tokenBridgeConfig: PublicKey;
tokenBridgeWrappedAsset: PublicKey;
tokenBridgeTransferAuthority: PublicKey;
coreBridgeData: PublicKey;
coreMessage: PublicKey;
tokenBridgeCoreEmitter: PublicKey;
coreEmitterSequence: PublicKey;
coreFeeCollector: PublicKey;
clock: PublicKey;
rent: PublicKey;
tokenBridgeProgram: PublicKey;
coreBridgeProgram: PublicKey;
};

type SendTbtcGatewayAccounts = {

}
custodian: PublicKey;
gatewayInfo: PublicKey;
wrappedTbtcToken: PublicKey;
wrappedTbtcMint: PublicKey;
tbtcMint: PublicKey;
senderToken: PublicKey;
sender: PublicKey;
tokenBridgeConfig: PublicKey;
tokenBridgeWrappedAsset: PublicKey;
tokenBridgeTransferAuthority: PublicKey;
coreBridgeData: PublicKey;
coreMessage: PublicKey;
tokenBridgeCoreEmitter: PublicKey;
coreEmitterSequence: PublicKey;
coreFeeCollector: PublicKey;
clock: PublicKey;
tokenBridgeSender: PublicKey;
rent: PublicKey;
tokenBridgeProgram: PublicKey;
coreBridgeProgram: PublicKey;
};

/**
* will off board from Solana to L1 (Ethereum)
*/
function sendTbtcWrapped(program: Program<typeof WormholeGatewayIdl>, args: SendTbtcArgs, accounts: SendTbtcWrappedAccounts) {
const tx = program.methods
.sendTbtcWrapped({
...args,
arbiterFee: new BN(0)
})
.accounts(accounts)
.transaction();
return tx;
function sendTbtcWrapped(
program: Program<typeof WormholeGatewayIdl>,
args: SendTbtcArgs,
accounts: SendTbtcWrappedAccounts
) {
const tx = program.methods
.sendTbtcWrapped({
...args,
arbiterFee: new BN(0),
})
.accounts(accounts)
.transaction();
return tx;
}

/**
* Send tBtc beween gateways allow burn and mint of tBtc
*/
function sendTbtcGateway(program: Program<typeof WormholeGatewayIdl>, args: SendTbtcArgs, accounts: SendTbtcGatewayAccounts) {
const tx = program.methods
.sendTbtcGateway(args)
.accounts(accounts)
.transaction();
return tx;
function sendTbtcGateway(
program: Program<typeof WormholeGatewayIdl>,
args: SendTbtcArgs,
accounts: SendTbtcGatewayAccounts
) {
const tx = program.methods
.sendTbtcGateway(args)
.accounts(accounts)
.transaction();
return tx;
}

export function newThresholdWormholeGateway(
Expand Down Expand Up @@ -169,20 +218,22 @@ export function newThresholdWormholeGateway(
custodianData.wrappedTbtcMint as string
);
const recipientToken = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
tbtcMint,
recipient
);
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
tbtcMint,
recipient
);
/*
const recipientTokenAta = Token.createAssociatedTokenAccountInstruction(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
tbtcMint,
recipientToken,
recipient, // owner
recipient // payer
)
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
tbtcMint,
recipientToken,
recipient, // owner
recipient // payer
);
console.log(recipientToken);
*/
const tokenBridgeWrappedAsset = tokenBridge.deriveWrappedMetaKey(
TOKEN_BRIDGE_PROGRAM_ID,
wrappedTbtcMint
Expand Down Expand Up @@ -279,66 +330,66 @@ export function newThresholdWormholeGateway(
);
const gatewayInfo = getGatewayInfoPDA(recipientChain);
const tokenBridgeSender = tokenBridge.deriveSenderAccountKey(
WORMHOLE_GATEWAY_PROGRAM_ID
WORMHOLE_GATEWAY_PROGRAM_ID
);
const args = {
amount: new BN(amount.toString()),
recipientChain,
recipient: recipientAddress,
nonce: 0,
amount: new BN(amount.toString()),
recipientChain,
recipient: recipientAddress,
nonce: 0,
};
const associatedTokenAccount = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
new PublicKey(senderToken), // this might error
new PublicKey(wallet.getAddress()!)
)
if (recipientChain === CHAIN_ID_ETH) { // L1 off board
const wrappedAccounts = {
custodian,
wrappedTbtcToken,
wrappedTbtcMint,
tbtcMint,
senderToken: associatedTokenAccount, // associated token account
sender: new PublicKey(wallet.getAddress()!), //sender, associated token account owner,
tokenBridgeConfig,
tokenBridgeWrappedAsset,
tokenBridgeTransferAuthority,
coreBridgeData,
coreMessage,
tokenBridgeCoreEmitter,
coreEmitterSequence,
coreFeeCollector,
clock: SYSVAR_CLOCK_PUBKEY,
rent: SYSVAR_RENT_PUBKEY,
tokenBridgeProgram: TOKEN_BRIDGE_PROGRAM_ID,
coreBridgeProgram: CORE_BRIDGE_PROGRAM_ID
}
return sendTbtcWrapped(program, args, wrappedAccounts);
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
new PublicKey(senderToken),
new PublicKey(wallet.getAddress()!)
);
if (recipientChain === CHAIN_ID_ETH) {
const wrappedAccounts = {
custodian,
wrappedTbtcToken,
wrappedTbtcMint,
tbtcMint,
senderToken: associatedTokenAccount, // associated token account
sender: new PublicKey(wallet.getAddress()!), //sender, associated token account owner,
tokenBridgeConfig,
tokenBridgeWrappedAsset,
tokenBridgeTransferAuthority,
coreBridgeData,
coreMessage,
tokenBridgeCoreEmitter,
coreEmitterSequence,
coreFeeCollector,
clock: SYSVAR_CLOCK_PUBKEY,
rent: SYSVAR_RENT_PUBKEY,
tokenBridgeProgram: TOKEN_BRIDGE_PROGRAM_ID,
coreBridgeProgram: CORE_BRIDGE_PROGRAM_ID,
};
return sendTbtcWrapped(program, args, wrappedAccounts);
} else {
const gatewayAccounts = {
custodian,
gatewayInfo,
wrappedTbtcToken,
wrappedTbtcMint,
tbtcMint,
senderToken: associatedTokenAccount, // associated token account
sender: new PublicKey(wallet.getAddress()!), //sender, associated token account owner,
tokenBridgeConfig,
tokenBridgeWrappedAsset,
tokenBridgeTransferAuthority,
coreBridgeData,
coreMessage,
tokenBridgeCoreEmitter,
coreEmitterSequence,
coreFeeCollector,
clock: SYSVAR_CLOCK_PUBKEY,
tokenBridgeSender,
rent: SYSVAR_RENT_PUBKEY,
tokenBridgeProgram: TOKEN_BRIDGE_PROGRAM_ID,
coreBridgeProgram: CORE_BRIDGE_PROGRAM_ID,
}
return sendTbtcGateway(program, args, gatewayAccounts);
const gatewayAccounts = {
custodian,
gatewayInfo,
wrappedTbtcToken,
wrappedTbtcMint,
tbtcMint,
senderToken: associatedTokenAccount, // associated token account
sender: new PublicKey(wallet.getAddress()!), //sender, associated token account owner,
tokenBridgeConfig,
tokenBridgeWrappedAsset,
tokenBridgeTransferAuthority,
coreBridgeData,
coreMessage,
tokenBridgeCoreEmitter,
coreEmitterSequence,
coreFeeCollector,
clock: SYSVAR_CLOCK_PUBKEY,
tokenBridgeSender,
rent: SYSVAR_RENT_PUBKEY,
tokenBridgeProgram: TOKEN_BRIDGE_PROGRAM_ID,
coreBridgeProgram: CORE_BRIDGE_PROGRAM_ID,
};
return sendTbtcGateway(program, args, gatewayAccounts);
}
};
return {
Expand Down
5 changes: 2 additions & 3 deletions src/hooks/useCheckIfWormholeWrapped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ import {
XPLA_LCD_CLIENT_CONFIG,
THRESHOLD_TBTC_CONTRACTS,
TBTC_ASSET_ADDRESS,
THRESHOLD_TBTC_SOLANA_MINT_TESTNET,
} from "../utils/consts";
import { getOriginalAssetNear, makeNearAccount } from "../utils/near";
import { LCDClient as XplaLCDClient } from "@xpla/xpla.js";
Expand Down Expand Up @@ -154,7 +153,7 @@ function useCheckIfWormholeWrapped(nft?: boolean) {
try {
// Check if is tBtc canonical on Solana
// TODO improve the check and centralice the login on just one place
if (THRESHOLD_TBTC_SOLANA_MINT_TESTNET === sourceAsset) {
if (THRESHOLD_TBTC_CONTRACTS[sourceChain] === sourceAsset) {
console.log("selected tBTC on canonical chain");
dispatch(
setSourceWormholeWrappedInfo({
Expand Down Expand Up @@ -182,7 +181,7 @@ function useCheckIfWormholeWrapped(nft?: boolean) {
if (!cancelled) {
dispatch(setSourceWormholeWrappedInfo(wrappedInfo));
}
}
}
} catch (e) {}
}
if (isTerraChain(sourceChain) && sourceAsset) {
Expand Down
12 changes: 9 additions & 3 deletions src/utils/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,17 @@ const THRESHOLD_TBTC_CONTRACTS_TESTNET: any = {
[CHAIN_ID_SOLANA]: "6DNSN2BJsaPFdFFc1zP37kkeNe4Usc1Sqkzr9C9vPWcU", // Solana TBTC Mint
} as const;

export const THRESHOLD_TBTC_SOLANA_MINT_TESTNET =
"6DNSN2BJsaPFdFFc1zP37kkeNe4Usc1Sqkzr9C9vPWcU";
export const THRESHOLD_TBTC_SOLANA_PROGRAM_TESTNET =
const THRESHOLD_TBTC_SOLANA_PROGRAM_TESTNET =
"Gj93RRt6QB7FjmyokAD5rcMAku7pq3Fk2Aa8y6nNbwsV";

// TODO update this when mainnet tbtc solana program is deployed
const THRESHOLD_TBTC_SOLANA_PROGRAM_MAINNET = "";

export const THRESHOLD_TBTC_SOLANA_PROGRAM: any =
CLUSTER === "mainnet"
? THRESHOLD_TBTC_SOLANA_PROGRAM_MAINNET
: THRESHOLD_TBTC_SOLANA_PROGRAM_TESTNET;

export const THRESHOLD_TBTC_CONTRACTS: any = {
...(CLUSTER === "mainnet"
? THRESHOLD_TBTC_CONTRACTS_MAINNET
Expand Down

0 comments on commit 60fcd91

Please sign in to comment.