Files
warp/src/__tests__/integration/basic/pst.test.ts
2022-06-13 09:18:11 +02:00

170 lines
5.8 KiB
TypeScript

import fs from 'fs';
import ArLocal from 'arlocal';
import Arweave from 'arweave';
import { JWKInterface } from 'arweave/node/lib/wallet';
import {
InteractionResult,
LoggerFactory,
PstContract,
PstState,
SmartWeave,
SmartWeaveNodeFactory
} from '@smartweave';
import path from 'path';
import { addFunds, mineBlock } from '../_helpers';
describe('Testing the Profit Sharing Token', () => {
let contractSrc: string;
let wallet: JWKInterface;
let walletAddress: string;
let initialState: PstState;
let arweave: Arweave;
let arlocal: ArLocal;
let smartweave: SmartWeave;
let pst: PstContract;
let pstVM: PstContract;
beforeAll(async () => {
// note: each tests suit (i.e. file with tests that Jest is running concurrently
// with another files has to have ArLocal set to a different port!)
arlocal = new ArLocal(1820, false);
await arlocal.start();
arweave = Arweave.init({
host: 'localhost',
port: 1820,
protocol: 'http'
});
LoggerFactory.INST.logLevel('error');
smartweave = SmartWeaveNodeFactory.forTesting(arweave);
wallet = await arweave.wallets.generate();
await addFunds(arweave, wallet);
walletAddress = await arweave.wallets.jwkToAddress(wallet);
contractSrc = fs.readFileSync(path.join(__dirname, '../data/token-pst.js'), 'utf8');
const stateFromFile: PstState = JSON.parse(fs.readFileSync(path.join(__dirname, '../data/token-pst.json'), 'utf8'));
initialState = {
...stateFromFile,
...{
owner: walletAddress,
balances: {
...stateFromFile.balances,
[walletAddress]: 555669
}
}
};
// deploying contract using the new SDK.
const contractTxId = await smartweave.createContract.deploy({
wallet,
initState: JSON.stringify(initialState),
src: contractSrc
});
// connecting to the PST contract
pst = smartweave.pst(contractTxId);
pstVM = smartweave.pst(contractTxId).setEvaluationOptions({
useVM2: true
}) as PstContract;
// connecting wallet to the PST contract
pst.connect(wallet);
pstVM.connect(wallet);
await mineBlock(arweave);
});
afterAll(async () => {
await arlocal.stop();
});
it('should read pst state and balance data', async () => {
expect(await pst.currentState()).toEqual(initialState);
expect(await pstVM.currentState()).toEqual(initialState);
expect((await pst.currentBalance('uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M')).balance).toEqual(10000000);
expect((await pstVM.currentBalance('uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M')).balance).toEqual(10000000);
expect((await pst.currentBalance('33F0QHcb22W7LwWR1iRC8Az1ntZG09XQ03YWuw2ABqA')).balance).toEqual(23111222);
expect((await pstVM.currentBalance('33F0QHcb22W7LwWR1iRC8Az1ntZG09XQ03YWuw2ABqA')).balance).toEqual(23111222);
expect((await pst.currentBalance(walletAddress)).balance).toEqual(555669);
expect((await pstVM.currentBalance(walletAddress)).balance).toEqual(555669);
});
it('should properly transfer tokens', async () => {
await pst.transfer({
target: 'uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M',
qty: 555
});
await mineBlock(arweave);
expect((await pst.currentState()).balances[walletAddress]).toEqual(555669 - 555);
expect((await pstVM.currentState()).balances[walletAddress]).toEqual(555669 - 555);
expect((await pst.currentState()).balances['uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M']).toEqual(10000000 + 555);
expect((await pstVM.currentState()).balances['uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M']).toEqual(
10000000 + 555
);
});
it('should properly view contract state', async () => {
const result = await pst.currentBalance('uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M');
const resultVM = await pst.currentBalance('uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M');
expect(result.balance).toEqual(10000000 + 555);
expect(resultVM.balance).toEqual(10000000 + 555);
expect(result.ticker).toEqual('EXAMPLE_PST_TOKEN');
expect(resultVM.ticker).toEqual('EXAMPLE_PST_TOKEN');
expect(result.target).toEqual('uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M');
expect(resultVM.target).toEqual('uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M');
});
it("should properly evolve contract's source code", async () => {
expect((await pst.currentState()).balances[walletAddress]).toEqual(555114);
expect((await pstVM.currentState()).balances[walletAddress]).toEqual(555114);
const newSource = fs.readFileSync(path.join(__dirname, '../data/token-evolve.js'), 'utf8');
const newSrcTxId = await pst.saveSource({ src: newSource });
await mineBlock(arweave);
await pst.evolve(newSrcTxId);
await mineBlock(arweave);
// note: the evolved balance always adds 555 to the result
expect((await pst.currentBalance(walletAddress)).balance).toEqual(555114 + 555);
expect((await pstVM.currentBalance(walletAddress)).balance).toEqual(555114 + 555);
});
it('should properly perform dry write with overwritten caller', async () => {
const newWallet = await arweave.wallets.generate();
const overwrittenCaller = await arweave.wallets.jwkToAddress(newWallet);
await pst.transfer({
target: overwrittenCaller,
qty: 1000
});
await mineBlock(arweave);
// note: transfer should be done from the "overwrittenCaller" address, not the "walletAddress"
const result: InteractionResult<PstState, unknown> = await pst.dryWrite(
{
function: 'transfer',
target: 'uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M',
qty: 333
},
overwrittenCaller
);
expect(result.state.balances[walletAddress]).toEqual(555114 - 1000);
expect(result.state.balances['uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M']).toEqual(10000000 + 555 + 333);
expect(result.state.balances[overwrittenCaller]).toEqual(1000 - 333);
});
});