dev.v2: collapse the ETH methods @ gusc1a-ossdev-jsl5

This commit is contained in:
Josh Siegel 2022-05-23 16:10:35 +00:00 committed by jumpsiegel
parent 7dc2b7b7ae
commit 0821f42f74
2 changed files with 163 additions and 187 deletions

View File

@ -6,71 +6,71 @@ const ci = !!process.env.CI;
// see devnet.md
export const ETH_NODE_URL = ci ? "ws://eth-devnet:8545" : "ws://localhost:8545";
export const ETH_PRIVATE_KEY =
"0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d";
"0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d";
export const ETH_CORE_BRIDGE_ADDRESS =
"0xC89Ce4735882C9F0f0FE26686c53074E09B0D550";
"0xC89Ce4735882C9F0f0FE26686c53074E09B0D550";
export const ETH_TOKEN_BRIDGE_ADDRESS =
"0x0290FB167208Af455bB137780163b7B7a9a10C16";
"0x0290FB167208Af455bB137780163b7B7a9a10C16";
export const SOLANA_HOST = ci
? "http://solana-devnet:8899"
: "http://localhost:8899";
? "http://solana-devnet:8899"
: "http://localhost:8899";
export const SOLANA_PRIVATE_KEY = new Uint8Array([
14, 173, 153, 4, 176, 224, 201, 111, 32, 237, 183, 185, 159, 247, 22, 161,
89, 84, 215, 209, 212, 137, 10, 92, 157, 49, 29, 192, 101, 164, 152, 70, 87,
65, 8, 174, 214, 157, 175, 126, 98, 90, 54, 24, 100, 177, 247, 77, 19, 112,
47, 44, 165, 109, 233, 102, 14, 86, 109, 29, 134, 145, 132, 141,
14, 173, 153, 4, 176, 224, 201, 111, 32, 237, 183, 185, 159, 247, 22, 161, 89,
84, 215, 209, 212, 137, 10, 92, 157, 49, 29, 192, 101, 164, 152, 70, 87, 65,
8, 174, 214, 157, 175, 126, 98, 90, 54, 24, 100, 177, 247, 77, 19, 112, 47,
44, 165, 109, 233, 102, 14, 86, 109, 29, 134, 145, 132, 141,
]);
export const SOLANA_CORE_BRIDGE_ADDRESS =
"Bridge1p5gheXUvJ6jGWGeCsgPKgnE3YgdGKRVCMY9o";
"Bridge1p5gheXUvJ6jGWGeCsgPKgnE3YgdGKRVCMY9o";
export const SOLANA_TOKEN_BRIDGE_ADDRESS =
"B6RHG3mfcckmrYN1UhmJzyS1XX3fZKbkeUcpJe9Sy3FE";
"B6RHG3mfcckmrYN1UhmJzyS1XX3fZKbkeUcpJe9Sy3FE";
export const TERRA_NODE_URL = ci
? "http://terra-terrad:1317"
: "http://localhost:1317";
? "http://terra-terrad:1317"
: "http://localhost:1317";
export const TERRA_CHAIN_ID = "localterra";
export const TERRA_GAS_PRICES_URL = ci
? "http://terra-fcd:3060/v1/txs/gas_prices"
: "http://localhost:3060/v1/txs/gas_prices";
? "http://terra-fcd:3060/v1/txs/gas_prices"
: "http://localhost:3060/v1/txs/gas_prices";
export const TERRA_CORE_BRIDGE_ADDRESS =
"terra18vd8fpwxzck93qlwghaj6arh4p7c5n896xzem5";
"terra18vd8fpwxzck93qlwghaj6arh4p7c5n896xzem5";
export const TERRA_TOKEN_BRIDGE_ADDRESS =
"terra10pyejy66429refv3g35g2t7am0was7ya7kz2a4";
"terra10pyejy66429refv3g35g2t7am0was7ya7kz2a4";
export const TERRA_PRIVATE_KEY =
"notice oak worry limit wrap speak medal online prefer cluster roof addict wrist behave treat actual wasp year salad speed social layer crew genius";
"notice oak worry limit wrap speak medal online prefer cluster roof addict wrist behave treat actual wasp year salad speed social layer crew genius";
export const TEST_ERC20 = "0x2D8BE6BF0baA74e0A907016679CaE9190e80dD0A";
export const TEST_SOLANA_TOKEN = "2WDq7wSs9zYrpx2kbHDA4RUTRch2CCTP6ZWaH4GNfnQQ";
export const WORMHOLE_RPC_HOSTS = ci
? ["http://guardian:7071"]
: ["http://localhost:7071"];
? ["http://guardian:7071"]
: ["http://localhost:7071"];
export type Environment = "devnet" | "testnet" | "mainnet";
export const CLUSTER: Environment = "devnet" as Environment; //This is the currently selected environment.
export const TERRA_HOST =
CLUSTER === "mainnet"
? {
URL: "https://lcd.terra.dev",
chainID: "columbus-5",
name: "mainnet",
}
: CLUSTER === "testnet"
? {
URL: "https://bombay-lcd.terra.dev",
chainID: "bombay-12",
name: "testnet",
}
: {
URL: TERRA_NODE_URL,
chainID: "columbus-5",
name: "localterra",
};
CLUSTER === "mainnet"
? {
URL: "https://lcd.terra.dev",
chainID: "columbus-5",
name: "mainnet",
}
: CLUSTER === "testnet"
? {
URL: "https://bombay-lcd.terra.dev",
chainID: "bombay-12",
name: "testnet",
}
: {
URL: TERRA_NODE_URL,
chainID: "columbus-5",
name: "localterra",
};
describe("consts should exist", () => {
it("has Solana test token", () => {
expect.assertions(1);
const connection = new Connection(SOLANA_HOST, "confirmed");
return expect(
connection.getAccountInfo(new PublicKey(TEST_SOLANA_TOKEN))
).resolves.toBeTruthy();
});
it("has Solana test token", () => {
expect.assertions(1);
const connection = new Connection(SOLANA_HOST, "confirmed");
return expect(
connection.getAccountInfo(new PublicKey(TEST_SOLANA_TOKEN))
).resolves.toBeTruthy();
});
});

