Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(SMA-598): Smart Account Withdrawals #424

Merged
merged 30 commits into from
Apr 10, 2024

Conversation

joepegler
Copy link
Collaborator

Summary

Public utility method which withdraws funds from Smart Account to recipient (usually EOA)

Related Issue: SMA-598

Change Type

  • Bug Fix
  • Refactor
  • New Feature
  • Breaking Change
  • Documentation Update
  • Performance Improvement
  • Other

Checklist

  • My code follows this project's style guidelines
  • I've reviewed my own code
  • I've added comments for any hard-to-understand areas
  • I've updated the documentation if necessary
  • My changes generate no new warnings
  • I've added tests that prove my fix is effective or my feature works
  • All unit tests pass locally with my changes
  • Any dependent changes have been merged and published

Additional Information

Branch Naming

@joepegler joepegler marked this pull request as ready for review February 20, 2024 11:00
@joepegler joepegler changed the title Smart Account Withdrawals Feat(SMA-598): Smart Account Withdrawals Feb 20, 2024
Copy link

linear bot commented Feb 20, 2024

* const { success } = await wait();
*/
public async withdraw(
recipient: Hex,
Copy link
Collaborator

Choose a reason for hiding this comment

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

we could also make recipient per each withdraw request

Copy link
Collaborator

Choose a reason for hiding this comment

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

optionally. if not provided use the main recipient given above

Copy link
Collaborator Author

@joepegler joepegler Feb 22, 2024

Choose a reason for hiding this comment

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

They can always call withdraw multiple times too per recipient in a loop that'd have the same effect. Actually having a method named 'withdraw' that doesn't go to the owner is slightly confusing, but I couldn't think of a clear path to finding the recipient without it being provided by the dev. I would have hardcoded this if I could have...

Can you think of a way to reliably find the owner in this case? I think it would be best now that I'm thinking about it...

Copy link
Collaborator

Choose a reason for hiding this comment

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

hmm thinking. we could have method single withdraw (which also acts as withdrawERC20)
and also withdrawMultipleERC20 and give them an option to provide receiver for each withdrawal.

these methods are risky though cause if we make mistake in sdk or choose to go rough then all tokens would be transferred to us

Copy link
Collaborator

Choose a reason for hiding this comment

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

I mean

[
   *    { address: USDT, amount: BigInt(1) }

here they could also provide recipient address, instead of having common recipient..or maybe if not provided then fallback to common recipient given above.

I agree with you it could by default go to the owner in most cases where owner is another hot wallet metamask EOA. but they might want to send elsewhere or to a friend.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

OK makes sense. Will add.

* const { wait } = await smartAccount.withdraw(
* account.pubKey, // recipient
* [
* { address: USDT, amount: BigInt(1) },
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are they supposed to give wei value?
or just like symbol 'USDT' and amount 0.5 is possible?

Copy link
Collaborator

Choose a reason for hiding this comment

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

we'd have to keep symbols and chain and token decimals mapping.
and then find out using current chainId..

Copy link
Collaborator Author

@joepegler joepegler Feb 22, 2024

Choose a reason for hiding this comment

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

No I'm not using symbols here, it's a can of worms (we'd need a database of ticker mappings, and it'd have to be 100% reliable) as there's no easy way to match a symbol to an address. Happy to revisit this if people ask for it though. As it is here the dev will need to know the addresses - I think that's fine.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes they give gwei value. In the tsdoc here I've made this clear. I also think this is what devs will expect. Doing the opposite is more likely to cause confusion

Copy link
Collaborator

Choose a reason for hiding this comment

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

got it

const balanceOfSABefore = (await checkBalance(publicClient, smartAccountAddress)) as bigint;
const balanceOfRecipientBefore = (await checkBalance(publicClient, smartAccountOwner)) as bigint;

const { wait } = await smartAccount.withdraw(smartAccountOwner, undefined /* null or undefined or [] */, {
Copy link
Collaborator

Choose a reason for hiding this comment

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

how do you choose the amount to withdraw if paymaster is not involved?

if a paymaster call fails, we would withdraw all which will fail on chain again

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

They could call the getBalances method :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Can you elaborate on this second point? I don't follow

Copy link
Collaborator

Choose a reason for hiding this comment

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

if I want to withdraw native tokens and max amount (say 1 Matic) what's there in SA
and if there is no paymaster or paymaster call fails then pnd goes '0x' which means account payas for gas in native tokens. so we can't transfer 1 and also pay 0.01 matic in gas

Copy link
Collaborator Author

@joepegler joepegler Feb 26, 2024

Choose a reason for hiding this comment

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

Yes that's right. I have thrown this error if a user does not provide his own amount in this case. dev will be expected to find his own amounts to withdraw if no paymaster is used. It'll be on the dev to find the right amount somehow

Copy link
Collaborator Author

@joepegler joepegler Feb 26, 2024

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm basically telling the dev that it's up to them to pick the amount of dust they want to leave behind, assuming they're not using a paymaster.

Copy link
Collaborator

@livingrockrises livingrockrises left a comment

Choose a reason for hiding this comment

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

review i

Copy link
Collaborator

@livingrockrises livingrockrises left a comment

Choose a reason for hiding this comment

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

review ii

Auto stash before merge of "feature/SMA-599_balances" and "origin/develop"

Add withdrawal tests

jsdoc comments

continued

Fix comments

comments

continued

Make `getBalance` available with no arguments

rename tokenAdress -> address

Throw when no amount + no paymaster is set

Tweak comment
@joepegler joepegler changed the base branch from feature/SMA-599_balances to develop February 26, 2024 11:10
@joepegler joepegler marked this pull request as draft March 4, 2024 16:21
@VGabriel45
Copy link
Collaborator

@joepegler the withdraw method implementation seems to be missing

@joepegler joepegler marked this pull request as ready for review April 3, 2024 17:56
@livingrockrises
Copy link
Collaborator

these are all new methods so let's send it and get more feedback.

@livingrockrises livingrockrises merged commit f7e5420 into develop Apr 10, 2024
2 checks passed
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.

4 participants