import { withCtxAwait } from '@scottburch/with-context'; import { expect } from 'chai'; import { defaultSwarmConfig } from '@bluzelle/testing'; import { getAccountBalance, getAllBalances, getBankParams, getDenomMetadata, getSpendableBalances, getSupplyOf, getTotalSupply } from './query'; import { startSwarmWithClient, stopSwarm } from '@bluzelle/testing/src/swarmUtils'; import { multiSend, send } from './tx'; import { passThroughAwait } from 'promise-passthrough'; import { Params } from '../../curium/lib/generated/cosmos/bank/v1beta1/bank'; import { isE2E } from '@bluzelle/testing/src/e2eUtils'; const testAddresses = [ 'bluzelle1ahtwerncxwadjzntry5n7pzypzwt220hu2ghfj', 'bluzelle1kaxu3n8tmtzsmg3zvlfwa7gs04tr07du554h40', 'bluzelle1daqsqhtdldrpt68qauf5wvtyanen2fmfjww7dl', 'bluzelle14gren5katxznytnhqjl6zt3u0asg2erqgngjzk', 'bluzelle1kq7tqye24lr6muyvdgjwk3vv90ad90z2mrsgcm', 'bluzelle1x9jw9wtle47uy0m8dytcz63jj5gkn08d3uuqtt', 'bluzelle1jn3zxn6c827zught0aanmalr5ehzfg8txjd927', 'bluzelle1c4r64lj9a8442d7luacpq2fwyag5nchld3cuxm', 'bluzelle13td8dk0unl7u5g58qp4wy304zr8hjlnw9kn35l', 'bluzelle1nwng2q2m42eu8wwpekf44ktnev45mqlk8kdgjd', 'bluzelle18gm2ngpud3ys9v84vnwy05y6srawahdk507xs5', 'bluzelle1aa65hq82qwg2uwfuas7gq04dgaxcaj2feqrnzf', 'bluzelle1w64thav7r7t6xhx3nsvda9purd7ax5rn8aukj7', 'bluzelle1pc6ncwdlx70uc4ryh0d8unzfeql0eme76q3ykz', ]; const testMultiSendParams = testAddresses.map((addr) => ({ outputAddress: addr, coins: [{ amount: '100', denom: 'ubnt' }] })); describe('bank module', function () { this.timeout(600_000); beforeEach(stopSwarm); after(stopSwarm); it('getAccountBalance should return account balance for the elt and g4 denoms', () => startSwarmWithClient({ config: defaultSwarmConfig, isE2E: isE2E() }) .then(ctx => Promise.all([getAccountBalance(ctx.bzSdk, ctx.auth.address, 'uelt'), getAccountBalance(ctx.bzSdk, ctx.auth.address, 'ug4')])) .then(([ueltBal,ug4Bal]) => { if(isE2E()){ expect(ueltBal).to.equal(0); expect(ug4Bal).to.equal(0); } else{ expect(ueltBal).to.equal(500000000000000); expect(ug4Bal).to.equal(500000000000000); } }) ); it('getAccountBalance should not charge gas', () => startSwarmWithClient({ config: defaultSwarmConfig, isE2E: isE2E() }) .then(withCtxAwait('balanceBefore', (ctx) => getAccountBalance(ctx.bzSdk, ctx.auth.address))) .then(withCtxAwait('balanceAfter', (ctx) => getAccountBalance(ctx.bzSdk, ctx.auth.address))) .then((ctx) => expect(ctx.balanceAfter).equal(ctx.balanceBefore)) ); it('getTotalsupply should return more than 3 balances for ubnt, uelt, ug4 and other coins', () => startSwarmWithClient({ config: defaultSwarmConfig, isE2E: isE2E() }) .then((ctx) => getTotalSupply(ctx.bzSdk)) .then((result) => expect(result.supply.length).to.greaterThanOrEqual(3)) ); it('getAllBalances should return more than 3 balances for ubnt, uelt, ug4 and other coins', () => startSwarmWithClient({ config: defaultSwarmConfig, isE2E: isE2E() }) .then((ctx) => getAllBalances(ctx.bzSdk, ctx.auth.address)) .then((result) => { if(isE2E()){ expect(result.balances.length).to.equal(1); expect(result.balances[0].amount).to.be.greaterThan(0); }else { expect(result.balances.length).to.equal(3); expect(result.balances[0].amount).to.be.greaterThan(0); expect(result.balances[1].amount).to.be.greaterThan(0); expect(result.balances[2].amount).to.be.greaterThan(0); } }) ); it('getSpendableBalances should return all 3 balances for ubnt, uelt, ug4', () => startSwarmWithClient({ config: defaultSwarmConfig, isE2E: isE2E() }) .then((ctx) => getSpendableBalances(ctx.bzSdk, ctx.auth.address)) .then((result) => { if(isE2E()){ expect(result.balances.length).to.equal(1); expect(result.balances[0].amount).to.be.greaterThan(0); }else { expect(result.balances.length).to.equal(3); expect(result.balances[0].amount).to.be.greaterThan(0); expect(result.balances[1].amount).to.be.greaterThan(0); expect(result.balances[2].amount).to.be.greaterThan(0); } }) ); it('getSupplyOf should return the same balance with the balance from the getTotalSupply', () => startSwarmWithClient({ config: defaultSwarmConfig, isE2E: isE2E() }) .then(withCtxAwait('fromTotal', (ctx) => getTotalSupply(ctx.bzSdk))) .then(withCtxAwait('fromSupplyOf', (ctx) => getSupplyOf(ctx.bzSdk, 'ubnt'))) .then((ctx) => { const ubntBal = ctx.fromTotal.supply.find(b => b.denom === 'ubnt') expect(ubntBal?.amount).to.be.equal(ctx.fromSupplyOf) } ) ); it("getBankParams should return the params of the bank module", () => startSwarmWithClient({ config: defaultSwarmConfig, isE2E: isE2E() }) .then((ctx) => getBankParams(ctx.bzSdk)) .then((result) => expect((result as Params).defaultSendEnabled).to.be.equal(true)) ); it.skip('getDenomMetadata should return the metadata of the bank module', () => startSwarmWithClient({ config: defaultSwarmConfig, isE2E: isE2E() }) .then((ctx) => getDenomMetadata(ctx.bzSdk, 'ubnt')) .then((result) => console.log(result)) ); it('balance should be changed after send transaction', () => startSwarmWithClient({ config: defaultSwarmConfig, isE2E: isE2E() }) .then(passThroughAwait((ctx) => send(ctx.bzSdk, 'bluzelle1ahtwerncxwadjzntry5n7pzypzwt220hu2ghfj', 200, { maxGas: 200_000, gasPrice: 0.1 }))) .then((ctx) => getAccountBalance(ctx.bzSdk, 'bluzelle1ahtwerncxwadjzntry5n7pzypzwt220hu2ghfj')) .then((result) => expect(result).to.be.equal(200)) ); it('balances should be changed after multiSend 2 different tokens to one address', function () { isE2E() && this.skip(); return startSwarmWithClient({ config: defaultSwarmConfig }) .then(withCtxAwait('spendableBal', (ctx) => getSpendableBalances(ctx.bzSdk, ctx.auth.address))) .then(passThroughAwait(ctx => multiSend(ctx.bzSdk, [{ outputAddress: 'bluzelle1ahtwerncxwadjzntry5n7pzypzwt220hu2ghfj', coins: [{ amount: '100', denom: ctx.spendableBal.balances[0].denom }] }, { outputAddress: 'bluzelle1ahtwerncxwadjzntry5n7pzypzwt220hu2ghfj', coins: [{ amount: '200', denom: ctx.spendableBal.balances[1].denom }] } ], { maxGas: 200_000, gasPrice: 0.1 }) )) .then((ctx) => getAllBalances(ctx.bzSdk, 'bluzelle1ahtwerncxwadjzntry5n7pzypzwt220hu2ghfj',)) .then((result) => { expect(result.balances[0].amount).equal(100); expect(result.balances[1].amount).equal(200); }) }); it('The gas used for multiSend should be smaller than sending several times', () => startSwarmWithClient({ config: defaultSwarmConfig, isE2E: isE2E() }) .then(withCtxAwait('multiSendResult', (ctx) => multiSend(ctx.bzSdk, testMultiSendParams, { maxGas: 200_000, gasPrice: 0.1 }))) .then(withCtxAwait('singleSendResult', (ctx) => send(ctx.bzSdk, 'bluzelle1ahtwerncxwadjzntry5n7pzypzwt220hu2ghfj', 100, { maxGas: 200_000, gasPrice: 0.1 }))) .then((ctx) => { expect((ctx.multiSendResult as unknown as { gasUsed: number }).gasUsed) .to .be .lessThan((ctx.singleSendResult as unknown as { gasUsed: number }).gasUsed * 14); }) ); });