Skip to content

Commit

Permalink
Improve transact methods
Browse files Browse the repository at this point in the history
  • Loading branch information
polarker committed Aug 7, 2024
1 parent 1382568 commit 82ddf01
Show file tree
Hide file tree
Showing 8 changed files with 367 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .project.json
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@
"bytecodeDebugPatch": "",
"codeHashDebug": "4aa5c769148cada8eeb1cd3791f6e793ed92009ac79ebb64dc79d4d7f2969c8b"
},
"Transact": {
"sourceFile": "test/transact.ral",
"sourceCodeHash": "be72f5d014486592f9ba43c41ded84a6c11dea338f2d0a86736ee6d1bd62f7fe",
"bytecodeDebugPatch": "",
"codeHashDebug": "e69364160d369d3459e19d1b2fcaae7f0cac7059357dba5616147ea18d0c1f0e"
},
"UpdateMapValue": {
"sourceFile": "test/map.ral",
"sourceCodeHash": "f797013ab9c7bf780bb8f452c0724703e877bbd9d71a60b400ed5009eb0f08e8",
Expand Down
45 changes: 45 additions & 0 deletions artifacts/test/Transact.ral.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"version": "v3.5.0",
"name": "Transact",
"bytecode": "01034027404e405c010100000009d353dbb7aab413c40de0b6b3a7640000a9a00013c40de0b6b3a76400002aa10002010200000009d3a248861cb413c40de0b6b3a7640000a8a00013c40de0b6b3a76400002ba10002010000000103d376c7c673a00002",
"codeHash": "e69364160d369d3459e19d1b2fcaae7f0cac7059357dba5616147ea18d0c1f0e",
"fieldsSig": {
"names": [
"totalDeposits"
],
"types": [
"U256"
],
"isMutable": [
true
]
},
"eventsSig": [],
"functions": [
{
"name": "deposit",
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
"returnTypes": []
},
{
"name": "withdraw",
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
"returnTypes": []
},
{
"name": "getTotalDeposits",
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
"returnTypes": [
"U256"
]
}
],
"constants": [],
"enums": []
}
234 changes: 234 additions & 0 deletions artifacts/ts/Transact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */

import {
Address,
Contract,
ContractState,
TestContractResult,
HexString,
ContractFactory,
EventSubscribeOptions,
EventSubscription,
CallContractParams,
CallContractResult,
TestContractParams,
ContractEvent,
subscribeContractEvent,
subscribeContractEvents,
testMethod,
callMethod,
multicallMethods,
fetchContractState,
ContractInstance,
getContractEventsCurrentCount,
TestContractParamsWithoutMaps,
TestContractResultWithoutMaps,
SignExecuteContractMethodParams,
SignExecuteScriptTxResult,
signExecuteMethod,
addStdIdToFields,
encodeContractFields,
} from "@alephium/web3";
import { default as TransactContractJson } from "../test/Transact.ral.json";
import { getContractByCodeHash } from "./contracts";
import {
AddStruct1,
AddStruct2,
Balances,
MapValue,
TokenBalance,
AllStructs,
} from "./types";

// Custom types for the contract
export namespace TransactTypes {
export type Fields = {
totalDeposits: bigint;
};

export type State = ContractState<Fields>;

export interface CallMethodTable {
deposit: {
params: Omit<CallContractParams<{}>, "args">;
result: CallContractResult<null>;
};
withdraw: {
params: Omit<CallContractParams<{}>, "args">;
result: CallContractResult<null>;
};
getTotalDeposits: {
params: Omit<CallContractParams<{}>, "args">;
result: CallContractResult<bigint>;
};
}
export type CallMethodParams<T extends keyof CallMethodTable> =
CallMethodTable[T]["params"];
export type CallMethodResult<T extends keyof CallMethodTable> =
CallMethodTable[T]["result"];
export type MultiCallParams = Partial<{
[Name in keyof CallMethodTable]: CallMethodTable[Name]["params"];
}>;
export type MultiCallResults<T extends MultiCallParams> = {
[MaybeName in keyof T]: MaybeName extends keyof CallMethodTable
? CallMethodTable[MaybeName]["result"]
: undefined;
};
export type MulticallReturnType<Callss extends MultiCallParams[]> =
Callss["length"] extends 1
? MultiCallResults<Callss[0]>
: { [index in keyof Callss]: MultiCallResults<Callss[index]> };

export interface SignExecuteMethodTable {
deposit: {
params: Omit<SignExecuteContractMethodParams<{}>, "args">;
result: SignExecuteScriptTxResult;
};
withdraw: {
params: Omit<SignExecuteContractMethodParams<{}>, "args">;
result: SignExecuteScriptTxResult;
};
getTotalDeposits: {
params: Omit<SignExecuteContractMethodParams<{}>, "args">;
result: SignExecuteScriptTxResult;
};
}
export type SignExecuteMethodParams<T extends keyof SignExecuteMethodTable> =
SignExecuteMethodTable[T]["params"];
export type SignExecuteMethodResult<T extends keyof SignExecuteMethodTable> =
SignExecuteMethodTable[T]["result"];
}

