From f8f5487f1ad389f9431828b4d87fee820434c9a4 Mon Sep 17 00:00:00 2001 From: bguillaumat Date: Wed, 7 Dec 2022 16:54:55 +0100 Subject: [PATCH] feat(sdk): Update sdk to handle the new meta info at squad create --- sdk/README.md | 2 +- sdk/lib/idl/mesh.json | 794 +++++++++++++++++++++++++++ sdk/lib/idl/roles.json | 548 ++++++++++++++++++ sdk/lib/idl/squads_mpl.json | 4 + sdk/lib/idl/txmeta.json | 49 ++ sdk/lib/idl/validator.json | 122 ++++ sdk/lib/sdk/src/index.d.ts | 4 +- sdk/lib/sdk/src/index.js | 12 +- sdk/lib/target/idl/squads_mpl.json | 4 + sdk/lib/target/types/squads_mpl.d.ts | 4 + sdk/lib/target/types/squads_mpl.js | 4 + sdk/package.json | 2 +- sdk/src/index.ts | 733 +++++++++++++------------ 13 files changed, 1932 insertions(+), 350 deletions(-) create mode 100644 sdk/lib/idl/mesh.json create mode 100644 sdk/lib/idl/roles.json create mode 100644 sdk/lib/idl/txmeta.json create mode 100644 sdk/lib/idl/validator.json diff --git a/sdk/README.md b/sdk/README.md index d8bcb67..c1859f8 100644 --- a/sdk/README.md +++ b/sdk/README.md @@ -18,7 +18,7 @@ import Squads from "@sqds/sdk"; // The 'wallet' passed in will be the signer/feePayer on all transactions through the Squads object. const squads = Squads.localnet(wallet); // or Squads.devnet(...); Squads.mainnet(...) -const multisigAccount = await squads.createMultisig(threshold, createKey, members); +const multisigAccount = await squads.createMultisig(threshold, createKey, members, name, description, image); ``` Generally you will want to import the default `Squads` class from `@sqds/sdk` and pass in a `Wallet` instance. This would come from your preferred client-side wallet adapter or would likely be a `NodeWallet` if running server-side. diff --git a/sdk/lib/idl/mesh.json b/sdk/lib/idl/mesh.json new file mode 100644 index 0000000..c2019b0 --- /dev/null +++ b/sdk/lib/idl/mesh.json @@ -0,0 +1,794 @@ +{ + "version": "0.0.1", + "name": "mesh", + "instructions": [ + { + "name": "create", + "accounts": [ + { + "name": "multisig", + "isMut": true, + "isSigner": false + }, + { + "name": "creator", + "isMut": true, + "isSigner": true + }, + { + "name": "systemProgram", + "isMut": false, + "isSigner": false + } + ], + "args": [ + { + "name": "externalAuthority", + "type": "publicKey" + }, + { + "name": "threshold", + "type": "u16" + }, + { + "name": "createKey", + "type": "publicKey" + }, + { + "name": "members", + "type": { + "vec": "publicKey" + } + } + ] + }, + { + "name": "addMember", + "accounts": [ + { + "name": "multisig", + "isMut": true, + "isSigner": false + }, + { + "name": "externalAuthority", + "isMut": true, + "isSigner": true + }, + { + "name": "rent", + "isMut": false, + "isSigner": false + }, + { + "name": "systemProgram", + "isMut": false, + "isSigner": false + } + ], + "args": [ + { + "name": "newMember", + "type": "publicKey" + } + ] + }, + { + "name": "removeMember", + "accounts": [ + { + "name": "multisig", + "isMut": true, + "isSigner": false + }, + { + "name": "externalAuthority", + "isMut": true, + "isSigner": true + } + ], + "args": [ + { + "name": "oldMember", + "type": "publicKey" + } + ] + }, + { + "name": "removeMemberAndChangeThreshold", + "accounts": [ + { + "name": "multisig", + "isMut": true, + "isSigner": false + }, + { + "name": "externalAuthority", + "isMut": true, + "isSigner": true + } + ], + "args": [ + { + "name": "oldMember", + "type": "publicKey" + }, + { + "name": "newThreshold", + "type": "u16" + } + ] + }, + { + "name": "addMemberAndChangeThreshold", + "accounts": [ + { + "name": "multisig", + "isMut": true, + "isSigner": false + }, + { + "name": "externalAuthority", + "isMut": true, + "isSigner": true + }, + { + "name": "rent", + "isMut": false, + "isSigner": false + }, + { + "name": "systemProgram", + "isMut": false, + "isSigner": false + } + ], + "args": [ + { + "name": "newMember", + "type": "publicKey" + }, + { + "name": "newThreshold", + "type": "u16" + } + ] + }, + { + "name": "changeThreshold", + "accounts": [ + { + "name": "multisig", + "isMut": true, + "isSigner": false + }, + { + "name": "externalAuthority", + "isMut": true, + "isSigner": true + } + ], + "args": [ + { + "name": "newThreshold", + "type": "u16" + } + ] + }, + { + "name": "addAuthority", + "accounts": [ + { + "name": "multisig", + "isMut": true, + "isSigner": false + }, + { + "name": "externalAuthority", + "isMut": true, + "isSigner": true + } + ], + "args": [] + }, + { + "name": "setExternalExecute", + "accounts": [ + { + "name": "multisig", + "isMut": true, + "isSigner": false + }, + { + "name": "externalAuthority", + "isMut": true, + "isSigner": true + } + ], + "args": [ + { + "name": "setting", + "type": "bool" + } + ] + }, + { + "name": "createTransaction", + "accounts": [ + { + "name": "multisig", + "isMut": true, + "isSigner": false + }, + { + "name": "transaction", + "isMut": true, + "isSigner": false + }, + { + "name": "creator", + "isMut": true, + "isSigner": true + }, + { + "name": "systemProgram", + "isMut": false, + "isSigner": false + } + ], + "args": [ + { + "name": "authorityIndex", + "type": "u32" + } + ] + }, + { + "name": "activateTransaction", + "accounts": [ + { + "name": "multisig", + "isMut": false, + "isSigner": false + }, + { + "name": "transaction", + "isMut": true, + "isSigner": false + }, + { + "name": "creator", + "isMut": true, + "isSigner": true + }, + { + "name": "systemProgram", + "isMut": false, + "isSigner": false + } + ], + "args": [] + }, + { + "name": "addInstruction", + "accounts": [ + { + "name": "multisig", + "isMut": false, + "isSigner": false + }, + { + "name": "transaction", + "isMut": true, + "isSigner": false + }, + { + "name": "instruction", + "isMut": true, + "isSigner": false + }, + { + "name": "creator", + "isMut": true, + "isSigner": true + }, + { + "name": "systemProgram", + "isMut": false, + "isSigner": false + } + ], + "args": [ + { + "name": "incomingInstruction", + "type": { + "defined": "IncomingInstruction" + } + }, + { + "name": "authorityIndex", + "type": { + "option": "u32" + } + }, + { + "name": "authorityBump", + "type": { + "option": "u8" + } + }, + { + "name": "authorityType", + "type": { + "defined": "MsAuthorityType" + } + } + ] + }, + { + "name": "approveTransaction", + "accounts": [ + { + "name": "multisig", + "isMut": false, + "isSigner": false + }, + { + "name": "transaction", + "isMut": true, + "isSigner": false + }, + { + "name": "member", + "isMut": true, + "isSigner": true + }, + { + "name": "systemProgram", + "isMut": false, + "isSigner": false + } + ], + "args": [] + }, + { + "name": "rejectTransaction", + "accounts": [ + { + "name": "multisig", + "isMut": false, + "isSigner": false + }, + { + "name": "transaction", + "isMut": true, + "isSigner": false + }, + { + "name": "member", + "isMut": true, + "isSigner": true + }, + { + "name": "systemProgram", + "isMut": false, + "isSigner": false + } + ], + "args": [] + }, + { + "name": "cancelTransaction", + "accounts": [ + { + "name": "multisig", + "isMut": true, + "isSigner": false + }, + { + "name": "transaction", + "isMut": true, + "isSigner": false + }, + { + "name": "member", + "isMut": true, + "isSigner": true + }, + { + "name": "systemProgram", + "isMut": false, + "isSigner": false + } + ], + "args": [] + }, + { + "name": "executeTransaction", + "accounts": [ + { + "name": "multisig", + "isMut": true, + "isSigner": false + }, + { + "name": "transaction", + "isMut": true, + "isSigner": false + }, + { + "name": "member", + "isMut": true, + "isSigner": true + } + ], + "args": [ + { + "name": "accountList", + "type": "bytes" + } + ] + }, + { + "name": "executeInstruction", + "accounts": [ + { + "name": "multisig", + "isMut": true, + "isSigner": false + }, + { + "name": "transaction", + "isMut": true, + "isSigner": false + }, + { + "name": "instruction", + "isMut": true, + "isSigner": false + }, + { + "name": "member", + "isMut": true, + "isSigner": true + } + ], + "args": [] + }, + { + "name": "changeExternalAuthority", + "accounts": [ + { + "name": "multisig", + "isMut": true, + "isSigner": false + }, + { + "name": "externalAuthority", + "isMut": true, + "isSigner": true + } + ], + "args": [ + { + "name": "newAuthority", + "type": "publicKey" + } + ] + } + ], + "accounts": [ + { + "name": "Ms", + "type": { + "kind": "struct", + "fields": [ + { + "name": "threshold", + "type": "u16" + }, + { + "name": "authorityIndex", + "type": "u16" + }, + { + "name": "transactionIndex", + "type": "u32" + }, + { + "name": "msChangeIndex", + "type": "u32" + }, + { + "name": "bump", + "type": "u8" + }, + { + "name": "createKey", + "type": "publicKey" + }, + { + "name": "allowExternalExecute", + "type": "bool" + }, + { + "name": "keys", + "type": { + "vec": "publicKey" + } + }, + { + "name": "externalAuthority", + "type": "publicKey" + } + ] + } + }, + { + "name": "MsTransaction", + "type": { + "kind": "struct", + "fields": [ + { + "name": "creator", + "type": "publicKey" + }, + { + "name": "ms", + "type": "publicKey" + }, + { + "name": "transactionIndex", + "type": "u32" + }, + { + "name": "authorityIndex", + "type": "u32" + }, + { + "name": "authorityBump", + "type": "u8" + }, + { + "name": "status", + "type": { + "defined": "MsTransactionStatus" + } + }, + { + "name": "instructionIndex", + "type": "u8" + }, + { + "name": "bump", + "type": "u8" + }, + { + "name": "approved", + "type": { + "vec": "publicKey" + } + }, + { + "name": "rejected", + "type": { + "vec": "publicKey" + } + }, + { + "name": "cancelled", + "type": { + "vec": "publicKey" + } + }, + { + "name": "executedIndex", + "type": "u8" + } + ] + } + }, + { + "name": "MsInstruction", + "type": { + "kind": "struct", + "fields": [ + { + "name": "programId", + "type": "publicKey" + }, + { + "name": "keys", + "type": { + "vec": { + "defined": "MsAccountMeta" + } + } + }, + { + "name": "data", + "type": "bytes" + }, + { + "name": "instructionIndex", + "type": "u8" + }, + { + "name": "bump", + "type": "u8" + }, + { + "name": "authorityType", + "type": { + "defined": "MsAuthorityType" + } + }, + { + "name": "authorityIndex", + "type": { + "option": "u32" + } + }, + { + "name": "authorityBump", + "type": { + "option": "u8" + } + }, + { + "name": "executed", + "type": "bool" + } + ] + } + } + ], + "types": [ + { + "name": "MsAccountMeta", + "type": { + "kind": "struct", + "fields": [ + { + "name": "pubkey", + "type": "publicKey" + }, + { + "name": "isSigner", + "type": "bool" + }, + { + "name": "isWritable", + "type": "bool" + } + ] + } + }, + { + "name": "IncomingInstruction", + "type": { + "kind": "struct", + "fields": [ + { + "name": "programId", + "type": "publicKey" + }, + { + "name": "keys", + "type": { + "vec": { + "defined": "MsAccountMeta" + } + } + }, + { + "name": "data", + "type": "bytes" + } + ] + } + }, + { + "name": "MsTransactionStatus", + "type": { + "kind": "enum", + "variants": [ + { + "name": "Draft" + }, + { + "name": "Active" + }, + { + "name": "ExecuteReady" + }, + { + "name": "Executed" + }, + { + "name": "Rejected" + }, + { + "name": "Cancelled" + } + ] + } + }, + { + "name": "MsAuthorityType", + "type": { + "kind": "enum", + "variants": [ + { + "name": "Default" + }, + { + "name": "Custom" + } + ] + } + } + ], + "errors": [ + { + "code": 6000, + "name": "KeyNotInMultisig" + }, + { + "code": 6001, + "name": "InvalidTransactionState" + }, + { + "code": 6002, + "name": "InvalidNumberOfAccounts" + }, + { + "code": 6003, + "name": "InvalidInstructionAccount" + }, + { + "code": 6004, + "name": "InvalidAuthorityIndex" + }, + { + "code": 6005, + "name": "InvalidAuthorityType" + }, + { + "code": 6006, + "name": "TransactionAlreadyExecuted" + }, + { + "code": 6007, + "name": "CannotRemoveSoloMember" + }, + { + "code": 6008, + "name": "InvalidThreshold" + }, + { + "code": 6009, + "name": "DeprecatedTransaction" + }, + { + "code": 6010, + "name": "InstructionFailed" + }, + { + "code": 6011, + "name": "MaxMembersReached" + }, + { + "code": 6012, + "name": "EmptyMembers" + }, + { + "code": 6013, + "name": "PartialExecution" + }, + { + "code": 6014, + "name": "InvalidExternalAuthority" + } + ] +} \ No newline at end of file diff --git a/sdk/lib/idl/roles.json b/sdk/lib/idl/roles.json new file mode 100644 index 0000000..93452a0 --- /dev/null +++ b/sdk/lib/idl/roles.json @@ -0,0 +1,548 @@ +{ + "version": "0.1.0", + "name": "roles", + "instructions": [ + { + "name": "createManager", + "accounts": [ + { + "name": "rolesManager", + "isMut": true, + "isSigner": false + }, + { + "name": "multisig", + "isMut": false, + "isSigner": false + }, + { + "name": "payer", + "isMut": true, + "isSigner": true + }, + { + "name": "systemProgram", + "isMut": false, + "isSigner": false + } + ], + "args": [] + }, + { + "name": "addUser", + "accounts": [ + { + "name": "user", + "isMut": true, + "isSigner": false + }, + { + "name": "rolesManager", + "isMut": true, + "isSigner": false + }, + { + "name": "multisig", + "isMut": false, + "isSigner": false + }, + { + "name": "payer", + "isMut": true, + "isSigner": true + }, + { + "name": "authority", + "isMut": false, + "isSigner": true + }, + { + "name": "transaction", + "isMut": false, + "isSigner": false + }, + { + "name": "rent", + "isMut": false, + "isSigner": false + }, + { + "name": "systemProgram", + "isMut": false, + "isSigner": false + } + ], + "args": [ + { + "name": "originKey", + "type": "publicKey" + }, + { + "name": "role", + "type": { + "defined": "Role" + } + }, + { + "name": "name", + "type": "string" + } + ] + }, + { + "name": "changeRole", + "accounts": [ + { + "name": "user", + "isMut": true, + "isSigner": false + }, + { + "name": "multisig", + "isMut": false, + "isSigner": false + }, + { + "name": "payer", + "isMut": true, + "isSigner": true + }, + { + "name": "authority", + "isMut": false, + "isSigner": true + }, + { + "name": "transaction", + "isMut": false, + "isSigner": false + }, + { + "name": "rent", + "isMut": false, + "isSigner": false + }, + { + "name": "systemProgram", + "isMut": false, + "isSigner": false + } + ], + "args": [ + { + "name": "role", + "type": { + "defined": "Role" + } + } + ] + }, + { + "name": "createProxy", + "accounts": [ + { + "name": "multisig", + "isMut": true, + "isSigner": false + }, + { + "name": "transaction", + "isMut": true, + "isSigner": false + }, + { + "name": "user", + "isMut": false, + "isSigner": false + }, + { + "name": "delegate", + "isMut": true, + "isSigner": false + }, + { + "name": "creator", + "isMut": true, + "isSigner": true + }, + { + "name": "systemProgram", + "isMut": false, + "isSigner": false + }, + { + "name": "squadsProgram", + "isMut": false, + "isSigner": false + }, + { + "name": "rent", + "isMut": false, + "isSigner": false + } + ], + "args": [ + { + "name": "authorityIndex", + "type": "u32" + } + ] + }, + { + "name": "addProxy", + "accounts": [ + { + "name": "multisig", + "isMut": false, + "isSigner": false + }, + { + "name": "transaction", + "isMut": true, + "isSigner": false + }, + { + "name": "instruction", + "isMut": true, + "isSigner": false + }, + { + "name": "user", + "isMut": false, + "isSigner": false + }, + { + "name": "delegate", + "isMut": true, + "isSigner": false + }, + { + "name": "creator", + "isMut": true, + "isSigner": true + }, + { + "name": "systemProgram", + "isMut": false, + "isSigner": false + }, + { + "name": "squadsProgram", + "isMut": false, + "isSigner": false + }, + { + "name": "rent", + "isMut": false, + "isSigner": false + } + ], + "args": [ + { + "name": "incomingInstruction", + "type": { + "defined": "IncomingInstruction" + } + } + ] + }, + { + "name": "activateProxy", + "accounts": [ + { + "name": "multisig", + "isMut": false, + "isSigner": false + }, + { + "name": "transaction", + "isMut": true, + "isSigner": false + }, + { + "name": "user", + "isMut": false, + "isSigner": false + }, + { + "name": "creator", + "isMut": true, + "isSigner": true + }, + { + "name": "delegate", + "isMut": true, + "isSigner": false + }, + { + "name": "systemProgram", + "isMut": false, + "isSigner": false + }, + { + "name": "squadsProgram", + "isMut": false, + "isSigner": false + } + ], + "args": [] + }, + { + "name": "approveProxy", + "accounts": [ + { + "name": "multisig", + "isMut": false, + "isSigner": false + }, + { + "name": "transaction", + "isMut": true, + "isSigner": false + }, + { + "name": "user", + "isMut": false, + "isSigner": false + }, + { + "name": "member", + "isMut": true, + "isSigner": true + }, + { + "name": "delegate", + "isMut": true, + "isSigner": false + }, + { + "name": "systemProgram", + "isMut": false, + "isSigner": false + }, + { + "name": "squadsProgram", + "isMut": false, + "isSigner": false + } + ], + "args": [] + }, + { + "name": "rejectProxy", + "accounts": [ + { + "name": "multisig", + "isMut": false, + "isSigner": false + }, + { + "name": "transaction", + "isMut": true, + "isSigner": false + }, + { + "name": "user", + "isMut": false, + "isSigner": false + }, + { + "name": "member", + "isMut": true, + "isSigner": true + }, + { + "name": "delegate", + "isMut": true, + "isSigner": false + }, + { + "name": "systemProgram", + "isMut": false, + "isSigner": false + }, + { + "name": "squadsProgram", + "isMut": false, + "isSigner": false + } + ], + "args": [] + }, + { + "name": "executeTxProxy", + "accounts": [ + { + "name": "multisig", + "isMut": true, + "isSigner": false + }, + { + "name": "transaction", + "isMut": true, + "isSigner": false + }, + { + "name": "user", + "isMut": false, + "isSigner": false + }, + { + "name": "member", + "isMut": true, + "isSigner": true + }, + { + "name": "delegate", + "isMut": true, + "isSigner": false + }, + { + "name": "squadsProgram", + "isMut": false, + "isSigner": false + } + ], + "args": [ + { + "name": "accountList", + "type": "bytes" + } + ] + } + ], + "accounts": [ + { + "name": "RolesManager", + "type": { + "kind": "struct", + "fields": [ + { + "name": "ms", + "type": "publicKey" + }, + { + "name": "roleIndex", + "type": "u32" + }, + { + "name": "bump", + "type": "u8" + } + ] + } + }, + { + "name": "User", + "type": { + "kind": "struct", + "fields": [ + { + "name": "name", + "type": "string" + }, + { + "name": "role", + "type": { + "defined": "Role" + } + }, + { + "name": "originKey", + "type": "publicKey" + }, + { + "name": "bump", + "type": "u8" + }, + { + "name": "ms", + "type": "publicKey" + }, + { + "name": "roleIndex", + "type": "u32" + } + ] + } + } + ], + "types": [ + { + "name": "MsAccountMeta", + "type": { + "kind": "struct", + "fields": [ + { + "name": "pubkey", + "type": "publicKey" + }, + { + "name": "isSigner", + "type": "bool" + }, + { + "name": "isWritable", + "type": "bool" + } + ] + } + }, + { + "name": "IncomingInstruction", + "type": { + "kind": "struct", + "fields": [ + { + "name": "programId", + "type": "publicKey" + }, + { + "name": "keys", + "type": { + "vec": { + "defined": "MsAccountMeta" + } + } + }, + { + "name": "data", + "type": "bytes" + } + ] + } + }, + { + "name": "Role", + "type": { + "kind": "enum", + "variants": [ + { + "name": "Initiate" + }, + { + "name": "Vote" + }, + { + "name": "Execute" + }, + { + "name": "InitiateAndExecute" + }, + { + "name": "InitiateAndVote" + }, + { + "name": "VoteAndExecute" + } + ] + } + } + ], + "errors": [ + { + "code": 6000, + "name": "InvalidRole" + } + ] +} \ No newline at end of file diff --git a/sdk/lib/idl/squads_mpl.json b/sdk/lib/idl/squads_mpl.json index 84a11b8..f85afad 100644 --- a/sdk/lib/idl/squads_mpl.json +++ b/sdk/lib/idl/squads_mpl.json @@ -35,6 +35,10 @@ "type": { "vec": "publicKey" } + }, + { + "name": "meta", + "type": "string" } ] }, diff --git a/sdk/lib/idl/txmeta.json b/sdk/lib/idl/txmeta.json new file mode 100644 index 0000000..4a56c82 --- /dev/null +++ b/sdk/lib/idl/txmeta.json @@ -0,0 +1,49 @@ +{ + "version": "0.1.0", + "name": "txmeta", + "instructions": [ + { + "name": "trackMeta", + "accounts": [ + { + "name": "member", + "isMut": true, + "isSigner": true + }, + { + "name": "multisig", + "isMut": false, + "isSigner": false + }, + { + "name": "transaction", + "isMut": false, + "isSigner": false + } + ], + "args": [ + { + "name": "meta", + "type": "string" + } + ] + } + ], + "errors": [ + { + "code": 6000, + "name": "Unauthorized", + "msg": "Signer is not a member of the specified multisig." + }, + { + "code": 6001, + "name": "InvalidOwner", + "msg": "The owner of the account is not the expected program." + }, + { + "code": 6002, + "name": "InvalidTransaction", + "msg": "The transaction is either not associated with the supplied multisig or it's creator is not the supplied signer" + } + ] +} \ No newline at end of file diff --git a/sdk/lib/idl/validator.json b/sdk/lib/idl/validator.json new file mode 100644 index 0000000..3bcddd2 --- /dev/null +++ b/sdk/lib/idl/validator.json @@ -0,0 +1,122 @@ +{ + "version": "0.1.0", + "name": "validator", + "instructions": [ + { + "name": "createValidatorManager", + "accounts": [ + { + "name": "multisig", + "isMut": false, + "isSigner": false + }, + { + "name": "validatorManager", + "isMut": true, + "isSigner": false + }, + { + "name": "creator", + "isMut": true, + "isSigner": true + }, + { + "name": "systemProgram", + "isMut": false, + "isSigner": false + } + ], + "args": [] + }, + { + "name": "createManagedValidator", + "accounts": [ + { + "name": "multisig", + "isMut": false, + "isSigner": false + }, + { + "name": "validatorManager", + "isMut": true, + "isSigner": false + }, + { + "name": "managedValidator", + "isMut": true, + "isSigner": false + }, + { + "name": "creator", + "isMut": true, + "isSigner": true + }, + { + "name": "systemProgram", + "isMut": false, + "isSigner": false + } + ], + "args": [ + { + "name": "validatorAddress", + "type": "publicKey" + }, + { + "name": "name", + "type": "string" + } + ] + } + ], + "accounts": [ + { + "name": "ValidatorManager", + "type": { + "kind": "struct", + "fields": [ + { + "name": "multisig", + "type": "publicKey" + }, + { + "name": "managedValidatorIndex", + "type": "u32" + }, + { + "name": "bump", + "type": "u8" + } + ] + } + }, + { + "name": "ManagedValidator", + "type": { + "kind": "struct", + "fields": [ + { + "name": "managedValidatorIndex", + "type": "u32" + }, + { + "name": "validatorAddress", + "type": "publicKey" + }, + { + "name": "multisig", + "type": "publicKey" + }, + { + "name": "bump", + "type": "u8" + }, + { + "name": "name", + "type": "string" + } + ] + } + } + ] +} \ No newline at end of file diff --git a/sdk/lib/sdk/src/index.d.ts b/sdk/lib/sdk/src/index.d.ts index c90d3a3..74fc034 100644 --- a/sdk/lib/sdk/src/index.d.ts +++ b/sdk/lib/sdk/src/index.d.ts @@ -56,8 +56,8 @@ declare class Squads { getNextUpgradeIndex(managedProgramPDA: PublicKey): Promise; getAuthorityPDA(multisigPDA: PublicKey, authorityIndex: number): PublicKey; private _createMultisig; - createMultisig(threshold: number, createKey: PublicKey, initialMembers: PublicKey[]): Promise; - buildCreateMultisig(threshold: number, createKey: PublicKey, initialMembers: PublicKey[]): Promise; + createMultisig(threshold: number, createKey: PublicKey, initialMembers: PublicKey[], name: string, description?: string, image?: string): Promise; + buildCreateMultisig(threshold: number, createKey: PublicKey, initialMembers: PublicKey[], name: string, description?: string, image?: string): Promise; private _createTransaction; createTransaction(multisigPDA: PublicKey, authorityIndex: number): Promise; buildCreateTransaction(multisigPDA: PublicKey, authorityIndex: number, transactionIndex: number): Promise; diff --git a/sdk/lib/sdk/src/index.js b/sdk/lib/sdk/src/index.js index 7f533c8..bb3f3a1 100644 --- a/sdk/lib/sdk/src/index.js +++ b/sdk/lib/sdk/src/index.js @@ -178,28 +178,28 @@ class Squads { getAuthorityPDA(multisigPDA, authorityIndex) { return (0, address_1.getAuthorityPDA)(multisigPDA, new bn_js_1.default(authorityIndex, 10), this.multisigProgramId)[0]; } - _createMultisig(threshold, createKey, initialMembers) { + _createMultisig(threshold, createKey, initialMembers, name, description = "", image = "") { if (!initialMembers.find((member) => member.equals(this.wallet.publicKey))) { initialMembers.push(this.wallet.publicKey); } const [multisigPDA] = (0, address_1.getMsPDA)(createKey, this.multisigProgramId); return [ this.multisig.methods - .create(threshold, createKey, initialMembers) + .create(threshold, createKey, initialMembers, JSON.stringify({ name, description, image })) .accounts({ multisig: multisigPDA, creator: this.wallet.publicKey }), multisigPDA, ]; } - createMultisig(threshold, createKey, initialMembers) { + createMultisig(threshold, createKey, initialMembers, name, description = "", image = "") { return __awaiter(this, void 0, void 0, function* () { - const [methods, multisigPDA] = this._createMultisig(threshold, createKey, initialMembers); + const [methods, multisigPDA] = this._createMultisig(threshold, createKey, initialMembers, JSON.stringify({ name, description, image })); yield methods.rpc(); return yield this.getMultisig(multisigPDA); }); } - buildCreateMultisig(threshold, createKey, initialMembers) { + buildCreateMultisig(threshold, createKey, initialMembers, name, description = "", image = "") { return __awaiter(this, void 0, void 0, function* () { - const [methods] = this._createMultisig(threshold, createKey, initialMembers); + const [methods] = this._createMultisig(threshold, createKey, initialMembers, JSON.stringify({ name, description, image })); return yield methods.instruction(); }); } diff --git a/sdk/lib/target/idl/squads_mpl.json b/sdk/lib/target/idl/squads_mpl.json index 68d3cbc..79baf21 100644 --- a/sdk/lib/target/idl/squads_mpl.json +++ b/sdk/lib/target/idl/squads_mpl.json @@ -35,6 +35,10 @@ "type": { "vec": "publicKey" } + }, + { + "name": "meta", + "type": "string" } ] }, diff --git a/sdk/lib/target/types/squads_mpl.d.ts b/sdk/lib/target/types/squads_mpl.d.ts index c4b0ab3..ec58a14 100644 --- a/sdk/lib/target/types/squads_mpl.d.ts +++ b/sdk/lib/target/types/squads_mpl.d.ts @@ -35,6 +35,10 @@ export declare type SquadsMpl = { "type": { "vec": "publicKey"; }; + }, + { + "name": "meta"; + "type": "string"; } ]; }, diff --git a/sdk/lib/target/types/squads_mpl.js b/sdk/lib/target/types/squads_mpl.js index 02adc63..3d311de 100644 --- a/sdk/lib/target/types/squads_mpl.js +++ b/sdk/lib/target/types/squads_mpl.js @@ -38,6 +38,10 @@ exports.IDL = { "type": { "vec": "publicKey" } + }, + { + "name": "meta", + "type": "string" } ] }, diff --git a/sdk/package.json b/sdk/package.json index aff4de6..01bc86e 100644 --- a/sdk/package.json +++ b/sdk/package.json @@ -1,6 +1,6 @@ { "name": "@sqds/sdk", - "version": "1.0.4", + "version": "1.0.6", "description": "An SDK for easily interacting with Squads Multisig Programs", "main": "lib/sdk/src/index.js", "files": [ diff --git a/sdk/src/index.ts b/sdk/src/index.ts index 746c6fe..b25d1b6 100644 --- a/sdk/src/index.ts +++ b/sdk/src/index.ts @@ -11,11 +11,11 @@ import { DEFAULT_PROGRAM_MANAGER_PROGRAM_ID, } from "./constants"; import squadsMplJSON from "../../target/idl/squads_mpl.json"; -import { SquadsMpl } from "../../target/types/squads_mpl"; +import {SquadsMpl} from "../../target/types/squads_mpl"; import programManagerJSON from "../../target/idl/program_manager.json"; -import { ProgramManager } from "../../target/types/program_manager"; -import { Wallet } from "@project-serum/anchor/dist/cjs/provider"; -import { AnchorProvider, Program } from "@project-serum/anchor"; +import {ProgramManager} from "../../target/types/program_manager"; +import {Wallet} from "@project-serum/anchor/dist/cjs/provider"; +import {AnchorProvider, Program} from "@project-serum/anchor"; import { InstructionAccount, ManagedProgramAccount, @@ -36,7 +36,7 @@ import { } from "./address"; import BN from "bn.js"; import * as anchor from "@project-serum/anchor"; -import { TransactionBuilder } from "./tx_builder"; +import {TransactionBuilder} from "./tx_builder"; class Squads { readonly connection: Connection; @@ -48,11 +48,11 @@ class Squads { private readonly programManager: Program; constructor({ - connection, - wallet, - multisigProgramId, - programManagerProgramId, - }: { + connection, + wallet, + multisigProgramId, + programManagerProgramId, + }: { connection: Connection; wallet: Wallet; multisigProgramId?: PublicKey; @@ -62,32 +62,32 @@ class Squads { this.wallet = wallet; this.multisigProgramId = multisigProgramId ?? DEFAULT_MULTISIG_PROGRAM_ID; this.provider = new AnchorProvider( - this.connection, - this.wallet, - {...AnchorProvider.defaultOptions(), commitment: "confirmed", preflightCommitment: "confirmed"} + this.connection, + this.wallet, + {...AnchorProvider.defaultOptions(), commitment: "confirmed", preflightCommitment: "confirmed"} ); this.multisig = new Program( - squadsMplJSON as SquadsMpl, - this.multisigProgramId, - this.provider + squadsMplJSON as SquadsMpl, + this.multisigProgramId, + this.provider ); this.programManagerProgramId = - programManagerProgramId ?? DEFAULT_PROGRAM_MANAGER_PROGRAM_ID; + programManagerProgramId ?? DEFAULT_PROGRAM_MANAGER_PROGRAM_ID; this.programManager = new Program( - programManagerJSON as ProgramManager, - this.programManagerProgramId, - this.provider + programManagerJSON as ProgramManager, + this.programManagerProgramId, + this.provider ); } static endpoint( - endpoint: string, - wallet: Wallet, - options?: { - commitmentOrConfig?: Commitment | ConnectionConfig; - multisigProgramId?: PublicKey; - programManagerProgramId?: PublicKey; - } + endpoint: string, + wallet: Wallet, + options?: { + commitmentOrConfig?: Commitment | ConnectionConfig; + multisigProgramId?: PublicKey; + programManagerProgramId?: PublicKey; + } ) { return new Squads({ connection: new Connection(endpoint, options?.commitmentOrConfig), @@ -95,52 +95,55 @@ class Squads { ...options, }); } + static mainnet( - wallet: Wallet, - options?: { - commitmentOrConfig?: Commitment | ConnectionConfig; - multisigProgramId?: PublicKey; - programManagerProgramId?: PublicKey; - } + wallet: Wallet, + options?: { + commitmentOrConfig?: Commitment | ConnectionConfig; + multisigProgramId?: PublicKey; + programManagerProgramId?: PublicKey; + } ) { return new Squads({ connection: new Connection( - "https://api.mainnet-beta.solana.com", - options?.commitmentOrConfig + "https://api.mainnet-beta.solana.com", + options?.commitmentOrConfig ), wallet, ...options, }); } + static devnet( - wallet: Wallet, - options?: { - commitmentOrConfig?: Commitment | ConnectionConfig; - multisigProgramId?: PublicKey; - programManagerProgramId?: PublicKey; - } + wallet: Wallet, + options?: { + commitmentOrConfig?: Commitment | ConnectionConfig; + multisigProgramId?: PublicKey; + programManagerProgramId?: PublicKey; + } ) { return new Squads({ connection: new Connection( - "https://api.devnet.solana.com", - options?.commitmentOrConfig + "https://api.devnet.solana.com", + options?.commitmentOrConfig ), wallet, ...options, }); } + static localnet( - wallet: Wallet, - options?: { - commitmentOrConfig?: Commitment | ConnectionConfig; - multisigProgramId?: PublicKey; - programManagerProgramId?: PublicKey; - } + wallet: Wallet, + options?: { + commitmentOrConfig?: Commitment | ConnectionConfig; + multisigProgramId?: PublicKey; + programManagerProgramId?: PublicKey; + } ) { return new Squads({ connection: new Connection( - "http://localhost:8899", - options?.commitmentOrConfig + "http://localhost:8899", + options?.commitmentOrConfig ), wallet, ...options, @@ -149,125 +152,136 @@ class Squads { private _addPublicKeys(items: any[], addresses: PublicKey[]): (any | null)[] { return items.map((item, index) => - item ? { ...item, publicKey: addresses[index] } : null + item ? {...item, publicKey: addresses[index]} : null ); } async getTransactionBuilder( - multisigPDA: PublicKey, - authorityIndex: number + multisigPDA: PublicKey, + authorityIndex: number ): Promise { const multisig = await this.getMultisig(multisigPDA); return new TransactionBuilder( - this.multisig.methods, - this.programManager.methods, - this.provider, - multisig, - authorityIndex, - this.multisigProgramId + this.multisig.methods, + this.programManager.methods, + this.provider, + multisig, + authorityIndex, + this.multisigProgramId ); } + async getMultisig(address: PublicKey): Promise { const accountData = await this.multisig.account.ms.fetch(address, "processed"); - return { ...accountData, publicKey: address } as MultisigAccount; + return {...accountData, publicKey: address} as MultisigAccount; } + async getMultisigs( - addresses: PublicKey[] + addresses: PublicKey[] ): Promise<(MultisigAccount | null)[]> { const accountData = await this.multisig.account.ms.fetchMultiple(addresses, "processed"); return this._addPublicKeys( - accountData, - addresses + accountData, + addresses ) as (MultisigAccount | null)[]; } + async getTransaction(address: PublicKey): Promise { const accountData = await this.multisig.account.msTransaction.fetch( - address, - "processed" + address, + "processed" ); - return { ...accountData, publicKey: address }; + return {...accountData, publicKey: address}; } + async getTransactions( - addresses: PublicKey[] + addresses: PublicKey[] ): Promise<(TransactionAccount | null)[]> { const accountData = await this.multisig.account.msTransaction.fetchMultiple( - addresses, - "processed" + addresses, + "processed" ); return this._addPublicKeys( - accountData, - addresses + accountData, + addresses ) as (TransactionAccount | null)[]; } + async getInstruction(address: PublicKey): Promise { const accountData = await this.multisig.account.msInstruction.fetch( - address, - "processed" + address, + "processed" ); - return { ...accountData, publicKey: address }; + return {...accountData, publicKey: address}; } + async getInstructions( - addresses: PublicKey[] + addresses: PublicKey[] ): Promise<(InstructionAccount | null)[]> { const accountData = await this.multisig.account.msInstruction.fetchMultiple( - addresses, - "processed" + addresses, + "processed" ); return this._addPublicKeys( - accountData, - addresses + accountData, + addresses ) as (InstructionAccount | null)[]; } async getProgramManager(address: PublicKey): Promise { const accountData = await this.programManager.account.programManager.fetch( - address, - "processed" + address, + "processed" ); - return { ...accountData, publicKey: address }; + return {...accountData, publicKey: address}; } + async getProgramManagers( - addresses: PublicKey[] + addresses: PublicKey[] ): Promise<(ProgramManagerAccount | null)[]> { const accountData = - await this.programManager.account.programManager.fetchMultiple(addresses, "processed"); + await this.programManager.account.programManager.fetchMultiple(addresses, "processed"); return this._addPublicKeys( - accountData, - addresses + accountData, + addresses ) as (ProgramManagerAccount | null)[]; } + async getManagedProgram(address: PublicKey): Promise { const accountData = await this.programManager.account.managedProgram.fetch( - address, - "processed" + address, + "processed" ); - return { ...accountData, publicKey: address }; + return {...accountData, publicKey: address}; } + async getManagedPrograms( - addresses: PublicKey[] + addresses: PublicKey[] ): Promise<(ManagedProgramAccount | null)[]> { const accountData = - await this.programManager.account.managedProgram.fetchMultiple(addresses, "processed"); + await this.programManager.account.managedProgram.fetchMultiple(addresses, "processed"); return this._addPublicKeys( - accountData, - addresses + accountData, + addresses ) as (ManagedProgramAccount | null)[]; } + async getProgramUpgrade(address: PublicKey): Promise { const accountData = await this.programManager.account.programUpgrade.fetch( - address, - "processed" + address, + "processed" ); - return { ...accountData, publicKey: address }; + return {...accountData, publicKey: address}; } + async getProgramUpgrades( - addresses: PublicKey[] + addresses: PublicKey[] ): Promise<(ProgramUpgradeAccount | null)[]> { const accountData = - await this.programManager.account.programUpgrade.fetchMultiple(addresses, "processed"); + await this.programManager.account.programUpgrade.fetchMultiple(addresses, "processed"); return this._addPublicKeys( - accountData, - addresses + accountData, + addresses ) as (ProgramUpgradeAccount | null)[]; } @@ -293,64 +307,78 @@ class Squads { getAuthorityPDA(multisigPDA: PublicKey, authorityIndex: number): PublicKey { return getAuthorityPDA( - multisigPDA, - new BN(authorityIndex, 10), - this.multisigProgramId + multisigPDA, + new BN(authorityIndex, 10), + this.multisigProgramId )[0]; } private _createMultisig( - threshold: number, - createKey: PublicKey, - initialMembers: PublicKey[] + threshold: number, + createKey: PublicKey, + initialMembers: PublicKey[], + name: string, + description = "", + image = "" ): [SquadsMethods, PublicKey] { if ( - !initialMembers.find((member) => member.equals(this.wallet.publicKey)) + !initialMembers.find((member) => member.equals(this.wallet.publicKey)) ) { initialMembers.push(this.wallet.publicKey); } const [multisigPDA] = getMsPDA(createKey, this.multisigProgramId); return [ this.multisig.methods - .create(threshold, createKey, initialMembers) - .accounts({ multisig: multisigPDA, creator: this.wallet.publicKey }), + .create(threshold, createKey, initialMembers, JSON.stringify({name, description, image})) + .accounts({multisig: multisigPDA, creator: this.wallet.publicKey}), multisigPDA, ]; } + async createMultisig( - threshold: number, - createKey: PublicKey, - initialMembers: PublicKey[] + threshold: number, + createKey: PublicKey, + initialMembers: PublicKey[], + name: string, + description = "", + image = "" ): Promise { const [methods, multisigPDA] = this._createMultisig( - threshold, - createKey, - initialMembers + threshold, + createKey, + initialMembers, + JSON.stringify({name, description, image}) ); await methods.rpc(); return await this.getMultisig(multisigPDA); } + async buildCreateMultisig( - threshold: number, - createKey: PublicKey, - initialMembers: PublicKey[] + threshold: number, + createKey: PublicKey, + initialMembers: PublicKey[], + name: string, + description = "", + image = "" ): Promise { const [methods] = this._createMultisig( - threshold, - createKey, - initialMembers + threshold, + createKey, + initialMembers, + JSON.stringify({name, description, image}) ); return await methods.instruction(); } + private async _createTransaction( - multisigPDA: PublicKey, - authorityIndex: number, - transactionIndex: number + multisigPDA: PublicKey, + authorityIndex: number, + transactionIndex: number ): Promise<[SquadsMethods, PublicKey]> { const [transactionPDA] = getTxPDA( - multisigPDA, - new BN(transactionIndex, 10), - this.multisigProgramId + multisigPDA, + new BN(transactionIndex, 10), + this.multisigProgramId ); return [ this.multisig.methods.createTransaction(authorityIndex).accounts({ @@ -361,43 +389,46 @@ class Squads { transactionPDA, ]; } + async createTransaction( - multisigPDA: PublicKey, - authorityIndex: number + multisigPDA: PublicKey, + authorityIndex: number ): Promise { const nextTransactionIndex = await this.getNextTransactionIndex( - multisigPDA + multisigPDA ); const [methods, transactionPDA] = await this._createTransaction( - multisigPDA, - authorityIndex, - nextTransactionIndex + multisigPDA, + authorityIndex, + nextTransactionIndex ); await methods.rpc(); return await this.getTransaction(transactionPDA); } + async buildCreateTransaction( - multisigPDA: PublicKey, - authorityIndex: number, - transactionIndex: number + multisigPDA: PublicKey, + authorityIndex: number, + transactionIndex: number ): Promise { const [methods] = await this._createTransaction( - multisigPDA, - authorityIndex, - transactionIndex + multisigPDA, + authorityIndex, + transactionIndex ); return await methods.instruction(); } + private async _addInstruction( - multisigPDA: PublicKey, - transactionPDA: PublicKey, - instruction: TransactionInstruction, - instructionIndex: number + multisigPDA: PublicKey, + transactionPDA: PublicKey, + instruction: TransactionInstruction, + instructionIndex: number ): Promise<[SquadsMethods, PublicKey]> { const [instructionPDA] = getIxPDA( - transactionPDA, - new BN(instructionIndex, 10), - this.multisigProgramId + transactionPDA, + new BN(instructionIndex, 10), + this.multisigProgramId ); return [ this.multisig.methods.addInstruction(instruction).accounts({ @@ -409,37 +440,40 @@ class Squads { instructionPDA, ]; } + async addInstruction( - transactionPDA: PublicKey, - instruction: TransactionInstruction + transactionPDA: PublicKey, + instruction: TransactionInstruction ): Promise { const transaction = await this.getTransaction(transactionPDA); const [methods, instructionPDA] = await this._addInstruction( - transaction.ms, - transactionPDA, - instruction, - transaction.instructionIndex + 1 + transaction.ms, + transactionPDA, + instruction, + transaction.instructionIndex + 1 ); await methods.rpc(); return await this.getInstruction(instructionPDA); } + async buildAddInstruction( - multisigPDA: PublicKey, - transactionPDA: PublicKey, - instruction: TransactionInstruction, - instructionIndex: number + multisigPDA: PublicKey, + transactionPDA: PublicKey, + instruction: TransactionInstruction, + instructionIndex: number ): Promise { const [methods] = await this._addInstruction( - multisigPDA, - transactionPDA, - instruction, - instructionIndex + multisigPDA, + transactionPDA, + instruction, + instructionIndex ); return await methods.instruction(); } + private async _activateTransaction( - multisigPDA: PublicKey, - transactionPDA: PublicKey + multisigPDA: PublicKey, + transactionPDA: PublicKey ): Promise { return this.multisig.methods.activateTransaction().accounts({ multisig: multisigPDA, @@ -447,30 +481,33 @@ class Squads { creator: this.wallet.publicKey, }); } + async activateTransaction( - transactionPDA: PublicKey + transactionPDA: PublicKey ): Promise { const transaction = await this.getTransaction(transactionPDA); const methods = await this._activateTransaction( - transaction.ms, - transactionPDA + transaction.ms, + transactionPDA ); await methods.rpc(); return await this.getTransaction(transactionPDA); } + async buildActivateTransaction( - multisigPDA: PublicKey, - transactionPDA: PublicKey + multisigPDA: PublicKey, + transactionPDA: PublicKey ): Promise { const methods = await this._activateTransaction( - multisigPDA, - transactionPDA + multisigPDA, + transactionPDA ); return await methods.instruction(); } + private async _approveTransaction( - multisigPDA: PublicKey, - transactionPDA: PublicKey + multisigPDA: PublicKey, + transactionPDA: PublicKey ): Promise { return this.multisig.methods.approveTransaction().accounts({ multisig: multisigPDA, @@ -478,27 +515,30 @@ class Squads { member: this.wallet.publicKey, }); } + async approveTransaction( - transactionPDA: PublicKey + transactionPDA: PublicKey ): Promise { const transaction = await this.getTransaction(transactionPDA); const methods = await this._approveTransaction( - transaction.ms, - transactionPDA + transaction.ms, + transactionPDA ); await methods.rpc(); return await this.getTransaction(transactionPDA); } + async buildApproveTransaction( - multisigPDA: PublicKey, - transactionPDA: PublicKey + multisigPDA: PublicKey, + transactionPDA: PublicKey ): Promise { const methods = await this._approveTransaction(multisigPDA, transactionPDA); return await methods.instruction(); } + private async _rejectTransaction( - multisigPDA: PublicKey, - transactionPDA: PublicKey + multisigPDA: PublicKey, + transactionPDA: PublicKey ): Promise { return this.multisig.methods.rejectTransaction().accounts({ multisig: multisigPDA, @@ -506,27 +546,30 @@ class Squads { member: this.wallet.publicKey, }); } + async rejectTransaction( - transactionPDA: PublicKey + transactionPDA: PublicKey ): Promise { const transaction = await this.getTransaction(transactionPDA); const methods = await this._rejectTransaction( - transaction.ms, - transactionPDA + transaction.ms, + transactionPDA ); await methods.rpc(); return await this.getTransaction(transactionPDA); } + async buildRejectTransaction( - multisigPDA: PublicKey, - transactionPDA: PublicKey + multisigPDA: PublicKey, + transactionPDA: PublicKey ): Promise { const methods = await this._rejectTransaction(multisigPDA, transactionPDA); return await methods.instruction(); } + private async _cancelTransaction( - multisigPDA: PublicKey, - transactionPDA: PublicKey + multisigPDA: PublicKey, + transactionPDA: PublicKey ): Promise { return this.multisig.methods.cancelTransaction().accounts({ multisig: multisigPDA, @@ -534,133 +577,137 @@ class Squads { member: this.wallet.publicKey, }); } + async cancelTransaction( - transactionPDA: PublicKey + transactionPDA: PublicKey ): Promise { const transaction = await this.getTransaction(transactionPDA); const methods = await this._cancelTransaction( - transaction.ms, - transactionPDA + transaction.ms, + transactionPDA ); await methods.rpc(); return await this.getTransaction(transactionPDA); } + async buildCancelTransaction( - multisigPDA: PublicKey, - transactionPDA: PublicKey + multisigPDA: PublicKey, + transactionPDA: PublicKey ): Promise { const methods = await this._cancelTransaction(multisigPDA, transactionPDA); return await methods.instruction(); } + private async _executeTransaction( - transactionPDA: PublicKey, - feePayer: PublicKey + transactionPDA: PublicKey, + feePayer: PublicKey ): Promise { const transaction = await this.getTransaction(transactionPDA); const ixList = await Promise.all( - [...new Array(transaction.instructionIndex)].map(async (a, i) => { - const ixIndexBN = new anchor.BN(i + 1, 10); - const [ixKey] = getIxPDA( - transactionPDA, - ixIndexBN, - this.multisigProgramId - ); - const ixAccount = await this.getInstruction(ixKey); - return { pubkey: ixKey, ixItem: ixAccount }; - }) + [...new Array(transaction.instructionIndex)].map(async (a, i) => { + const ixIndexBN = new anchor.BN(i + 1, 10); + const [ixKey] = getIxPDA( + transactionPDA, + ixIndexBN, + this.multisigProgramId + ); + const ixAccount = await this.getInstruction(ixKey); + return {pubkey: ixKey, ixItem: ixAccount}; + }) ); const ixKeysList: anchor.web3.AccountMeta[] = ixList - .map(({ pubkey, ixItem }) => { - const ixKeys: anchor.web3.AccountMeta[] = - ixItem.keys as anchor.web3.AccountMeta[]; - const addSig = anchor.utils.sha256.hash("global:add_member"); - const ixDiscriminator = Buffer.from(addSig, "hex"); - const addData = Buffer.concat([ixDiscriminator.slice(0, 8)]); - const addAndThreshSig = anchor.utils.sha256.hash( - "global:add_member_and_change_threshold" - ); - const ixAndThreshDiscriminator = Buffer.from(addAndThreshSig, "hex"); - const addAndThreshData = Buffer.concat([ - ixAndThreshDiscriminator.slice(0, 8), - ]); - const ixData = ixItem.data as any; - - const formattedKeys = ixKeys.map((ixKey, keyInd) => { - if ( - (ixData.includes(addData) || ixData.includes(addAndThreshData)) && - keyInd === 2 - ) { + .map(({pubkey, ixItem}) => { + const ixKeys: anchor.web3.AccountMeta[] = + ixItem.keys as anchor.web3.AccountMeta[]; + const addSig = anchor.utils.sha256.hash("global:add_member"); + const ixDiscriminator = Buffer.from(addSig, "hex"); + const addData = Buffer.concat([ixDiscriminator.slice(0, 8)]); + const addAndThreshSig = anchor.utils.sha256.hash( + "global:add_member_and_change_threshold" + ); + const ixAndThreshDiscriminator = Buffer.from(addAndThreshSig, "hex"); + const addAndThreshData = Buffer.concat([ + ixAndThreshDiscriminator.slice(0, 8), + ]); + const ixData = ixItem.data as any; + + const formattedKeys = ixKeys.map((ixKey, keyInd) => { + if ( + (ixData.includes(addData) || ixData.includes(addAndThreshData)) && + keyInd === 2 + ) { + return { + pubkey: feePayer, + isSigner: false, + isWritable: ixKey.isWritable, + }; + } return { - pubkey: feePayer, + pubkey: ixKey.pubkey, isSigner: false, isWritable: ixKey.isWritable, }; - } - return { - pubkey: ixKey.pubkey, - isSigner: false, - isWritable: ixKey.isWritable, - }; - }); - - return [ - { pubkey, isSigner: false, isWritable: false }, - { pubkey: ixItem.programId, isSigner: false, isWritable: false }, - ...formattedKeys, - ]; - }) - .reduce((p, c) => p.concat(c), []); + }); + + return [ + {pubkey, isSigner: false, isWritable: false}, + {pubkey: ixItem.programId, isSigner: false, isWritable: false}, + ...formattedKeys, + ]; + }) + .reduce((p, c) => p.concat(c), []); // [ix ix_account, ix program_id, key1, key2 ...] const keysUnique: anchor.web3.AccountMeta[] = ixKeysList.reduce( - (prev, curr) => { - const inList = prev.findIndex( - (a) => a.pubkey.toBase58() === curr.pubkey.toBase58() - ); - // if its already in the list, and has same write flag - if (inList >= 0 && prev[inList].isWritable === curr.isWritable) { - return prev; - } else { - prev.push({ - pubkey: curr.pubkey, - isWritable: curr.isWritable, - isSigner: curr.isSigner, - }); - return prev; - } - }, - [] as anchor.web3.AccountMeta[] + (prev, curr) => { + const inList = prev.findIndex( + (a) => a.pubkey.toBase58() === curr.pubkey.toBase58() + ); + // if its already in the list, and has same write flag + if (inList >= 0 && prev[inList].isWritable === curr.isWritable) { + return prev; + } else { + prev.push({ + pubkey: curr.pubkey, + isWritable: curr.isWritable, + isSigner: curr.isSigner, + }); + return prev; + } + }, + [] as anchor.web3.AccountMeta[] ); const keyIndexMap = ixKeysList.map((a) => { return keysUnique.findIndex( - (k) => - k.pubkey.toBase58() === a.pubkey.toBase58() && - k.isWritable === a.isWritable + (k) => + k.pubkey.toBase58() === a.pubkey.toBase58() && + k.isWritable === a.isWritable ); }); const executeIx = await this.multisig.methods - .executeTransaction(Buffer.from(keyIndexMap)) - .accounts({ - multisig: transaction.ms, - transaction: transactionPDA, - member: feePayer, - }) - .instruction(); + .executeTransaction(Buffer.from(keyIndexMap)) + .accounts({ + multisig: transaction.ms, + transaction: transactionPDA, + member: feePayer, + }) + .instruction(); executeIx.keys = executeIx.keys.concat(keysUnique); return executeIx; } + async executeTransaction( - transactionPDA: PublicKey, - feePayer?: PublicKey, - signers?: Signer[] + transactionPDA: PublicKey, + feePayer?: PublicKey, + signers?: Signer[] ): Promise { const payer = feePayer ?? this.wallet.publicKey; const executeIx = await this._executeTransaction(transactionPDA, payer); - const { blockhash } = await this.connection.getLatestBlockhash(); + const {blockhash} = await this.connection.getLatestBlockhash(); const lastValidBlockHeight = await this.connection.getBlockHeight(); const executeTx = new anchor.web3.Transaction({ blockhash, @@ -671,127 +718,133 @@ class Squads { await this.provider.sendAndConfirm(executeTx, signers); return await this.getTransaction(transactionPDA); } + async buildExecuteTransaction( - transactionPDA: PublicKey, - feePayer?: PublicKey + transactionPDA: PublicKey, + feePayer?: PublicKey ): Promise { const payer = feePayer ?? this.wallet.publicKey; return await this._executeTransaction(transactionPDA, payer); } + private async _executeInstruction( - transactionPDA: PublicKey, - instructionPDA: PublicKey + transactionPDA: PublicKey, + instructionPDA: PublicKey ): Promise { const transaction = await this.getTransaction(transactionPDA); const instruction = await this.getInstruction(instructionPDA); const remainingAccountKeys: anchor.web3.AccountMeta[] = [ - { pubkey: instruction.programId, isSigner: false, isWritable: false }, + {pubkey: instruction.programId, isSigner: false, isWritable: false}, ].concat( - (instruction.keys as anchor.web3.AccountMeta[]).map((key) => ({ - ...key, - isSigner: false, - })) + (instruction.keys as anchor.web3.AccountMeta[]).map((key) => ({ + ...key, + isSigner: false, + })) ); return this.multisig.methods - .executeInstruction() - .accounts({ - multisig: transaction.ms, - transaction: transactionPDA, - instruction: instructionPDA, - member: this.wallet.publicKey, - }) - .remainingAccounts(remainingAccountKeys); + .executeInstruction() + .accounts({ + multisig: transaction.ms, + transaction: transactionPDA, + instruction: instructionPDA, + member: this.wallet.publicKey, + }) + .remainingAccounts(remainingAccountKeys); } + async executeInstruction( - transactionPDA: PublicKey, - instructionPDA: PublicKey + transactionPDA: PublicKey, + instructionPDA: PublicKey ): Promise { const methods = await this._executeInstruction( - transactionPDA, - instructionPDA + transactionPDA, + instructionPDA ); await methods.rpc(); return await this.getInstruction(instructionPDA); } + async buildExecuteInstruction( - transactionPDA: PublicKey, - instructionPDA: PublicKey + transactionPDA: PublicKey, + instructionPDA: PublicKey ): Promise { const methods = await this._executeInstruction( - transactionPDA, - instructionPDA + transactionPDA, + instructionPDA ); return await methods.instruction(); } async createProgramManager( - multisigPDA: PublicKey + multisigPDA: PublicKey ): Promise { const [programManagerPDA] = getProgramManagerPDA( - multisigPDA, - this.programManagerProgramId + multisigPDA, + this.programManagerProgramId ); await this.programManager.methods - .createProgramManager() - .accounts({ multisig: multisigPDA, programManager: programManagerPDA }) - .rpc(); + .createProgramManager() + .accounts({multisig: multisigPDA, programManager: programManagerPDA}) + .rpc(); return await this.getProgramManager(programManagerPDA); } + async createManagedProgram( - multisigPDA: PublicKey, - programAddress: PublicKey, - name: string + multisigPDA: PublicKey, + programAddress: PublicKey, + name: string ): Promise { const [programManagerPDA] = getProgramManagerPDA( - multisigPDA, - this.programManagerProgramId + multisigPDA, + this.programManagerProgramId ); const [managedProgramPDA] = getManagedProgramPDA( - programManagerPDA, - new BN(await this.getNextProgramIndex(programManagerPDA), 10), - this.programManagerProgramId + programManagerPDA, + new BN(await this.getNextProgramIndex(programManagerPDA), 10), + this.programManagerProgramId ); await this.programManager.methods - .createManagedProgram(programAddress, name) - .accounts({ - multisig: multisigPDA, - programManager: programManagerPDA, - managedProgram: managedProgramPDA, - }) - .rpc(); + .createManagedProgram(programAddress, name) + .accounts({ + multisig: multisigPDA, + programManager: programManagerPDA, + managedProgram: managedProgramPDA, + }) + .rpc(); return await this.getManagedProgram(managedProgramPDA); } + async createProgramUpgrade( - multisigPDA: PublicKey, - managedProgramPDA: PublicKey, - bufferAddress: PublicKey, - spillAddress: PublicKey, - authorityAddress: PublicKey, - upgradeName: string + multisigPDA: PublicKey, + managedProgramPDA: PublicKey, + bufferAddress: PublicKey, + spillAddress: PublicKey, + authorityAddress: PublicKey, + upgradeName: string ): Promise { const [programManagerPDA] = getProgramManagerPDA( - multisigPDA, - this.programManagerProgramId + multisigPDA, + this.programManagerProgramId ); const [programUpgradePDA] = getProgramUpgradePDA( - managedProgramPDA, - new BN(await this.getNextUpgradeIndex(managedProgramPDA), 10), - this.programManagerProgramId + managedProgramPDA, + new BN(await this.getNextUpgradeIndex(managedProgramPDA), 10), + this.programManagerProgramId ); await this.programManager.methods - .createProgramUpgrade( - bufferAddress, - spillAddress, - authorityAddress, - upgradeName - ) - .accounts({ - multisig: multisigPDA, - programManager: programManagerPDA, - managedProgram: managedProgramPDA, - programUpgrade: programUpgradePDA, - }) - .rpc(); + .createProgramUpgrade( + bufferAddress, + spillAddress, + authorityAddress, + upgradeName + ) + .accounts({ + multisig: multisigPDA, + programManager: programManagerPDA, + managedProgram: managedProgramPDA, + programUpgrade: programUpgradePDA, + }) + .rpc(); return await this.getProgramUpgrade(programUpgradePDA); } }