Skip to content

Lib: Implement swapAndSplit function - #239

Open
lgalende wants to merge 8 commits into
mainfrom
lib/swap-and-split
Open

Lib: Implement swapAndSplit function#239
lgalende wants to merge 8 commits into
mainfrom
lib/swap-and-split

Conversation

@lgalende

@lgalende lgalende commented Jul 28, 2026

Copy link
Copy Markdown
Member


let totalPctBps: u32 = 0
for (let i = 0; i < allocations.length; i++) totalPctBps += allocations[i].pctBps
if (totalPctBps !== MAX_PCT_BPS) throw new Error('Total allocation percentage must be 10_000 bps')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (totalPctBps !== MAX_PCT_BPS) throw new Error('Total allocation percentage must be 10_000 bps')
if (totalPctBps !== MAX_PCT_BPS) throw new Error('Total allocation percentage must add up to 10_000 bps')

for (let i = 0; i < allocations.length - 1; i++) {
const pctBps = allocations[i].pctBps
dynamicCall1.addCall(MIMIC_HELPER, PCT_SELECTOR, [
EvmDynamicArg.variable(0, 0, false), // amount (swap output)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems to be constant, maybe it can be abstracted into a variable to improve its readability

const recipient = allocations[i].recipient
dynamicCall2.addCall(tokenOut, TRANSFER_SELECTOR, [
EvmDynamicArg.literal([new EvmEncodeParam('address', recipient.toString())], false), // to
EvmDynamicArg.variable(1, i, false), // value (dynamicCall1 sub 'i' result)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, we can use a variable inside the loop to improve its readability, similar to recipient

const dynamicCall4 = EvmDynamicCallBuilder.forChain(chainId).addUser(MIMIC_PUBLIC_SMART_ACCOUNT)
dynamicCall4.addCall(tokenOut, TRANSFER_SELECTOR, [
EvmDynamicArg.literal([new EvmEncodeParam('address', lastRecipient.toString())], false), // to
EvmDynamicArg.variable(3, 0, false), // value (dynamicCall3 result)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

EvmDynamicArg.variable(3, 0, false), // value (dynamicCall3 result)
])

builder.addOperationBuilder(dynamicCall4)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would name dynamicCall1, 2, 3, and 4 properly


builder.addOperationBuilder(dynamicCall1)

// Transfer the corresponding amounts to each recipient, except the last one

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, why "except the last one"


builder.addOperationBuilder(swap)

// Calculate the corresponding amount for each allocation, except the last one

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't fully get why "except the last one"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants