-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fce3c94
commit 874dc27
Showing
7 changed files
with
127 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export class Contract {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import * as path from 'path'; | ||
import { readFile as readFileCallback } from 'fs'; | ||
import { promisify } from 'util'; | ||
import { Serialize } from 'eosjs'; | ||
|
||
const readFile = promisify(readFileCallback); | ||
|
||
import { Contract } from './contract'; | ||
import { Account, AccountManager } from '../accounts'; | ||
import { EOSManager } from '../eosManager'; | ||
|
||
export class ContractDeployer { | ||
public static async deployAtName<T extends Contract>( | ||
account: Account, | ||
contractIdentifier: string | ||
) { | ||
console.log('Available keys', await EOSManager.signatureProvider.getAvailableKeys()); | ||
|
||
const buffer = new Serialize.SerialBuffer({ | ||
textEncoder: EOSManager.api.textEncoder, | ||
textDecoder: EOSManager.api.textDecoder, | ||
}); | ||
|
||
const abiPath = path.join('.lamington', 'compiled_contracts', `${contractIdentifier}.abi`); | ||
const wasmPath = path.join('.lamington', 'compiled_contracts', `${contractIdentifier}.wasm`); | ||
|
||
let abi = JSON.parse(await readFile(abiPath, 'utf8')); | ||
const wasm = await readFile(wasmPath); | ||
|
||
const abiDefinition = EOSManager.api.abiTypes.get(`abi_def`); | ||
|
||
// We need to make sure the abi has every field in abiDefinition.fields | ||
// otherwise serialize throws | ||
if (!abiDefinition) | ||
throw new Error('Could not retrieve abiDefinition from EOS API when flattening ABIs.'); | ||
|
||
abi = abiDefinition.fields.reduce( | ||
(acc, { name: fieldName }) => Object.assign(acc, { [fieldName]: acc[fieldName] || [] }), | ||
abi | ||
); | ||
abiDefinition.serialize(buffer, abi); | ||
|
||
const requiredKeys = await EOSManager.rpc.getRequiredKeys({ | ||
transaction: { | ||
actions: [ | ||
{ | ||
account: `eosio`, | ||
name: `setcode`, | ||
authorization: account.active, | ||
data: { | ||
account: account.name, | ||
vmtype: 0, | ||
vmversion: 0, | ||
code: wasm.toString(`hex`), | ||
}, | ||
}, | ||
{ | ||
account: `eosio`, | ||
name: `setabi`, | ||
authorization: account.active, | ||
data: { | ||
account: account.name, | ||
abi: Buffer.from(buffer.asUint8Array()).toString(`hex`), | ||
}, | ||
}, | ||
], | ||
}, | ||
availableKeys: await EOSManager.signatureProvider.getAvailableKeys(), | ||
}); | ||
|
||
console.log('Required keys', requiredKeys); | ||
|
||
await EOSManager.transact({ | ||
actions: [ | ||
{ | ||
account: `eosio`, | ||
name: `setcode`, | ||
authorization: account.active, | ||
data: { | ||
account: account.name, | ||
vmtype: 0, | ||
vmversion: 0, | ||
code: wasm.toString(`hex`), | ||
}, | ||
}, | ||
{ | ||
account: `eosio`, | ||
name: `setabi`, | ||
authorization: account.active, | ||
data: { | ||
account: account.name, | ||
abi: Buffer.from(buffer.asUint8Array()).toString(`hex`), | ||
}, | ||
}, | ||
], | ||
}); | ||
|
||
return new Contract(); | ||
} | ||
|
||
public static async deployClean<T extends Contract>(contractIdentifier: string) { | ||
const account = await AccountManager.createAccount(); | ||
|
||
return await ContractDeployer.deployAtName<T>(account, contractIdentifier); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './contract'; | ||
export * from './contractDeployer'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './accounts'; | ||
export * from './contracts'; | ||
|
||
import * as cliUtils from './cli/utils'; | ||
export const CLI = cliUtils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters