Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
talkol opened this issue May 2, 2022 · 0 comments
Open

Comments

@talkol
Copy link
Member

talkol commented May 2, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant