Skip to content

Commit

Permalink
refactor: make referral account id optional (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanofa authored Jun 6, 2024
1 parent d8808ef commit f5c2d34
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
5 changes: 1 addition & 4 deletions examples/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createWalletClient, http } from "viem";

import { getEmptyBytes, getRandomBytes } from "../src/common/utils/bytes.js";
import { getRandomBytes } from "../src/common/utils/bytes.js";
import {
NetworkType,
FolksCore,
Expand All @@ -23,7 +23,6 @@ async function main() {
FolksCore.setNetwork(NetworkType.TESTNET);

const accountId: AccountId = getRandomBytes(BYTES32_LENGTH) as AccountId;
const refAccountId: AccountId = getEmptyBytes(BYTES32_LENGTH) as AccountId;

// read
const accountInfo = await FolksAccount.read.accountInfo(accountId);
Expand All @@ -46,12 +45,10 @@ async function main() {

const prepareCreateAccountCall = await FolksAccount.prepare.createAccount(
accountId,
refAccountId,
adapters,
);
const createAccountCallRes = await FolksAccount.write.createAccount(
accountId,
refAccountId,
prepareCreateAccountCall,
);

Expand Down
3 changes: 3 additions & 0 deletions src/common/constants/lending.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { getAccountIdBytes } from "../utils/bytes.js";

export const NULL_ACCOUNT_ID = getAccountIdBytes("");
7 changes: 5 additions & 2 deletions src/common/utils/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import { bytesToHex, pad, toHex } from "viem";

import { BYTES32_LENGTH } from "../constants/bytes.js";

import type { AccountId } from "../types/lending.js";
import type { Hex } from "viem";

export function getEmptyBytes(length: number): string {
return pad("0x", { size: length });
}

export function getAccountIdBytes(accountId: string): Hex {
return pad(toHex(Buffer.from(accountId)), { size: BYTES32_LENGTH });
export function getAccountIdBytes(accountId: string): AccountId {
return pad(toHex(Buffer.from(accountId)), {
size: BYTES32_LENGTH,
}) as AccountId;
}

export function convertNumberToBytes(
Expand Down
9 changes: 5 additions & 4 deletions src/xchain/modules/folks-account.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FolksHubAccount } from "../../chains/evm/hub/modules/index.js";
import { getHubChain } from "../../chains/evm/hub/utils/chain.js";
import { FolksEvmAccount } from "../../chains/evm/spoke/modules/index.js";
import { NULL_ACCOUNT_ID } from "../../common/constants/lending.js";
import { ChainType } from "../../common/types/chain.js";
import { Action } from "../../common/types/message.js";
import { assertAdapterSupportsDataMessage } from "../../common/utils/adapter.js";
Expand Down Expand Up @@ -42,8 +43,8 @@ import type {
export const prepare = {
async createAccount(
accountId: AccountId,
refAccountId: AccountId,
adapters: MessageAdapters,
refAccountId: AccountId = NULL_ACCOUNT_ID,
) {
const folksChain = FolksCore.getSelectedFolksChain();

Expand Down Expand Up @@ -110,8 +111,8 @@ export const prepare = {
accountId: AccountId,
folksChainIdToInvite: FolksChainId,
addressToInvite: GenericAddress,
refAccountId: AccountId,
adapters: MessageAdapters,
refAccountId: AccountId = NULL_ACCOUNT_ID,
) {
const folksChain = FolksCore.getSelectedFolksChain();

Expand Down Expand Up @@ -313,8 +314,8 @@ export const prepare = {
export const write = {
async createAccount(
accountId: AccountId,
refAccountId: AccountId,
prepareCall: PrepareCreateAccountCall,
refAccountId: AccountId = NULL_ACCOUNT_ID,
) {
const folksChain = FolksCore.getSelectedFolksChain();

Expand All @@ -338,8 +339,8 @@ export const write = {
accountId: AccountId,
folksChainIdToInvite: FolksChainId,
addressToInvite: GenericAddress,
refAccountId: AccountId,
prepareCall: PrepareInviteAddressCall,
refAccountId: AccountId = NULL_ACCOUNT_ID,
) {
const folksChain = FolksCore.getSelectedFolksChain();

Expand Down

0 comments on commit f5c2d34

Please sign in to comment.