Skip to content

Commit

Permalink
Expose low-level voucher helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
tuler committed May 3, 2024
1 parent 79fd512 commit 79b1b04
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 65 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-yaks-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@deroll/wallet": minor
---

expose low-level voucher helpers
102 changes: 101 additions & 1 deletion packages/wallet/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { AdvanceRequestData, Payload } from "@deroll/core";
import { AdvanceRequestData, Payload, Voucher } from "@deroll/core";
import {
Address,
Hex,
decodeAbiParameters,
encodeFunctionData,
erc20Abi,
erc721Abi,
getAddress,
hexToBigInt,
hexToBool,
Expand All @@ -18,6 +22,7 @@ import {
erc721PortalAddress,
etherPortalAddress,
} from "./rollups";
import { cartesiDAppAbi, erc1155Abi } from "./abi";

export type { WalletApp } from "./wallet";

Expand Down Expand Up @@ -163,3 +168,98 @@ export const isERC1155SingleDeposit = (data: AdvanceRequestData): boolean =>

export const isERC1155BatchDeposit = (data: AdvanceRequestData): boolean =>
getAddress(data.metadata.msg_sender) === erc1155BatchPortalAddress;

export const createWithdrawEtherVoucher = (
application: Address,
receiver: Address,
value: bigint,
): Voucher => {
const call = encodeFunctionData({
abi: cartesiDAppAbi,
functionName: "withdrawEther",
args: [receiver, value],
});
return {
destination: application, // application Address
payload: call,
};
};

export const createERC20TransferVoucher = (
token: Address,
recipient: Address,
amount: bigint,
): Voucher => {
const call = encodeFunctionData({
abi: erc20Abi,
functionName: "transfer",
args: [recipient, amount],
});

// create voucher to the IERC20 transfer
return {
destination: token,
payload: call,
};
};

export const createERC721TransferVoucher = (
token: Address,
from: Address,
to: Address,
tokenId: bigint,
): Voucher => {
const call = encodeFunctionData({
abi: erc721Abi,
functionName: "safeTransferFrom",
args: [from, to, tokenId],
});

// create voucher to the IERC721 transfer
return {
destination: token,
payload: call,
};
};

export const createERC1155SingleTransferVoucher = (
token: Address,
from: Address,
to: Address,
tokenId: bigint,
value: bigint,
data: Hex,
): Voucher => {
const call = encodeFunctionData({
abi: erc1155Abi,
functionName: "safeTransferFrom",
args: [from, to, tokenId, value, data],
});

// create voucher to the IERC1155 transfer
return {
destination: token,
payload: call,
};
};

export const createERC1155BatchTransferVoucher = (
token: Address,
from: Address,
to: Address,
tokenIds: bigint[],
values: bigint[],
data: Hex,
): Voucher => {
const call = encodeFunctionData({
abi: erc1155Abi,
functionName: "safeBatchTransferFrom",
args: [from, to, tokenIds, values, data],
});

// create voucher to the IERC1155 transfer
return {
destination: token,
payload: call,
};
};
93 changes: 29 additions & 64 deletions packages/wallet/src/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { AdvanceRequestHandler, Voucher } from "@deroll/core";
import {
Address,
Hex,
encodeFunctionData,
erc20Abi,
erc721Abi,
getAddress,
isAddress,
} from "viem";

import { cartesiDAppAbi, erc1155Abi } from "./abi";
import { Address, Hex, getAddress, isAddress } from "viem";

import { dAppAddressRelayAddress } from "./rollups";
import {
createERC1155BatchTransferVoucher,
createERC1155SingleTransferVoucher,
createERC20TransferVoucher,
createERC721TransferVoucher,
createWithdrawEtherVoucher,
isERC1155BatchDeposit,
isERC1155SingleDeposit,
isERC20Deposit,
Expand Down Expand Up @@ -446,15 +442,7 @@ export class WalletAppImpl implements WalletApp {
wallet.ether = wallet.ether - value;

// create voucher
const call = encodeFunctionData({
abi: cartesiDAppAbi,
functionName: "withdrawEther",
args: [address, value],
});
return {
destination: this.dapp, // dapp Address
payload: call,
};
return createWithdrawEtherVoucher(this.dapp, address, value);
}

withdrawERC20(token: Address, address: Address, amount: bigint): Voucher {
Expand All @@ -475,17 +463,7 @@ export class WalletAppImpl implements WalletApp {
// reduce balance right away
wallet.erc20[token] -= amount;

const call = encodeFunctionData({
abi: erc20Abi,
functionName: "transfer",
args: [address, amount],
});

// create voucher to the IERC20 transfer
return {
destination: token,
payload: call,
};
return createERC20TransferVoucher(token, address, amount);
}

withdrawERC721(token: Address, address: Address, tokenId: bigint): Voucher {
Expand All @@ -511,17 +489,8 @@ export class WalletAppImpl implements WalletApp {
// remove tokenId right away
wallet.erc721[token].delete(tokenId);

const call = encodeFunctionData({
abi: erc721Abi,
functionName: "safeTransferFrom",
args: [this.dapp, address, tokenId],
});

// create voucher to the IERC721 transfer
return {
destination: token,
payload: call,
};
// create voucher
return createERC721TransferVoucher(token, this.dapp, address, tokenId);
}

withdrawERC1155(
Expand Down Expand Up @@ -553,17 +522,15 @@ export class WalletAppImpl implements WalletApp {
// reduce balance right away
wallet.erc1155[token].set(tokenId, balance - value);

const call = encodeFunctionData({
abi: erc1155Abi,
functionName: "safeTransferFrom",
args: [this.dapp, address, tokenId, value, data],
});

// create voucher to the IERC721 transfer
return {
destination: token,
payload: call,
};
// create voucher
return createERC1155SingleTransferVoucher(
token,
this.dapp,
address,
tokenId,
value,
data,
);
}

withdrawBatchERC1155(
Expand Down Expand Up @@ -609,16 +576,14 @@ export class WalletAppImpl implements WalletApp {
wallet.erc1155[token].set(tokenId, balance - value);
});

const call = encodeFunctionData({
abi: erc1155Abi,
functionName: "safeBatchTransferFrom",
args: [this.dapp, address, tokenIds, values, data],
});

// create voucher to the IERC721 transfer
return {
destination: token,
payload: call,
};
// create voucher
return createERC1155BatchTransferVoucher(
token,
this.dapp,
address,
tokenIds,
values,
data,
);
}
}

0 comments on commit 79b1b04

Please sign in to comment.