Skip to content

Commit

Permalink
multi-group spec
Browse files Browse the repository at this point in the history
  • Loading branch information
pragmaxim committed Oct 7, 2024
1 parent 9feb212 commit 0efe700
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 29 deletions.
14 changes: 8 additions & 6 deletions packages/web3-test/src/test-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ export async function getSigner(alphAmount = ONE_ALPH * 100n, group = 0): Promis
}
const rootWallet = new PrivateKeyWallet({ privateKey: testPrivateKey })
const wallet = PrivateKeyWallet.Random(group)
const destinations = [{ address: wallet.address, attoAlphAmount: alphAmount }]
await rootWallet.signAndSubmitTransferTx({ signerAddress: testAddress, destinations })
if (alphAmount > 0n) {
const destinations = [{ address: wallet.address, attoAlphAmount: alphAmount }]
await rootWallet.signAndSubmitTransferTx({ signerAddress: testAddress, destinations })
}
return wallet
} catch (_) {
throw new Error('Failed to get signer, please restart the devnet')
} catch (error) {
throw new Error(`Failed to get signer, please restart the devnet due to : ${error}`)
}
}

Expand All @@ -118,8 +120,8 @@ export async function getSigners(
wallets.push(wallet)
}
return wallets
} catch (_) {
throw new Error('Failed to get signers, please restart the devnet')
} catch (error) {
throw new Error(`Failed to get signers, please restart the devnet due tot : ${error}`)
}
}

Expand Down
59 changes: 36 additions & 23 deletions test/transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ 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 { subscribeToTxStatus } from '../packages/web3'
import { subscribeToTxStatus, TransactionBuilder } from '../packages/web3'
import { node } from '../packages/web3'
import { SubscribeOptions, sleep } from '../packages/web3'
import { web3 } from '../packages/web3'
Expand All @@ -25,7 +25,8 @@ import { PrivateKeyWallet } from '@alephium/web3-wallet'
import { ONE_ALPH } from '../packages/web3/src'
import { Add, Sub, AddMain } from '../artifacts/ts'
import { getSigner } from '@alephium/web3-test'
import { TransactionBuilder } from '@alephium/web3/dist/src/signer/tx-builder'

jest.setTimeout(10_000)

