Skip to content

Commit

Permalink
enable feature for all register
Browse files Browse the repository at this point in the history
  • Loading branch information
fewensa committed Aug 20, 2024
1 parent 3e9ff58 commit 2f6ec67
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 75 deletions.
5 changes: 3 additions & 2 deletions src/ecosys/safe.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const cachedNonce = {};

export async function init(options) {
const {register, lifecycle, signer} = options;
if (!lifecycle.accepted) return;
if (!register.safeWalletAddress && !register.sourceSafeWalletAddress && !register.targetSafeWalletAddress) {
return;
}
Expand All @@ -21,7 +22,7 @@ export async function init(options) {
register,
chain: lifecycle.sourceChain,
safeWalletUrl: register.sourceSafeWalletUrl,
safeWalletAddress: register.safeWalletAddress ?? register.sourceSafeWalletAddress,
safeWalletAddress: register.sourceSafeWalletAddress ?? register.safeWalletAddress,
signer,
});
cachedSafe[lifecycle.sourceChain.code] = safe;
Expand All @@ -40,7 +41,7 @@ export async function init(options) {
register,
chain: lifecycle.targetChain,
safeWalletUrl: register.targetSafeWalletUrl,
safeWalletAddress: register.safeWalletAddress ?? register.targetSafeWalletAddress,
safeWalletAddress: register.targetSafeWalletAddress ?? register.safeWalletAddress,
signer,
});
cachedSafe[lifecycle.targetChain.code] = safe;
Expand Down
29 changes: 14 additions & 15 deletions src/register/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ async function handle(options) {
process.exit(1);
}

const accepted = arg.option('accept');
options.lifecycle = {
accepted,
sourceChain,
targetChain,
sourceToken,
Expand All @@ -176,23 +178,20 @@ async function handle(options) {
return;
}

const accepted = arg.option('accept');
if (accepted) {
await safe.init(options);
switch (register.type) {
case 'lnv3':
await lnv3.register(options);
break;
case 'lnv2-default':
await lnv2Default.register(options);
break;
case 'lnv2-opposite':
await lnv2Opposite.register(options);
break;
}
await safe.init(options);
switch (register.type) {
case 'lnv3':
await lnv3.register(options);
break;
case 'lnv2-default':
await lnv2Default.register(options);
break;
case 'lnv2-opposite':
await lnv2Opposite.register(options);
break;
}

await ensureLock(ensureLockOptions, true);
await ensureLock(ensureLockOptions, accepted);
console.log(chalk.green(`the bridge ${_identifyRegisterName(register)} registered`));
}

Expand Down
103 changes: 61 additions & 42 deletions src/register/lnv2_default.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ async function registerWithCall(options, callOptions) {
lifecycle.targetToken.address,
]);
await $`echo cast send ${approveFlags}`;
approveFlags.unshift(`--private-key=${signer}`);
const txApprove = await $`cast send ${approveFlags}`.quiet();
console.log(txApprove.stdout);
if (lifecycle.accepted) {
approveFlags.unshift(`--private-key=${signer}`);
const txApprove = await $`cast send ${approveFlags}`.quiet();
console.log(txApprove.stdout);
}
}

const depositFlagsValue = lifecycle.targetToken.type === 'native'
Expand All @@ -117,9 +119,11 @@ async function registerWithCall(options, callOptions) {
` --value=${depositFlagsValue}`,
]);
await $`echo cast send ${depositFlags}`;
depositFlags.unshift(`--private-key=${signer}`);
const txDeposit = await $`cast send ${depositFlags}`.quiet();
console.log(txDeposit.stdout);
if (lifecycle.accepted) {
depositFlags.unshift(`--private-key=${signer}`);
const txDeposit = await $`cast send ${depositFlags}`.quiet();
console.log(txDeposit.stdout);
}
}

if (withdrawFlags.length) {
Expand All @@ -128,20 +132,24 @@ async function registerWithCall(options, callOptions) {
lifecycle.contractAddress,
]);
await $`echo cast send ${withdrawFlags}`;
withdrawFlags.unshift(`--private-key=${signer}`);
const txWithdraw = await $`cast send ${withdrawFlags}`.quiet();
await $`cast calldata ${withdrawFlags}`;
console.log(txWithdraw.stdout);
if (lifecycle.accepted) {
withdrawFlags.unshift(`--private-key=${signer}`);
const txWithdraw = await $`cast send ${withdrawFlags}`.quiet();
await $`cast calldata ${withdrawFlags}`;
console.log(txWithdraw.stdout);
}
}

setFeeFlags.unshift(...[
...sourceSendFlags,
lifecycle.contractAddress,
]);
await $`echo cast send ${setFeeFlags}`;
setFeeFlags.unshift(`--private-key=${signer}`);
const txSetFee = await $`cast send ${setFeeFlags}`.quiet();
console.log(txSetFee.stdout);
if (lifecycle.accepted) {
setFeeFlags.unshift(`--private-key=${signer}`);
const txSetFee = await $`cast send ${setFeeFlags}`.quiet();
console.log(txSetFee.stdout);
}
}

async function registerWithSafe(options, callOptions) {
Expand Down Expand Up @@ -176,22 +184,27 @@ async function registerWithSafe(options, callOptions) {
});
}

console.log(p0Transactions);
const targetSafeWalletAddress = register.targetSafeWalletAddress ?? register.safeWalletAddress;
if (p0Transactions.length) {
const p0 = await safe.propose({
definition,
safeSdk: targetSafeSdk,
safeService: targetSafeService,
safeAddress: register.targetSafeWalletAddress ?? register.safeWalletAddress,
senderAddress: signerAddress,
transactions: p0Transactions,
});
console.log(
chalk.green('proposed deposit transaction to'),
`${lifecycle.targetChain.code}: ${register.safeWalletAddress ?? register.targetSafeWalletAddress} (safe)`
);
if (p0 && arg.isDebug()) {
console.log(p0);
if (lifecycle.accepted) {
const p0 = await safe.propose({
definition,
safeSdk: targetSafeSdk,
safeService: targetSafeService,
safeAddress: targetSafeWalletAddress,
senderAddress: signerAddress,
transactions: p0Transactions,
});
console.log(
chalk.green('proposed deposit transaction to'),
`${lifecycle.targetChain.code}: ${targetSafeWalletAddress} (safe)`
);
if (p0 && arg.isDebug()) {
console.log(p0);
}
} else {
console.log(`call safe: [${targetSafeWalletAddress}] ${register.targetSafeWalletUrl}`);
console.log(p0Transactions);
}
}

Expand All @@ -211,20 +224,26 @@ async function registerWithSafe(options, callOptions) {
});
}

const p1 = await safe.propose({
definition,
safeSdk: sourceSafeSdk,
safeService: sourceSafeService,
safeAddress: register.sourceSafeWalletAddress ?? register.safeWalletAddress,
senderAddress: signerAddress,
transactions: p1Transactions,
});
console.log(
chalk.green('proposed register transaction to'),
`${lifecycle.sourceChain.code}: ${register.safeWalletAddress ?? register.sourceSafeWalletAddress} (safe)`
);
if (p1 && arg.isDebug()) {
console.log(p1);
const sourceSafeWalletAddress = register.sourceSafeWalletAddress ?? register.safeWalletAddress;
if (lifecycle.accepted) {
const p1 = await safe.propose({
definition,
safeSdk: sourceSafeSdk,
safeService: sourceSafeService,
safeAddress: sourceSafeWalletAddress,
senderAddress: signerAddress,
transactions: p1Transactions,
});
console.log(
chalk.green('proposed register transaction to'),
`${lifecycle.sourceChain.code}: ${sourceSafeWalletAddress} (safe)`
);
if (p1 && arg.isDebug()) {
console.log(p1);
}
} else {
console.log(`call safe: [${sourceSafeWalletAddress}] ${register.sourceSafeWalletUrl}`);
console.log(p1Transactions);
}
}

26 changes: 18 additions & 8 deletions src/register/lnv2_opposite.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ async function registerWithCall(options, callOptions) {
lifecycle.sourceToken.address,
]);
await $`echo cast send ${approveFlags}`;
approveFlags.unshift(`--private-key=${signer}`);
const txApprove = await $`cast send ${approveFlags}`.quiet();
console.log(txApprove.stdout);
if (lifecycle.accepted) {
approveFlags.unshift(`--private-key=${signer}`);
const txApprove = await $`cast send ${approveFlags}`.quiet();
console.log(txApprove.stdout);
}
}

const setFeeFlagsValue = lifecycle.sourceToken.type === 'native'
Expand All @@ -95,9 +97,11 @@ async function registerWithCall(options, callOptions) {
`--value=${setFeeFlagsValue}`,
]);
await $`echo cast send ${setFeeFlags}`;
setFeeFlags.unshift(`--private-key=${signer}`);
const txSetFee = await $`cast send ${setFeeFlags}`.quiet();
console.log(txSetFee.stdout);
if (lifecycle.accepted) {
setFeeFlags.unshift(`--private-key=${signer}`);
const txSetFee = await $`cast send ${setFeeFlags}`.quiet();
console.log(txSetFee.stdout);
}
}

async function registerWithSafe(options, callOptions) {
Expand Down Expand Up @@ -130,17 +134,23 @@ async function registerWithSafe(options, callOptions) {
data: txSetFee.stdout.trim(),
});

const safeWalletAddress = register.sourceSafeWalletAddress ?? register.safeWalletAddress;
if (!lifecycle.accepted) {
console.log(`call safe: [${safeWalletAddress}] ${register.sourceSafeWalletUrl}`);
console.log(p0Transactions);
return;
}
const p1 = await safe.propose({
definition,
safeSdk: sourceSafeSdk,
safeService: sourceSafeService,
safeAddress: register.sourceSafeWalletAddress ?? register.safeWalletAddress,
safeAddress: safeWalletAddress,
senderAddress: signerAddress,
transactions: p0Transactions,
});
console.log(
chalk.green('proposed register transaction to'),
`${lifecycle.sourceChain.code}: ${register.safeWalletAddress ?? register.sourceSafeWalletAddress} (safe)`
`${lifecycle.sourceChain.code}: ${safeWalletAddress} (safe)`
);
if (p1 && arg.isDebug()) {
console.log(p1);
Expand Down
26 changes: 18 additions & 8 deletions src/register/lnv3.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,23 @@ async function registerWithCall(options, callOptions) {
`--value=${depositFlagsValue}`,
]);
await $`echo cast send ${depositFlags}`
depositFlags.unshift(`--private-key=${signer}`);
const txDeposit = await $`cast send ${depositFlags}`.quiet();
console.log(txDeposit.stdout);
if (lifecycle.accepted) {
depositFlags.unshift(`--private-key=${signer}`);
const txDeposit = await $`cast send ${depositFlags}`.quiet();
console.log(txDeposit.stdout);
}
}
if (withdrawFlags.length) {
withdrawFlags.unshift(...[
...sendFlags,
lifecycle.contractAddress,
]);
await $`echo cast send ${withdrawFlags}`
withdrawFlags.unshift(`--private-key=${signer}`);
const txWithdraw = await $`cast send ${withdrawFlags}`.quiet();
console.log(txWithdraw.stdout);
if (lifecycle.accepted) {
withdrawFlags.unshift(`--private-key=${signer}`);
const txWithdraw = await $`cast send ${withdrawFlags}`.quiet();
console.log(txWithdraw.stdout);
}
}
}

Expand Down Expand Up @@ -175,17 +179,23 @@ async function registerWithSafe(options, callOptions) {
});
}

const safeWalletAddress = register.sourceSafeWalletAddress ?? register.safeWalletAddress;
if (!lifecycle.accepted) {
console.log(`call safe: [${safeWalletAddress}] ${register.sourceSafeWalletUrl}`);
console.log(transactions);
return;
}
const p0 = await safe.propose({
definition,
safeSdk: sourceSafeSdk,
safeService: sourceSafeService,
safeAddress: register.sourceSafeWalletAddress ?? register.safeWalletAddress,
safeAddress: safeWalletAddress,
senderAddress: signerAddress,
transactions,
});
console.log(
chalk.green('proposed register transaction to'),
`${lifecycle.sourceChain.code}: ${register.safeWalletAddress ?? register.sourceSafeWalletAddress} (safe)`
`${lifecycle.sourceChain.code}: ${safeWalletAddress} (safe)`
);
if (p0 && arg.isDebug()) {
console.log(p0);
Expand Down

0 comments on commit 2f6ec67

Please sign in to comment.