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

Cycles prop tests #2178

Merged
merged 15 commits into from
Nov 13, 2024
12 changes: 7 additions & 5 deletions examples/ckbtc/wallet/backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import {
// TODO I don't know if we need the minter or ckbtc or icrc canisters anymore?

export default class {
ckBtcPrincipal = getCkBtcPrincipal();
minterPrincipal = getMinterPrincipal();

@update([], IDL.Nat64)
async getBalance(): Promise<bigint> {
return await call(getCkBtcPrincipal(), 'icrc1_balance_of', {
return await call(this.ckBtcPrincipal, 'icrc1_balance_of', {
paramIdlTypes: [Account],
returnIdlType: IDL.Nat,
args: [
Expand All @@ -29,7 +32,7 @@ export default class {
@update([], UpdateBalanceResult)
async updateBalance(): Promise<UpdateBalanceResult> {
const updateBalanceResult: UpdateBalanceResult = await call(
getMinterPrincipal(),
this.minterPrincipal,
'update_balance',
{
paramIdlTypes: [UpdateBalanceArgs],
Expand All @@ -50,7 +53,7 @@ export default class {

@update([], IDL.Text)
async getDepositAddress(): Promise<string> {
return await call(getMinterPrincipal(), 'get_btc_address', {
return await call(this.minterPrincipal, 'get_btc_address', {
paramIdlTypes: [GetBtcAddressArgs],
returnIdlType: IDL.Text,
args: [
Expand All @@ -65,7 +68,7 @@ export default class {
// TODO get rid of Result
@update([IDL.Text, IDL.Nat], TransferResult)
async transfer(to: string, amount: bigint): Promise<TransferResult> {
return await call(getCkBtcPrincipal(), 'icrc1_transfer', {
return await call(this.ckBtcPrincipal, 'icrc1_transfer', {
paramIdlTypes: [TransferArgs],
returnIdlType: TransferResult,
args: [
Expand Down Expand Up @@ -99,7 +102,6 @@ function padPrincipalWithZeros(blob: Uint8Array): Uint8Array {

function getCkBtcPrincipal(): string {
if (process.env.CK_BTC_PRINCIPAL !== undefined) {
console.info(process.env.CK_BTC_PRINCIPAL);
return process.env.CK_BTC_PRINCIPAL;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { call, canisterBalance, IDL, notify, query, trap, update } from 'azle';
import { call, canisterBalance, IDL, notify, query, update } from 'azle';

export default class {
cyclesPrincipal = getCyclesPrincipal();

// Reports the number of cycles returned from the Cycles canister
@update([], IDL.Nat64)
async sendCycles(): Promise<bigint> {
return await call(getCyclesPrincipal(), 'receiveCycles', {
return await call(this.cyclesPrincipal, 'receiveCycles', {
returnIdlType: IDL.Nat64,
cycles: 1_000_000n
});
}

@update([])
sendCyclesNotify(): void {
return notify(getCyclesPrincipal(), 'receiveCycles', {
return notify(this.cyclesPrincipal, 'receiveCycles', {
cycles: 1_000_000n
});
}
Expand All @@ -24,8 +26,8 @@ export default class {
}

function getCyclesPrincipal(): string {
return (
process.env.CYCLES_PRINCIPAL ??
trap('process.env.CYCLES_PRINCIPAL is undefined')
);
if (process.env.CYCLES_PRINCIPAL !== undefined) {
return process.env.CYCLES_PRINCIPAL;
}
throw new Error('process.env.CYCLES_PRINCIPAL is undefined');
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { call, IDL, query, trap, update } from 'azle';
import { call, IDL, query, update } from 'azle';
import {
Account,
TransferArgs,
Expand All @@ -16,58 +16,60 @@ import {
} from 'azle/canisters/icrc_2';

export default class {
icrcPrincipal = getIcrcPrincipal();

@query([], IDL.Vec(IDL.Tuple(IDL.Text, Value)), { composite: true })
async icrc1_metadata(): Promise<[Text, Value][]> {
return await call(getIcrcPrincipal(), 'icrc1_metadata', {
return await call(this.icrcPrincipal, 'icrc1_metadata', {
returnIdlType: IDL.Vec(IDL.Tuple(IDL.Text, Value))
});
}

@query([], IDL.Text, { composite: true })
async icrc1_name(): Promise<string> {
return await call(getIcrcPrincipal(), 'icrc1_name', {
return await call(this.icrcPrincipal, 'icrc1_name', {
returnIdlType: IDL.Text
});
}

@query([], IDL.Nat8, { composite: true })
async icrc1_decimals(): Promise<number> {
return await call(getIcrcPrincipal(), 'icrc1_decimals', {
return await call(this.icrcPrincipal, 'icrc1_decimals', {
returnIdlType: IDL.Nat8
});
}

@query([], IDL.Text, { composite: true })
async icrc1_symbol(): Promise<string> {
return await call(getIcrcPrincipal(), 'icrc1_symbol', {
return await call(this.icrcPrincipal, 'icrc1_symbol', {
returnIdlType: IDL.Text
});
}

@query([], IDL.Nat, { composite: true })
async icrc1_fee(): Promise<string> {
return await call(getIcrcPrincipal(), 'icrc1_fee', {
return await call(this.icrcPrincipal, 'icrc1_fee', {
returnIdlType: IDL.Nat
});
}

@query([], IDL.Nat, { composite: true })
async icrc1_total_supply(): Promise<bigint> {
return await call(getIcrcPrincipal(), 'icrc1_total_supply', {
return await call(this.icrcPrincipal, 'icrc1_total_supply', {
returnIdlType: IDL.Nat
});
}

@query([], IDL.Opt(Account), { composite: true })
async icrc1_minting_account(): Promise<[Account] | []> {
return await call(getIcrcPrincipal(), 'icrc1_minting_account', {
return await call(this.icrcPrincipal, 'icrc1_minting_account', {
returnIdlType: IDL.Opt(Account)
});
}

@query([Account], IDL.Nat, { composite: true })
async icrc1_balance_of(account: Account): Promise<bigint> {
return await call(getIcrcPrincipal(), 'icrc1_balance_of', {
return await call(this.icrcPrincipal, 'icrc1_balance_of', {
paramIdlTypes: [Account],
returnIdlType: IDL.Nat,
args: [account]
Expand All @@ -76,7 +78,7 @@ export default class {

@update([TransferArgs], TransferResult)
async icrc1_transfer(transferArgs: TransferArgs): Promise<TransferResult> {
return await call(getIcrcPrincipal(), 'icrc1_transfer', {
return await call(this.icrcPrincipal, 'icrc1_transfer', {
paramIdlTypes: [TransferArgs],
returnIdlType: TransferResult,
args: [transferArgs]
Expand All @@ -85,14 +87,14 @@ export default class {

@query([], IDL.Vec(SupportedStandard), { composite: true })
async icrc1_supported_standards(): Promise<SupportedStandard[]> {
return await call(getIcrcPrincipal(), 'icrc1_supported_standards', {
return await call(this.icrcPrincipal, 'icrc1_supported_standards', {
returnIdlType: IDL.Vec(SupportedStandard)
});
}

@update([ApproveArgs], ApproveResult)
async icrc2_approve(approveArgs: ApproveArgs): Promise<ApproveResult> {
return await call(getIcrcPrincipal(), 'icrc2_approve', {
return await call(this.icrcPrincipal, 'icrc2_approve', {
paramIdlTypes: [ApproveArgs],
returnIdlType: ApproveResult,
args: [approveArgs]
Expand All @@ -103,7 +105,7 @@ export default class {
async icrc2_transfer_from(
transferFromArgs: TransferFromArgs
): Promise<TransferFromResult> {
return await call(getIcrcPrincipal(), 'icrc2_transfer_from', {
return await call(this.icrcPrincipal, 'icrc2_transfer_from', {
paramIdlTypes: [TransferFromArgs],
returnIdlType: TransferFromResult,
args: [transferFromArgs]
Expand All @@ -114,7 +116,7 @@ export default class {
async icrc2_allowance(
allowanceArgs: AllowanceArgs
): Promise<AllowanceResult> {
return await call(getIcrcPrincipal(), 'icrc2_allowance', {
return await call(this.icrcPrincipal, 'icrc2_allowance', {
paramIdlTypes: [AllowanceArgs],
returnIdlType: AllowanceResult,
args: [allowanceArgs]
Expand All @@ -123,8 +125,8 @@ export default class {
}

function getIcrcPrincipal(): string {
return (
process.env.ICRC_PRINCIPAL ??
trap('process.env.ICRC_PRINCIPAL is undefined')
);
if (process.env.ICRC_PRINCIPAL !== undefined) {
return process.env.ICRC_PRINCIPAL;
}
throw new Error('process.env.ICRC_PRINCIPAL is undefined');
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
} from 'azle/canisters/icp';

export default class {
icpCanisterPrincipal = getIcpCanisterPrincipal();

@update(
[IDL.Text, IDL.Nat64, IDL.Nat64, IDL.Opt(IDL.Nat64)],
TransferResult
Expand All @@ -31,7 +33,7 @@ export default class {
createdAtTime.length === 0
? []
: [{ timestamp_nanos: createdAtTime[0] }];
return await call(getIcpCanisterPrincipal(), 'transfer', {
return await call(this.icpCanisterPrincipal, 'transfer', {
paramIdlTypes: [TransferArgs],
returnIdlType: TransferResult,
args: [
Expand All @@ -53,7 +55,7 @@ export default class {

@update([IDL.Text], Tokens)
async getAccountBalance(address: string): Promise<Tokens> {
return await call(getIcpCanisterPrincipal(), 'account_balance', {
return await call(this.icpCanisterPrincipal, 'account_balance', {
paramIdlTypes: [AccountBalanceArgs],
returnIdlType: Tokens,
args: [
Expand All @@ -66,7 +68,7 @@ export default class {

@update([], TransferFee)
async getTransferFee(): Promise<TransferFee> {
return await call(getIcpCanisterPrincipal(), 'transfer_fee', {
return await call(this.icpCanisterPrincipal, 'transfer_fee', {
paramIdlTypes: [TransferFeeArg],
returnIdlType: TransferFee,
args: [{}]
Expand All @@ -77,7 +79,7 @@ export default class {
async getBlocks(
getBlocksArgs: GetBlocksArgs
): Promise<QueryBlocksResponse> {
return await call(getIcpCanisterPrincipal(), 'query_blocks', {
return await call(this.icpCanisterPrincipal, 'query_blocks', {
paramIdlTypes: [GetBlocksArgs],
returnIdlType: QueryBlocksResponse,
args: [getBlocksArgs]
Expand All @@ -86,7 +88,7 @@ export default class {

@update([], IDL.Text)
async getSymbol(): Promise<string> {
const symbolResult = await call(getIcpCanisterPrincipal(), 'symbol', {
const symbolResult = await call(this.icpCanisterPrincipal, 'symbol', {
returnIdlType: SymbolResult
});

Expand All @@ -95,7 +97,7 @@ export default class {

@update([], IDL.Text)
async getName(): Promise<string> {
const nameResult = await call(getIcpCanisterPrincipal(), 'name', {
const nameResult = await call(this.icpCanisterPrincipal, 'name', {
returnIdlType: NameResult
});

Expand All @@ -105,7 +107,7 @@ export default class {
@update([], IDL.Nat32)
async getDecimals(): Promise<number> {
const decimalsResult = await call(
getIcpCanisterPrincipal(),
this.icpCanisterPrincipal,
'decimals',
{ returnIdlType: DecimalsResult }
);
Expand All @@ -115,7 +117,7 @@ export default class {

@update([], Archives)
async getArchives(): Promise<Archives> {
return await call(getIcpCanisterPrincipal(), 'archives', {
return await call(this.icpCanisterPrincipal, 'archives', {
returnIdlType: Archives
});
}
Expand Down
4 changes: 4 additions & 0 deletions tests/property/ic_api/cycles/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.azle
.dfx
dfx_generated
node_modules
23 changes: 23 additions & 0 deletions tests/property/ic_api/cycles/dfx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"canisters": {
"cycles": {
"type": "azle",
"main": "src/cycles/index.ts",
"declarations": {
"output": "test/dfx_generated/cycles",
"node_compatibility": true
}
},
"intermediary": {
"type": "azle",
"main": "src/intermediary/index.ts",
"declarations": {
"output": "test/dfx_generated/intermediary",
"node_compatibility": true
},
"custom": {
"env": ["CYCLES_PRINCIPAL"]
}
}
}
}
11 changes: 11 additions & 0 deletions tests/property/ic_api/cycles/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testTimeout: 100_000_000,
transform: {
'^.+\\.ts$': ['ts-jest', { isolatedModules: true }],
'^.+\\.js$': 'ts-jest'
},
transformIgnorePatterns: ['/node_modules/(?!(azle)/)'] // Make sure azle is transformed
};
Loading