Skip to content

Commit

Permalink
chore: change output address when set
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardocbsr committed Aug 5, 2024
1 parent c7aff34 commit 6fce9cf
Show file tree
Hide file tree
Showing 16 changed files with 231 additions and 346 deletions.
33 changes: 19 additions & 14 deletions packages/common/scripts/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,37 @@ export const addPredicate = async (__dirname: string) => {
}

fs.mkdirSync(newPath);
fs.copyFileSync(
`${predicatePath}/factories/VerificationPredicateAbi__factory.ts`,
`${newPath}/predicate.ts`,
);

const date = new Date();
const code = `/* Autogenerated file. Do not edit manually. */\n\nexport const generationDate = ${date
.getTime()
.toString()};\nexport { VerificationPredicateAbi__factory as Predicate } from './predicate';\n`;
fs.appendFileSync(`${newPath}/index.ts`, code);
const code = [
'/* Autogenerated file. Do not edit manually. */',
'',
`export const generationDate = ${date.getTime().toString()};`,
'// biome-ignore lint: Autogenerated file',
`export const abi = ${JSON.stringify(predicate.abi)};`,
`export const bin = '${predicate.bin}';`,
];

fs.appendFileSync(`${newPath}/index.ts`, code.join('\n'));
fs.rmSync(predicatePath, { recursive: true, force: true });
console.info(`Predicate added: ${version}`);
};

export const syncPredicate = async (__dirname: string) => {
const dictPath = `${__dirname}/index.ts`;
const header = '/* Autogenerated file. Do not edit manually. */\n\n';
const header =
"/* Autogenerated file. Do not edit manually. */\n\nimport type { Predicate } from '@fuel-connectors/common';\n";
fs.writeFileSync(dictPath, header);

const versions = await Promise.all(
fs
.readdirSync(__dirname)
.filter((f) => !f.includes('.ts'))
.map(async (f) => {
const { generationDate } = await import(`${__dirname}/${f}/index.ts`);
return { version: f, date: generationDate };
const { generationDate, abi, bin } = await import(
`${__dirname}/${f}/index.ts`
);
return { abi, bin, date: generationDate, version: f };
}),
);
if (versions.length === 0) {
Expand All @@ -77,16 +81,17 @@ export const syncPredicate = async (__dirname: string) => {
}

const headers = versions.map(
(v) => `import { Predicate as Predicate${v.date}} from './${v.version}';`,
(v) =>
`import { abi as abi${v.date}, bin as bin${v.date}, generationDate as generationDate${v.date}} from './${v.version}';`,
);

const code = `\n\nexport default {\n${versions
.map(
(v) =>
`\t'${v.version}': { predicate: Predicate${v.date}, generatedAt: ${v.date} }`,
`\t'${v.version}':{ predicate: {abi: abi${v.date}, bin: bin${v.date}}, generatedAt: generationDate${v.date} }`,
)
.join(',\n')}
};\n`;
} as Record<string, Predicate>;\n`;
fs.appendFileSync(dictPath, `${headers.join('\n')}${code}`);
console.info('Dictionary updated.');
};
Expand Down
50 changes: 33 additions & 17 deletions packages/common/src/PredicateConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
FuelConnectorEventTypes,
type JsonAbi,
type Network,
OutputType,
type TransactionRequestLike,
type Version,
ZeroBytes32,
Expand All @@ -25,7 +26,6 @@ import type {
MaybeAsync,
Predicate,
PredicateConfig,
PredicateWithAddress,
PreparedTransaction,
ProviderDictionary,
} from './types';
Expand Down Expand Up @@ -57,7 +57,7 @@ export abstract class PredicateConnector extends FuelConnector {
protected abstract requireConnection(): MaybeAsync<void>;
protected abstract walletAccounts(): Promise<Array<string>>;

protected async getBalancedPredicate(): Promise<Maybe<PredicateWithAddress>> {
protected async getBalancedPredicate(): Promise<Maybe<PredicateFactory>> {
const predicateVersions = Object.entries(this.getPredicateVersions()).map(
([key, pred]) => ({
pred,
Expand All @@ -83,24 +83,31 @@ export abstract class PredicateConnector extends FuelConnector {
const balance = await predicate.getBalance();

if (balance.toString() !== bn(0).toString()) {
return predicateVersion;
return predicateInstance;
}
}

return null;
}

protected getNewestPredicate(): Maybe<PredicateWithAddress> {
protected getNewestPredicate(): Maybe<PredicateFactory> {
const predicateVersions = Object.entries(this.getPredicateVersions()).map(
([key, pred]) => ({
pred,
key,
}),
);

return predicateVersions.sort(
const newest = predicateVersions.sort(
(a, b) => Number(b.pred.generatedAt) - Number(a.pred.generatedAt),
)[0];
if (!newest) return null;

return new PredicateFactory(
this.getWalletAdapter(),
newest.pred.predicate,
newest.key,
);
}

protected async setupPredicate(): Promise<PredicateFactory> {
Expand All @@ -119,12 +126,8 @@ export abstract class PredicateConnector extends FuelConnector {
(await this.getBalancedPredicate()) ?? this.getNewestPredicate();
if (!predicate) throw new Error('No predicate found');

this.predicateAddress = predicate.key;
this.predicateAccount = new PredicateFactory(
this.getWalletAdapter(),
predicate.pred.predicate,
predicate.key,
);
this.predicateAddress = predicate.getRoot();
this.predicateAccount = predicate;

return this.predicateAccount;
}
Expand Down Expand Up @@ -157,12 +160,25 @@ export abstract class PredicateConnector extends FuelConnector {

const transactionRequest = transactionRequestify(transaction);
const newestPredicate = this.getNewestPredicate();
if (!!newestPredicate && this.predicateAddress !== newestPredicate.key) {
const coinInput = transactionRequest.getCoinInputs();
transactionRequest.addChangeOutput(
Address.fromAddressOrString(newestPredicate.key),
coinInput[0]?.assetId as BytesLike,
);
if (!!newestPredicate && !this.predicateAccount.equals(newestPredicate)) {
const coinOutput = transactionRequest.getCoinOutputs();
const predicateAddress =
newestPredicate.getPredicateAddress(walletAccount);

// If there are no outputs, add a change output to the newest predicate
if (transactionRequest.getChangeOutputs().length === 0) {
transactionRequest.addChangeOutput(
Address.fromString(predicateAddress),
coinOutput[0]?.assetId as BytesLike,
);
}
// If there are outputs, change output addresses to the newest predicate
else {
transactionRequest.outputs.forEach((output) => {
if (output.type !== OutputType.Change) return;
output.to = Address.fromAddressOrString(predicateAddress).toB256();
});
}
}

const transactionFee = transactionRequest.maxFee.toNumber();
Expand Down
6 changes: 4 additions & 2 deletions packages/common/src/PredicateFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@ export class PredicateFactory {
private abi: JsonAbi;
private bytecode: BytesLike;
private adapter: PredicateWalletAdapter;
private root: BytesLike = ZeroBytes32;
private root: string;

constructor(
adapter: PredicateWalletAdapter,
{ abi, bin }: PredicateConfig,
root: BytesLike,
root: string,
) {
this.adapter = adapter;
this.abi = abi;
this.bytecode = bin;
this.root = root;
}

getRoot = (): string => this.root;

getPredicateAddress = memoize((address: string | B256Address): string => {
// @ts-expect-error processPredicateData is only available in the Predicate class
const { predicateBytes } = Predicate.processPredicateData(
Expand Down
5 changes: 0 additions & 5 deletions packages/common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ export interface Predicate {
generatedAt: number;
}

export interface PredicateWithAddress {
key: string;
pred: Predicate;
}

export interface EIP1193Provider extends EventEmitter {
request(args: {
method: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/common/tsup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export default defineConfig((options) => ({
external: ['fuels'],
entry: {
index: 'src/index.ts',
'scripts/index.ts': 'scripts/index.ts',
'scripts/index': 'scripts/index.ts',
},
}));
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
/* Autogenerated file. Do not edit manually. */

export const generationDate = 1722565073112;
export { VerificationPredicateAbi__factory as Predicate } from './predicate';
export const generationDate = 1722897433097;
// biome-ignore lint: Autogenerated file
export const abi = {
encoding: '1',
types: [
{ typeId: 0, type: 'b256', components: null, typeParameters: null },
{ typeId: 1, type: 'bool', components: null, typeParameters: null },
{ typeId: 2, type: 'u64', components: null, typeParameters: null },
],
functions: [
{
inputs: [{ name: 'witness_index', type: 2, typeArguments: null }],
name: 'main',
output: { name: '', type: 1, typeArguments: null },
attributes: null,
},
],
loggedTypes: [],
messagesTypes: [],
configurables: [
{
name: 'SIGNER',
configurableType: { name: '', type: 0, typeArguments: null },
offset: 3000,
},
],
};
export const bin =
'0x1af03000740000020000000000000bb85dffc00110ffff009100002050ebf00050e4002050e0400020f8330058fbe00250fbe004740000151aec50009100000871400003614502001349100076480006724800021345148076440001360000006141024a740000016141020c5d4100001ae9000020f8330058fbe00250fbe004740000181a43d000244000009500001f960800001aec5000910000401a43a0001a4b80001a4fe0005047b02072500020284505005043b0207244002028ed0440724400201a43b000284904401af40000920000401af93000980800009700001f4af80000950001ff960800001aec5000910007d81a43a0001a53e0005d47f00c104510c05d4bf00d104920c05d4ff00e104d30c0614104015057b3781ae900001ae5500020f8330058fbe00250fbe004740000da1a43d0005057b79872580040285505805063b7981a4000007254002028ed05405043b02072540020284115405043b04072440020284124405043b5e8724400602843b4405043b5e85047b5c872480020284534805047b5c87248003c104904805057b5e85055502072580020725c003c28490580414555c05043b5c85047b0d0724800201b480480104914807254002028493540724800201b481480104914807254002028493540504bb170724c0040284914c05047b730724c0040284524c05047b730504bb2a0724c0020284904c03e4584801a408000134100407640000a5043b1f05fec003e5047b73050490008724c0040284914c0504bb5187244004828490440740000065043b0885fec10115fec0019504bb51872440048284904405043b6c872440048284124405043b2c072440048284124405d43b0a313410040764000565043b6c85047b30872480048284504805d43b0d91341000076400001360000005043b308504100085047b64872480040284504805047b4987248004028450480504bb3d81ae910001ae5200020f8330058fbe00250fbe0047400022b1a47d000504bb130724c0040284914c0724400201b44044010452440504bb4d8724c0040284904c05043b4181ae920001ae5000020f8330058fbe00250fbe0047400021a1a43d000504bb1b0724c0040284904c0724000201b40140010412400504bb238724c0020284914c050452020724c0020284504c05043b45872440040284124405047b3b81ae900001ae5100020f8330058fbe00250fbe004740000571a43d0005047b71072480020284504805043b2785fec004f5047b710504bb6a8724c0020284914c05047b6a87044000c5047b6a8504bb110724c0020284914c050450008724c0020284524c0504bb56072440028284904407400000a5043b2c0504100405047b0605fec100c50491020724c0008284904c0504bb56072400028284914005043b77072440028284124405d43b0ac134100001a441000764000011a4400007644000174000017504040005047b770504bb350724c0028284914c05d47b0ee1345100076440001360000005047b35050451008504bb688724c0020284914c05047b688504bb588724c0020284904c05043b5a8724c0020284114c0a1412420764000021af40000740000011af41000920007d81af9400098080000970001ff4af800009500000f960800001aec50001a43a0001a4790001a4be000724c0040284504c01af510001af92000980800009700000f4af800009500007f960800001aec5000910000b81a43a0001a4790001a4be0005d4ff00f104d30c01aebb00020f8330058fbe00250fbe004740000301a53d0005d5540005d5140015fed50105fed40115fec00125053b0805057b01072580040285505801ae950001ae5400020f8330058fbe00250fbe0047400002f5043b0805053b0987254002028513540504fb0985053b06872540018285105401ae9400020f8330058fbe00250fbe0047400016d1a53d0005057b05072580018285505801ae9500020f8330058fbe00250fbe004740000e91a43d000414d440072400020284534001af51000920000b81af92000980800009700007f4af8000095000007960800001aec50001a43a0001a47e0001a480000264800001a4870005f4120005f4000011af500001af9100098080000970000074af800009500001f960800001aec5000910000401a43a0001a4790001a4be000724c002028ed04c01aebb0001ae5100020f8330058fbe00250fbe0047400001050410020504fb02072500020284d05001ae930001ae5100020f8330058fbe00250fbe004740000061af40000920000401af92000980800009700001f4af80000950001ff960800001aec5000910001281a43a0001a5b90001a57e00072440020264400001a447000504bb018724c0020284904c05d4120005d4d20015d5120025d4920035f4500005f4530015f4540025f4520035043b0381ae91000724400201ae510001ae1000020f8330058fbe00250fbe004740000be1a43d0005047b0d07248001028450480504bb0581ae910001ae5200020f8330058fbe00250fbe004740000d11a47d000504bb0f0724c0010284904c01ae9200020f8330058fbe00250fbe004740000ea1a43d0007248001028ed14805fed00025043b078724400182843b4401ae9000020f8330058fbe00250fbe004740000731a63d000134180007640006a5d41600213410000764000605d416002104106005d456002504bb090724c0010284964c01ae9200020f8330058fbe00250fbe004740000821a4bd000164924007648000174000036504bb0b0724c0010284964c01ae9200020f8330058fbe00250fbe004740000941a4bd000504fb0a072500010284d65001ae9300020f8330058fbe00250fbe0047400006d1a4fd000155104c07650000174000007264000001a507000155d3000765c000174000001285124c01a494000504fb0481ae920001ae500001ae1300020f8330058fbe00250fbe004740000651a4bd000504fb11872500010284d2500504bb118504fb0e072500010284d2500504bb0681ae930001ae5200020f8330058fbe00250fbe004740000741a4bd000724c0010285924c0504bb0c0724c0010284964c01ae9200020f8330058fbe00250fbe0047400005e1a4bd00010452440504bb100724c0018284bb4c01ae9200020f8330058fbe00250fbe004740000911a4bd000284526005f5900021aebb00020f8330058fbe00250fbe004740000197400000772400018285bb4001aebb00020f8330058fbe00250fbe004740000111af40000920001281af9500098080000970001ff4af8000095000003960800001aec50001a43a0001a47e0005d4100021af500001af9100098080000970000034af800009500000f960800001aec5000910000101a43a0001a47e0001aebb00020f8330058fbe00250fbe004750000f31a4bd000724c0010284124c05f4000021af40000920000101af91000980800009700000f4af8000095000003960800001aec50001a43a0001a47e0005d4100011af500001af9100098080000970000034af800009500000f960800001aec5000910000101a43a0001a4790001a4b80001a4fe0005fed00005fed10011a43b00072440010284904401af52000920000101af93000980800009700000f4af8000095000003960800001aec50001a43a0001a47e0005d4100001af500001af9100098080000970000034af800009500003f960800001aec5000910000201a43a0001a4790001a4be000504fb01072500010284d05001ae9300020f8330058fbe00250fbe004740000131a4fd000264c00001a5070001555300076540001740000057254001028ed05401a43b0005d410000285104c05f4540005f4530011af51000920000201af92000980800009700003f4af8000095000007960800001aec5000910000101a43a0001a47e0007248001028ed04801a43b0005d4100011af50000920000101af9100098080000970000074af8000095000007960800001aec5000910000101a43a0001a47e0007248001028ed04801aebb00020f8330058fbe00250fbe004750000481a43d0001af50000920000101af9100098080000970000074af800009500000f960800001aec50001a43a0001a4790001a4be000724c0040284504c01af510001af92000980800009700000f4af8000047000000000000000000000000000000000000000000000000000000000000000000000019457468657265756d205369676e6564204d6573736167653a0a33320000000000000000000000000000000000000000000000000000000000000000000000000000000000000ad80000000000000af00000000000000ae80000000000000708';
13 changes: 9 additions & 4 deletions packages/evm-connector/src/generated/predicates/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
/* Autogenerated file. Do not edit manually. */

import { Predicate as Predicate1722565073112 } from './0xca07380d3c0d9890519ee727bf522eaf19e2acefab240c9a22b0aa90f41c87d6';
import type { Predicate } from '@fuel-connectors/common';
import {
abi as abi1722897433097,
bin as bin1722897433097,
generationDate as generationDate1722897433097,
} from './0xca07380d3c0d9890519ee727bf522eaf19e2acefab240c9a22b0aa90f41c87d6';

export default {
'0xca07380d3c0d9890519ee727bf522eaf19e2acefab240c9a22b0aa90f41c87d6': {
predicate: Predicate1722565073112,
generatedAt: 1722565073112,
predicate: { abi: abi1722897433097, bin: bin1722897433097 },
generatedAt: generationDate1722897433097,
},
};
} as Record<string, Predicate>;
Loading

0 comments on commit 6fce9cf

Please sign in to comment.