Skip to content

Commit

Permalink
Tests with fake controller addresses. Get method for request window.
Browse files Browse the repository at this point in the history
  • Loading branch information
1ixi1 committed Jun 9, 2023
1 parent a92dc3e commit 34ecb08
Show file tree
Hide file tree
Showing 9 changed files with 455 additions and 330 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"test": "jest"
},
"devDependencies": {
"@ton-community/blueprint": "^0.9.0",
"@ton-community/sandbox": "^0.10.0",
"@ton-community/blueprint": "^0.10.0",
"@ton-community/sandbox": "^0.11.0",
"@ton-community/test-utils": "^0.2.0",
"@types/jest": "^29.5.0",
"@types/node": "^18.15.5",
Expand Down
3 changes: 3 additions & 0 deletions tests/ControllerPool.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Selected interest rate should be greater or eqal to pool's interest

Then pool should correctly add the loan to it's list.

#### Marks
- 'Request loan bounce should only be accepted from pool address' is
already [done](https://github.com/EmelyanenkoK/jetton_pool/blob/controller_tests/tests/Controller.spec.ts#L434)

## Loan repayment

Expand Down
422 changes: 96 additions & 326 deletions tests/ControllerPool.spec.ts

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions tests/Smoke.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ describe('Pool', () => {
let poolJetton: SandboxContract<DAOJettonMinter>;
let deployer: SandboxContract<TreasuryContract>;

jest.setTimeout(60000); // TODO: remove this

beforeAll(async () => {
blockchain = await Blockchain.create();
deployer = await blockchain.treasury('deployer', {balance: toNano("1000000000")});
Expand Down
76 changes: 76 additions & 0 deletions utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Address, Tuple, TupleItem, TupleItemInt, TupleReader, toNano } from "ton";
import { Cell, Slice, Sender, SenderArguments, ContractProvider, Message, beginCell, Dictionary, MessageRelaxed, Transaction } from "ton-core";
import { Blockchain } from "@ton-community/sandbox";
import { computeMessageForwardFees, MsgPrices } from "./fees";


const randomAddress = (wc: number = 0) => {
const buf = Buffer.alloc(32);
for (let i = 0; i < buf.length; i++) {
buf[i] = Math.floor(Math.random() * 256);
}
return new Address(wc, buf);
};

const differentAddress = (oldAddr:Address) => {

let newAddr = oldAddr;

do {
newAddr = randomAddress(newAddr.workChain);
} while(newAddr.equals(oldAddr));

return newAddr;
}

export const getRandom = (min:number, max:number) => {
return Math.random() * (max - min) + min;
}

enum roundMode {floor, ceil, round};

export const getRandomInt = (min:number, max:number, mode: roundMode = roundMode.floor) => {
let res = getRandom(min, max);

if(mode == roundMode.floor) {
res = Math.floor(res);
}
else if(mode == roundMode.ceil) {
res = Math.ceil(res);
}
else {
res = Math.round(res);
}

return res;
}

export const getRandomTon = (min:number, max:number): bigint => {
return toNano(getRandom(min, max).toFixed(9));
}

export const buff2bigint = (buff: Buffer) : bigint => {
return BigInt("0x" + buff.toString("hex"));
}

export const bigint2buff = (num:bigint) : Buffer => {
return Buffer.from(num.toString(16), 'hex')
}

export const computedGeneric = (trans:Transaction) => {
if(trans.description.type !== "generic")
throw("Expected generic transaction");
if(trans.description.computePhase.type !== "vm")
throw("Compute phase expected")
return trans.description.computePhase;
};

export const getMsgExcess = (trans:Transaction, msg:Message, value:bigint, msgConf:MsgPrices) => {
const fwdFees = computeMessageForwardFees(msgConf, msg);
return value - computedGeneric(trans).gasFees - fwdFees.remaining - fwdFees.fees;
}


export {
differentAddress,
};
8 changes: 8 additions & 0 deletions wrappers/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,12 @@ export class Controller implements Contract {
.endCell(),
});
}

async getRequestWindow(provider: ContractProvider) {
const { stack } = await provider.get("request_window_time", [])
return {
since: stack.readNumber(),
until: stack.readNumber()
};
}
}
2 changes: 1 addition & 1 deletion wrappers/PayoutMinter.compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Address } from 'ton-core';
export const compile: CompilerConfig = {
preCompileHook: async () => {
await compileFunc('PayoutWallet');
const consigliere_address = path.join(__dirname, '..', 'contracts', 'awaited_minter', 'contracts', 'auto', 'consigliere_address.func');
const consigliere_address = path.join(__dirname, '..', 'contracts', 'auto', 'consigliere_address.func');
if (!fs.existsSync(consigliere_address)) {
throw new Error('Consigliere address not defined in contracts/auto/consigliere_address.func, use setConsigliere');
}
Expand Down
2 changes: 1 addition & 1 deletion wrappers/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function poolConfigToCell(config: PoolConfig): Cell {
return beginCell()
.storeUint(0, 8) // state NORMAL
.storeCoins(0) // total_balance
.storeUint(100, 16) // interest_rate
.storeUint(100, 16) // minimal interest_rate
.storeInt(config.optimistic_deposit_withdrawals, 1) // optimistic_deposit_withdrawals
.storeInt(-1n, 1) // deposits_open?
.storeUint(0, 256) // saved_validator_set_hash
Expand Down
Loading

0 comments on commit 34ecb08

Please sign in to comment.