forked from textury/arlocal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
318 additions
and
18 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains 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,205 @@ | ||
import fs from 'fs'; | ||
|
||
import ArLocal from '../../src/app'; | ||
import Arweave from 'arweave'; | ||
import { JWKInterface } from 'arweave/node/lib/wallet'; | ||
import { | ||
getTag, | ||
LoggerFactory, | ||
PstContract, | ||
PstState, | ||
SmartWeave, | ||
SmartWeaveNodeFactory, | ||
SmartWeaveTags, | ||
} from 'redstone-smartweave'; | ||
import path from 'path'; | ||
|
||
describe('Testing the Go WASM Profit Sharing Token', () => { | ||
let wallet: JWKInterface; | ||
let walletAddress: string; | ||
|
||
let initialState: PstState; | ||
|
||
let arweave: Arweave; | ||
let arlocal: ArLocal; | ||
let smartweave: SmartWeave; | ||
let pst: PstContract; | ||
|
||
let contractTxId: string; | ||
|
||
let properForeignContractTxId: string; | ||
let wrongForeignContractTxId: string; | ||
|
||
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(1150, false); | ||
await arlocal.start(); | ||
|
||
arweave = Arweave.init({ | ||
host: 'localhost', | ||
port: 1150, | ||
protocol: 'http', | ||
}); | ||
|
||
LoggerFactory.INST.logLevel('error'); | ||
|
||
smartweave = SmartWeaveNodeFactory.memCached(arweave); | ||
|
||
wallet = await arweave.wallets.generate(); | ||
|
||
const address = await arweave.wallets.getAddress(wallet); | ||
await arweave.api.get(`/mint/${address}/100000000000000000000`); | ||
walletAddress = await arweave.wallets.jwkToAddress(wallet); | ||
|
||
const contractSrc = fs.readFileSync(path.join(__dirname, './data/wasm/go-pst.wasm')); | ||
const stateFromFile: PstState = JSON.parse(fs.readFileSync(path.join(__dirname, './data/token-pst.json'), 'utf8')); | ||
|
||
initialState = { | ||
...stateFromFile, | ||
...{ | ||
owner: walletAddress, | ||
balances: { | ||
...stateFromFile.balances, | ||
[walletAddress]: 555669, | ||
}, | ||
}, | ||
}; | ||
properForeignContractTxId = await smartweave.createContract.deploy({ | ||
wallet, | ||
initState: JSON.stringify({ | ||
...initialState, | ||
...{ | ||
ticker: 'FOREIGN_PST', | ||
name: 'foreign contract', | ||
}, | ||
}), | ||
src: contractSrc, | ||
}); | ||
|
||
wrongForeignContractTxId = await smartweave.createContract.deploy({ | ||
wallet, | ||
initState: JSON.stringify({ | ||
...initialState, | ||
...{ | ||
ticker: 'FOREIGN_PST_2', | ||
name: 'foreign contract 2', | ||
}, | ||
}), | ||
src: contractSrc, | ||
}); | ||
|
||
// deploying contract using the new SDK. | ||
contractTxId = await smartweave.createContract.deploy({ | ||
wallet, | ||
initState: JSON.stringify(initialState), | ||
src: contractSrc, | ||
}); | ||
|
||
// connecting to the PST contract | ||
pst = smartweave.pst(contractTxId); | ||
|
||
// connecting wallet to the PST contract | ||
pst.connect(wallet); | ||
|
||
await arweave.api.get('mine'); | ||
}); | ||
|
||
afterAll(async () => { | ||
await arlocal.stop(); | ||
}); | ||
|
||
it('should properly deploy contract', async () => { | ||
const contractTx = await arweave.transactions.get(contractTxId); | ||
|
||
expect(contractTx).not.toBeNull(); | ||
expect(getTag(contractTx, SmartWeaveTags.CONTRACT_TYPE)).toEqual('wasm'); | ||
expect(getTag(contractTx, SmartWeaveTags.WASM_LANG)).toEqual('go'); | ||
|
||
const contractSrcTx = await arweave.transactions.get(getTag(contractTx, SmartWeaveTags.CONTRACT_SRC_TX_ID)); | ||
expect(getTag(contractSrcTx, SmartWeaveTags.CONTENT_TYPE)).toEqual('application/wasm'); | ||
expect(getTag(contractSrcTx, SmartWeaveTags.WASM_LANG)).toEqual('go'); | ||
}); | ||
|
||
it('should read pst state and balance data', async () => { | ||
expect(await pst.currentState()).toEqual(initialState); | ||
|
||
expect((await pst.currentBalance('uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M')).balance).toEqual(10000000); | ||
expect((await pst.currentBalance('33F0QHcb22W7LwWR1iRC8Az1ntZG09XQ03YWuw2ABqA')).balance).toEqual(23111222); | ||
expect((await pst.currentBalance(walletAddress)).balance).toEqual(555669); | ||
}); | ||
it('should properly transfer tokens', async () => { | ||
await pst.transfer({ | ||
target: 'uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M', | ||
qty: 555, | ||
}); | ||
|
||
await arweave.api.get('mine'); | ||
|
||
expect((await pst.currentState()).balances[walletAddress]).toEqual(555669 - 555); | ||
expect((await pst.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')).balance; | ||
expect(result).toEqual(10000000 + 555); | ||
}); | ||
|
||
// note: the dummy logic on the test contract should add 1000 tokens | ||
// to each address, if the foreign contract state 'ticker' field = 'FOREIGN_PST' | ||
it('should properly read foreign contract state', async () => { | ||
await pst.writeInteraction({ | ||
function: 'foreignCall', | ||
contractTxId: wrongForeignContractTxId, | ||
}); | ||
await arweave.api.get('mine'); | ||
expect((await pst.currentState()).balances[walletAddress]).toEqual(555669 - 555); | ||
expect((await pst.currentState()).balances['uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M']).toEqual(10000000 + 555); | ||
|
||
await pst.writeInteraction({ | ||
function: 'foreignCall', | ||
contractTxId: properForeignContractTxId, | ||
}); | ||
await arweave.api.get('mine'); | ||
expect((await pst.currentState()).balances[walletAddress]).toEqual(555669 - 555 + 1000); | ||
expect((await pst.currentState()).balances['uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M']).toEqual( | ||
10000000 + 555 + 1000, | ||
); | ||
}); | ||
|
||
it('should properly handle runtime errors', async () => { | ||
const result = await pst.dryWrite({ | ||
target: 'uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M', | ||
qty: 555, | ||
}); | ||
|
||
expect(result.type).toEqual('exception'); | ||
expect(result.errorMessage).toEqual('[RE:WTF] unknown function: '); | ||
}); | ||
|
||
it('should properly handle contract errors', async () => { | ||
const result = await pst.dryWrite({ | ||
function: 'transfer', | ||
target: 'uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M', | ||
qty: 0, | ||
}); | ||
|
||
expect(result.type).toEqual('error'); | ||
expect(result.errorMessage).toEqual('[CE:ITQ] invalid transfer qty'); | ||
}); | ||
|
||
it('should honor gas limits', async () => { | ||
pst.setEvaluationOptions({ | ||
gasLimit: 9000000, | ||
}); | ||
|
||
const result = await pst.dryWrite({ | ||
function: 'transfer', | ||
target: 'uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M', | ||
qty: 555, | ||
}); | ||
|
||
expect(result.type).toEqual('exception'); | ||
expect(result.errorMessage.startsWith('[RE:OOG] Out of gas!')).toBeTruthy(); | ||
}); | ||
}); |
This file contains 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 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 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.