Skip to content

Commit

Permalink
Start to add finality settings
Browse files Browse the repository at this point in the history
  • Loading branch information
barnjamin committed Oct 17, 2023
1 parent d60b7cf commit 8a7f3e6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 23 deletions.
21 changes: 20 additions & 1 deletion core/base/src/constants/finality.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { Network } from "./networks";
import { ChainName } from "./chains";
import { constMap, RoArray } from "../utils";

// Number of blocks before a transaction is considered final
const shareFinalities = [
["Ethereum", 64],
["Sepolia", 64],
["Solana", 32],
["Polygon", 64],
["Polygon", 64], // Note this is faster on !Mainnet currently but only because of guardian settings
["Bsc", 15],
["Avalanche", 1],
["Fantom", 1],
Expand All @@ -14,6 +16,7 @@ const shareFinalities = [
["Sui", 0],
["Aptos", 0],
["Sei", 0],
["Algorand", 0],
] as const;

const finalityThresholds = [
Expand Down Expand Up @@ -41,6 +44,7 @@ const finalityThresholds = [

export const finalityThreshold = constMap(finalityThresholds);

// number of milliseconds between blocks
const blockTimeMilliseconds = [
["Acala", 12000],
["Algorand", 3300],
Expand Down Expand Up @@ -81,3 +85,18 @@ const blockTimeMilliseconds = [
] as const satisfies RoArray<readonly [ChainName, number]>;

export const blockTime = constMap(blockTimeMilliseconds);

// Some chains are required to post proof of their blocks to other chains
// and the transaction containing that proof must be finalized
// before a transaction contained in one of those blocks is considered final
// const rollupContracts = [
// // mainnet/polygon/ethereum 0x86E4Dc95c7FBdBf52e33D563BbDB00823894C287
// // mainnet/optimism/ethereum 0xdfe97868233d1aa22e815a266982f2cf17685a27
// // mainnet/arbitrum/ethereum 0x1c479675ad559dc151f6ec7ed3fbf8cee79582b6
// //
// // testnet/polygon/ethereum 0x2890ba17efe978480615e330ecb65333b880928e
// // testnet/arbitrum/ethereum 0x45af9ed1d03703e480ce7d328fb684bb67da5049 // TODO, is there another one??
// // testnet/optimism/ethereum 0xe6dfba0953616bacab0c9a8ecb3a9bba77fc15c0
// //
// ];
//
64 changes: 42 additions & 22 deletions examples/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,55 @@ import { SolanaPlatform } from "@wormhole-foundation/connect-sdk-solana";
import { getStuff } from "./helpers";

(async function () {
const mwh = new Wormhole("Mainnet", []);
const mf: [number, string][] = [];
for (const [cn, cd] of Object.entries(mwh.conf.chains)) {
const ttf = cd.finalityThreshold * cd.blockTime + cd.blockTime;
mf.push([ttf, cn]);
}
const msort = mf.sort((a, b) => a[0] - b[0]);
console.log("Mainnet");
console.log(msort);

const twh = new Wormhole("Testnet", []);
const tf: [number, string][] = [];
for (const [cn, cd] of Object.entries(twh.conf.chains)) {
const ttf = cd.finalityThreshold * cd.blockTime + cd.blockTime;
tf.push([ttf, cn]);
}
const tsort = tf.sort((a, b) => a[0] - b[0]);
console.log("Testnet");
console.log(tsort);

// init Wormhole object, passing config for which network
// to use (e.g. Mainnet/Testnet) and what Platforms to support
const network: Network = "Testnet";
const wh = new Wormhole(network, [
EvmPlatform,
SolanaPlatform,
CosmwasmPlatform,
]);
// const network: Network = "Mainnet";
// const wh = new Wormhole(network, [
// EvmPlatform,
// SolanaPlatform,
// CosmwasmPlatform,
// ]);

const chain: ChainName = "Injective";
const chainCtx = wh.getChain(chain) as CosmwasmChain;
// const chain: ChainName = "Injective";
// const chainCtx = wh.getChain(chain) as CosmwasmChain;

const { signer } = await getStuff(chainCtx);
const walletAddr = toNative(chain, signer.address());
// const { signer } = await getStuff(chainCtx);
// const walletAddr = toNative(chain, signer.address());

const tb = await chainCtx.getTokenBridge();
// const tb = await chainCtx.getTokenBridge();

const recvChain = wh.getChain("Ethereum");
const { address: receiver } = await getStuff(recvChain);
// const recvChain = wh.getChain("Ethereum");
// const { address: receiver } = await getStuff(recvChain);

//
console.log("creating transaction...");
const xfer = tb.transfer(walletAddr, receiver, "native", 1_000_000n);
// //
// console.log("creating transaction...");
// const xfer = tb.transfer(walletAddr, receiver, "native", 1_000_000n);

// Gather transactions
const unsigned = [];
for await (const msg of xfer) unsigned.push(msg);
const signedTxns = await signer.sign(unsigned);
// // Gather transactions
// const unsigned = [];
// for await (const msg of xfer) unsigned.push(msg);
// const signedTxns = await signer.sign(unsigned);

const txids = await chainCtx.sendWait(signedTxns);
console.log("Sent transactions: ", txids);
// const txids = await chainCtx.sendWait(signedTxns);
// console.log("Sent transactions: ", txids);
})();

0 comments on commit 8a7f3e6

Please sign in to comment.