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

Support Chained Transactions #427

Merged
merged 15 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions .project.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
"bytecodeDebugPatch": "=4-2+18=11-1+3=10+ca7e020748656c6c6f2c200121",
"codeHashDebug": "eb4209d8f543d9f623d72578f7ed9b271d62cf396dcce42d10f5e68dba3cecd3"
},
"Deposit": {
"sourceFile": "test/transact.ral",
"sourceCodeHash": "9280bdda186be31318d56d5fb1dfb8153fc8350a83ac37015de0f1cd20dc78cc",
"bytecodeDebugPatch": "",
"codeHashDebug": ""
},
"DeprecatedNFTTest1": {
"sourceFile": "nft/deprecated_nft.ral",
"sourceCodeHash": "ef29c0be80692016b71dc5a400cf093884acef3e07ed2c7ead78bc3927e0f478",
Expand Down Expand Up @@ -216,13 +222,13 @@
},
"MultiDeposit": {
"sourceFile": "test/transact.ral",
"sourceCodeHash": "04184bcb0fbfe358d2175574d6479ec9882445ef09a1bc521f3e742847afceb6",
"sourceCodeHash": "9280bdda186be31318d56d5fb1dfb8153fc8350a83ac37015de0f1cd20dc78cc",
"bytecodeDebugPatch": "",
"codeHashDebug": ""
},
"MultiWithdraw": {
"sourceFile": "test/transact.ral",
"sourceCodeHash": "04184bcb0fbfe358d2175574d6479ec9882445ef09a1bc521f3e742847afceb6",
"sourceCodeHash": "9280bdda186be31318d56d5fb1dfb8153fc8350a83ac37015de0f1cd20dc78cc",
"bytecodeDebugPatch": "",
"codeHashDebug": ""
},
Expand Down Expand Up @@ -306,7 +312,7 @@
},
"Transact": {
"sourceFile": "test/transact.ral",
"sourceCodeHash": "04184bcb0fbfe358d2175574d6479ec9882445ef09a1bc521f3e742847afceb6",
"sourceCodeHash": "9280bdda186be31318d56d5fb1dfb8153fc8350a83ac37015de0f1cd20dc78cc",
"bytecodeDebugPatch": "",
"codeHashDebug": "85e3e9a803741af8e92bd43b1b07cde53f39b86cab0ef1a85bab12b10d691b55"
},
Expand Down Expand Up @@ -334,6 +340,12 @@
"bytecodeDebugPatch": "",
"codeHashDebug": "873e095edb39cdb4b11b1157003daeacad06d259a938cd270e22b8e89b75feea"
},
"Withdraw": {
"sourceFile": "test/transact.ral",
"sourceCodeHash": "9280bdda186be31318d56d5fb1dfb8153fc8350a83ac37015de0f1cd20dc78cc",
"bytecodeDebugPatch": "",
"codeHashDebug": ""
},
"WithdrawNFTCollectionTest": {
"sourceFile": "nft/withdraw_nft_collection_test.ral",
"sourceCodeHash": "b06eed2252a147738da24c9098452123c444d325f8de4d6134d10fea92b3ee45",
Expand Down
25 changes: 25 additions & 0 deletions artifacts/test/Deposit.ral.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"version": "v3.7.0",
"name": "Deposit",
"bytecodeTemplate": "01010300000007b413c40de0b6b3a7640000a20c0c{0}0100",
"fieldsSig": {
"names": [
"c"
],
"types": [
"Transact"
],
"isMutable": [
false
]
},
"functions": [
{
"name": "main",
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
"returnTypes": []
}
]
}
25 changes: 25 additions & 0 deletions artifacts/test/Withdraw.ral.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"version": "v3.7.0",
"name": "Withdraw",
"bytecodeTemplate": "010103000000040c0c{0}0101",
"fieldsSig": {
"names": [
"c"
],
"types": [
"Transact"
],
"isMutable": [
false
]
},
"functions": [
{
"name": "main",
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
"returnTypes": []
}
]
}
12 changes: 12 additions & 0 deletions artifacts/ts/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getContractByCodeHash } from "./contracts";
import { default as AddMainScriptJson } from "../add/AddMain.ral.json";
import { default as CallScript0ScriptJson } from "../test/CallScript0.ral.json";
import { default as CallScript1ScriptJson } from "../test/CallScript1.ral.json";
import { default as DepositScriptJson } from "../test/Deposit.ral.json";
import { default as DestroyAddScriptJson } from "../add/DestroyAdd.ral.json";
import { default as GreeterMainScriptJson } from "../greeter/GreeterMain.ral.json";
import { default as InsertIntoMapScriptJson } from "../test/InsertIntoMap.ral.json";
Expand All @@ -26,6 +27,7 @@ import { default as TemplateArrayVarScriptJson } from "../test/TemplateArrayVar.
import { default as TestAssertScriptJson } from "../test/TestAssert.ral.json";
import { default as UpdateMapValueScriptJson } from "../test/UpdateMapValue.ral.json";
import { default as UpdateUserAccountScriptJson } from "../test/UpdateUserAccount.ral.json";
import { default as WithdrawScriptJson } from "../test/Withdraw.ral.json";
import { default as WithdrawNFTCollectionTestScriptJson } from "../nft/WithdrawNFTCollectionTest.ral.json";
import {
AddStruct1,
Expand Down Expand Up @@ -57,6 +59,11 @@ export const CallScript1 = new ExecutableScript<
getContractByCodeHash
);