describe('transactions', function () {
let signer: PrivateKeyWallet
Expand All @@ -37,32 +38,40 @@ describe('transactions', function () {

it('should build multi-group transfer', async () => {
const nodeProvider = web3.getCurrentNodeProvider()
const signer0 = await getSigner(100n * ONE_ALPH, 0)
const signer1 = await getSigner(0n, 1)
const signer2 = await getSigner(0n, 2)
const signer0 = await getSigner(100n * ONE_ALPH, 1)
const signer1 = await getSigner(0n, 2)
const signer2 = await getSigner(0n, 3)
const signer3 = await getSigner(0n, 3)
const signer4 = await getSigner(0n, 0)

const signer0Balance = await nodeProvider.addresses.getAddressesAddressBalance(signer0.address)
expect(BigInt(signer0Balance.balance)).toBe(100n * ONE_ALPH)

const transferFrom0to1and2 = await TransactionBuilder.from(nodeProvider).buildMultiGroupTransferTx(
{
signerAddress: signer0.address,
destinations: [signer1, signer2].map((signer) => ({
address: signer.address,
attoAlphAmount: 2n * ONE_ALPH
attoAlphAmount: 10n * ONE_ALPH
}))
},
signer0.publicKey
await signer0.getPublicKey(signer0.address)
)

const transferFrom0to1and2Result = await Promise.all(
transferFrom0to1and2.map(async (tx) => {
return await signer.signAndSubmitUnsignedTx({
signerAddress: signer0.publicKey,
return await signer0.signAndSubmitUnsignedTx({
signerAddress: signer0.address,
unsignedTx: tx.unsignedTx
})
})
)

const signer1Balance = await nodeProvider.addresses.getAddressesAddressBalance(signer1.address)
expect(BigInt(signer1Balance.balance)).toBe(10n * ONE_ALPH)
const signer2Balance = await nodeProvider.addresses.getAddressesAddressBalance(signer2.address)
expect(BigInt(signer2Balance.balance)).toBe(10n * ONE_ALPH)

const transferFrom1to3and4 = await TransactionBuilder.from(nodeProvider).buildMultiGroupTransferTx(
{
signerAddress: signer1.address,
Expand All @@ -76,8 +85,8 @@ describe('transactions', function () {

const transferFrom1to3and4Result = await Promise.all(
transferFrom1to3and4.map(async (tx) => {
return await signer.signAndSubmitUnsignedTx({
signerAddress: signer1.publicKey,
return await signer1.signAndSubmitUnsignedTx({
signerAddress: signer1.address,
unsignedTx: tx.unsignedTx
})
})
Expand All @@ -96,27 +105,31 @@ describe('transactions', function () {

const transferFrom2to3and4Result = await Promise.all(
transferFrom2to3and4.map(async (tx) => {
return await signer.signAndSubmitUnsignedTx({
signerAddress: signer2.publicKey,
return await signer2.signAndSubmitUnsignedTx({
signerAddress: signer2.address,
unsignedTx: tx.unsignedTx
})
})
)

const signer1Balance = await nodeProvider.addresses.getAddressesAddressBalance(signer0.address)
const signer2Balance = await nodeProvider.addresses.getAddressesAddressBalance(signer1.address)
const signer3Balance = await nodeProvider.addresses.getAddressesAddressBalance(signer2.address)
const signer0FinalBalance = await nodeProvider.addresses.getAddressesAddressBalance(signer0.address)
const signer1FinalBalance = await nodeProvider.addresses.getAddressesAddressBalance(signer1.address)
const signer2FinalBalance = await nodeProvider.addresses.getAddressesAddressBalance(signer2.address)
const signer3FinalBalance = await nodeProvider.addresses.getAddressesAddressBalance(signer3.address)
const signer4FinalBalance = await nodeProvider.addresses.getAddressesAddressBalance(signer4.address)

const gasCostTransferFrom0to1and2 = transferFrom0to1and2Result.reduce((sum, item) => sum + BigInt(item.gasAmount) * BigInt(item.gasPrice), BigInt(0))

Check failure on line 121 in test/transaction.test.ts

View workflow job for this annotation

GitHub Actions / build (20)

Replace `(sum,·item)·=>·sum·+·BigInt(item.gasAmount)·*·BigInt(item.gasPrice),·BigInt(0)` with `⏎······(sum,·item)·=>·sum·+·BigInt(item.gasAmount)·*·BigInt(item.gasPrice),⏎······BigInt(0)⏎····`
const gasCostTransferFrom1to3and4 = transferFrom1to3and4Result.reduce((sum, item) => sum + BigInt(item.gasAmount) * BigInt(item.gasPrice), BigInt(0))

Check failure on line 122 in test/transaction.test.ts

View workflow job for this annotation

GitHub Actions / build (20)

Replace `(sum,·item)·=>·sum·+·BigInt(item.gasAmount)·*·BigInt(item.gasPrice),·BigInt(0)` with `⏎······(sum,·item)·=>·sum·+·BigInt(item.gasAmount)·*·BigInt(item.gasPrice),⏎······BigInt(0)⏎····`
const gasCostTransferFrom2to3and4 = transferFrom2to3and4Result.reduce((sum, item) => sum + BigInt(item.gasAmount) * BigInt(item.gasPrice), BigInt(0))

Check failure on line 123 in test/transaction.test.ts

View workflow job for this annotation

GitHub Actions / build (20)

Replace `(sum,·item)·=>·sum·+·BigInt(item.gasAmount)·*·BigInt(item.gasPrice),·BigInt(0)` with `⏎······(sum,·item)·=>·sum·+·BigInt(item.gasAmount)·*·BigInt(item.gasPrice),⏎······BigInt(0)⏎····`
const expectedSigner1Balance = 100n * ONE_ALPH - 10n * ONE_ALPH - gasCostTransferFrom0to1and2
const expectedSigner2Balance = 10n * ONE_ALPH - 5n * ONE_ALPH - gasCostTransferFrom1to3and4
const expectedSigner3Balance = 5n * ONE_ALPH

expect(BigInt(signer1Balance.balance)).toBe(expectedSigner1Balance)
expect(BigInt(signer2Balance.balance)).toBe(expectedSigner2Balance)
expect(BigInt(signer3Balance.balance)).toBe(expectedSigner3Balance)
const expectedSigner0Balance = 100n * ONE_ALPH - 20n * ONE_ALPH - gasCostTransferFrom0to1and2
const expectedSigner1Balance = 10n * ONE_ALPH - 2n * ONE_ALPH - gasCostTransferFrom1to3and4
const expectedSigner2Balance = 10n * ONE_ALPH - 2n * ONE_ALPH - gasCostTransferFrom2to3and4

expect(BigInt(signer0FinalBalance.balance)).toBe(expectedSigner0Balance)
expect(BigInt(signer1FinalBalance.balance)).toBe(expectedSigner1Balance)
expect(BigInt(signer2FinalBalance.balance)).toBe(expectedSigner2Balance)
expect(BigInt(signer3FinalBalance.balance)).toBe(4n * ONE_ALPH)
expect(BigInt(signer4FinalBalance.balance)).toBe(4n * ONE_ALPH)
})

it('should subscribe transaction status', async () => {
Expand Down

0 comments on commit 0efe700

Please sign in to comment.