class Factory extends ContractFactory<TransactInstance, TransactTypes.Fields> {
encodeFields(fields: TransactTypes.Fields) {
return encodeContractFields(
addStdIdToFields(this.contract, fields),
this.contract.fieldsSig,
AllStructs
);
}

at(address: string): TransactInstance {
return new TransactInstance(address);
}

tests = {
deposit: async (
params: Omit<
TestContractParamsWithoutMaps<TransactTypes.Fields, never>,
"testArgs"
>
): Promise<TestContractResultWithoutMaps<null>> => {
return testMethod(this, "deposit", params, getContractByCodeHash);
},
withdraw: async (
params: Omit<
TestContractParamsWithoutMaps<TransactTypes.Fields, never>,
"testArgs"
>
): Promise<TestContractResultWithoutMaps<null>> => {
return testMethod(this, "withdraw", params, getContractByCodeHash);
},
getTotalDeposits: async (
params: Omit<
TestContractParamsWithoutMaps<TransactTypes.Fields, never>,
"testArgs"
>
): Promise<TestContractResultWithoutMaps<bigint>> => {
return testMethod(
this,
"getTotalDeposits",
params,
getContractByCodeHash
);
},
};
}

// Use this object to test and deploy the contract
export const Transact = new Factory(
Contract.fromJson(
TransactContractJson,
"",
"e69364160d369d3459e19d1b2fcaae7f0cac7059357dba5616147ea18d0c1f0e",
AllStructs
)
);

// Use this class to interact with the blockchain
export class TransactInstance extends ContractInstance {
constructor(address: Address) {
super(address);
}

async fetchState(): Promise<TransactTypes.State> {
return fetchContractState(Transact, this);
}

view = {
deposit: async (
params?: TransactTypes.CallMethodParams<"deposit">
): Promise<TransactTypes.CallMethodResult<"deposit">> => {
return callMethod(
Transact,
this,
"deposit",
params === undefined ? {} : params,
getContractByCodeHash
);
},
withdraw: async (
params?: TransactTypes.CallMethodParams<"withdraw">
): Promise<TransactTypes.CallMethodResult<"withdraw">> => {
return callMethod(
Transact,
this,
"withdraw",
params === undefined ? {} : params,
getContractByCodeHash
);
},
getTotalDeposits: async (
params?: TransactTypes.CallMethodParams<"getTotalDeposits">
): Promise<TransactTypes.CallMethodResult<"getTotalDeposits">> => {
return callMethod(
Transact,
this,
"getTotalDeposits",
params === undefined ? {} : params,
getContractByCodeHash
);
},
};

transact = {
deposit: async (
params: TransactTypes.SignExecuteMethodParams<"deposit">
): Promise<TransactTypes.SignExecuteMethodResult<"deposit">> => {
return signExecuteMethod(Transact, this, "deposit", params);
},
withdraw: async (
params: TransactTypes.SignExecuteMethodParams<"withdraw">
): Promise<TransactTypes.SignExecuteMethodResult<"withdraw">> => {
return signExecuteMethod(Transact, this, "withdraw", params);
},
getTotalDeposits: async (
params: TransactTypes.SignExecuteMethodParams<"getTotalDeposits">
): Promise<TransactTypes.SignExecuteMethodResult<"getTotalDeposits">> => {
return signExecuteMethod(Transact, this, "getTotalDeposits", params);
},
};

async multicall<Callss extends TransactTypes.MultiCallParams[]>(
...callss: Callss
): Promise<TransactTypes.MulticallReturnType<Callss>> {
return (await multicallMethods(
Transact,
this,
callss,
getContractByCodeHash
)) as TransactTypes.MulticallReturnType<Callss>;
}
}
2 changes: 2 additions & 0 deletions artifacts/ts/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
Sub,
TokenTest,
TokenTestStd,
Transact,
UserAccount,
Warnings,
WrongNFTTest,
Expand Down Expand Up @@ -57,6 +58,7 @@ export function getContractByCodeHash(codeHash: string): Contract {
Sub,
TokenTest,
TokenTestStd,
Transact,
UserAccount,
Warnings,
WrongNFTTest,
Expand Down
1 change: 1 addition & 0 deletions artifacts/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export * from "./OwnerOnly";
export * from "./Sub";
export * from "./TokenTest";
export * from "./TokenTestStd";
export * from "./Transact";
export * from "./UserAccount";
export * from "./Warnings";
export * from "./WrongNFTTest";
Expand Down
21 changes: 21 additions & 0 deletions contracts/test/transact.ral
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Contract Transact(
mut totalDeposits: U256
) {
@using(preapprovedAssets = true, assetsInContract = true)
pub fn deposit() -> () {
transferTokenToSelf!(callerAddress!(), ALPH, 1 alph)
totalDeposits = totalDeposits + 1 alph
return
}

@using(preapprovedAssets = false, assetsInContract = true)
pub fn withdraw() -> () {
transferTokenFromSelf!(callerAddress!(), ALPH, 1 alph)
totalDeposits = totalDeposits - 1 alph
return
}

pub fn getTotalDeposits() -> U256 {
return totalDeposits
}
}
4 changes: 4 additions & 0 deletions packages/web3/src/contract/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1737,6 +1737,10 @@ function getBytecodeTemplate(
const argsLength = '00'
const returnsLength = '00'

if (methodUsePreapprovedAssets && !scriptUseApprovedAssets) {
throw new Error('The contract call requires preapproved assets but none are provided')
}

const [templateVarStoreLocalInstrs, templateVarsLength] = getTemplateVarStoreLocalInstrs(functionSig, structs)

const approveAlphInstrs: string[] = getApproveAlphInstrs(methodUsePreapprovedAssets ? attoAlphAmount : undefined)
Expand Down
Loading

0 comments on commit 82ddf01

Please sign in to comment.