Skip to content

Commit

Permalink
fix types for helixconf
Browse files Browse the repository at this point in the history
  • Loading branch information
fewensa committed Aug 1, 2024
1 parent f7618fb commit 232682a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 66 deletions.
44 changes: 17 additions & 27 deletions src/ecosys/safe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ethers} from "ethers";
// import {ethers} from "ethers";
import Safe from "@safe-global/protocol-kit";
import SafeApiKit from "@safe-global/api-kit";

Expand All @@ -13,71 +13,61 @@ export async function init(options) {
}
if (register.sourceSafeWalletUrl) {
let safe;
if (cachedSafe[lifecycle.sourceChainName]) {
safe = cachedSafe[lifecycle.sourceChainName];
if (cachedSafe[lifecycle.sourceChain.code]) {
safe = cachedSafe[lifecycle.sourceChain.code];
} else {
safe = await initSafe({
register,
chainRpc: lifecycle.sourceChainRpc,
chain: lifecycle.sourceChain,
safeWalletUrl: register.sourceSafeWalletUrl,
safeWalletAddress: register.safeWalletAddress ?? register.sourceSafeWalletAddress,
signer,
});
cachedSafe[lifecycle.sourceChainName] = safe;
cachedSafe[lifecycle.sourceChain.code] = safe;
}

options.sourceSafeSdk = safe.safeSdk;
options.sourceSafeService = safe.safeService;
options.sourceProvider = safe.provider;
options.sourceNetwork = safe.network;
options.sourceSigner = safe.wallet;
// options.sourceSigner = safe.wallet;
}
if (register.targetSafeWalletUrl) {
let safe;
if (cachedSafe[lifecycle.targetChainName]) {
safe = cachedSafe[lifecycle.targetChainName];
if (cachedSafe[lifecycle.targetChain.code]) {
safe = cachedSafe[lifecycle.targetChain.code];
} else {
safe = await initSafe({
register,
chainRpc: lifecycle.targetChainRpc,
chain: lifecycle.targetChain,
safeWalletUrl: register.targetSafeWalletUrl,
safeWalletAddress: register.safeWalletAddress ?? register.targetSafeWalletAddress,
signer,
});
cachedSafe[lifecycle.targetChainName] = safe;
cachedSafe[lifecycle.targetChain.code] = safe;
}

options.targetSafeSdk = safe.safeSdk;
options.targetSafeService = safe.safeService;
options.targetProvider = safe.provider;
options.targetNetwork = safe.network;
options.targetSigner = safe.wallet;
// options.targetSigner = safe.wallet;
}
}

async function initSafe(options) {
const {chainRpc, signer, safeWalletUrl, safeWalletAddress} = options;
const provider = new ethers.JsonRpcProvider(chainRpc);
const wallet = new ethers.Wallet(signer, provider);
const safeSdk = await Safe.init({
provider,
const {chain, signer, safeWalletUrl, safeWalletAddress} = options;
const safeSdk = await Safe.default.init({
provider: chain.rpc,
signer,
safeAddress: safeWalletAddress,
});

const network = await provider.getNetwork();
console.log(`init safe for chain ${network.chainId} with ${safeWalletUrl}`);
const safeService = new SafeApiKit({
chainId: network.chainId,
console.log(`init safe for chain ${chain.id} with ${safeWalletUrl}`);
const safeService = new SafeApiKit.default({
chainId: chain.id,
txServiceUrl: safeWalletUrl,
// txServiceUrl: 'https://httpbin.org/anything',
});
return {
safeSdk,
safeService,
provider,
network,
wallet,
};
}

Expand Down
10 changes: 1 addition & 9 deletions src/register/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,8 @@ async function refactorConfig(options) {
return includeConfigs;
}

// function _stdRegisterInfo(register, lifecycle) {
// const {sourceChain} = lifecycle;
// if (!register.contract) {
// register.contract = sourceChain.protocol[register.type];
// }
// }

async function handle(options) {
const {definition, register} = options;
const {register} = options;
const [sourceChainName, targetChainName] = register.bridge.split('->');

let relayerAddress = register.safeWalletAddress ?? register.sourceSafeWalletAddress;
Expand Down Expand Up @@ -158,7 +151,6 @@ async function handle(options) {
contractAddress: sourceChain.protocol[register.type].toLowerCase(),
relayerAddress: relayerAddress.toLowerCase(),
};
// _stdRegisterInfo(register, lifecycle);

options.lifecycle = lifecycle;

Expand Down
33 changes: 9 additions & 24 deletions src/register/lnv2_default.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,6 @@ import {isDisableApprove} from "../ecosys/tool.js";
export async function register(options) {
const {register, lifecycle} = options;

// const _sourceChainId = await $`cast chain-id --rpc-url=${lifecycle.sourceChainRpc}`;
// const _targetChainId = await $`cast chain-id --rpc-url=${lifecycle.targetChainRpc}`;
// let _sourceTokenDecimal;
// try {
// _sourceTokenDecimal = await $`cast call --rpc-url=${lifecycle.sourceChainRpc} ${register.sourceTokenAddress} 'decimals()()'`;
// _sourceTokenDecimal = _sourceChainId.stdout.trim();
// } catch (e) {
// console.log(chalk.yellow(`[warn] can not query decimal from contract(${lifecycle.sourceChainName}): ${e}`));
// }
// let _targetTokenDecimal;
// try {
// _targetTokenDecimal = await $`cast call --rpc-url=${lifecycle.targetChainRpc} ${register.targetTokenAddress} 'decimals()()'`;
// _targetTokenDecimal = _targetTokenDecimal.stdout.trim();
// } catch (e) {
// console.log(chalk.yellow(`[warn] can not query decimal from contract(${lifecycle.targetChainName}): ${e}`));
// }

const sourceChainId = lifecycle.sourceChain.id;
const targetChainId = lifecycle.targetChain.id;
const sourceTokenDecimal = BigInt(lifecycle.sourceToken.decimals);
Expand Down Expand Up @@ -164,14 +147,16 @@ async function registerWithCall(options, callOptions) {

async function registerWithSafe(options, callOptions) {
const {
register, lifecycle, definition,
sourceSafeSdk, sourceSafeService, sourceSigner,
targetSafeSdk, targetSafeService, targetSigner,
register, lifecycle, definition, signer,
sourceSafeSdk, sourceSafeService,
targetSafeSdk, targetSafeService,
} = options;
const {approveFlags, depositFlags, setFeeFlags, withdrawFlags, sourceDepositToTarget, targetChainId} = callOptions;

const txApprove = await $`cast calldata ${approveFlags}`;
const txSetFee = await $`cast calldata ${setFeeFlags}`;
const _signerAddress = await $`cast wallet address ${signer}`.quiet();
const signerAddress = _signerAddress.stdout.trim();

const p0Transactions = [];
if (!tool.isDisableApprove({definition, symbol: register.symbol, chainId: targetChainId})) {
Expand All @@ -198,7 +183,7 @@ async function registerWithSafe(options, callOptions) {
safeSdk: targetSafeSdk,
safeService: targetSafeService,
safeAddress: register.targetSafeWalletAddress ?? register.safeWalletAddress,
senderAddress: targetSigner.address,
senderAddress: signerAddress,
transactions: p0Transactions,
});
console.log(
Expand All @@ -212,15 +197,15 @@ async function registerWithSafe(options, callOptions) {

const p1Transactions = [
{
to: register.contract,
to: lifecycle.contractAddress,
value: '0',
data: txSetFee.stdout.trim(),
},
];
if (withdrawFlags.length) {
const txWithdraw = await $`cast calldata ${withdrawFlags}`;
p1Transactions.push({
to: register.contract,
to: lifecycle.contractAddress,
value: '0',
data: txWithdraw.stdout.trim(),
});
Expand All @@ -231,7 +216,7 @@ async function registerWithSafe(options, callOptions) {
safeSdk: sourceSafeSdk,
safeService: sourceSafeService,
safeAddress: register.sourceSafeWalletAddress ?? register.safeWalletAddress,
senderAddress: sourceSigner.address,
senderAddress: signerAddress,
transactions: p1Transactions,
});
console.log(
Expand Down
10 changes: 6 additions & 4 deletions src/register/lnv2_opposite.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,15 @@ async function registerWithCall(options, callOptions) {

async function registerWithSafe(options, callOptions) {
const {
register, lifecycle, definition,
sourceSafeSdk, sourceSafeService, sourceSigner,
register, lifecycle, definition, signer,
sourceSafeSdk, sourceSafeService,
} = options;
const {approveFlags, setFeeFlags, withdrawFlags, sourceDeposit, sourceChainId} = callOptions;

const txApprove = await $`cast calldata ${approveFlags}`;
const txSetFee = await $`cast calldata ${setFeeFlags}`;
const _signerAddress = await $`cast wallet address ${signer}`.quiet();
const signerAddress = _signerAddress.stdout.trim();

const p0Transactions = [];

Expand All @@ -120,7 +122,7 @@ async function registerWithSafe(options, callOptions) {
});
}
p0Transactions.push({
to: register.contract,
to: lifecycle.contractAddress,
value: lifecycle.sourceToken.type === 'native'
? sourceDeposit.toString()
: '0',
Expand All @@ -132,7 +134,7 @@ async function registerWithSafe(options, callOptions) {
safeSdk: sourceSafeSdk,
safeService: sourceSafeService,
safeAddress: register.sourceSafeWalletAddress ?? register.safeWalletAddress,
senderAddress: sourceSigner.address,
senderAddress: signerAddress,
transactions: p0Transactions,
});
console.log(
Expand Down
6 changes: 4 additions & 2 deletions src/register/lnv3.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,13 @@ async function registerWithCall(options, callOptions) {


async function registerWithSafe(options, callOptions) {
const {register, lifecycle, definition, sourceSafeSdk, sourceSafeService, sourceSigner} = options;
const {register, lifecycle, signer, definition, sourceSafeSdk, sourceSafeService} = options;
const {approvalFlags, depositFlags, setFeeFlags, withdrawFlags, sourceDeposit, sourceChainId} = callOptions;

const txApprove = await $`cast calldata ${approvalFlags}`;
const txSetFee = await $`cast calldata ${setFeeFlags}`;
const _signerAddress = await $`cast wallet address ${signer}`.quiet();
const signerAddress = _signerAddress.stdout.trim();

const transactions = [];
if (!tool.isDisableApprove({definition, symbol: register.symbol, chainId: sourceChainId})) {
Expand Down Expand Up @@ -178,7 +180,7 @@ async function registerWithSafe(options, callOptions) {
safeSdk: sourceSafeSdk,
safeService: sourceSafeService,
safeAddress: register.sourceSafeWalletAddress ?? register.safeWalletAddress,
senderAddress: sourceSigner.address,
senderAddress: signerAddress,
transactions,
});
console.log(
Expand Down

0 comments on commit 232682a

Please sign in to comment.