-
Notifications
You must be signed in to change notification settings - Fork 32
Transfer from one to many groups #424
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
Open
pragmaxim
wants to merge
27
commits into
master
Choose a base branch
from
multi-group-transfer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
c011269
mutli-group-transfer
pragmaxim 0cd8a3d
fix linter errors
pragmaxim 63f205a
rename-to-lending-test
pragmaxim 49efee6
track request time
pragmaxim 125cd49
cleanup tracking
pragmaxim 6b127ce
avoid changing signer interface for multi-group feature
pragmaxim 544284a
multi-group endpoint openapi name fix
pragmaxim 2729e03
Distribute wealth in lending bot
pragmaxim 9feb212
generate wallet per user in lending bot
pragmaxim 0efe700
multi-group spec
pragmaxim 23a720d
docker image with dev-alephium multi-group support
pragmaxim 00596a7
Transaction test fix
pragmaxim aae1c93
Merge branch 'master' into multi-group-transfer
pragmaxim f1cad93
using alephium docker image with multi-group feature
pragmaxim 68c694c
fixing racing condition
pragmaxim 0748c1c
lint:fix lending and transaction tests
pragmaxim bdc704c
cleanup after merging master
pragmaxim b881c41
update schemas after merging master
pragmaxim b1e953d
Merge branch 'master' into multi-group-transfer
pragmaxim c7403b7
Merge branch 'master' into multi-group-transfer
pragmaxim 85a0835
fixes after merging new features
pragmaxim 2efbdf7
removing test.only modifier
pragmaxim d241853
rename multi-transfer to transfer-from-one-to-many-groups
pragmaxim 5c0a77e
Merge branch 'master' into multi-group-transfer
pragmaxim 76fd33c
upgrade to 3.9.0
pragmaxim 45b9e7b
docker image version fix
pragmaxim 1270de4
Merge branch 'master' into multi-group-transfer
pragmaxim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| /* | ||
| Copyright 2018 - 2022 The Alephium Authors | ||
| This file is part of the alephium project. | ||
|
|
||
| The library is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU Lesser General Public License as published by | ||
| the Free Software Foundation, either version 3 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| The library is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU Lesser General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU Lesser General Public License | ||
| along with the library. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| import { | ||
| convertAlphAmountWithDecimals, | ||
| DEFAULT_GAS_ALPH_AMOUNT, | ||
| NodeProvider, | ||
| number256ToNumber, | ||
| ONE_ALPH, | ||
| SignerProviderSimple, | ||
| SignTransferTxParams, | ||
| SignTransferTxResult, | ||
| TransactionBuilder | ||
| } from '@alephium/web3' | ||
| import { testNodeWallet } from '@alephium/web3-test' | ||
| import { PrivateKeyWallet, deriveHDWalletPrivateKeyForGroup } from '@alephium/web3-wallet' | ||
|
|
||
| class LendingBot { | ||
| private readonly nodeProvider: NodeProvider // This can be initialized with node url + api key in a real application | ||
| readonly userGroups: Map<string, number> | ||
| readonly userWallets: Map<string, PrivateKeyWallet> | ||
|
|
||
| constructor(nodeProvider: NodeProvider) { | ||
| this.nodeProvider = nodeProvider | ||
| this.userGroups = new Map() | ||
| this.userWallets = new Map() | ||
| } | ||
|
|
||
| addUser(userId: string): PrivateKeyWallet { | ||
| if (this.userGroups.has(userId)) { | ||
| throw new Error(`User ${userId} already exists`) | ||
| } | ||
|
|
||
| const groupNumber = this.userGroups.size | ||
| const wallet = PrivateKeyWallet.Random(groupNumber, this.nodeProvider) | ||
| this.userGroups.set(userId, groupNumber) | ||
| this.userWallets.set(userId, wallet) | ||
| return this.getUserWallet(userId) | ||
| } | ||
|
|
||
| getUserWallet(userId: string): PrivateKeyWallet { | ||
| const groupNumber = this.userGroups.get(userId) | ||
| if (groupNumber === undefined) { | ||
| throw new Error(`User ${userId} does not exist`) | ||
| } | ||
| const wallet = this.userWallets.get(userId) | ||
| if (wallet === undefined) { | ||
| throw new Error(`User ${userId} wallet does not exist`) | ||
| } | ||
| return wallet as PrivateKeyWallet | ||
| } | ||
|
|
||
| getUserAddress(userId: string) { | ||
| return this.getUserWallet(userId).address | ||
| } | ||
|
|
||
| async signAndSubmitTransferFromOneToManyGroups( | ||
| signer: SignerProviderSimple, | ||
| params: SignTransferTxParams | ||
| ): Promise<SignTransferTxResult[]> { | ||
| const buildTxResults = await TransactionBuilder.from(this.nodeProvider).buildTransferFromOneToManyGroups( | ||
| params, | ||
| await signer.getPublicKey(params.signerAddress) | ||
| ) | ||
| const results: SignTransferTxResult[] = [] | ||
| for (const tx of buildTxResults) { | ||
| const result = await signer.signAndSubmitUnsignedTx({ | ||
| signerAddress: params.signerAddress, | ||
| unsignedTx: tx.unsignedTx | ||
| }) | ||
| results.push(result) | ||
| } | ||
| return results | ||
| } | ||
|
|
||
| async getUserBalance(userId: string) { | ||
| const userWallet = this.getUserWallet(userId) | ||
| const balance = await userWallet.nodeProvider.addresses.getAddressesAddressBalance(userWallet.address) | ||
| return number256ToNumber(balance.balance, 18) | ||
| } | ||
|
|
||
| async distributeWealth(users: string[], deposit: bigint) { | ||
| const signer = await testNodeWallet() | ||
| const signerAddress = (await signer.getSelectedAccount()).address | ||
|
|
||
| const destinations = users.map((user) => ({ | ||
| address: this.addUser(user).address, | ||
| attoAlphAmount: deposit | ||
| })) | ||
|
|
||
| await this.signAndSubmitTransferFromOneToManyGroups(signer, { | ||
| signerAddress, | ||
| destinations | ||
| }) | ||
| } | ||
|
|
||
| async transfer(fromUserId: string, toUserData: [string, number][]) { | ||
| const signer = this.getUserWallet(fromUserId) | ||
| const signerAddress = signer.address | ||
|
|
||
| const destinations = toUserData.map(([user, amount]) => ({ | ||
| address: this.getUserAddress(user), | ||
| attoAlphAmount: convertAlphAmountWithDecimals(amount)! | ||
| })) | ||
|
|
||
| await this.signAndSubmitTransferFromOneToManyGroups(signer, { | ||
| signerAddress, | ||
| destinations | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| jest.setTimeout(15_000) | ||
|
|
||
| async function track<T>(label: string, fn: () => Promise<T>): Promise<T> { | ||
| const start = Date.now() | ||
| const result = await fn() | ||
| const end = Date.now() | ||
| console.log(`${label} completed in ${end - start} milliseconds`) | ||
| return result | ||
| } | ||
|
|
||
| describe('lendingbot', function () { | ||
| it('should work', async function () { | ||
| const lendingBot = new LendingBot(new NodeProvider('http://127.0.0.1:22973')) | ||
|
|
||
| // each user will start with 1 ALPH | ||
| const users = ['user0', 'user1', 'user2'] | ||
| const deposit = ONE_ALPH | ||
|
|
||
| await track('Distributing alphs among users', async () => { | ||
| await lendingBot.distributeWealth(users, deposit) | ||
| }) | ||
|
|
||
| await track('Check user balances', async () => { | ||
| for (const user of users) { | ||
| const balance = await lendingBot.getUserBalance(user) | ||
| expect(balance).toEqual(1.0) | ||
| } | ||
| }) | ||
|
|
||
| await track('user0 lends to user1 and user2', async () => { | ||
| await lendingBot.transfer('user0', [ | ||
| ['user1', 0.1], | ||
| ['user2', 0.2] | ||
| ]) | ||
| }) | ||
|
|
||
| await track('user1 lends to user2', async () => { | ||
| await lendingBot.transfer('user1', [['user2', 0.3]]) | ||
| }) | ||
|
|
||
| await track('Check user balances', async () => { | ||
| const balance0 = await lendingBot.getUserBalance('user0') | ||
| const balance1 = await lendingBot.getUserBalance('user1') | ||
| const balance2 = await lendingBot.getUserBalance('user2') | ||
|
|
||
| expect(balance0).toEqual(1.0 - 0.1 - 0.2 - DEFAULT_GAS_ALPH_AMOUNT * 2) | ||
| expect(balance1).toEqual(1.0 + 0.1 - 0.3 - DEFAULT_GAS_ALPH_AMOUNT) | ||
| expect(balance2).toEqual(1.0 + 0.2 + 0.3) | ||
| }) | ||
|
|
||
| await track('user1 returns to user0', async () => { | ||
| await lendingBot.transfer('user1', [['user0', 0.1]]) | ||
| }) | ||
|
|
||
| await track('user2 returns to user0 and user1', async () => { | ||
| await lendingBot.transfer('user2', [ | ||
| ['user0', 0.2], | ||
| ['user1', 0.3] | ||
| ]) | ||
| }) | ||
|
|
||
| await track('Check user balances', async () => { | ||
| const finalBalance0 = await lendingBot.getUserBalance('user0') | ||
| const finalBalance1 = await lendingBot.getUserBalance('user1') | ||
| const finalBalance2 = await lendingBot.getUserBalance('user2') | ||
| expect(finalBalance0).toEqual(1.0 - 0.1 - 0.2 + 0.1 + 0.2 - DEFAULT_GAS_ALPH_AMOUNT * 2) | ||
| expect(finalBalance1).toEqual(1.0 + 0.1 - 0.3 - 0.1 + 0.3 - DEFAULT_GAS_ALPH_AMOUNT * 2) | ||
| expect(finalBalance2).toEqual(1.0 + 0.2 + 0.3 - 0.2 - 0.3 - DEFAULT_GAS_ALPH_AMOUNT * 2) | ||
|
|
||
| console.log('Final balances', { finalBalance0, finalBalance1, finalBalance2 }) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please let me know if the
SignerProviderSimplecan have this method public. Not important much, it would be easier to have a single method for build+sign+submit with just aPrivateKeyWallet