export const Deposit = new ExecutableScript<{ c: HexString }>(
Script.fromJson(DepositScriptJson, "", AllStructs),
getContractByCodeHash
);

export const DestroyAdd = new ExecutableScript<{
add: HexString;
caller: Address;
Expand Down Expand Up @@ -145,6 +152,11 @@ export const UpdateUserAccount = new ExecutableScript<{
getContractByCodeHash
);

export const Withdraw = new ExecutableScript<{ c: HexString }>(
Script.fromJson(WithdrawScriptJson, "", AllStructs),
getContractByCodeHash
);

export const WithdrawNFTCollectionTest = new ExecutableScript<{
collection: HexString;
amount: bigint;
Expand Down
8 changes: 8 additions & 0 deletions contracts/test/transact.ral
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,11 @@ TxScript MultiWithdraw(c: [Transact; 2]) {
c[0].withdrawToken(1e18)
c[1].withdrawToken(1e18)
}

TxScript Deposit(c: Transact) {
c.deposit{callerAddress!() -> ALPH: 1 alph}()
}

TxScript Withdraw(c: Transact) {
c.withdraw()
}
2 changes: 1 addition & 1 deletion docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ services:
condition: service_healthy

alephium:
image: alephium/alephium:v3.7.0
image: liuhongchao/dev-alephium:3.7.0_29-43e3ae8d
polarker marked this conversation as resolved.
Show resolved Hide resolved
restart: unless-stopped
ports:
- 19973:19973/tcp
Expand Down
6 changes: 4 additions & 2 deletions packages/web3-test/src/test-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ export async function getSigner(alphAmount = ONE_ALPH * 100n, group = 0): Promis
}
const rootWallet = new PrivateKeyWallet({ privateKey: testPrivateKey })
const wallet = PrivateKeyWallet.Random(group)
const destinations = [{ address: wallet.address, attoAlphAmount: alphAmount }]
await rootWallet.signAndSubmitTransferTx({ signerAddress: testAddress, destinations })
if (alphAmount > 0n) {
const destinations = [{ address: wallet.address, attoAlphAmount: alphAmount }]
await rootWallet.signAndSubmitTransferTx({ signerAddress: testAddress, destinations })
}
return wallet
Copy link

@pragmaxim pragmaxim Oct 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot make sense of it as a newcomer, we sign the transfer with rootWallet and return PrivateKeyWallet.Random(group) which does not seem to hold any important information 🤔 How can we later use it to sign transactions? As we do signer1.publicKey / signer1.address

SOLVED: Sorry I understand now, it is a generator

} catch (_) {
throw new Error('Failed to get signer, please restart the devnet')
Copy link

@pragmaxim pragmaxim Oct 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please print the error cause it reveals the reason of failure :

  } catch (error) {
    throw new Error(`Failed to get signer, please restart the devnet due to : ${error}`)
  }

Expand Down
114 changes: 104 additions & 10 deletions packages/web3/src/api/api-alephium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,51 @@ export interface BrokerInfo {
}
}

/** BuildChainedDeployContractTx */
export interface BuildChainedDeployContractTx {
value: BuildDeployContractTx
type: string
}

/** BuildChainedDeployContractTxResult */
export interface BuildChainedDeployContractTxResult {
value: BuildDeployContractTxResult
type: string
}

/** BuildChainedExecuteScriptTx */
export interface BuildChainedExecuteScriptTx {
value: BuildExecuteScriptTx
type: string
}

/** BuildChainedExecuteScriptTxResult */
export interface BuildChainedExecuteScriptTxResult {
value: BuildExecuteScriptTxResult
type: string
}

