ton-community / ton-contract-executor

The TON Contract Executor allows you to write, debug, and fully test your contracts before launching them to the TON blockchain.
64 stars 11 forks source link

Maintaining contract balance between calls (or returning c7 contents) #3

Open talkol opened 2 years ago

talkol commented 2 years ago

Contract balance between calls

Users would expect the following new test in SmartContract.spec.ts to pass:

    it('should maintain balance between calls', async () => {
        const source = `      
            () recv_internal(int my_balance, int msg_value, cell in_msg_full, slice in_msg_body) {
                return ();
            }

            int get_bal() method_id {
                var [bal, _] = get_balance();
                return bal;
            }
        `
        let contract = await SmartContract.fromFuncSource(source, new Cell())
        await contract.sendInternalMessage(new InternalMessage({
            to: Address.parse('EQD4FPq-PRDieyQKkizFTRtSDyucUIqrj0v_zXJmqaDp6_0t'),
            value: toNano(42),
            bounce: false,
            body: new CommonMessageInfo({
                body: new CellMessage(new Cell())
            })
        }))
        let res = await contract.invokeGetMethod('get_bal', [])

        expect(res.result[0]).toBeInstanceOf(BN)
        expect(res.result[0]).toEqual(toNano(42))
    })

Yes it fails:

    - Expected  - 1
    + Received  + 1

    - "9c7652400"
    + "03e8"

Exporting the value of c7 register

Thank you for allowing to configure c7 register! This indeed allows setting the contract balance easily with a command like:

contract.setC7Config({
    balance: balance.toNumber(),
});

But ideally we would want to configure this behavior to be automatic - so things like balance would be maintained automatically between calls. If users want to disable the automatic maintenance of c7, we can add a flag to SmartContractConfig.

I propose to add the exported contents of the c7 register to the result of every successful execution in TVMExecutionResultOk. This would require exporting the c7 contents as JSON from the c code at the end of vm_exec (and rebuilding the wasm). After doing that, we will have easy control of what to do with it from TypeScript.

We'll be happy to help!

We love this project. We're a team of several TON developers working on an AMM (DEX). Your project is the only decent way to test contracts in the TON ecosystem. We understand that you're probably busy, but we'll be happy to help you maintain it. We have a good understanding of your code and if you want the help, we can add features like c7 export by ourselves as a PR.

@talkol @doronaviguy