-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
5 changed files
with
184 additions
and
13 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 |
---|---|---|
@@ -1 +1,78 @@ | ||
export class ContractKit {} | ||
import { | ||
ABI, | ||
ABICache, | ||
ABICacheInterface, | ||
ABIDef, | ||
APIClient, | ||
Name, | ||
NameType, | ||
Session, | ||
} from '@wharfkit/session' | ||
import {Contract} from './contract' | ||
|
||
export interface ContractKitArgs { | ||
client?: APIClient | ||
session?: Session | ||
} | ||
|
||
export interface ABIDefinition { | ||
name: NameType | ||
abi: ABIDef | ||
} | ||
|
||
export interface ContractKitOptions { | ||
abiCache?: ABICacheInterface | ||
abis?: ABIDefinition[] | ||
} | ||
|
||
const defaultContractKitOptions: ContractKitOptions = {} | ||
|
||
export class ContractKit { | ||
readonly abiCache: ABICacheInterface | ||
readonly client: APIClient | ||
|
||
constructor(args: ContractKitArgs, options: ContractKitOptions = defaultContractKitOptions) { | ||
// Use either the client given or get it from the session. | ||
if (args.client) { | ||
this.client = args.client | ||
} else if (args.session) { | ||
this.client = args.session.client | ||
} else { | ||
throw new Error( | ||
'Either a `client` or `session` must be passed when initializing the ContractKit.' | ||
) | ||
} | ||
|
||
// Use either the specified cache, the cache from the session, or create one | ||
if (options.abiCache) { | ||
this.abiCache = options.abiCache | ||
} else if (args.session) { | ||
this.abiCache = args.session.abiCache | ||
} else { | ||
this.abiCache = new ABICache(this.client) | ||
} | ||
|
||
// If any ABIs are provided during construction, inject them into the cache | ||
if (options.abis) { | ||
options.abis.forEach(({name, abi}) => | ||
this.abiCache.setAbi(Name.from(name), ABI.from(abi)) | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* Load a contract by name from an API endpoint | ||
* | ||
* @param contract The name of the contract to load | ||
* @returns | ||
*/ | ||
async load(contract: NameType): Promise<Contract> { | ||
const name = Name.from(contract) | ||
const abiDef = await this.abiCache.getAbi(name) | ||
return new Contract({ | ||
abi: ABI.from(abiDef), | ||
client: this.client, | ||
name, | ||
}) | ||
} | ||
} |
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,94 @@ | ||
import {assert} from 'chai' | ||
|
||
import {Contract, ContractKit, ContractKitArgs, ContractKitOptions} from '$lib' | ||
import {makeClient, mockSession} from '@wharfkit/mock-data' | ||
import {ABI, ABICache, Name} from '@wharfkit/session' | ||
|
||
const mockClient = makeClient('https://jungle4.greymass.com') | ||
const mockContractKitArgs: ContractKitArgs = { | ||
client: mockClient, | ||
} | ||
const mockContractKitOptions: ContractKitOptions = {} | ||
|
||
suite('kit', function () { | ||
suite('construct', function () { | ||
test('defaults', function () { | ||
const kit = new ContractKit(mockContractKitArgs) | ||
assert.instanceOf(kit, ContractKit) | ||
}) | ||
test('throws if no client/session', function () { | ||
assert.throws(() => new ContractKit({})) | ||
}) | ||
test('args: client', function () { | ||
assert.doesNotThrow(() => { | ||
const kit = new ContractKit({client: mockClient}) | ||
assert.instanceOf(kit, ContractKit) | ||
}) | ||
}) | ||
test('args: session', function () { | ||
assert.doesNotThrow(() => { | ||
const kit = new ContractKit({session: mockSession}) | ||
assert.instanceOf(kit, ContractKit) | ||
}) | ||
}) | ||
test('options: abiCache', function () { | ||
const kit = new ContractKit(mockContractKitArgs, { | ||
abiCache: new ABICache(mockClient), | ||
}) | ||
assert.instanceOf(kit, ContractKit) | ||
assert.instanceOf(kit.abiCache, ABICache) | ||
}) | ||
suite('options: abis', function () { | ||
test('untyped', async function () { | ||
const kit = new ContractKit(mockContractKitArgs, { | ||
abis: [ | ||
{ | ||
name: 'foo', | ||
abi: {version: 'eosio::abi/1.2'}, | ||
}, | ||
{ | ||
name: 'bar', | ||
abi: {version: 'eosio::abi/1.2'}, | ||
}, | ||
], | ||
}) | ||
assert.instanceOf(kit, ContractKit) | ||
assert.equal(kit.abiCache.cache.size, 2) | ||
const foo = await kit.load('foo') | ||
assert.instanceOf(foo, Contract) | ||
const bar = await kit.load('bar') | ||
assert.instanceOf(bar, Contract) | ||
}) | ||
test('typed', async function () { | ||
const kit = new ContractKit(mockContractKitArgs, { | ||
abis: [ | ||
{ | ||
name: Name.from('foo'), | ||
abi: ABI.from({version: 'eosio::abi/1.2'}), | ||
}, | ||
{ | ||
name: Name.from('bar'), | ||
abi: ABI.from({version: 'eosio::abi/1.2'}), | ||
}, | ||
], | ||
}) | ||
assert.instanceOf(kit, ContractKit) | ||
assert.equal(kit.abiCache.cache.size, 2) | ||
const foo = await kit.load('foo') | ||
assert.instanceOf(foo, Contract) | ||
const bar = await kit.load('bar') | ||
assert.instanceOf(bar, Contract) | ||
}) | ||
}) | ||
}) | ||
suite('load', function () { | ||
let contractKit | ||
setup(() => { | ||
contractKit = new ContractKit(mockContractKitArgs) | ||
}) | ||
test('fetches abi', async function () { | ||
const contract = await contractKit.load('eosio.token') | ||
assert.instanceOf(contract, 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