View File

@ -78,46 +78,32 @@ export async function transferFromEth(
recipientChain: ChainId | ChainName,
recipientAddress: Uint8Array,
relayerFee: ethers.BigNumberish = 0,
overrides: PayableOverrides & { from?: string | Promise<string> } = {}
overrides: PayableOverrides & { from?: string | Promise<string> } = {},
payload: Uint8Array | null = null
) {
const recipientChainId = coalesceChainId(recipientChain);
const bridge = Bridge__factory.connect(tokenBridgeAddress, signer);
const v = await bridge.transferTokens(
tokenAddress,
amount,
recipientChainId,
recipientAddress,
relayerFee,
createNonce(),
overrides
);
const receipt = await v.wait();
return receipt;
}
export async function transferFromEthWithPayload(
tokenBridgeAddress: string,
signer: ethers.Signer,
tokenAddress: string,
amount: ethers.BigNumberish,
recipientChain: ChainId | ChainName,
recipientAddress: Uint8Array,
payload : Uint8Array,
relayerFee: ethers.BigNumberish,
overrides: PayableOverrides & { from?: string | Promise<string> } = {}
) {
const recipientChainId = coalesceChainId(recipientChain);
const bridge = Bridge__factory.connect(tokenBridgeAddress, signer);
const v = await bridge.transferTokensWithPayload(
tokenAddress,
amount,
recipientChainId,
recipientAddress,
relayerFee,
createNonce(),
payload,
overrides
);
const v =
payload === null
? await bridge.transferTokens(
tokenAddress,
amount,
recipientChainId,
recipientAddress,
relayerFee,
createNonce(),
overrides
)
: await bridge.transferTokensWithPayload(
tokenAddress,
amount,
recipientChainId,
recipientAddress,
relayerFee,
createNonce(),
payload,
overrides
);
const receipt = await v.wait();
return receipt;
}
@ -129,47 +115,34 @@ export async function transferFromEthNative(
recipientChain: ChainId | ChainId,
recipientAddress: Uint8Array,
relayerFee: ethers.BigNumberish = 0,
overrides: PayableOverrides & { from?: string | Promise<string> } = {}
overrides: PayableOverrides & { from?: string | Promise<string> } = {},
payload: Uint8Array | null = null
) {
const recipientChainId = coalesceChainId(recipientChain);
const bridge = Bridge__factory.connect(tokenBridgeAddress, signer);
const v = await bridge.wrapAndTransferETH(
recipientChainId,
recipientAddress,
relayerFee,
createNonce(),
{
...overrides,
value: amount,
}
);
const receipt = await v.wait();
return receipt;
}
export async function transferFromEthNativeWithPayload(
tokenBridgeAddress: string,
signer: ethers.Signer,
amount: ethers.BigNumberish,
recipientChain: ChainId | ChainId,
recipientAddress: Uint8Array,
relayerFee: ethers.BigNumberish,
payload : Uint8Array,
overrides: PayableOverrides & { from?: string | Promise<string> } = {}
) {
const recipientChainId = coalesceChainId(recipientChain);
const bridge = Bridge__factory.connect(tokenBridgeAddress, signer);
const v = await bridge.wrapAndTransferETHWithPayload(
recipientChainId,
recipientAddress,
relayerFee,
createNonce(),
payload,
{
...overrides,
value: amount,
}
);
const v =
payload === null
? await bridge.wrapAndTransferETH(
recipientChainId,
recipientAddress,
relayerFee,
createNonce(),
{
...overrides,
value: amount,
}
)
: await bridge.wrapAndTransferETHWithPayload(
recipientChainId,
recipientAddress,
relayerFee,
createNonce(),
payload,
{
...overrides,
value: amount,
}
);
const receipt = await v.wait();
return receipt;
}
@ -266,7 +239,7 @@ export async function transferNativeSol(
targetAddress: Uint8Array,
targetChain: ChainId | ChainName,
relayerFee: BigInt = BigInt(0),
payload : Uint8Array | null = null
payload: Uint8Array | null = null
) {
//https://github.com/solana-labs/solana-program-library/blob/master/token/js/client/token.js
const rentBalance = await Token.getMinBalanceRentForExemptAccount(connection);
@ -298,8 +271,11 @@ export async function transferNativeSol(
);
//Normal approve & transfer instructions, except that the wSOL is sent from the ancillary account.
const { transfer_native_ix, transfer_native_with_payload_ix, approval_authority_address } =
await importTokenWasm();
const {
transfer_native_ix,
transfer_native_with_payload_ix,
approval_authority_address,
} = await importTokenWasm();
const nonce = createNonce().readUInt32LE(0);
const transferIx = await getBridgeFeeIx(
connection,
@ -317,8 +293,8 @@ export async function transferNativeSol(
let messageKey = Keypair.generate();
const ix = ixFromRust(
(payload != null) ?
transfer_native_with_payload_ix(
payload !== null
? transfer_native_with_payload_ix(
tokenBridgeAddress,
bridgeAddress,
payerAddress,
@ -332,7 +308,7 @@ export async function transferNativeSol(
coalesceChainId(targetChain),
payload
)
: transfer_native_ix(
: transfer_native_ix(
tokenBridgeAddress,
bridgeAddress,
payerAddress,
@ -384,7 +360,7 @@ export async function transferFromSolana(
originChain?: ChainId | ChainName,
fromOwnerAddress?: string,
relayerFee: BigInt = BigInt(0),
payload : Uint8Array | null = null
payload: Uint8Array | null = null
) {
const originChainId: ChainId | undefined = originChain
? coalesceChainId(originChain)
@ -418,66 +394,66 @@ export async function transferFromSolana(
}
const ix = ixFromRust(
isSolanaNative
? (payload != null) ?
transfer_native_with_payload_ix(
tokenBridgeAddress,
bridgeAddress,
payerAddress,
messageKey.publicKey.toString(),
fromAddress,
mintAddress,
nonce,
amount,
relayerFee,
targetAddress,
coalesceChainId(targetChain),
payload
? payload !== null
? transfer_native_with_payload_ix(
tokenBridgeAddress,
bridgeAddress,
payerAddress,
messageKey.publicKey.toString(),
fromAddress,
mintAddress,
nonce,
amount,
relayerFee,
targetAddress,
coalesceChainId(targetChain),
payload
)
: transfer_native_ix(
tokenBridgeAddress,
bridgeAddress,
payerAddress,
messageKey.publicKey.toString(),
fromAddress,
mintAddress,
nonce,
amount,
relayerFee,
targetAddress,
coalesceChainId(targetChain)
: transfer_native_ix(
tokenBridgeAddress,
bridgeAddress,
payerAddress,
messageKey.publicKey.toString(),
fromAddress,
mintAddress,
nonce,
amount,
relayerFee,
targetAddress,
coalesceChainId(targetChain)
)
: payload !== null
? transfer_wrapped_with_payload_ix(
tokenBridgeAddress,
bridgeAddress,
payerAddress,
messageKey.publicKey.toString(),
fromAddress,
fromOwnerAddress || payerAddress,
originChainId as number, // checked by isSolanaNative
originAddress as Uint8Array, // checked by throw
nonce,
amount,
relayerFee,
targetAddress,
coalesceChainId(targetChain),
payload
)
: transfer_wrapped_ix(
tokenBridgeAddress,
bridgeAddress,
payerAddress,
messageKey.publicKey.toString(),
fromAddress,
fromOwnerAddress || payerAddress,
originChainId as number, // checked by isSolanaNative
originAddress as Uint8Array, // checked by throw
nonce,
amount,
relayerFee,
targetAddress,
coalesceChainId(targetChain)
)
: (payload != null) ?
transfer_wrapped_with_payload_ix(
tokenBridgeAddress,
bridgeAddress,
payerAddress,
messageKey.publicKey.toString(),
fromAddress,
fromOwnerAddress || payerAddress,
originChainId as number, // checked by isSolanaNative
originAddress as Uint8Array, // checked by throw
nonce,
amount,
relayerFee,
targetAddress,
coalesceChainId(targetChain),
payload
)
: transfer_wrapped_ix(
tokenBridgeAddress,
bridgeAddress,
payerAddress,
messageKey.publicKey.toString(),
fromAddress,
fromOwnerAddress || payerAddress,
originChainId as number, // checked by isSolanaNative
originAddress as Uint8Array, // checked by throw
nonce,
amount,
relayerFee,
targetAddress,
coalesceChainId(targetChain)
)
);
const transaction = new SolanaTransaction().add(transferIx, approvalIx, ix);
const { blockhash } = await connection.getRecentBlockhash();
@ -511,7 +487,7 @@ export async function transferFromAlgorand(
receiver: string,
chain: ChainId | ChainName,
fee: bigint,
payload : Uint8Array | null = null
payload: Uint8Array | null = null
): Promise<TransactionSignerPair[]> {
const recipientChainId = coalesceChainId(chain);
const tokenAddr: string = getApplicationAddress(tokenBridgeId);
@ -631,8 +607,8 @@ export async function transferFromAlgorand(
bigIntToBytes(recipientChainId, 8),
bigIntToBytes(fee, 8),
];
if (payload != null) {
args.push(payload);
if (payload !== null) {
args.push(payload);
}
let acTxn = makeApplicationCallTxnFromObject({
from: senderAddr,