/** BuildChainedTransferTx */
export interface BuildChainedTransferTx {
value: BuildTransferTx
type: string
}

/** BuildChainedTransferTxResult */
export interface BuildChainedTransferTxResult {
value: BuildTransferTxResult
type: string
}

/** BuildChainedTx */
export type BuildChainedTx = BuildChainedDeployContractTx | BuildChainedExecuteScriptTx | BuildChainedTransferTx

/** BuildChainedTxResult */
export type BuildChainedTxResult =
| BuildChainedDeployContractTxResult
| BuildChainedExecuteScriptTxResult
| BuildChainedTransferTxResult

/** BuildDeployContractTx */
export interface BuildDeployContractTx {
/** @format hex-string */
Expand Down Expand Up @@ -353,8 +398,8 @@ export interface BuildSweepMultisig {
targetBlockHash?: string
}

/** BuildTransaction */
export interface BuildTransaction {
/** BuildTransferTx */
export interface BuildTransferTx {
/** @format hex-string */
fromPublicKey: string
/** @format hex-string */
Expand All @@ -369,8 +414,8 @@ export interface BuildTransaction {
targetBlockHash?: string
}

/** BuildTransactionResult */
export interface BuildTransactionResult {
/** BuildTransferTxResult */
export interface BuildTransferTxResult {
unsignedTx: string
/** @format gas */
gasAmount: number
Expand Down Expand Up @@ -1204,6 +1249,8 @@ export interface TransactionTemplate {
unsigned: UnsignedTx
inputSignatures: string[]
scriptSignatures: string[]
/** @format int64 */
seenAt: number
}

/** Transfer */
Expand Down Expand Up @@ -1603,7 +1650,7 @@ export class HttpClient<SecurityDataType = unknown> {

/**
* @title Alephium API
* @version 3.7.0
* @version 3.7.1
* @baseUrl ../
*/
export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
Expand Down Expand Up @@ -2529,12 +2576,12 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
*
* @tags Transactions
* @name PostTransactionsBuild
* @summary Build an unsigned transaction to a number of recipients
* @summary Build an unsigned transfer transaction to a number of recipients
* @request POST:/transactions/build
*/
postTransactionsBuild: (data: BuildTransaction, params: RequestParams = {}) =>
postTransactionsBuild: (data: BuildTransferTx, params: RequestParams = {}) =>
this.request<
BuildTransactionResult,
BuildTransferTxResult,
BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable
>({
path: `/transactions/build`,
Expand All @@ -2555,7 +2602,7 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
*/
postTransactionsBuildMultiAddresses: (data: BuildMultiAddressesTransaction, params: RequestParams = {}) =>
this.request<
BuildTransactionResult,
BuildTransferTxResult,
BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable
>({
path: `/transactions/build-multi-addresses`,
Expand Down Expand Up @@ -2652,6 +2699,32 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
...params
}).then(convertHttpResponse),

/**
* No description
*
* @tags Transactions
* @name GetTransactionsRichDetailsTxid
* @summary Get transaction with enriched input information when node indexes are enabled.
* @request GET:/transactions/rich-details/{txId}
*/
getTransactionsRichDetailsTxid: (
txId: string,
query?: {
/** @format int32 */
fromGroup?: number
/** @format int32 */
toGroup?: number
},
params: RequestParams = {}
) =>
this.request<RichTransaction, BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
path: `/transactions/rich-details/${txId}`,
method: 'GET',
query: query,
format: 'json',
...params
}).then(convertHttpResponse),

/**
* No description
*
Expand Down Expand Up @@ -2728,6 +2801,27 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
query: query,
format: 'json',
...params
}).then(convertHttpResponse),

/**
* No description
*
* @tags Transactions
* @name PostTransactionsBuildChained
* @summary Build a chain of transactions
* @request POST:/transactions/build-chained
*/
postTransactionsBuildChained: (data: BuildChainedTx[], params: RequestParams = {}) =>
this.request<
BuildChainedTxResult[],
BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable
>({
path: `/transactions/build-chained`,
method: 'POST',
body: data,
type: ContentType.Json,
format: 'json',
...params
}).then(convertHttpResponse)
}
mempool = {
Expand Down Expand Up @@ -3095,7 +3189,7 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
*/
postMultisigBuild: (data: BuildMultisig, params: RequestParams = {}) =>
this.request<
BuildTransactionResult,
BuildTransferTxResult,
BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable
>({
path: `/multisig/build`,
Expand Down
Loading
Loading